懒虫小鑫
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
小鑫是个大懒虫,但是这一天妈妈要小鑫去山上搬些矿石去城里卖以补贴家用。小鑫十分的不开心。不开心归不开心,小鑫还是要做这件事情的。
我们把这个事情简化一下。有n块矿石,设第i块矿石由两个数字wi和pi表示。分别表示这块石头的重量和可以卖的价钱。小鑫每次只能搬一块矿石去城里卖,所以他决定每次都会搬重量最小的那块。如果恰好有几块重量相等,那就在这几块中挑选价值最高的带走。
由于路程原因。小鑫每天只能打m个来回,也就意味着他只能卖掉m块矿石。你能计算出他能得到多少钱么?
Input
输入数据有多组,到文件结束。
对于每一组数据,第一行为n,m。m≤n≤10000。
接下来有n行,每行两个数代表石头的w与p。
Output
对于每组数据,输出有一行为一个数,为答案。
Example Input
4 2
1 2
1 3
2 2
3 4
Example Output
5
Hint
Author
#include<stdio.h>
#include<string.h>
struct no
{
int m;
int p;
}s[10086];
int cmp(struct no x,struct no y) //规定小于关系
{
if(x.m==y.m)
return x.p<y.p;
else
return x.m > y.m;
}
typedef int (*P)(struct no,struct no);
int part(struct no s[],int low,int high,P cmp)
{
struct no key = s[low];
while(low<high)
{
while(low<high&&cmp(s[high],key))
high--;
s[low] = s[high];
while(low<high&&!cmp(s[low],key))
low++;
s[high] = s[low];
}
s[low] = key;
return low;
}
void mysort(struct no s[],int low,int high,P cmp)
{
if(low<high)
{
if(low<high)
{
int m = part(s,low,high,cmp);
mysort(s,low,m-1,cmp);
mysort(s,m+1,high,cmp);
}
}
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%d %d",&s[i].m,&s[i].p);
}
mysort(s,0,n-1,cmp);
int sum = 0;
for(int i=0;i<m;i++)
{
sum +=s[i].p;
}
printf("%d\n",sum);
}
}
/***************************************************
User name: mack
Result: Accepted
Take time: 60ms
Take Memory: 260KB
Submit time: 2017-04-20 17:07:08
****************************************************/