传送门
题意:给出n个物品,每个物品有两个属性:种类ai(1<=ai<=n)和标记bi(bi为0或者1)。要求选出最多数量的物品满足:各个种类之间的选取数量不同,并且使bi=1的物品尽可能多
题解:容易想到直接统计出每个种类的物品个数sumi以及每个种类物品bi=1的数量,显然应该按照数量非递增的种类开始取,如果当前可以取的数量之前已经取过,那么将当前可以取的物品的数量sumi减去1,同时维护bi=1的数量,发现整个过程可以使用一个优先队列维护。每次弹出取的数量最多并且bi=1尽可能多的物品,如果当前的数量前面已经取过,那么把该物品的数量sumi-1,修改bi=1的个数,然后重新放到优先队列里,如果当前数量前面没有取过,那么直接弹出并且加到答案上即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
const int maxn=2e5+5;
const int maxn2=3e6+5;
const ll mod=1e9+7;
const ll inf=1e18;
struct pot{
int x;
int y;
bool operator<(const struct pot &aa)const{
if(x!=aa.x)return x<aa.x;
return y<aa.y;
}
}p[maxn];
int a[maxn],x1[maxn],y1[maxn];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
a[i]=p[i].x;
}
sort(a+1,a+1+n);
int siz=unique(a+1,a+1+n)-(a+1);
for(int i=1;i<=siz;i++){
x1[i]=y1[i]=0;
}
for(int i=1;i<=n;i++){
int pos=lower_bound(a+1,a+1+siz,p[i].x)-a;
x1[pos]++;
y1[pos]+=(p[i].y==1);
}
priority_queue<struct pot>pq;
for(int i=1;i<=siz;i++){
pq.push({x1[i],y1[i]});
}
int ac1,ac2;
ac1=ac2=0;
int fr=-1;
while(!pq.empty()){
struct pot aa=pq.top();
pq.pop();
if(aa.x==fr){
if(aa.x-1>0)pq.push({aa.x-1,min(aa.y,aa.x-1)});
}
else{
fr=aa.x;
ac1+=aa.x;
ac2+=aa.y;
}
}
printf("%d %d\n",ac1,ac2);
}
return 0;
}