CF-1183G

G. Candy Box (hard version)

题意

$q(1\le q\le 2e5)$ 次询问,每次询问$n(1\le n\le 2e5)$ 个物体,每个物体的种类为 $a_i(1\le a_i\le n)$,喜欢与否为 $f_i(0\le f_i\le1)$,要尽量取多的物体,要求每种物体的数量不同,在保证数量最多的情况下,要让喜欢的最少

题解

按种类数排序,总数记为 $x_i$ ,$f=0$ 的数量为 $y_i$,从 $n->1$ 扫描,对于当前 $i$,将满足条件的 $y_i$ 加入优先队列

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
using namespace std;
#define forl(i, l, r) for (int i = l; i <= r; i++)
#define forr(i, r, l) for (int i = r; i >= l; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define fro1(i, n) for (int i = 1; i <= n; i++)
#define for0(i, n) for (int i = 0; i < n; i++)
#define fro0(i, n) for (int i = 0; i < n; i++)
#define meminf(a) memset(a, inf, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define memcp(a,b) memcpy(a,b,sizeof(b))
#define oper(type) bool operator <(const type y)const
#define mp make_pair
#define pu_b push_back
#define pu_f push_front
#define po_b pop_back
#define po_f pop_front
#define fi first
#define se second
#define whiel while
#define retrun return
typedef pair<long long, long long> pll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef long double db;
typedef long long ll;
typedef int itn;
int in(int &a,int &b,int &c,int &d){return scanf("%d%d%d%d",&a,&b,&c,&d);}
int in(int &a,int &b,int &c){return scanf("%d%d%d",&a,&b,&c);}
int in(int &a,int &b){return scanf("%d%d",&a,&b);}
int in(ll &a){return scanf("%lld",&a);}
int in(int &a){return scanf("%d",&a);}
int in(char *s){return scanf("%s",s);}
int in(char &s){return scanf("%c",&s);}
int in(db &a){return scanf("%Lf",&a);}
void out(int a){printf("%d ",a);}
void outln(int a){printf("%d\n",a);}
void out(ll a){printf("%lld ",a);}
void outln(ll a){printf("%lld\n",a);}
const db pi = acos((db)-1);
const ll inf =0x3f3f3f3f;
const db eps = 1e-8;
const int N = 2.1e5;
const ll mod = 1e9+7;
int sign(db a) { return a < -eps ? -1 : a > eps;}
int db_cmp(db a, db b){ return sign(a-b); }

pii a[N];
priority_queue<int>Q;
int main() {
itn q,n,x,y;
in(q);
whiel(q--){
in(n);
for0(i,n){
in(x,y);
a[x].fi++;
a[x].se+=y;
}
sort(a+1,a+n+1);
ll an1=0,an2=0;
int id=n;
for(itn i=n;i;i--){
while(id&&a[id].fi>=i){
Q.push(a[id--].se);
}
if(Q.size()){
an1+=i;
an2+=min(i,Q.top());
Q.pop();
}
}
for1(i,n)a[i]=pii(0,0);
whiel(Q.size())Q.pop();
out(an1);
outln(an2);
}
return 0;
}