poj1456Supermarket 贪心解法
来源:互联网 发布:vmware安装mac os x 编辑:程序博客网 时间:2024/06/10 13:21
传送门
题意:
给出n个产品的利润和贩卖截止时间,单位时间只能贩卖一个产品.求最终获得的最大利润.
分析:
要获得最大利润,在同一时间肯定优先考虑利润较大的产品;
所以可以根据利润进行排序,优先处理利润大的产品,最终获得的肯定是最大利润.
#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <vector>#include <bitset>#include <list>#include <sstream>#include <set>#include <functional>using namespace std;#define INF 0x3f3f3f3f#define MAX 100typedef long long ll;struct node{int p,d;bool operator < (const node&a)const{return p < a.p;}}v[10005];int n;int place[10005];int use[10005];int main(int argc, char const* argv[]){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);#endifwhile (scanf("%d",&n)!=EOF){ll ans = 0;for (int i = 0; i < n; i += 1) {scanf("%d%d",&v[i].p,&v[i].d);}sort(v,v+n);memset(place,0,sizeof(place));memset(use,0,sizeof(use));for (int i = n-1; i >= 0; i -= 1){int t = v[i].d;while (use[t]&&t>0){t = t-1;}if (t>0){ans += v[i].p;use[t] = 1;}}printf("%lld\n",ans);}return 0;}可以对上述代码进行优化,每次处理产品后记录相同截止时间的下个可用时间点.这样后面截止时间相同的就不用从后往前查找了.
#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <vector>#include <bitset>#include <list>#include <sstream>#include <set>#include <functional>using namespace std;#define INF 0x3f3f3f3f#define MAX 100typedef long long ll;struct node{int p,d;bool operator < (const node&a)const{return p < a.p;}}v[10005];int n;int nex[10005];int use[10005];int main(int argc, char const* argv[]){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);#endifwhile (scanf("%d",&n)!=EOF){ll ans = 0;memset(nex,0,sizeof(nex));memset(use,0,sizeof(use));for (int i = 0; i < n; i += 1) {scanf("%d%d",&v[i].p,&v[i].d);nex[v[i].d] = v[i].d;}sort(v,v+n);for (int i = n-1; i >= 0; i -= 1){int t = nex[v[i].d];while (use[t]&&t>0) t = t-1;//找到可用时间点if (t>0){ans += v[i].p;use[t] = 1;nex[v[i].d] = t-1;}}printf("%lld\n",ans);}return 0;}
阅读全文
0 0
- poj1456Supermarket 贪心解法
- POJ1456Supermarket(贪心+优先队列)
- 贪心算法uva11729解法
- hdu5375Gray code (贪心解法
- poj3253Fence Repair解法之贪心
- 基于贪心的背包解法
- 多机调度问题的贪心解法
- 买书折扣问题的贪心解法
- 田忌赛马: POJ 2287(贪心解法)
- hdu1052田忌赛马,贪心解法
- POJ1328的快排+贪心解法
- [CQOI2012]组装 (非贪心,数学解法)
- m-图着色问题(贪心解法)
- JS贪心解法入门:找零问题
- TYVJ 1048 田忌赛马 贪心解法
- HDU1050Moving Tables(贪心,树状数组两种解法)
- POJ 3253 fence Repair 一个很有趣的贪心解法
- 活动选择问题的动归和贪心解法
- C语言实验——判断素数(循环结构)
- BST 二叉搜索树 C++ 算法导论
- python中字母与ascii码的相互转换
- day-23-SQL语句_综合案例
- 如何用Redis做LRU-Cache
- poj1456Supermarket 贪心解法
- 神奇的八秒定律
- day-24-HTML
- Java用 * 打印图形-菱形、空心菱形、三角形等
- ubuntu创建新用户并增加管理员权限详解
- 百度云的高速下载技巧系列1---油猴插件和脚本(PC)
- 关于色光三原色与颜料三原色
- Python数据分析与挖掘实战代码纠错 代码3-3
- ElasticSearch之基于地理位置的搜索