POJ 1456 贪心
来源:互联网 发布:windows ad域博客 编辑:程序博客网 时间:2024/06/05 16:12
首先按照最晚售出时间对所有商品排序。然后从最晚售出时间最早的开始考虑。如果在之前还有时间段是空着的,那么我们就把这个商品在那个时间段出售。如果之前没有时间空着了,那么我们在前面找一个价格最小的商品,如果当前商品价值大于最小价值,就替换那个最小价值的商品。如果当前商品价值比之前最小价值还要小,就不替换。
代码如下:
#include <algorithm>#include <iostream>#include <iterator>#include <sstream>#include <fstream>#include <istream>#include <ostream>#include <complex>#include <cstring>#include <utility>#include <cstdlib>#include <cstdio>#include <vector>#include <string>#include <cctype>#include <ctime>#include <cmath>#include <queue>#include <stack>#include <list>#include <new>#include <set>#include <map>using namespace std;typedef pair<int, int> pii;const int INF = 0x3f3f3f3f;const int maxn = 10005;struct data{int t, p;};data a[maxn];int ans[maxn];bool cmp(data a, data b){ return a.t < b.t;}int main(){ //freopen("1.txt", "r", stdin); int n; while (~scanf("%d", &n)){ for (int i = 0; i < n; i++){ scanf("%d%d", &a[i].p, &a[i].t); } sort(a, a + n, cmp); memset(ans, 0, sizeof(ans)); int sum = 0; for (int i = 0; i < n; i++){ int ok = 0; for (int j = 1; j <= a[i].t; j++){ if (ans[j] == 0){//如果之前有空余时间,就把当前商品在那个时候出售 ans[j] = a[i].p; sum += a[i].p; ok = 1; break; } } if (!ok){//如果之前没有空余时间,就找到之前价值最小的商品和当前商品比较 int mmin = INF, Index; for (int j = 1; j <= a[i].t; j++){ if (ans[j] < mmin){ mmin = ans[j]; Index = j; } } if (mmin < a[i].p){ sum = sum - ans[Index] + a[i].p; ans[Index] = a[i].p; } } } printf("%d\n", sum); } return 0;}
0 0
- POJ-1456 Supermarket【贪心】
- POJ 1456:Supermarket【贪心】
- POJ 1456 (贪心)
- POJ 1456 贪心
- poj 1456 贪心
- POJ-1456 Supermarket 贪心
- POJ-1456-Supermarket [贪心]
- POJ 1456 Supermarket hash+贪心
- POJ 1456 Supermarket(贪心)
- POJ 1456 Supermarket(贪心)
- POJ-1456 Supermarket (贪心)
- Supermarket POJ 1456 (贪心)
- POJ 1456 Supermarket(贪心)
- POJ-1456---Supermarket (贪心)
- 【经典贪心】poj 1456 Supermarket (贪心, 并查集)
- poj 1456 Supermarket (贪心, 并查集)
- POJ 1456 Supermarket【贪心 + 并查集】
- poj 1456 贪心+并查集优化
- 模式识别笔记(1)
- 第九周项目3--稀疏矩阵的三元组表示的实现及应用2
- 从头安装Ubuntu记录:安装系统
- Centos 6.5将python 2.6 升级到 2.7
- gdb 条件断点
- POJ 1456 贪心
- CCF201503-3 节日 JAVA实现
- JAVA 之 java概述
- 微信公众号开发:PHP,对接微信公众账号的测试文档wx_sample.php的解析
- 第九周项目4--广义表算法库及应用1
- 数据库基础知识2
- spark与hadoop对比
- [Linux]多线程同步之pthread_cond_wait()学习笔记
- c语言基本数据类型