zoukankan      html  css  js  c++  java
  • HDU 4302 Contest 1

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

    注意输出大小写。。。

    #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
    ",++kase,ans);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    变量对象,作用域链,闭包,匿名函数,this关键字,原型链,构造器,js预编译,对象模型,执行模型,prototype继承
    iptables-snat-dnat-设置
    salt-ssh
    linux-vsftp
    网站申请HTTPS 访问
    shell 处理文件脚本
    last与lastb命令 读取的日志文件
    Linux-server-sshd
    Nginx 日志切割
    修改或隐藏服务器名称需要修改源码
  • 原文地址:https://www.cnblogs.com/jie-dcai/p/4046106.html
Copyright © 2011-2022 走看看