晾衣服poj只是支持c++98

对于某一个题目,需要对结果进行测试看是否符合条件的时候:将所有的结果排序,用二分枚举结果进行检验,方法不行,动态规划

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

long long k, n;
int t=1;
long long das[1000006];

bool  judge (long long x) {
	double sum=0;
	long long temp;
	for(int i=1; i<= n; ++i) {
		if (das[i] > x) {
			sum+=(das[i]-x+k-2)/(k-1);
		}
		if(sum>x)
			return 0;
	}
	return 1;
}
void detal(long long l,long long r) {

	long long mid,ans;
	while(l<=r) {
		mid=(l+r)/2;
		if(judge(mid)) {
			ans=mid;
			r=mid-1;
		} else l=mid+1;
	}
	cout<<ans<<endl;
	cout<<endl;
}
int main() {
	ios::sync_with_stdio(false);
//	cin.tie(0);
//	cout.tie(0);
	cin>>n;
	for(int i=1; i<=n; i++) {
		cin>>das[i];
	}
	cin>>k;
	sort(das+1,das+n+1);
	if(k==1) {
		cout<<das[n]<<endl;
		return 0;
	}
	detal(1,das[n]);
	return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务