URAL1917_Titan Ruins: Better late than killed_二分搜索

来源:互联网 发布:安卓埋雷软件 编辑:程序博客网 时间:2024/06/06 06:48

好久没有1A了。。。。


题意:

一个长度为n的数组,可以进行一种操作:选一个值k,消灭数组中所有小于等于k的数,但同时得到一个数k*m,m为此次操作被消灭的数的个数,要求任意一次操作k*m要小于等于一个值p,求最多消灭几个数字,以及最少需要进行多少次操作能达成。



Input

The first line contains two integers: n is the number of coins andp is the maximum shockwave power the wizards can survive (1 ≤ n ≤ 1000; 1 ≤ p ≤ 10 9). The second line containsn integers ai, which are the resistance limits of the coins (1 ≤ai ≤ 10 6).

Output

Output two integers separated with a space: the maximum amount of coins the wizards can destroy without killing themselves and the minimum number of spells they have to cast the Annihilation Spell to destroy all these coins.


一直尝试操作知道不能操作,维护数组一直有序,二分讨论当前操作的k值是多少,二分中的每次尝试通过upper_bound查找m,根据m*k和p的大小关系判断是否可以,找到一次合法操作以后更改数组(把删除的数字改为inf),重新排序,更改答案变量,再次尝试操作,知道找不到合法操作。复杂度O(N*N*LOGN)


代码如下:

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>using namespace std;#define mxn 1010#define inf 0x3f3f3f3fint n,p;int a[mxn];int ans1,ans2;bool ok(int in){int loc=upper_bound(a,a+n,in)-a-1;return (loc+1)*in<=p;}int bin(int l,int r){if(l==r)return 0;int m;while(l+1<r){m=(l+r)>>1;if(ok(m))l=m;elser=m;}int loc=upper_bound(a,a+n,l)-a-1;if(loc==-1)return -1;ans1+=loc+1;for(int i=0;i<=loc;++i)a[i]=inf;sort(a,a+n);return l;}int main(){while(scanf("%d%d",&n,&p)!=EOF){for(int i=0;i<n;++i)scanf("%d",&a[i]);sort(a,a+n);ans1=0,ans2=0;int maxx=a[n-1]+1,tem;while(true){tem=bin(0,maxx);getchar();if(tem==-1)break;++ans2;}printf("%d %d\n",ans1,ans2);}return 0;}


0 0