标签:first nbsp 练习 ace algo += type sort for
练习1 CF 1020 C
有n个选民, m个党派, 第i个选民初始投$p_i$一票, 可以花费$c_i$改变投票, 求最少花费使得第一个党派的票数严格最大
假设最终第一个党派得票数$x$, 枚举$x$, 则对于所有票数$\ge x$的党派, 贪心选择尽量小的c改选第一个党派.若最后票数还不够, 再从没改过的人中选择尽量小的改选即可
#include <iostream> #include <algorithm> #include <cstdio> #include <vector> #define REP(i,a,n) for(int i=a;i<=n;++i) #define pb push_back #define x first #define y second using namespace std; typedef long long ll; typedef pair<int,int> pii; const int N = 4e5+10, INF = 0x3f3f3f3f; int n, m; vector<pii> s[N]; pii a[N]; int vis[N]; int main() { scanf("%d%d", &n, &m); REP(i,1,n) { int x, y; scanf("%d%d", &x, &y); s[x].pb({y,i}); a[i] = {x==1?1e9:y,i}; } sort(a+1, a+1+n); REP(i,2,m) sort(s[i].begin(),s[i].end()); ll ans = 1e18; REP(i,1,n) { int tot = 0; ll c = 0; REP(j,2,m) if (s[j].size()>=i) { for (int k=0; k<=s[j].size()-i; ++k) { ++tot, c+=s[j][k].x, vis[s[j][k].y]=1; } } REP(j,1,n) { if (s[1].size()+tot<i&&!vis[a[j].y]) { ++tot, c+=a[j].x; } } if (s[1].size()+tot==i) { ans = min(ans, c); } REP(i,1,n) vis[i]=0; } printf("%lld\n", ans); }
标签:first nbsp 练习 ace algo += type sort for
原文地址:https://www.cnblogs.com/uid001/p/10411614.html