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
- URAL1917_Titan Ruins: Better late than killed_二分搜索
- Ural 1917 Titan Ruins: Better late than killed
- Better Late Than Never
- Better Late Than Never!
- Inblogit, better than wordpress?
- rails better than php ?
- Functional is better than function
- Pipelined Functions: Better Than DBMS_OUTPUT
- Today is Better than yesterday
- Why Google better than baidu
- Line is better than words
- NOW IS BETTER THAN NEVER!
- Two is better than one
- English-Better Than Its Bite
- 【Life】 Never Too Late, Just Do it Better!
- Late Counting (POJ 2386) 深度优先搜索
- why not better than her for English?
- Bangladeshi Garment Exports Better than Expectation
- 站群--插件--点击量
- 云南晋宁项目施工方与村民冲突 已致8死18伤(图)(1)
- 书评第002篇:《汇编语言(第3版)》
- Integer to Roman
- 中国已成军事大国 可遏制日本重走军国主义老路
- URAL1917_Titan Ruins: Better late than killed_二分搜索
- CentOS 开发问题
- 找钱方式:递归,循环的解法
- 朝鲜半岛不再被关注 金正恩玩消失成功转移焦点
- spoj705( 求不相同的子串个数)
- 【Unity3D_Shader】流水Shader的名词查找和疑问
- MYSQL的explain工具简介
- matlab .m文件转换成exe可执行文件
- 判断是否存在一条直线穿过所有的线段