549. Dumbbells
Memory limit: 262144 kilobytes
output: standard
A sports shop has n dumbbells in store. Each of them is characterised by its mass mi and cost ci. Recently the shop manager faced the following non-trivial problem. He has to find the maximum number of sports sets that satisfy the following requirements:
- each set must contain exactly k dumbbells;
- each set must have dumbbells of k distinct masses;
- for each pair of sets the masses of dumbbells must coincide , that is, the masses in the sets must be equal as .
The manager's task is to make the maximum number of such sets. If there are several ways to make the maximum possible number of sets, he should choose the one that has the maximum total cost of dumbbells that are contained in the chosen sets. Note that the primary goal is to maximize the number of sets and maximization of the total cost is the secondary goal.
sample input | sample output |
7 2 16 1 4 6 16 7 7 100 32 9 4 6 32 1 | 2 22 |
sample input | sample output |
4 2 1 2 2 1 4 3 1 7 | 1 10 |
In the second sample the manager can make only one set. It consists of the third and the fourth dumbbells.
關鍵在於分成的不同集合裏的質量壹壹對應!
#include <bits/stdc++.h>
using namespace std;
const int M=4000+5;
struct ss{
int id;
int num;
}sss[M];
struct node{
int m;
int c;
}no[M];
int ans[M];
void init(){
for(int i=1;i<=4000;i++){
sss[i].id=i;
sss[i].num=0;
}
}
bool cmp(node x,node y){
if(sss[x.m].num!=sss[y.m].num){
return sss[x.m].num>sss[y.m].num;
}
if(x.m!=y.m){
return x.m>y.m;
}
return x.c>y.c;
}
bool cmp2(ss x,ss y){
return x.num>y.num;
}
bool cmp3(ss x,ss y){
return x.id<y.id;
}
bool cmp4(int x,int y){
return x>y;
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
init();
set<int> s;
for(int i=1;i<=n;i++){
scanf("%d%d",&no[i].m,&no[i].c);
s.insert(no[i].m);
sss[no[i].m].num++;
}
if(s.size()<k){
printf("0 0");
return 0;
}
sort(no+1,no+1+n,cmp);
sort(sss+1,sss+1+4000,cmp2);
int w=sss[k].num;
sort(sss+1,sss+1+4000,cmp3);
int index=0;
for(int i=1;i<=n;i++){
if(w>sss[no[i].m].num)break;
if(i==1||no[i].m!=no[i-1].m){
int now=0;
for(int j=i;j<i+w;j++){
now+=no[j].c;
}
ans[++index]=now;
}
}
sort(ans+1,ans+1+index,cmp4);
int res=0;
for(int i=1;i<=k;i++){
res+=ans[i];
}
printf("%d %d",w,res);
return 0;
}