维护两个优先队列即可。要注意,当出现蛋糕的位置刚好在狗的位置时,存在右边。

注意输出大小写。。。

#include <iostream>
#include <queue>
#include <cstdio> using namespace std; struct RP{
int pos;
bool operator < (const RP &t)const{
if(pos>t.pos) return true;
return false;
}
};
struct LP{
int pos;
bool operator < (const LP &t)const{
if(pos<t.pos) return true;
return false;
}
}; priority_queue<LP>LeftPush;
priority_queue<RP>RightPush;
LP tmpL;
RP tmpR; void Clear(){
while(!LeftPush.empty())
LeftPush.pop();
while(!RightPush.empty())
RightPush.pop();
} int main(){
int T,P,N,POS,kase=0;
int op,position;
int direct,left,right;
bool flagL,flagR;
scanf("%d",&T);
int ans;
while(T--){
Clear();
POS=0;
direct=1;
ans=0;
scanf("%d%d",&P,&N);
while(N--){
scanf("%d",&op);
if(!op){
scanf("%d",&position);
if(position>=POS){
tmpR.pos=position;
RightPush.push(tmpR);
}
else if(position<POS){
tmpL.pos=position;
LeftPush.push(tmpL);
}
}
else{
flagR=flagL=false;
if(!RightPush.empty()){
flagR=true;
tmpR=RightPush.top();
}
if(!LeftPush.empty()){
flagL=true;
tmpL=LeftPush.top();
}
if(!flagR&&!flagL){
// direct=-1;
continue;
}
else if(!flagR&&flagL){
direct=-1;
ans+=(POS-tmpL.pos);
POS=tmpL.pos;
LeftPush.pop();
}
else if(flagR&&!flagL){
direct=1;
ans+=(tmpR.pos-POS);
POS=tmpR.pos;
RightPush.pop();
}
else{
left=POS-tmpL.pos;
right=tmpR.pos-POS;
if(right<left){
direct=1;
ans+=right;
RightPush.pop();
POS=tmpR.pos;
}
else if(left<right){
ans+=left;
direct=-1;
LeftPush.pop();
POS=tmpL.pos;
}
else {
if(direct>0){
direct=1;
ans+=right;
RightPush.pop();
POS=tmpR.pos;
}
else{
ans+=left;
direct=-1;
LeftPush.pop();
POS=tmpL.pos;
}
}
}
}
}
printf("Case %d: %d\n",++kase,ans);
}
return 0;
}

  

HDU 4302 Contest 1的更多相关文章

  1. hdu 4302 Holedox Eating

    http://acm.hdu.edu.cn/showproblem.php?pid=4302 #include <cstdio> #include <cstring> #inc ...

  2. HDU 4302 Holedox Eating(multiset)

    http://acm.hdu.edu.cn/showproblem.php?pid=4302 题意: 在一条直线上,会有多条命令,如果是0,那么就会在x位置处出现一个蛋糕,如果是1,某人就会找到最近的 ...

  3. HDU 5045 Contest(状压DP)

    Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...

  4. hdu - 5045 - Contest(国家压缩dp)

    意甲冠军:N个人M通过主打歌有自己的期望,每个问题发送人玩.它不能超过随机播放的次数1,追求最大业绩预期 (1 ≤ N ≤ 10,1 ≤ M ≤ 1000). 主题链接:pid=5045" ...

  5. [ACM] hdu 5045 Contest (减少国家Dp)

    Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...

  6. HDU 4302 Holedox Eating (STL + 模拟)

    Holedox Eating Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. HDU–5988-Coding Contest(最小费用最大流变形)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  8. hdu 4302 优先队列

    进一步学习了优先队列的用法 题意:一只小动物在直线上走,起始位置为零,之后会出现食物,动物要去距离自己最短的食物那,若两边的食物距离相等,则选择之前走的方向的食物 0 x,代表x的位置出现了食物,1代 ...

  9. hdu 5045 Contest(状态压缩DP)

    题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...

随机推荐

  1. 带有public static void main方法的类,其中的成员变量必须是static的,否则main方法没法调用。除非是main里的局部变量。因为main方法就是static的啊。

    带有public static void main方法的类,其中的成员变量必须是static的,否则main方法没法调用.除非是main里的局部变量.因为main方法就是static的啊.

  2. Linux Kernel(Android) 加密算法总结(一)(cipher、compress、digest)

    1. Linux内核支持哪些加密算法 ? 内核支持的加密算法非常多,包含: 对称加密算法.如AES,3DES. 对称password体制的发展趋势将以分组password为重点. 分组password ...

  3. 深入浅出CChart 每日一课——快乐高四第九课 于无声处,CChart内置功能介绍之数据存取篇

    笨笨长期以来一直使用Origin软件画图和处理数据,但Origin软件没有编程语言的接口.笨笨开发CChart的一个潜在的目标.是想实现Origin软件的功能.当然这是一个不可能达到的目标.Origi ...

  4. hdu5319 Painter(模拟)

    题目链接:点击打开链接 题目大意:给一个矩形.有两把刷子,一把刷红色,一把刷蓝色,红色的方向是东南,蓝色的方向是西北,红色加蓝色等于绿色,如今已知这面墙当前的状态.求从白墙到这个状态最少刷了多少次. ...

  5. Error: CompareBaseObjectsInternal can only be called from the main thread

    Posted: 01:39 PM 06-17-2013 hi, we're working on a project where we need to do some calculations on ...

  6. Android学习笔记之ProgressBar案例分析

    (1) <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  7. 2015.04.21,外语,读书笔记-《Word Power Made Easy》 11 “如何辱骂敌人” SESSION 31

    1.no reverence iconoclast([ai'kɔnәklæst]  n. 毁坏宗教神像的人, 提倡打破旧习的人)藐视传统.在青年的反叛期很容易出现iconoclasm([ai'kɔnә ...

  8. [jzoj 5661] 药香沁鼻 解题报告 (DP+dfs序)

    interlinkage: https://jzoj.net/senior/#contest/show/2703/0 description: solution: 注意到这本质就是一个背包,只是选了一 ...

  9. 36.QT地图

    widget.h #ifndef MAPWIDGET_H #define MAPWIDGET_H #include <QGraphicsView> #include <QLabel& ...

  10. 制作ubuntu的U盘启动盘

    在制作U盘启动盘之前,请各位先格式化你的U盘. 制作U盘启动盘的工具有很多种,我们这里为大家介绍的是用软碟通制作.所有我们需要有这个软件,如果大家没有可以百度“软碟通”,下载安装一个.然后点击打开.在 ...