Google Code jam 2013 Round 1B A题
2013-05-05 23:56
375 查看
分类:其他 难度:2
Problem
Armin is playing Osmos, a physics-based puzzle game developed by Hemisphere Games. In this game, he plays a "mote", moving around and absorbing smaller motes.A "mote" in English is a small particle. In this game, it's a thing that absorbs (or is absorbed by) other things! The game in this problem has a similar idea to Osmos, but does not assume you have played the game.
When Armin's mote absorbs a smaller mote, his mote becomes bigger by the smaller mote's size. Now that it's bigger, it might be able to absorb even more motes. For example: suppose Armin's mote has size 10, and there are other motes of sizes 9, 13 and 19.
At the start, Armin's mote can only absorb the mote of size 9. When it absorbs that, it will have size 19. Then it can only absorb the mote of size 13. When it absorbs that, it'll have size 32. Now Armin's mote can absorb the last mote.
Note that Armin's mote can absorb another mote if and only if the other mote is smaller. If the other mote is the same size as his, his mote can't absorb it.
You are responsible for the program that creates motes for Armin to absorb. The program has already created some motes, of various sizes, and has created Armin's mote. Unfortunately, given his mote's size and the list of other motes, it's possible that there's
no way for Armin's mote to absorb them all.
You want to fix that. There are two kinds of operations you can perform, in any order, any number of times: you can add a mote of any positive integer size to the game, or you can remove any one of the existing motes. What is the minimum number of times
you can perform those operations in order to make it possible for Armin's mote to absorb every other mote?
For example, suppose Armin's mote is of size 10 and the other motes are of sizes [9, 20, 25, 100]. This game isn't currently solvable, but by adding a mote of size 3 and removing the mote of size 100, you can make it solvable in only 2 operations. The answer
here is 2.
Input
The first line of the input gives the number of test cases, T. T test cases follow. The first line of each test case gives the size of Armin's mote, A,and the number of other motes,N. The second line contains the N sizes of the other motes. All the mote sizes given will be integers.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the minimum number of operations needed to make the game solvable.Limits
1 ≤ T ≤ 100.Small dataset
1 ≤ A ≤ 100.1 ≤ all mote sizes ≤ 100.
1 ≤ N ≤ 10.
Large dataset
1 ≤ A ≤ 106.1 ≤ all mote sizes ≤ 106.
1 ≤ N ≤ 100.
Sample
Input | Output |
4 2 2 2 1 2 4 2 1 1 6 10 4 25 20 9 100 1 4 1 1 1 1 | Case #1: 0 Case #2: 1 Case #3: 2 Case #4: 4 |
先将n个球由小到大排序,然后依次遍历n个球,每次将a值累加球的大小,ret记录进行操作2的次数,当a值小于等于第i个球的大小时,重复a+=a-1,ret++,直到a大于当前球,则遍历到i时进行的操作为ret次操作2加上(n-i)次操作1(删除剩余的点),记录最小的ret+n-i,即为所求。注意边界情况即可。
优化:
此种方法复杂度为O(n^2),因为每次增加a值使其大于当前球的复杂度不是O(1),可以通过数学计算变成O(1),设当前球大小为x,设a累加m次的值大于x,
即:a+(a-1)*(2^m-1) > x,可得:m = ceil( log2(x+1-a)/(a-1) + 1 ), ceil为上取整。
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> using namespace std; const int N=110; int a,n,mote ; int cmp(const void*x,const void *y) { return *(int*)x-*(int*)y; } int cal(int a,int n) { if(a==1) return n; int i,j,ret=0,ans=0; for(i=0;i<n;i++) { if(a<=mote[i]) { if(ret==0) ans = n-i; else { if(ret+n-i < ans) ans = ret+n-i; } double tmp = mote[i]+1; double ta = ceil(log((tmp-a)/(a-1.0)+1.0) / log(2.0)); ret += ta; a += (a-1)*(pow(2.0,ta)-1); } a+=mote[i]; //printf("%d %d %d %d\n",i,a,ret,ans); } if(ret < ans) ans = ret; return ans; } int main() { freopen("A-large.in","r",stdin); freopen("A-large.out","w",stdout); int t; scanf("%d",&t); for(int cnt=1;cnt<=t;cnt++) { scanf("%d%d",&a,&n); int i,j; for(i=0;i<n;i++) scanf("%d",&mote[i]); qsort(mote,n,sizeof(int),cmp); //for(i=0;i<n;i++) // printf("%d\n",mote[i]); printf("Case #%d: %d\n",cnt,cal(a,n)); } }
相关文章推荐
- Google Code jam 2013 Round 1C A题
- Good Luck Google-code-jam 2013-Round-1A
- 吸收遍历Google Code jam 2013 Round 1B A题
- Google CodeJam Qualification Round 2013
- Google CodeJam Round 1C 2013
- Google CodeJam Round 1A 2013
- google codejam Round 1C 2013 The Great Wall
- Google Code Jam Great China Test Round 2013
- Google CodeJam Round 2 2013
- Google Code jam 2013 Round 1A A题
- Google Code Jam Great China Round A
- [C++]Saving the Universe——Google Code Jam Qualification Round 2008
- Google codejam 2010 Round 2 :: Grazing Google Goats
- 2016 Google Code Jam Round 1A (A. The Last Word,B. Rank and File,C. BFFs(二元环))
- 2009 Google Code Jam Qualification Round 题解
- Google CodeJam Qualification Round 2014
- Google Code Jam 2016 Round2
- No Cheating(Google Code Jam 2008 Round 3C)二分图最大独立集
- googlecodejam.Qualification Round Africa 2010.Problem A. Store Credit
- Google Code Jam 2015 Round 1A Mushroom Monster 水