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

注意输出大小写。。。

#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. 计算机网络系统--Microsoft Lync 与 腾讯通RTX 对比(转载)

    原文网址: http://it.vsharing.com/226.html ------------------------------- 上海大学统一通信平台现在尚未实施,一直在测试微软的Lync. ...

  2. Hadoop使用Java进行文件修改删除操作

    Hadoop使用Java进行文件修改删除操作 学习了:http://blog.csdn.net/menghuannvxia/article/details/44651061 学习了:http://bl ...

  3. “XXX.Index”不扩展类“System.Web.UI.Page”,因此此处不同意的问题

    "XXX.Index"不扩展类"System.Web.UI.Page",因此此处不同意的问题 原因:设计页面继承的路径和后台.cs页面类的路径不一致造成的 看下 ...

  4. SVG中一些较为具体的文档

    绘制线头尾标记 http://tutorials.jenkov.com/svg/marker-element.html 微软关于svg的文档 http://msdn.microsoft.com/zh- ...

  5. 【Swift】学习笔记(六)——函数

    函数  懂编程语言的来说这个是最主要的了,不论什么语言都有函数这个概念.函数就是完毕特定任务的独立代码块. 函数怎么创建: 1.创建一个无參无返回值的函数(实际上全部的函数都有返回值,这个函数返回vo ...

  6. java中文件路径读取

    windows下 1)相对路径 public static final String TestDataExcelFilePath="src/omstestdata.xlsx"; 2 ...

  7. [C++设计模式]observer 观察者模式

    有这么一种松耦合的需求: 有一些类的对象对类A对象的状态变化非常感兴趣,不会改变类A的对象,也不会被类A的对象改变,想以一种较小的代价观察对类A对象状态变化. 以下的几种方式也能实现上述目的 (1)通 ...

  8. Swift - 制作一个在线流媒体音乐播放器(使用StreamingKit库)

    在之前的文章中,我介绍了如何使用 AVPlayer 制作一个简单的音乐播放器(点击查看1.点击查看2).虽然这个播放器也可以播放网络音频,但其实际上是将音频文件下载到本地后再播放的. 本文演示如何使用 ...

  9. [SCOI 2007] 排列

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1072 [算法] 状压DP [代码] #include<bits/stdc++. ...

  10. Centos上JDK的安装搭建

    一.下载 yum search java|grep jdk //查找所有jdk版本 二.选择安装1.8 yum install java-1.8.0-openjdk-src-debug.x86_64 ...