点击打开链接
商人小鑫
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
小鑫是个商人,当然商人最希望的就是多赚钱,小鑫也一样。
这天,他来到了一个遥远的国度。那里有着n件商品,对于第i件商品需要付出ci的价钱才能得到。当然,对于第i件商品,小鑫在自己心中有一个估价pi:代表着当他买下这件商品后带回他的国家可以卖出的价格。小鑫只能带回m件商品,你能帮他计算一下他最多能赚多少钱么?
这天,他来到了一个遥远的国度。那里有着n件商品,对于第i件商品需要付出ci的价钱才能得到。当然,对于第i件商品,小鑫在自己心中有一个估价pi:代表着当他买下这件商品后带回他的国家可以卖出的价格。小鑫只能带回m件商品,你能帮他计算一下他最多能赚多少钱么?
Input
输入有多组,到文件结束。(注:数据有很多组,请用高效率算法)
对于每一组数据。第一行是n,m。m≤n≤10000000。
紧接着有n行,每一行有两个数 c ,p。第i行代表着ci,pi。ci≤pi
数据都在int范围内 。
Output
对于每组输入数据只输出一行一个数,代表小鑫能赚多少钱。
Example Input
4 2 1 2 1 3 2 2 3 4
Example Output
3
Hint
Author
lin
#include <bits/stdc++.h>
using namespace std;
int a[10000000+140];///每件商品的利润
int cmd(int a,int b)
{
return a>b;
}
int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=0; i<n; i++)
{
int t1,t2;
cin>>t1>>t2;
a[i]=t2-t1;///计算出利润
}
sort(a,a+n,cmd);
int sum=0;///利润
for(int i=0; i<m; i++)
{
sum+=a[i];
}
cout<<sum<<endl;
}
return 0;
}
/**********
Example Input
4 2
1 2
1 3
2 2
3 4
Example Output
3
-*/
就是身为菜鸟的我也要说一句这题真是:淼淼淼淼淼淼淼淼
#include <bits/stdc++.h>
using namespace std;
int a[10000000+140];///每件商品的利润
int cmd(int a,int b)
{
return a>b;
}
int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=0; i<n; i++)
{
int t1,t2;
cin>>t1>>t2;
a[i]=t2-t1;///计算出利润
}
sort(a,a+n,cmd);
int sum=0;///利润
for(int i=0; i<m; i++)
{
sum+=a[i];
}
cout<<sum<<endl;
}
return 0;
}
/**********
Example Input
4 2
1 2
1 3
2 2
3 4
Example Output
3
-*/