浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html

题目传送门:http://poj.org/problem?id=1456

堆作法:https://www.cnblogs.com/AKMer/p/10287566.html

贪心的想,我们尽量先把利润高的商品安排了。假如把利润高的物品安排在第\(x\)天,显然比安排任何利润比他低的商品在第\(x\)天更优。这就保证了我们先卖利润高的物品的贪心正确性。另外,如果能尽量把安排的日子靠后就靠后,这样拥有决策包含性的性质,这种贪心显然是最优的。

时间复杂度:\(O(nlogn)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std; const int maxn=1e4+5; int n,ans;
int fa[maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} struct Com {
int v,t; bool operator<(const Com &a)const {
return v>a.v;
}
}p[maxn]; int find(int x) {
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
} int main() {
while(~scanf("%d",&n)) {
for(int i=1;i<=n;i++)
p[i].v=read(),p[i].t=read();
sort(p+1,p+n+1),ans=0;
for(int i=1;i<=10000;i++)fa[i]=i;
for(int i=1;i<=n;i++) {
int day=find(p[i].t);
if(!day)continue;//说明没有空闲日子卖这个商品了
ans+=p[i].v,fa[day]=day-1;
}
printf("%d\n",ans);
}
return 0;
}

最新文章

  1. 把汉字转换为html实体编码
  2. C# WebBrowser控件使用教程与技巧收集
  3. JDBC Boilerplate
  4. [日常训练]curves
  5. css-a与a:link的一些认识
  6. iOS学习笔记---oc语言第四天
  7. ping命令找不到
  8. set,multiset容器类型
  9. Linux系统学习笔记:文件描述符标志
  10. C程序的存储空间布局
  11. KVO 进阶
  12. 关于SSDT
  13. IDEA破解 Intellij IDEA license server 激活(可用)
  14. svn提交出现错误 svn: Working copy &#39;D:\...&#39;locked.
  15. zoj3469 区间dp好题
  16. es-03-DSL的简单使用
  17. 使用node.js的开发框架express创建一个web应用
  18. amcharts categoryAxis
  19. uva11354 LCA+最小生成树+dp
  20. [UI] 精美UI界面欣赏[6]

热门文章

  1. redis memcached MongoDB
  2. Java多线程的集合类
  3. 权限【TLCL】
  4. springboot项目支持war部署tomcat
  5. 对象存储API
  6. DNS安装配置
  7. php:使用XHProf查找PHP性能瓶颈
  8. asp.net 基础知识
  9. Mysql时间戳函数和ip转换函数
  10. Container容器调用构造函数