549. Dumbbells
Time limit per test: 1 second(s)
Memory limit: 262144 kilobytes
Memory limit: 262144 kilobytes
input: standard
output: standard
output: standard
A sports shop has n dumbbells in store. Each of them is characterised by its mass m i and cost c i . 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.
Input
The first line of the input contains integers
n
and
k
(1 ≤
n
,
k
≤ 4000). Next
n
lines contain descriptions of the dumbbells, one per line. Each description consists of a pair of integers
m
i
,
c
i
(1 ≤
m
i
,
c
i
≤ 4000),
m
i
is the mass of the
i
-th dumbbell, and
c
i
is its cost.
Output
In the only output line print two integers —
t
and
s
, where
t
is the maximum number of sets, and
s
is the maximum total cost of dumbbells in
t
choosen sets. If the manager can't make at least one set, print a pair of zeroes.
Example(s)
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 |
Note
In the first sample the manager should make two sets. One of the possible solutions is as follows: the first set contains the second and the seventh dumbbells, the second set contains the fifth and the sixth dumbbells.
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;
}