Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:can any one tell me why my program wrong?In Reply To:can any one tell me why my program wrong? Posted by:opensky at 2008-11-15 16:23:12 #include<iostream> #include<string> #include<algorithm> #include<cstdio> #include<vector> #include<map> using namespace std; struct person { string name,stat; }ts; string dlist[10101],wlist[100101],plist[100101],order,sname; long n,m,dl,wl,pl,times; long wa,now; map<string ,long> dh,wh,ph; vector <string> flist; vector<person> cou; inline bool cmp1 (person a,person b) { return (a.name<b.name); } inline bool cmp2 (person a,person b) { return (a.name>b.name); } void mem() { wl=dl=pl=0; flist.clear(); cou.clear(); wa=0; now=0; dh.erase(dh.begin(),dh.end()); wh.erase(wh.begin(),wh.end()); ph.erase(ph.begin(),ph.end()); dh.clear(); ph.clear(); wh.clear(); } void up(long x) { if (x<=1) return ; string t=""; if (wa==0) { if ((x/2>=1)&&(wlist[x]<wlist[x/2])) { t=wlist[x]; wlist[x]=wlist[x/2]; wlist[x/2]=t; wh[wlist[x]]=x; wh[wlist[x/2]]=x/2; if (x/2>1) up(x/2); } }else { if ((x/2>=1)&&(wlist[x]>wlist[x/2])) { t=wlist[x]; wlist[x]=wlist[x/2]; wlist[x/2]=t; wh[wlist[x]]=x; wh[wlist[x/2]]=x/2; if (x/2>1) up(x/2); } } } void down(long x) { if (x>=wl) return ; string t=""; long best=x; if (wa==0) { if ((x*2<=wl)&&(wlist[best]>wlist[x*2])) { best=x*2; } if ((x*2+1<=wl)&&(wlist[best]>wlist[x*2+1])) { best=x*2+1; } if (best-x) { t=wlist[x]; wlist[x]=wlist[best]; wlist[best]=t; wh[wlist[x]]=x; wh[wlist[best]]=best; down(best); } }else { if ((x*2<=wl)&&(wlist[best]<wlist[x*2])) { best=x*2; } if ((x*2+1<=wl)&&(wlist[best]<wlist[x*2+1])) { best=x*2+1; } if (best-x) { t=wlist[x]; wlist[x]=wlist[best]; wlist[best]=t; wh[wlist[x]]=x; wh[wlist[best]]=best; down(best); } } } int main() { scanf("%d",×); while (times--) { scanf("%d%d",&n,&m); wa=0; for (long a=1;a<=m;++a) { cin>>order>>sname; if (order=="Sort") { if ((sname=="desc")&&(wa==0)) { wa=1; for (long b=1;b<=wl;++b) { up(b); } } if ((sname=="asc")&&(wa==1)) { wa=0; for (long b=1;b<=wl;++b) { up(b); //down(a); } } } if (order=="New") { if (dl<n) { dl++; dlist[dl]=sname; dh[sname]=dl; }else { wl++; wlist[wl]=sname; wh[sname]=wl; up(wl); } } if (order=="Pause") { bool end=false; now=dh[sname]; if (now) { dh[sname]=0; pl++; plist[pl]=sname; ph[sname]=pl; dlist[now]=dlist[dl]; //dh[dlist[now]]=now; if (now!=wl) dh[dlist[now]]=now; if (wl) { dlist[dl]=wlist[1]; wh[wlist[1]]=0; dh[dlist[dl]]=dl; wlist[1]=wlist[wl]; wh[wlist[1]]=1; wlist[wl]=""; wl--; if (wl>=2) down(1); }else { dlist[dl]=""; dl--; } } //end=true; if (!now) { now=wh[sname]; wh[sname]=0; pl++; plist[pl]=sname; ph[plist[pl]]=pl; wh[sname]=0; wlist[now]=wlist[wl]; if (now!=wl) wh[wlist[now]]=now; wlist[wl]=""; wl--; if (wl>=2) down(1); } } if (order=="Continue") { now=ph[sname]; ph[sname]=0; if (dl<n) { dl++; dlist[dl]=sname; dh[dlist[dl]]=dl; }else { wl++; wlist[wl]=sname; wh[wlist[wl]]=wl; up(wl); } plist[now]=plist[pl]; if (now!=pl) ph[plist[now]]=now; plist[pl]=""; pl--; } if (order=="Finish") { now=dh[sname]; dh[sname]=0; if (now) { flist.push_back(sname); dlist[now]=dlist[dl]; if (wl) { dlist[dl]=wlist[1]; dh[dlist[dl]]=dl; if (wl>1) { wlist[1]=wlist[wl]; wh[wlist[1]]=1; }else { wh[wlist[1]]=0; } wlist[wl]=""; wl--; if (wl>=2) down(1); }else { dlist[dl]=""; dl--; } } } } for (long a=0;a<flist.size();++a) { ts.name=flist[a]; ts.stat="finished"; cou.push_back(ts); /*cout<<flist[a]; printf(" finished\n");*/ } for (long a=1;a<=dl;++a) { ts.name=dlist[a]; ts.stat="downloading"; cou.push_back(ts); /*cout<<dlist[a]; printf(" downloading\n");*/ dlist[a]=""; } for (long a=1;a<=wl;++a) { ts.name=wlist[a]; ts.stat="waiting"; cou.push_back(ts); /* cout<<wlist[a]; printf(" waiting\n");*/ wlist[a]=""; } for (long a=1;a<=pl;++a) { ts.name=plist[a]; ts.stat="paused"; cou.push_back(ts); /* cout<<plist[a]; printf(" paused\n");*/ plist[a]=""; } if (wa==0) sort(cou.begin(),cou.end(),cmp1); else sort(cou.begin(),cou.end(),cmp2); for (long a=0;a<cou.size();++a) { cout<<cou[a].name; printf(" "); cout<<cou[a].stat; printf("\n"); } printf("\n"); if (times) { mem(); } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator