题目链接

题意:给你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的更多相关文章

  1. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  2. Supermarket POJ - 1456

    A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...

  3. 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 ...

  4. 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 ...

  5. Codeforces 815C Karen and Supermarket 树形dp

    Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...

  6. G - Supermarket

    A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...

  7. POJ 1456 - Supermarket - [贪心+小顶堆]

    题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...

  8. CF815C Karen and Supermarket

    题目链接 CF815C Karen and Supermarket 题解 只要在最大化数量的前提下,最小化花费就好了 这个数量枚举ok, dp[i][j][1/0]表示节点i的子树中买了j件商品 i ...

  9. POJ-1456 Supermarket(贪心,并查集优化)

    Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10725 Accepted: 4688 Descript ...

  10. NYOJ 208 Supermarket (模拟+并查集)

    题目链接 描述 A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Pr ...

随机推荐

  1. 测开之路七十六:性能测试蓝图之html

    <!-- 继承base模板 -->{% extends 'base.html' %} {% block script %} <!-- 从cdn引入ace edter的js --> ...

  2. PHP中的九大缓存技术

    1.全页面静态化缓存也就是将页面全部生成html静态页面,用户访问时直接访问的静态页面,而不会去走php服务器解析的流程.此种方式,在CMS系统中比较常见,比如dedecms: 一种比较常用的实现方式 ...

  3. SQL常用语句之数据库数据类型-篇幅2

    系统数据类型: 1.二进制数据类型      2.整数数据类型 3.浮点数据类型         4.精确小数数据类型 5.货币数据类型         6.日期/时间数据类型 7.字符数据类型    ...

  4. JavaScript List

    function List() {    this.listSize = 0;    this.pos = 0;    this.dataSource = [];    this.clear = fu ...

  5. 将IDEA工程代码提交到Github

    1.git安装配置 1.下载git https://git-scm.com/download/win 2.安装 傻瓜式安装即可,记住安装的目录 3.配置 2.配置git SSH 1.首先申请一个Git ...

  6. MySQL基础(创建库,创建表,添加数据)

    CREATE DATABASE 数据库名; CREATE TABLE student2(sno VARCHAR(20) NOT NULL PRIMARY KEY COMMENT"学号&quo ...

  7. 清除mac中自动记录的git用户名和密码

    应用程序-实用工具-双击钥匙串-右上角搜索github-右击选项删除

  8. [Bzoj3223][Tyvj1729] 文艺平衡树(splay/无旋Treap)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3223 平衡树处理区间问题的入门题目,普通平衡树那道题在维护平衡树上是以每个数的值作为维护 ...

  9. 1571. [Usaco2009 Open]滑雪课Ski

    传送门 可以想到 $dp$,设 $f[i][j]$ 表示当前等级为 $i$,时间为 $j$ 的最大滑雪次数 显然上课不会上让自己等级降低的课,所以第一维 $i$ 满足无后效性 然后直接枚举 $i,j$ ...

  10. JS中去除字符串空白符

    海纳百川,有容乃大 1.通过原型创建字符串的trim() //去除字符串两边的空白 String.prototype.trim=function(){ return this.replace(/(^\ ...