版本比较
[AGC025C] Interval Game 题解
正文
增加 14 行删除 14 行
47 行未改动48495051525354525354555657583 行未改动62636465666667686970717273747576727374757677787979808182838 行未改动9293949596969798989910010110210310410410510610710810911011111211211311411547 行未改动struct node{ int l,r;}a[N];priority_queue<P>lheap;//维护最大l priority_queue<P,vector<P>,greater<P> >rheap;//维护最小r void init()//初始化 priority_queue<P>lheap;//维护最大lpriority_queue<P,vector<P>,greater<P> >rheap;//维护最小rvoid init()//初始化{ cnt=now=0; while(lheap.size()) lheap.pop(); while(rheap.size()) rheap.pop();3 行未改动 rheap.push(make_pair(a[i].r,i)); vst[i]=0; }}int findl()//向左走并计算代价 int findl()//向左走并计算代价{ int res=0; int val=lheap.top().first,pos=lheap.top().second; lheap.pop(); while(vst[pos]&&lheap.size()) val=lheap.top().first,pos=lheap.top().second,lheap.pop(); //要一直找到第一个没有用过的区间 if(a[pos].l<=now&&now<=a[pos].r) return 0;//如果在里面就跳过计算 res=a[pos].l-now;//计算代价 now=a[pos].l;//更新now vst[pos]=1;//打标记 //要一直找到第一个没有用过的区间 if(a[pos].l<=now&&now<=a[pos].r) return 0;//如果在里面就跳过计算 res=a[pos].l-now;//计算代价 now=a[pos].l;//更新now vst[pos]=1;//打标记 return res;}int findr()//向右走并计算代价 int findr()//向右走并计算代价{ int res=0; int val=rheap.top().first,pos=rheap.top().second; rheap.pop();8 行未改动int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d %d",&a[i].l,&a[i].r);
init(); while(++cnt<=n)//第一步向左 while(++cnt<=n)//第一步向左 { if(cnt&1) ans1+=findl(); else ans1+=findr(); } ans1+=abs(now);
init(); while(++cnt<=n)//第一步向右 { if(cnt&1) ans2+=findr(); else ans2+=findl(); } ans2+=abs(now); printf("%lld",max(ans1,ans2));//两者取大的 printf("%lld",max(ans1,ans2));//两者取大的 return 0;}```