supermarket
题意:给你n个商品,商品的利润和商品的过期时间,商品必须在过期时间内卖才能算利润,每天只能卖一件商品,问利润最大值。
思路:用并查集+贪心的思路,先给商品从大到小排序,然后选择过期时间的根节点,再将根节点和根节点-1的时间merge,当根节点不为0,累计加上利润
最后求得最大值。因为过期时间具有传递性,你在第n天卖等价于第n-1天卖,很容易证得思路的正确性。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
#define ll long long
using namespace std;
struct point
{
int x;
int y;
}a[];
int fa[];
bool cmp(point a,point b)
{
return a.x>b.x;
}
int get(int k)
{
return fa[k]==k?k:fa[k]=get(fa[k]);
}
void merge(int x,int y)
{
fa[get(x)]=get(y);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
for(int i=;i<=;i++)
{
fa[i]=i;
}
sort(a,a+n,cmp);
/* for(int i=0;i<n;i++)
{
printf("x:%d y:%d\n",a[i].x,a[i].y);
}*/
int sum=;
for(int i=;i<n;i++)
{
int r=get(a[i].y);
// printf("r:%d\n",r);
if(r!=)
{
sum+=a[i].x;
merge(r,r-);
}
}
printf("%d\n",sum);
}
}
supermarket的更多相关文章
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- Supermarket POJ - 1456
A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...
- codeforces 815C Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...
- codeforces round #419 E. Karen and Supermarket
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...
- Codeforces 815C Karen and Supermarket 树形dp
Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...
- G - Supermarket
A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...
- POJ 1456 - Supermarket - [贪心+小顶堆]
题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...
- CF815C Karen and Supermarket
题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...
- POJ-1456 Supermarket(贪心,并查集优化)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10725 Accepted: 4688 Descript ...
- NYOJ 208 Supermarket (模拟+并查集)
题目链接 描述 A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Pr ...
随机推荐
- RESTful再理解
目录 目录 前言 RESTful的目的 REST的含义 表现层 状态转化 无状态协议HTTP 最后 前言 这是在经过一段时间的积累后,对RESTFul框架的再一次更深入的理解.希望能够将零散的知识点连 ...
- 在reshard过程中,将会询问reshard多少slots:
How many slots do you want to move (from 1 to 16384)?,取值范围为1~16384,其中16384为redis cluster的拥有的slots总数, ...
- flask为blueprint增加error_handler
对整个app增加errorhandler,只需如下: @portal_page.errorhandler(404) def page_not_found(error): cats = Category ...
- vue-router 路由元信息 终于搞明白了路由元信息是个啥了
vue-router 路由元信息:https://blog.csdn.net/wenyun_kang/article/details/70987840 终于搞明白了路由元信息是个啥了:https:// ...
- Python入门习题5.蒙特卡罗方法计算圆周率
#CalPi.py from random import random from math import sqrt from time import clock DARTS = 10000000 hi ...
- Python入门习题1.温度转换
这一节的课堂例题为: 例1.编写一个Python程序,完成摄氏度到华氏度,华氏度到摄氏度的温度转换. 解: (1)分析问题:利用程序实现温度转换,由用户输入温度值,程序给出输出结果. (2)划分边界: ...
- Codeforces 1110E (差分)
题面 传送门 分析 一开始考虑贪心和DP,发现不行 考虑差分: 设d[i]=c[i+1]-c[i] (i<n) 那么一次操作会如何影响差分数组呢? \(c[i]'=c[i+1]+c[i-1]-c ...
- hdu6341 Problem J. Let Sudoku Rotate (dfs)
题目传送门 题意: 给你16个16宫格的数独,里面是0~F,你可以逆时针旋转里面的每个16宫格 问你它是从标准数独逆时针旋转多少次得到? 思路: 可以知道每个16宫已经是标准的了,接下来只要考虑每行. ...
- 源码分析--ConcurrentHashMap与HashTable(JDK1.8)
ConcurrentHashMap和Hashtable都是线程安全的K-V型容器.本篇从源码入手,简要说明它们两者的实现原理和区别. 与HashMap类似,ConcurrentHashMap底层也是以 ...
- Git--02 Devops介绍及git安装部署
目录 1. Devops介绍 01. 运维介绍 02. Devops是什么 03. Devops能干嘛 04. Devops如何实现 2. Git版本控制系统 01. 版本控制系统简介 02. 为什么 ...