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 ...
随机推荐
- java实现扫二维码登录功能
哈哈哈 http://blog.sina.com.cn/s/blog_7f416edf0102vb8h.html http://blog.sina.com.cn/s/blog_7f416edf0102 ...
- Android BGABadgeView:BGABadgeLinearLayout以整体线性布局作为BadgeView(3)
Android BGABadgeView:BGABadgeLinearLayout以整体线性布局作为BadgeView(3) Android BGABadgeView不仅可以把某个View作为B ...
- [TyvjP1050] 最长公共子序列(DP)
传送门 f[i][j] 表示第 1 个串匹配到第 i 位,第 2 个串匹配到第 j 位的答案. f[i][j] = max(f[i - 1][j], f[i][j - 1]) (a[i] != ...
- sql 日期问题从周转换到日期
alter procedure p_date@year int=2005, --年份@week int=33, --第几周@firstday datetime =null output, ...
- 【BZOJ2142】礼物(扩展lucas定理,中国剩余定理合并方程)
题意:有n件礼物,m个人,每个人分别需要w[i]件礼物,求分礼物的不同方案数 mod P 提示:设P=p1^c1 * p2^c2 * p3^c3 * … *pt ^ ct,pi为质数. 1≤n≤10^ ...
- n个点中求任意两点组成斜率的最大值
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1100 首先按x坐标排序,然后相邻的三个点A,B,C 组成的三条直线必然有 ...
- POJ1328 Radar Installation 解题报告
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- easyui north 穿透
穿透layout的north 原理 穿透下面的层只要使该层的position置于fix即可 如果该层还有下一级,则下一层级的position置于absolute即可 示例代码 #menu.active ...
- 使用百度网盘实现自动备份VPS
http://ju.outofmemory.cn/entry/51536 经过轰轰烈烈的一轮网盘大战,百度网盘的容量已经接近无限(比如我的是3000多G ),而且百度网盘已经开放API,所以用来备份V ...
- Setting up Storm and Running Your First Topology
http://www.haroldnguyen.com/blog/2015/01/setting-up-storm-and-running-your-first-topology/ --------- ...