传送门
文章目录
- 题意:
- 思路:
题意:
n≤2e5,m≤n,p≤1e9n\le2e5,m\le n,p\le 1e9n≤2e5,m≤n,p≤1e9
思路:
首先需要发现一些性质,假设preipre_iprei代表所有mj<im_j< imj<i的pjp_jpj和。可以发现,如果我们处理到了人数达到iii才能主动投票的某些人的时候,所有<i<i<i的人无论是被收买来的,还是没有花费任何代价来的,都不需要关心。我们只需要关心所有mj≥im_j\ge imj≥i的人。如果preipre_iprei的人数达不到iii,那么我们只能从所有mj≥im_j\ge imj≥i的人中选出前i−preii-pre_ii−prei小的人收买他们,才能达到iii的要求,之后才能进行i+1i+1i+1。
当然上面的过程是正着想的,具体的代码可以对mim_imi开一个桶,倒着来,每次将mj=im_j=imj=i的pjp_jpj都加入优先队列中,拿出来即可。
复杂度O(nlogn)O(nlogn)O(nlogn)
// Problem: E1. Voting (Easy Version)
// Contest: Codeforces - Educational Codeforces Round 75 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1251/E1
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
int p[N];
vector<int>v[N];int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);int _; scanf("%d",&_);while(_--) {scanf("%d",&n);for(int i=0;i<=n;i++) v[i].clear();for(int i=1;i<=n;i++) {int x; scanf("%d%d",&x,&p[i]);v[x].pb(p[i]);}int cnt=0,pre=n;LL ans=0;priority_queue<int,vector<int>,greater<int>>q;for(int i=n;i>=1;i--) {pre-=v[i].size();int need=i-pre;for(auto x:v[i]) q.push(x);while(cnt<need) {ans+=q.top(); q.pop();cnt++;}}printf("%lld\n",ans);}return 0;
}
/**/