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无状态原则
目录 目录 前言 无状态原则 Web服务的状态 基于状态的Web服务 基于无状态的Web服务 总结两者的区别 前言 在上篇RESTful基础知识中整体的介绍了RESTful架构设计思想的框架,在往后的 ...
- 用Delphi从内存流中判断图片格式[转]
http://blog.163.com/tfn2008%40yeah/blog/static/110321319201222243214337/ 用Delphi从内存流中判断图片格式[转] 2012- ...
- mysql-M-S-S模型 中继器 级联
1.基础环境 三台虚机并且安装有mysql 并且同步好数据库 2.主服务器-创建账号并授权 mysql> create user 'mslave'@'X.X.X.X' identified by ...
- JDBC常用接口、类介绍
JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager 管理JDBC驱动的服务类,主要通过它获取Connectio ...
- (appium+python)UI自动化_02_appium启动手机app
前提:需先安装配置好appium+python自动化环境,已配置好环境的小伙伴可以参考以下步骤启动Android app,具体步骤如下: 一.USB连接手机 (1)手机USB连接电脑 (2)手机打开开 ...
- 字符串模式匹配算法系列(二):KMP算法
算法背景: KMP算法是由Donald Knuth和Vaughan Pratt于1970年共同提出的,而James H.Morris也几乎同时间独立提出了这个算法.因此人们将其称作“克努特-莫里斯-普 ...
- 获取EasyUI日期输入框的值
var date = $('#PurDate').datebox('getValue');
- golang的数据类型之整型类型
数据类型: 整数 : int, int32, int64, uint, uint32, uint64 字符串 : string 布尔:bool 浮点:float32 float64 uint 表示无符 ...
- 两数相加(java版本)
(一).单链表实现 package com.lin.leetcode.addTwoNumbers; /** * Created by Yaooo on 2019/8/26. */ public cla ...
- [BZOJ 3991][SDOI2015]寻宝游戏(dfs序)
题面 小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择一个村庄,瞬间转移到这个村庄,然后可以任意在地图的道路 ...