URAL1917_Titan Ruins: Better late than killed_二分搜索

好久没有1A了。。。。


题意:

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



Input

The first line contains two integers: n is the number of coins and p is the maximum shockwave power the wizards can survive (1 ≤ n ≤ 1000; 1 ≤ p ≤ 10 9). The second line contains n integers a i, which are the resistance limits of the coins (1 ≤ a i ≤ 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 0x3f3f3f3f
int 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;
		else	r=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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值