bzoj1528 sam-Toy Cars(贪心,优先队列)
「BZOJ1528」[POI2005] sam – Toy Cars
Description
Input
Output
Sample Input
1
2
3
1
3
1
2
Sample Output
/*
贪心,优先队列
首先这个贪心啊,每次删除下次最晚再玩的玩具,要维护next表示下次的位置。然后优先队列。
这样这个玩具这段时间内不会对答案有贡献,且时间最长。
开始想成了每次删除使用总数最少的玩具,发现这个反例大大的有,还不好写。
然后就是代码,问题来自于如何维护已经在队列里的玩具的next
可以每次碰到在队列里的的玩具时把k扩大1,把这个玩具再次放到优先队列。
此时这个玩具的next一定比已经在队列里的这个玩具的next大(好拗口)。
这样就起到了更新next的效果。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> #define N 500001 using namespace std;
int n,m,k,ans;
int cnt[N],c[N],Last[N];
bool vis[N];
struct node{
int num,nxt;
friend bool operator < (node x,node y)
{
return x.nxt<y.nxt;
}
}a[N];
priority_queue<node>q; int main()
{
scanf("%d%d%d",&n,&k,&m);
for(int i=;i<=m;i++)
scanf("%d",&a[i].num);
for(int i=;i<=n;i++) Last[i]=m+;
for(int i=m;i>=;i--)
a[i].nxt=Last[a[i].num],Last[a[i].num]=i;
for(int i=;i<=m;i++)
{
if(vis[a[i].num]){k++;q.push(a[i]); continue;}
else
{
if(q.size()==k)
{
node x=q.top();q.pop();
vis[x.num]=;
}
q.push(a[i]);
ans++;vis[a[i].num]=;
}
}
printf("%d\n",ans);
return ;
}
bzoj1528 sam-Toy Cars(贪心,优先队列)的更多相关文章
- [BZOJ1528][POI2005]sam-Toy Cars(贪心)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1528 分析:这个贪心很好想,因为每次如果加入一种玩具,那么必须要删掉一种玩具,就变成了 ...
- P3419 [POI2005]SAM-Toy Cars / SP688 SAM - Toy Cars
一道很妙的贪心题 题面 我们考虑当我们插入时会面临的两种情况 当地上的玩具,不满 \(k\) 个时,那我们直接放就可以了. 当满了 \(k\) 个的时候,我们就要从地上拿出一个来给当前的腾位置. 这就 ...
- bzoj1528[POI2005]sam-Toy Cars*&&bzoj1826[JSOI2010]缓存交换
bzoj1528[POI2005]sam-Toy Cars bzoj1826[JSOI2010]缓存交换 题意: Jasio有n个不同的玩具,它们都被放在了很高的架子上,地板上不会有超过k个玩具.当J ...
- hihoCoder 1309:任务分配 贪心 优先队列
#1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, ...
- 周赛-Toy Cars 分类: 比赛 2015-08-08 15:41 5人阅读 评论(0) 收藏
Toy Cars time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列
C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- Codeforces Round #303 (Div. 2) A. Toy Cars 水题
A. Toy Cars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/problem ...
随机推荐
- hdu1978
{ scanf( { scanf( ;i<n;i++) ;j<m;j++) scanf( ...
- 【git】Git 提示fatal: remote origin already exists 错误解决办法
今天使用git 添加远程github仓库的时候提示错误:fatal: remote origin already exists. 最后找到解决办法如下: 1.先删除远程 Git 仓库 $ git re ...
- Gym100812 L 扩展欧几里得
L. Knights without Fear and Reproach time limit per test 2.0 s memory limit per test 256 MB input st ...
- POJ2632 Crashing Robots 解题报告
Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is neede ...
- noip 2011
铺地毯 题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号从小到大的顺 ...
- SQLServer2008 快捷键集合
CTRL + SHIFT + B 生成解决方案 CTRL + F7 ...
- anaconda是个啥?
Anaconda具有跨平台(win/mac/linux).包管理(类似于pip).环境管理(类似于virtualenv)的特点,因此很适合快速在新的机器上部署Python环境 Anaconda利用工具 ...
- Linux学习系列之MySQL备份
MySQL排除表备份 #!/bin/bash #created by 90root #date: 20160809 date_y=$(date +%Y) date_m=$(date +%m) time ...
- android PercentRelativeLayout 支持百分比来设置控件的宽高
Android 最终官方支持按百分比来设置控件的宽高了. 我们先来看看效果: 看一下布局: PercentRelativeLayout <android.support.percen ...
- Spring MVC不要在@Service bean中保存状态
先看这么一段代码: @Service public class AccountService { private String message; public void foo1() { if (tr ...