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.
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.
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.
1 ≤ T ≤ 100.
1 ≤ A ≤ 100.
1 ≤ all mote sizes ≤ 100.
1 ≤ N ≤ 10.
1 ≤ A ≤ 106.
1 ≤ all mote sizes ≤ 106.
1 ≤ N ≤ 100.
Input |
Output |
4 |
Case #1: 0 |
题意分析:给出自己的球初始大小a和球的个数n,以及n个球的大小,自己能吸收比自己小的球,然后大小增加吸收球的大小,每次可做两种操作:1、删掉一个球,2、增加一个球供自己吸收。求最少经过几次操作,能吸收所有的球。可知,操作1一定是从后边开始删,操作2一定是加当前的a-1。
先将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[N]; 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)); } }