Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1830 Accepted Submission(s): 634
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.
/* HDU 4302 G++ 281ms 532K */ #include<stdio.h> #include<queue> #include<iostream> #include<algorithm> using namespace std; struct cmp { bool operator()(int x,int y) { return x>y; } }; priority_queue<int,vector<int>,cmp>q; priority_queue<int>q2; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; int L,n; int A,B; int iCase=0; scanf("%d",&T); while(T--) { iCase++; scanf("%d%d",&L,&n); while(!q.empty())q.pop(); while(!q2.empty())q2.pop(); int x=0; int ans=0; int t=1; while(n--) { scanf("%d",&A); if(A==0) { scanf("%d",&B); if(B>=x)q.push(B); else q2.push(B); } else { if(!q.empty()&&!q2.empty()) { int temp1=q.top(); int temp2=q2.top(); if(temp1-x<x-temp2) { t=1; ans+=q.top()-x; x=q.top(); q.pop(); } else if(temp1-x>x-temp2) { t=-1; ans+=x-q2.top(); x=q2.top(); q2.pop(); } else if(t==1) { ans+=q.top()-x; x=q.top(); q.pop(); } else { ans+=x-q2.top(); x=q2.top(); q2.pop(); } } else if(!q.empty()) { t=1; ans+=q.top()-x; x=q.top(); q.pop(); } else if(!q2.empty()) { t=-1; ans+=x-q2.top(); x=q2.top(); q2.pop(); } } } printf("Case %d: %d\n",iCase,ans); } return 0; }
线段树:
/* HDU 4302 G++ 687ms 5652K */ #include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; const int MAXN=100010; const int INF=0x3f3f3f3f; struct Node { int l,r; int t; int Min,Max; }segTree[MAXN*3]; void Build(int i,int l,int r) { segTree[i].l=l; segTree[i].r=r; segTree[i].t=0; if(l==r) { segTree[i].Min=INF; segTree[i].Max=-1; return; } int mid=(l+r)>>1; Build(i<<1,l,mid); Build((i<<1)|1,mid+1,r); segTree[i].Max=max(segTree[i<<1].Max,segTree[(i<<1)|1].Max); segTree[i].Min=min(segTree[i<<1].Min,segTree[(i<<1)|1].Min); } void add(int i,int t) { if(segTree[i].l==t&&segTree[i].r==t) { segTree[i].Max=segTree[i].Min=t; segTree[i].t++; return; } int mid=(segTree[i].l+segTree[i].r)>>1; if(t<=mid)add(i<<1,t); else add((i<<1)|1,t); segTree[i].Max=max(segTree[i<<1].Max,segTree[(i<<1)|1].Max); segTree[i].Min=min(segTree[i<<1].Min,segTree[(i<<1)|1].Min); } void del(int i,int t) { if(segTree[i].l==t&&segTree[i].r==t) { segTree[i].t--; if(segTree[i].t==0) { segTree[i].Min=INF; segTree[i].Max=-1; } return; } int mid=(segTree[i].l+segTree[i].r)>>1; if(t<=mid)del(i<<1,t); else del((i<<1)|1,t); segTree[i].Max=max(segTree[i<<1].Max,segTree[(i<<1)|1].Max); segTree[i].Min=min(segTree[i<<1].Min,segTree[(i<<1)|1].Min); } int query1(int i,int l,int r)//查询最大值 { if(segTree[i].l==l&&segTree[i].r==r) { return segTree[i].Max; } int mid=(segTree[i].l+segTree[i].r)>>1; if(r<=mid)return query1(i<<1,l,r); else if(l>mid) return query1((i<<1)|1,l,r); else return max(query1(i<<1,l,mid),query1((i<<1)|1,mid+1,r)); } int query2(int i,int l,int r)//查询最大值 { if(segTree[i].l==l&&segTree[i].r==r) { return segTree[i].Min; } int mid=(segTree[i].l+segTree[i].r)>>1; if(r<=mid)return query2(i<<1,l,r); else if(l>mid) return query2((i<<1)|1,l,r); else return min(query2(i<<1,l,mid),query2((i<<1)|1,mid+1,r)); } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int x; int T; int n; int m; int flag; scanf("%d",&T); int a,b; int iCase=0; while(T--) { iCase++; scanf("%d%d",&n,&m); Build(1,0,n); int flag=1;//往前的 x=0; int ans=0; while(m--) { scanf("%d",&a); if(a==0) { scanf("%d",&b); add(1,b); } else { int t1=query1(1,0,x); int t2=query2(1,x,n); if(t1==-1&&t2!=INF) { ans+=t2-x; x=t2; del(1,t2); flag=1; } else if(t1!=-1&&t2==INF) { ans+=x-t1; x=t1; del(1,t1); flag=-1; } else if(t1!=-1&&t2!=INF) { if(x-t1>t2-x) { ans+=t2-x; x=t2; del(1,t2); flag=1; } else if(x-t1<t2-x) { ans+=x-t1; x=t1; del(1,t1); flag=-1; } else { if(flag==1) { ans+=t2-x; x=t2; del(1,t2); flag=1; } else { ans+=x-t1; x=t1; del(1,t1); flag=-1; } } } } } printf("Case %d: %d\n",iCase,ans); } return 0; }