poj1456 Supermarket
来源:互联网 发布:linux永久设置密码 编辑:程序博客网 时间:2024/06/05 17:52
原题: http://poj.org/problem?id=1456
解法1:stl+ priority_queue 很好的思路 79ms
#include<cstdio>#include<algorithm>#include<queue>using namespace std;struct W{int v;int d;}w[10010];bool operator <(W a,W b){return a.v<b.v;}int max(int a,int b){if(a>=b)return a;return b;}int cmp(W a,W b){return a.d>b.d;}int main(){int n;while(~scanf("%d",&n)){priority_queue<W>q1;int m=0;for(int i=0;i<n;i++){scanf("%d %d",&w[i].v,&w[i].d);m=max(w[i].d,m);}sort(w,w+n,cmp);int sum=0,pos=0;;for(int t=m;t>=1;t--) //枚举每一天{while(pos<n && w[pos].d==t){q1.push(w[pos]);pos++;}if(!q1.empty()){sum=sum+q1.top().v;q1.pop();}}printf("%d\n",sum);}return 0;}
解法2: 157ms
#include<cstdio>#include<algorithm>using namespace std;struct W{int v;int d;}w[10010];int cmp(W a,W b)//先按时间排序小->大,留够时间给后面的商品卖 {if(a.v!=b.v){return a.v>b.v;//再按价值排序大->小,尽量在范围内卖出最大价值的商品 }else{return a.d<b.d;}}int max(int a,int b){if(a>=b)return a;return b;}int main(){int n;while(~scanf("%d",&n)){for(int i=0;i<n;i++){scanf("%d %d",&w[i].v,&w[i].d);}sort(w,w+n,cmp);int dp[10010]={0};int sum=0;for(int i=0;i<n;i++){int d=w[i].d;while(dp[d]!=0 && d>=1){d--;}if(d>=1){dp[d]=w[i].v;sum=sum+dp[d];}}printf("%d\n",sum);}return 0;}
阅读全文
0 0
- POJ1456 Supermarket
- poj1456 - Supermarket
- POJ1456 Supermarket
- 【poj1456 】supermarket
- poj1456 Supermarket
- POJ1456 Supermarket 贪心 DP
- poj1456 Supermarket(贪心)
- POJ1456 Supermarket (贪心)
- poj1456 Supermarket【优先队列】
- POJ1456 Supermarket简单贪心(无优化)题解
- poj1456
- POJ1456
- poj1456
- poj1456
- Supermarket
- Supermarket
- Supermarket
- Supermarket
- 深入浅出Redis任务队列
- 字符编码笔记
- jsday11补充二(弹来弹去的广告 关键是Interval计时和来回判断)
- C++实现的线程池
- 主席树 两篇
- poj1456 Supermarket
- pandas中Groupby的使用(三)-根据dtype对列进行分组
- Bagging与随机森林算法原理小结(详解)
- Retrofit
- D3系列第一弹——绘制饼图
- CentOS 系统简易搭建FTP服务(四步足矣)
- java学习_javaIo
- 数据结构编程笔记二十:第七章 图 最小生成树算法的实现
- Android studio使用.9图片报错