代码笔记:排序算法初级比较
来源:互联网 发布:武汉矩阵科技有限公司 编辑:程序博客网 时间:2024/06/06 04:28
Think:
1感觉自己的代码可能会超时,但是提交AC,自己需要思考更优化的算法
2贪心算法思想:由当前的最优解最终得到最终最优解(局部最优解得到最终最优解)
商人小鑫
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
小鑫是个商人,当然商人最希望的就是多赚钱,小鑫也一样。
这天,他来到了一个遥远的国度。那里有着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
以下为Accepted代码——sort()函数
#include <bits/stdc++.h>using namespace std;int a[10000004];int main(){ int n, m, i, c, p, sum; while(scanf("%d %d", &n, &m) != EOF) { sum = 0; for(i = 0; i < n; i++) { scanf("%d %d", &c, &p); a[i] = p-c; } sort(a, a+n); for(i = n-1; i >= n-m; i--) sum += a[i]; printf("%d\n", sum); } return 0;}/***************************************************User name: Result: AcceptedTake time: 24msTake Memory: 560KBSubmit time: 2017-04-15 15:55:14****************************************************/
以下为Accepted代码——sort()函数
#include <bits/stdc++.h>using namespace std;int a[10000004];int main(){ int n, m, i, c, p, sum; while(scanf("%d %d", &n, &m) != EOF) { sum = 0; for(i = 0; i < n; i++) { scanf("%d %d", &c, &p); a[i] = p-c; } sort(a, a+n, greater<int>()); for(i = 0; i < m; i++) sum += a[i]; printf("%d\n", sum); } return 0;}/***************************************************User name: Result: AcceptedTake time: 40msTake Memory: 556KBSubmit time: 2017-04-15 15:59:37****************************************************/
以下为Accepted代码——qsort()函数
#include <bits/stdc++.h>using namespace std;int a[10000004];int cmp(const void *a, const void *b){ return ((*(int *)a) - (*(int *)b));}int main(){ int n, m, i, c, p, sum; while(scanf("%d %d", &n, &m) != EOF) { sum = 0; for(i = 0; i < n; i++) { scanf("%d %d", &c, &p); a[i] = p-c; } qsort(&a[0], n, sizeof(a[0]), cmp); for(i = n-1; i >= n-m; i--) sum += a[i]; printf("%d\n", sum); } return 0;}/***************************************************User name: Result: AcceptedTake time: 52msTake Memory: 960KBSubmit time: 2017-04-15 15:57:49****************************************************/
以下为Accepted代码——priority_queue()容器
#include <bits/stdc++.h>using namespace std;int main(){ int n, m, i, c, p, sum; while(scanf("%d %d", &n, &m) != EOF) { sum = 0; priority_queue<int, vector<int>, greater<int> > q; for(i = 0; i < n; i++) { scanf("%d %d", &c, &p); if((int)q.size() < m) q.push(p-c); else { q.push(p-c); q.pop(); } } while(!q.empty()) { sum += q.top(); q.pop(); } printf("%d\n", sum); } return 0;}/***************************************************User name: Result: AcceptedTake time: 44msTake Memory: 164KBSubmit time: 2017-04-15 16:11:04****************************************************/
0 0
- 代码笔记:排序算法初级比较
- 比较排序算法笔记
- 《算法》阅读笔记-2.1初级排序算法
- 算法复习笔记 | 排序算法比较
- 初级排序算法实现
- 初级排序算法总结
- 初级排序算法
- 排序算法之初级排序
- 排序算法之初级排序
- (算法)初级排序算法
- 三种初级排序算法(冒泡、选择、直接插入)java实现及其性能比较
- 初级算法排序(一)
- 算法(2.1 初级排序)
- 初级排序算法(algs4)
- 初级排序算法(二)
- 几个初级排序算法总结
- 排序算法-比较排序
- [算法学习笔记]几个排序算法的比较
- ffmpeg--学习笔记1-基本使用
- js校验座机和手机
- 深度学习中激活函数
- Pybrain学习笔记-3 使用Pybrain构建前馈神经网络
- ApplicationContext——ClassPathXmlApplication(一)
- 代码笔记:排序算法初级比较
- 输出3~100的所有素数
- 第一天学习_笔记
- C++文件读写
- JZOJ 4.15 1663——【AHOI2009】维护序列
- java求三个整数的最大值
- 登山
- iOS 开发中的八种锁(Lock)
- 努力做更好的自己