E. Red-Black Pepper

Problem - E - Codeforces

思路:这个题还是比较好想的,我们对于每一对来说,我们都期望选择美味值最大的那一个,所以我们可以统计出来如果都选择美味值最大的那一个得到的总和,以及此时选择第一个的数量,选择第二个的数量,那么如果不能够这样选择的话,假如说此时选择第一个的数量多了,那么我们就需要从a中选择一部分让它换为选择b,这样会使得答案变小,那么我们期望选择谁呢,肯定选择把第一个变成第二个减少的小的那些,所以我们可以用一个东西存一下第二个减第一个,此时如果第一个多了,我们只需要降序排列之后,将sum+lsum[cnt]就是答案,同理第二个也需要按照同样的方法处理,这样我们就能够O(1)的进行求最总的答案了,那么接下来再分析输入,对于一对输入a,b来说我们期望就是找到x,y使得a*x+b*y=n,同时再第一列中选择a*x个,在第二列中选择b*y个,求最大值,那么首先对于a*x+b*y=n显然是一个可以进行扩展欧几里得算法的,当n%gcd(a,b)!=0时无解,接下来先求得最小的正数解,然后能够知道递增的数值,然后我们只需要找到最接近于cnta,cntb的x,y即可,有两种可能,一种是x*a<=cnta,另一种是x*a>=cnta的,所以我们只要枚举一下这两种,取一个最大值即可

// Problem: E. Red-Black Pepper
// Contest: Codeforces - Educational Codeforces Round 135 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1728/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms

#include<bits/stdc++.h>
#include<sstream>
#include<cassert>
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
const double eps=1e-7;
const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}

int T,hackT;
int n,m,k;
PII w[N];
ll l[N],r[N];
ll sum=0;
int len,ren;
ll lsum[N],rsum[N];

ll exgcd(ll a,ll b,ll &x,ll &y) {
	if(!b) {
		x=1,y=0;
		return a;
	}
	
	ll d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}

ll get(int cnt1,int cnt2,int sa,int sb) {
	if(cnt1==sa) return sum;
	else if(cnt1>sa) {
		int tlen=cnt1-sa;
		return sum+rsum[tlen];
	}else {
		int tlen=sa-cnt1;
		return sum+lsum[tlen];
	}
}

void solve() {
	n=read();
	
	for(int i=1;i<=n;i++) w[i].fi=read(),w[i].se=read();
	
	int sa=0,sb=0;
	sum=0;
	for(int i=1;i<=n;i++) {
		if(w[i].fi>=w[i].se) sa++;
		else sb++;
		sum+=max(w[i].fi,w[i].se);
	}
	
	len=0,ren=0;
	for(int i=1;i<=n;i++) {
		if(w[i].fi>=w[i].se) {
			l[++len]=w[i].se-w[i].fi;
		}else {
			r[++ren]=w[i].fi-w[i].se;
		}
	}
	
	sort(l+1,l+1+len,cmp0);
	sort(r+1,r+1+ren,cmp0);
	
	for(int i=1;i<=len;i++) lsum[i]=lsum[i-1]+l[i];
	for(int i=1;i<=ren;i++) rsum[i]=rsum[i-1]+r[i];
	
	m=read();
	while(m--) {
		ll a=read(),b=read();
		
		ll x,y;
		ll d=exgcd(a,b,x,y);
		
		if(n%d!=0) printf("-1\n");
		else {
			x=x*n/d,y=y*n/d;

			ll res=-llINF;
			ll ta=a/d,tb=b/d;
			if(tb!=1) x=(x%tb+tb)%tb;

			y=(n-x*a)/b;
				
			if(x*a+y*b!=n||x<0||y<0) {
				printf("-1\n");
				continue;
			}
			
			res=max(res,get(x*a,y*b,sa,sb));
			
			ll t1=(sa-x*a)/(tb*a);
			ll temp1=x+t1*tb,temp2=y-t1*ta;
			if(temp1>=0&&temp2>=0&&temp1*a+temp2*b==n) {
				res=max(res,get(temp1*a,temp2*b,sa,sb));
			}
			
			temp1+=tb,temp2-=ta;
			if(temp1>=0&&temp2>=0&&temp1*a+temp2*b==n) {
				res=max(res,get(temp1*a,temp2*b,sa,sb));
			}
			
			if(res==-llINF) printf("-1\n");
			else printf("%lld\n",res);
		}
	}
}   

int main() {
    // init();
    // stin();
	// ios::sync_with_stdio(false); 

    // scanf("%d",&T);
    T=1; 
    while(T--) hackT++,solve();
    
    return 0;       
}          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值