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

注意输出大小写。。。

#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. 在magento的eav模型中如何在更新记录时只在value表的原值上更新

    1,一般情况下,当我们在调用getModel在load某条实体接着更新对应实体上的值是,都不会覆盖原来的实体value表上的值,而是保留原来的,并在value表上重新创建一条值记录,比如初始表如下: ...

  2. 开启WIFI

    C:\Windows\system32>netsh wlan set hostednetwork mode=allow ssid=wuyechun-wifi k ey= 承载网络模式已设置为允许 ...

  3. iis配置问题报错 -- Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies

    具体提示: Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. 试图加载格式不正确的程序. ...

  4. 假设让我又一次设计一款Android App

    转载请注明出处: 本文来自aspook的博客:http://blog.csdn.net/ahence/article/details/47154419 开发工具的选择 开发工具我将选用Android  ...

  5. ANT公布SVN WEB项目到TOMCAT以及利用post-commit自己主动提交编译更新

    开发者在本地提交更新到SVNserver后.往往须要測试人员又一次測试.为了将更新内容即时反映到測试server.能够利用post-commit脚本将SVN更新同步到測试server中. (1)利用S ...

  6. UVA 11987 Almost Union-Find 并查集单点修改

                                     Almost Union-Find I hope you know the beautiful Union-Find structur ...

  7. HTML5游戏实战之20行代码实现打地鼠

    之前写过一篇打地鼠的博客70行的代码实现打地鼠游戏,细致思考过后,发现70行代码都有点多余了,应用tangide的控件特性,能够将代码量缩减到20行左右. 先show一下终于成果,点击试玩:打地鼠.或 ...

  8. Django迁移到mysql数据库时的错误

    pip install mysqlclient Collecting mysqlclient Using cached https://files.pythonhosted.org/packages/ ...

  9. 关于TCP的三次握手和四次分手 专题

    客户端TCP状态迁移:CLOSED->SYN_SENT->ESTABLISHED->FIN_WAIT_1->FIN_WAIT_2->TIME_WAIT->CLOSE ...

  10. layer Tips参数使用

    layer.tips(content, follow, options) - tips层type:4的深度定制.也是我本人比较喜欢的一个层类型,因为它拥有和msg一样的低调和自觉,而且会智能定位,即灵 ...