CodeForces-999D Equalize the Remainders (贪心+神奇的STL)

题意:给你一个n,m;其中n一定能被m整除,然后给你n个数

有一种操作   选择n个数中的任意一个,使其+1;

条件:

  Ci 属于[0,m-1]  Ci代表ai模m的余数为i的个数 且都等于n/m;

  (

    比如n=4,m=2;n/m=2;

    a1=0,a2=1,a3=2,a4=3;

    Ci属于[0,1];

    a1%m=0;a2%m=1;a3%m=0;a4%m=1;

    所以C1=C2=n/m=2;

  )

求:最少需要操作几次才能满足以上要求

 

代码如下:

 

 1 #include <iostream>
 2 #include <cstdio> 
 3 #include <set> 
 4 #include <algorithm> 
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn=2e5+5;
 8 ll cnt[maxn],a[maxn],val,res,ans;
 9 set<ll> s;
10 set<ll> ::iterator it;
11 int main()
12 {
13     int n,m;
14     cin>>n>>m;
15     for(int i=0;i<m;cnt[i++]=n/m)
16         s.insert(i);
17     for(int i=1;i<=n;i++)
18     {
19         cin>>a[i];
20         val=a[i]%m;
21         if(val>(*s.rbegin()))res=*s.begin();
22         else res=*s.lower_bound(val);
23         if(!--cnt[res])s.erase(res);
24         ans=ans+(res-val+m)%m;
25         a[i]=a[i]+(res-val+m)%m;
26     }
27     cout<<ans<<endl;
28     for(int i=1;i<=n;i++)
29         cout<<a[i]<<" ";
30     cout<<endl;
31     return 0;
32 }

 

转载于:https://www.cnblogs.com/Cherry93/p/9930387.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值