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:四叉数TLE……orz。。。In Reply To:四叉数TLE……orz。。。 Posted by:dongshimou at 2015-02-24 18:12:44 #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; int s[110000],ans,t,r[110000]; struct node { int x,y; }k[110000]; int cmp(const void *x1,const void *x2) { node n1=*(node *)x1; node n2=*(node *)x2; if (n1.x!=n2.x) return n1.x-n2.x; else return n1.y-n2.y; } int lowbit(int x) { return x & -x; } int qiuhe(int x) { int ans=0; while(x>=1) { ans+=s[x]; x-=lowbit(x); } return ans; }int n; void gaibian(int x) { while(x<=n) { s[x]+=1; x+=lowbit(x); } } int main() { int t,m,i,kd; while (scanf("%d",&n)!=EOF) { memset(s,0,sizeof(s)); if (n==0)break; for (i=1;i<=n;i++) { scanf("%d",&r[i]); k[i].x=r[i]; k[i].y=i; } qsort(k+1,n,sizeof(node ),cmp); t=0; kd=-99999; for (i=1;i<=n;i++) { if (k[i].x>kd) { kd=k[i].x; k[i].x=++t; r[k[i].y]=k[i].x; } else { k[i].x=t; r[k[i].y]=k[i].x; } } ans=0; for (i=1;i<=n;i++) { gaibian(r[i]); ans+=i-qiuhe(r[i]); } printf("%d\n",ans); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator