Supermarket POJ - 1456(贪心)
题目大意:n个物品,每个物品有一定的保质期d和一定的利润p,一天只能出售一个物品,问最大利润是多少?
题解:这是一个贪心的题目,有两种做法。
1 首先排序,从大到小排,然后每个物品,按保质期从后往前找,找到第一个没被占用的日期,然后出售。
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll N=1e4+;
struct stu{
ll a,b;
bool friend operator < (const stu &x,const stu &y){
if(x.a!=y.a) return x.a>y.a;
else return x.b>y.b;
}
}arr[N];
bool mark[N];
int main(){
ll n;
while(cin>>n){
for(ll i=;i<=n;i++){
scanf("%d%d",&arr[i].a,&arr[i].b);
}
memset(mark,,sizeof mark);
ll sum=;
sort(arr+,arr++n);
for(ll i=;i<=n;i++){
if(mark[arr[i].b]==) {
sum+=arr[i].a;
mark[arr[i].b]=;
}
else {
for(ll j=arr[i].b;j>=;j--){
if(!mark[j]){
sum+=arr[i].a;
mark[j]=;
break;
}
}
}
}
cout<<sum<<endl;
} return ;
}
2 用并查集来维护一下日期,如果说日期a被占用了,fa[a]=a-1,就是往前提上一天,
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll N=1e4+;
struct stu{
ll a,b;
bool friend operator < (const stu &x,const stu &y){
if(x.a!=y.a) return x.a>y.a;
else return x.b>y.b;
}
}arr[N];
ll fa[N];
ll find(ll x){
if(fa[x]==-) return x;
else return fa[x]=find(fa[x]);
}
int main(){
ll n;
while(cin>>n){
for(ll i=;i<=n;i++){
scanf("%lld%lld",&arr[i].a,&arr[i].b);
}
sort(arr+,arr++n);
memset(fa,-,sizeof fa);
ll sum=;
for(ll i=;i<=n;i++){
ll c=find(arr[i].b);
if(c>){
sum+=arr[i].a;
fa[c]=c-;
}
}
cout<<sum<<endl;
}
return ;
}
反思:这是完全是一个贪心的题目,并查集只是一种让他跑的更快的工具而已,可能是专项训练的缘故,思维被束缚了,不敢往贪心方向想....
Supermarket POJ - 1456(贪心)的更多相关文章
- Supermarket POJ - 1456 贪心+并查集
#include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...
- (并查集 贪心思想)Supermarket -- POJ --1456
链接: http://poj.org/problem?id=1456 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- POJ 1456 (贪心+并查集) Supermarket
有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 ...
- Day5 - H - Supermarket POJ - 1456
A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...
- POJ - 1456 贪心+并查集
做法一:直接贪心,按照利润排序,然后直接尽量给每个活动安排到最晚的时间即可.时间复杂度O(n * d)当d都为10000时,很容易超时.由于这题数据比较水,所有贪心未超时. AC代码 #include ...
- Supermarket POJ - 1456
A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...
- poj 1456 贪心+STL
题意:有n个商品,每个商品如果能在截止日期之前售出就会获得相应利益,求能获得的最大利益 一开始对每个时间进行贪心,后来发现后面的商品可以放到之前来卖,然后就wa了 这里就直接对价格排序,把物品尽量放到 ...
- POJ - 1456 贪心 堆常用操作 注意细节
题意:给定n个商品的deadline和profit,求每天卖一件的情况下的最大获利 显然是一道贪心 按deadline从小到大排序好,动态维护小根(profit)堆的大小<=当前deadline ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
随机推荐
- rest_framework序列化,反序列化
序列化组件 from rest_framework.response import Response1.Response本质也是继承了httpresponse,比httpResponse还强大,传入一 ...
- Trie 字典树,hdu1251
参考博客:https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 字典树就是单词树,顺着一条路径到达终止结点就形成一个单词,该单词的前缀包含在这 ...
- 支持向量机(Support Vector Machine)
本博客是针对Andrew NG在Coursera上发布的Machine Learning课程SVM部分的学习笔记. 目录 前言 最优化目标(Optimization Objective) 最大化边界的 ...
- Layui+Servlet+MyBatis+Mysql实现的大学生创新竞赛管理平台
项目简介 项目来源于:https://gitee.com/fly-liuhao/SCMS 原仓库中未上传jar包及登录异常,现将修改过的源码上传到百度网盘上. 链接:https://pan.baidu ...
- Windows下用Python你会几种copy文件的方法?
1. [代码]1. os.system ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import os import temp ...
- python实现十大经典排序算法
Python实现十大经典排序算法 代码最后面会给出完整版,或者可以从我的Githubfork,想看动图的同学可以去这里看看: 小结: 运行方式,将最后面的代码copy出去,直接python sort. ...
- 6.Maven构建过程的各个环节
构建过程中的各个环节 [1]清理:将以前编译得到的旧的class字节码文件删除,为下一次编译做准备 [2]编译:将Java源程序编译成class字节码文件 [3]测试:自动测试,自动调用junit程序 ...
- lly的瞬移方块(并查集)
lly的瞬移方块 Description llyllylly最近发明了一个叫瞬移方块的游戏,为啥llyllylly这么闲呢,这得从一只蝙蝠说起..... llyllylly决定给大家也分享一下这个游戏 ...
- gitlab问题
1.gitclone前http://10.10.11.4:9999/SZRDC/I`````````.git改为http://git.berchina.com:9999/~~~~``` 2.Cloni ...
- Python模块---制作属于自己的有声小说
操作环境 Python版本: anaconda3 python3.7.4 操作系统: Ubuntu19.10 编译器: pycharm社区版 用到的模块: pyttsx3,requests pysst ...