POJ 1456
来源:互联网 发布:js中NaN 编辑:程序博客网 时间:2024/06/05 18:22
大体上的思路是这样的:首先,按照售价顺序排序,然后从最大的开始卖,并且都尽量晚卖出,用并查集标记就好,比较水,不多说,直接上代码:
#include <stdio.h>#include <string.h>#include <algorithm>>using namespace std;pair<int,int> a[10005];int father[20005];int find(int x){ if(x==father[x]) return x; else return father[x]=find(father[x]);}int main(){ int n; int i,j,k; int ans; while(scanf("%d",&n)!=EOF){ for(i=0;i<20000;i++) father[i]=i; for(i=0;i<n;i++){ scanf("%d%d",&a[i].first,&a[i].second); } sort(a,a+n); ans=0; for(i=n-1;i>=0;i--){ // printf("yes1\n"); int ff=find(a[i].first+10000); //printf("yes2\n"); //printf("%d %d\n",fs,a[i].second); while(a[i].second){ int date=a[i].second; int f=find(date); if(f==date){ father[f]= ff; ans+=a[i].first; break; } a[i].second--; } } printf("%d\n",ans); } return 0;}
好吧 我用上面的代码过了之后回去仔细想了下。。。。发现根本不用并查集。。。。直接用judge数组标记就好了。。。这件事告诉我们还是别用百度找题目的好啊。。。。
就是一道贪心水题。。。
#include <stdio.h>#include <string.h>#include <algorithm>>using namespace std;pair<int,int> a[10005];int judge[20005];int main(){ int n; int i,j,k; int ans; while(scanf("%d",&n)!=EOF){ memset(judge,0,sizeof(judge)); for(i=0;i<n;i++){ scanf("%d%d",&a[i].first,&a[i].second); } sort(a,a+n); ans=0; for(i=n-1;i>=0;i--){ // printf("yes1\n"); //printf("yes2\n"); //printf("%d %d\n",fs,a[i].second); while(a[i].second){ int date=a[i].second; if(judge[a[i].second]==0){ judge[a[i].second]=1; ans+=a[i].first; break; } a[i].second--; } } printf("%d\n",ans); } return 0;}
0 0
- poj 1456
- poj 1456
- POJ 1456
- poj 1456
- POJ 1456
- POJ 1456
- 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 Supermarket
- poj 1456 DP
- POJ 1456 Supermarket
- C++ UNICODE 文件读写相关(转载)
- oracle 创建用户
- TCP速率与窗口,带宽,RTT之间的关系
- 第四周项目二-三角形类2
- HDU 4584(2013杭州邀请赛I题-水)
- POJ 1456
- 学习IOS开发的第11天
- 两个关于a,b的面试题
- 黑马程序员--高新技术<一>eclipse开发工具及相关知识加强
- System.gc
- 测井曲线绘图显示算法总结——双缓冲技术
- php 二维码生成
- Struts2.x+Spring3.x+Hibernate4.x 整合(五)Struts 升级到 2.3.15.3
- jQuery学习笔记(1)