用multiset过
#include<cstdio> #include<cstring> #include<iostream> #include<set> #include<cmath> using namespace std; int main() { int t,ww=1; scanf("%d",&t); for(ww=1;ww<=t;ww++){ multiset<int >q; int i,n,m; scanf("%d%d",&n,&m); int pos=0,dir=1; int ans=0; for(i=0;i<m;i++){ int a,b; scanf("%d",&a); if(a==0){ scanf("%d",&b); q.insert(b); } else{ if(q.empty()) continue; set<int>::iterator s1,s2; s1=q.lower_bound(pos); int tt1=*s1-pos; if(s1==q.begin()){ if(tt1>=0){ dir=1; ans+=tt1; pos=*s1; q.erase(s1); } else{ dir=0; ans+=tt1; pos=*s1; q.erase(s1); } continue; } s1--; s2=s1; s1++; int tt2=abs(pos-*s2); if(tt1<tt2){ ans+=tt1; pos=*s1; dir=1; q.erase(s1); } else if(tt1>tt2){ ans+=tt2; pos=*s2; dir=0; q.erase(s2); } else{ if(dir==1) { ans+=tt1; pos=*s1; q.erase(s1); } else{ ans+=tt2; pos=*s2; q.erase(s2); } } } } printf("Case %d: %d\n",ww,ans); } }
hdu 4302 STL multiset,布布扣,bubuko.com
原文:http://blog.csdn.net/cnh294141800/article/details/22995973