• 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的,所以我们只要枚举一下这两种,取一个最大值即可

    1. // Problem: E. Red-Black Pepper
    2. // Contest: Codeforces - Educational Codeforces Round 135 (Rated for Div. 2)
    3. // URL: https://codeforces.com/contest/1728/problem/E
    4. // Memory Limit: 256 MB
    5. // Time Limit: 2000 ms
    6. #include
    7. #include
    8. #include
    9. #define fi first
    10. #define se second
    11. #define i128 __int128
    12. using namespace std;
    13. typedef long long ll;
    14. typedef double db;
    15. typedef pair<int,int> PII;
    16. const double eps=1e-7;
    17. const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
    18. const long long int llINF=0x3f3f3f3f3f3f3f3f;
    19. inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
    20. while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
    21. inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
    22. inline void write(ll x,char ch) {write(x);putchar(ch);}
    23. void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
    24. bool cmp0(int a,int b) {return a>b;}
    25. template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
    26. template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
    27. void hack() {printf("\n----------------------------------\n");}
    28. int T,hackT;
    29. int n,m,k;
    30. PII w[N];
    31. ll l[N],r[N];
    32. ll sum=0;
    33. int len,ren;
    34. ll lsum[N],rsum[N];
    35. ll exgcd(ll a,ll b,ll &x,ll &y) {
    36. if(!b) {
    37. x=1,y=0;
    38. return a;
    39. }
    40. ll d=exgcd(b,a%b,y,x);
    41. y-=a/b*x;
    42. return d;
    43. }
    44. ll get(int cnt1,int cnt2,int sa,int sb) {
    45. if(cnt1==sa) return sum;
    46. else if(cnt1>sa) {
    47. int tlen=cnt1-sa;
    48. return sum+rsum[tlen];
    49. }else {
    50. int tlen=sa-cnt1;
    51. return sum+lsum[tlen];
    52. }
    53. }
    54. void solve() {
    55. n=read();
    56. for(int i=1;i<=n;i++) w[i].fi=read(),w[i].se=read();
    57. int sa=0,sb=0;
    58. sum=0;
    59. for(int i=1;i<=n;i++) {
    60. if(w[i].fi>=w[i].se) sa++;
    61. else sb++;
    62. sum+=max(w[i].fi,w[i].se);
    63. }
    64. len=0,ren=0;
    65. for(int i=1;i<=n;i++) {
    66. if(w[i].fi>=w[i].se) {
    67. l[++len]=w[i].se-w[i].fi;
    68. }else {
    69. r[++ren]=w[i].fi-w[i].se;
    70. }
    71. }
    72. sort(l+1,l+1+len,cmp0);
    73. sort(r+1,r+1+ren,cmp0);
    74. for(int i=1;i<=len;i++) lsum[i]=lsum[i-1]+l[i];
    75. for(int i=1;i<=ren;i++) rsum[i]=rsum[i-1]+r[i];
    76. m=read();
    77. while(m--) {
    78. ll a=read(),b=read();
    79. ll x,y;
    80. ll d=exgcd(a,b,x,y);
    81. if(n%d!=0) printf("-1\n");
    82. else {
    83. x=x*n/d,y=y*n/d;
    84. ll res=-llINF;
    85. ll ta=a/d,tb=b/d;
    86. if(tb!=1) x=(x%tb+tb)%tb;
    87. y=(n-x*a)/b;
    88. if(x*a+y*b!=n||x<0||y<0) {
    89. printf("-1\n");
    90. continue;
    91. }
    92. res=max(res,get(x*a,y*b,sa,sb));
    93. ll t1=(sa-x*a)/(tb*a);
    94. ll temp1=x+t1*tb,temp2=y-t1*ta;
    95. if(temp1>=0&&temp2>=0&&temp1*a+temp2*b==n) {
    96. res=max(res,get(temp1*a,temp2*b,sa,sb));
    97. }
    98. temp1+=tb,temp2-=ta;
    99. if(temp1>=0&&temp2>=0&&temp1*a+temp2*b==n) {
    100. res=max(res,get(temp1*a,temp2*b,sa,sb));
    101. }
    102. if(res==-llINF) printf("-1\n");
    103. else printf("%lld\n",res);
    104. }
    105. }
    106. }
    107. int main() {
    108. // init();
    109. // stin();
    110. // ios::sync_with_stdio(false);
    111. // scanf("%d",&T);
    112. T=1;
    113. while(T--) hackT++,solve();
    114. return 0;
    115. }

  • 相关阅读:
    MATLAB数据导入
    Python selenium模块的常用方法【更新中】
    http是什么?http的基础知识教程详解(2024-04-24)
    JS中BOM编程:设置浏览器地址栏地址
    大模型时代的具身智能系列专题(九)
    Java学习笔记4.4.1 包装类 - 基本类型与包装类相互转换
    Elasticsearch基本操作-RESTful操作(更新中)
    Linux nohup 命令
    【算法】数学之旅,根据素数特征寻找底数
    Mybatis实现分页查询
  • 原文地址:https://blog.csdn.net/zzzyyzz_/article/details/133044060