codeforce C. Save the Nature (前缀、二分)

本文详细解析了Codeforces竞赛中的一道题目,采用C++实现,重点介绍了如何使用二分查找算法来解决复杂的问题,包括数据预处理、排序、前缀和及数学运算等关键步骤。

题目:https://codeforces.com/contest/1223/problem/C

注意:二分的写法,容易搞错

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn=200005;
const int mod=1e9+7;
LL p[maxn];
LL x,a,y,b,k;
LL sum[maxn];
LL gcd(LL a,LL b)
{
	if(b==0) return a;
	return gcd(b,a%b);
}
bool cmp(LL a,LL b)
{
	return a>b;
}
int main()
{
	ios::sync_with_stdio(false);
	int q;
	cin>>q;
	while(q--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>p[i];
		}
		sort(p+1,p+n+1,cmp);
		sum[0]=0;
		for(int i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+p[i];
		}
		cin>>x>>a>>y>>b>>k; 
		if(x<y)
			swap(x,y),swap(a,b);
		LL l,r;
		l=1;r=n;
		LL ans;
		LL lcm=a*b/gcd(a,b);
		while(l<=r)
		{
			LL mid=(l+r)/2;
			
			LL both=mid/lcm;
			LL aval=mid/a-both;
			LL bval=mid/b-both;
			LL zsum=0;
			zsum+=sum[both]/100*(x+y);
			zsum+=(sum[both+aval]-sum[both])/100*x;
			zsum+=(sum[both+aval+bval]-sum[both+aval])/100*y;
			if(zsum>=k)
			{
				//ans=mid;
				r=mid-1;
			}
			else
			{
				l=mid+1;
			}
		}
		if(l>n)
			cout << -1 << endl;
		else
			cout << l << endl;
	} 
	return 0;	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值