hdu 4302 Holedox Eating(线段树或MAP,SET,4级)

来源:互联网 发布:番禺网络推广公司电话 编辑:程序博客网 时间:2024/06/05 02:17

Holedox Eating

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2341    Accepted Submission(s): 792


Problem Description
Holedox is a small animal which can be considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere in the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If there are many pieces of cake in different directions Holedox can choose, Holedox will choose one in the direction which is the direction of its last movement. If there are no cakes present, Holedox just stays where it is.
 

Input
The input consists of several test cases. The first line of the input contains a single integer T (1 <= T <= 10), the number of test cases, followed by the input data for each test case.The first line of each case contains two integers L,n(1<=L,n<=100000), representing the length of the pipe, and the number of events.
The next n lines, each line describes an event. 0 x(0<=x<=L, x is a integer) represents a piece of cake appears in the x position; 1 represent Holedox wants to eat a cake.
In each case, Holedox always starts off at the position 0.
 

Output
Output the total distance Holedox will move. Holedox don’t need to return to the position 0.
 

Sample Input
310 80 10 510 20 0111 10 70 10 510 20 01110 80 10 10 510 20 011
 

Sample Output
Case 1: 9Case 2: 4Case 3: 2
 

Author
BUPT
 

Source
2012 Multi-University Training Contest 1
 

Recommend
zhuyuanchen520

思路:模拟就行了,如果是用线段树,那就找0->pos的最大值和pos->L的最小值,线段树存插入区间断最大值和最小值。


#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define lson t<<1#define rson t<<1|1#define mid (l+r)/2using namespace std;const int oo=1e9;const int mm=1e5+9;class node{ public:int mi,ma,l,r,num;}rt[mm*4];int L,n;void build(int t,int l,int r){ rt[t].l=l;rt[t].r=r;rt[t].num=0;rt[t].mi=oo;rt[t].ma=-oo; if(l==r){return;} build(lson,l,mid);build(rson,mid+1,r);}void update(int t,int id,bool flag){ if(rt[t].l==rt[t].r&&rt[t].l==id) { if(flag){rt[t].num++;rt[t].mi=rt[t].ma=id;} else {rt[t].num--;if(!rt[t].num)rt[t].ma=-oo,rt[t].mi=oo;} return; } if(rt[lson].r>=id)update(lson,id,flag); else update(rson,id,flag); rt[t].num=rt[lson].num+rt[rson].num; rt[t].ma=max(rt[lson].ma,rt[rson].ma); rt[t].mi=min(rt[lson].mi,rt[rson].mi);}int query(int t,int l,int r,bool flag){  if(l<=rt[t].l&&rt[t].r<=r)  {   if(flag)return rt[t].ma;   else return rt[t].mi;  }  int ret;  if(flag)ret=-oo;  else ret=oo;  if(l<=rt[lson].r)  {if(flag)ret=max(ret,query(lson,l,r,flag));   else ret=min(ret,query(lson,l,r,flag));  }  if(r>=rt[rson].l)  {if(flag)ret=max(ret,query(rson,l,r,flag));   else ret=min(ret,query(rson,l,r,flag));  }  return ret;}int main(){ int cas,a,b,num,ans;bool dir; while(~scanf("%d",&cas)) {for(int ca=1;ca<=cas;++ca)  {  scanf("%d%d",&L,&n); /// puts("yes");  build(1,0,L);  dir=1;int pos=0;ans=num=0;  for(int i=0;i<n;++i)  {   scanf("%d",&a);   if(!a)   {   scanf("%d",&b);update(1,b,1);++num;   }   else   { if(!num)continue;--num;    int ll=query(1,0,pos,1);    int rr=query(1,pos,L,0);    ///cout<<ll<<" "<<rr<<" "<<ans<<endl;    if(ll==-oo){ans+=rr-pos;update(1,rr,0);pos=rr;dir=1;continue;}    if(rr==oo){ans+=pos-ll;update(1,ll,0);pos=ll;dir=0;continue;}    a=pos-ll;b=rr-pos;    if(a<b){ans+=a;update(1,ll,0);pos=ll;dir=0;continue;}    else if(a>b){ans+=b;update(1,rr,0);pos=rr;dir=1;continue;}    else if(a==b)    {      if(dir){ans+=b;update(1,rr,0);pos=rr;dir=1;continue;}      else {ans+=a;update(1,ll,0);pos=ll;dir=0;continue;}    }   }  }  printf("Case %d: %d\n",ca,ans);  } }}


#include<iostream>#include<cstring>#include<cstdio>#include<map>using namespace std;map<int,int>mp;const int oo=1e9;bool dir;map<int,int>::iterator it,il,ir;int main(){  int cas,l,n,a,b,num;    while(~scanf("%d",&cas))    {for(int ca=1;ca<=cas;++ca)     {mp.clear();mp[oo]=1;mp[-oo]=1;     num=0;mp[0]=1;     it=il=ir=mp.find(0);     int ans=0;dir=1;     scanf("%d%d",&l,&n);     for(int i=0;i<n;++i)     {       scanf("%d",&a);       if(!a)       {++num;       scanf("%d",&a);mp[a]++;       }       else       { if(!num)continue;--num;       il=ir=it;       --il;++ir;        int z=it->first;         --mp[z];///去除z         if(mp[z]){continue;}         else mp.erase(it);         if(il->first==-oo){ans+=ir->first-it->first;it=ir;dir=1;continue;}         if(ir->first==oo){ans+=it->first-il->first;it=il;dir=0;continue;}         a=it->first-il->first;b=ir->first-it->first;         if(a<b){ans+=a;it=il;dir=0;continue;}         else if(a>b){ans+=b;it=ir;dir=1;continue;}         else if(a==b)         {if(dir){ans+=b;it=ir;}          else {ans+=a;it=il;}         }       }     }     printf("Case %d: %d\n",ca,ans);     }    }}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 房贷担保费没交怎么办 营业执照过期1年怎么办 营业执照和公章丢失怎么办 个体户怎么办对公账户 公章法人章丢失怎么办 广州买房没有社保怎么办 辞职后计生关系怎么办 广州换工作社保怎么办 学校更名了盖章怎么办 工商注册资金没有交怎么办 住公司宿舍怎么办居住证 住在公司宿舍怎么办居住证 滴滴没有的车型怎么办 假的租房合同怎么办 代注册公司被骗怎么办 公司跨区迁址怎么办 公司搬走注册地怎么办 注册公司没有房产证怎么办 公司不运营了怎么办 公司注销了账户怎么办 租户不变更地址怎么办 营业执照忘审了怎么办 工商营业执照年检过期怎么办 个体营业执照没有年报怎么办? 个体工商户一年没有申报怎么办 个体工商户逾期未申报怎么办 个体户没报税过怎么办 农业银行证书过期了怎么办 ca证书丢了怎么办 ca证书被锁怎么办 上个月忘记清卡怎么办 财务人员进入税务黑名单怎么办 社保本丢了怎么办 贷款车辆登记证书怎么办 发票薄丢了怎么办? 汽车发票丢了怎么办 税票弄丢了怎么办 交强险正本丢了怎么办 个体营业执照正本丢失怎么办 简易注销后税务怎么办 拒绝了日历邀请怎么办