P13878 [蓝桥杯 2023 省 Java/Python A] 平均
clki
·
·
个人记录
先读题:
有一个长度为 n 的数组(n 是 10 的倍数),每个数 a
i
都是区间 [0,9] 中的整数。小明发现数组里每种数出现的次数不太平均,而更改第 i 个数的代价为 b
i
,他想更改若干个数的值使得这 10 种数出现的次数相等(都等于
10
n
),请问代价和最少为多少。
所以,我们只需枚举那个数字大于十,减掉并加代价即可
```cpp
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
struct node{
int x,y;
}a[N];
int n,cnt[N],ans;
bool cmp(node u,node v){
return u.y < v.y;
}
signed main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].x>>a[i].y;
cnt[a[i].x]++;//数字是a[i].x,代价是a[i].y
}
sort(a + 1,a + 1 + n ,cmp);//排序
for(int i=1;i<=n;i++){
if(cnt[a[i].x] > n / 10){
cnt[a[i].x]--;//修改
ans += a[i].y;
}
}
cout<<ans;
return 0;
}
```