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 |
已解决……In Reply To:这道题怎么用逆序对解? Posted by:15632869526 at 2018-02-26 17:39:19 好吧,其实是我个ZZ在细节上写错了。 附上代码(已AC): #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int MAXN = 1e7+200; struct stu{ long long s,e; }road[MAXN]; long long a[MAXN],ans,tmp[MAXN]; bool comp(const stu& A,const stu& B){ if(A.s!=B.s) return A.s<B.s; else return A.e<B.e; } void Merge(int s,int m,int e){ int pb=0,p1=s,p2=m+1; while(p1<=m&&p2<=e){ if(a[p1]<=a[p2]) tmp[pb++]=a[p1++]; else { tmp[pb++]=a[p2++]; ans+=m-p1+1; } } while(p1<=m) tmp[pb++]=a[p1++]; while(p2<=e) tmp[pb++]=a[p2++]; for(int i=0; i<e-s+1; i++) a[s+i]=tmp[i]; } void Mergesort(int s,int e){ if(s<e){ int m=(e-s)/2+s; Mergesort(s,m); Mergesort(m+1,e); Merge(s,m,e); } } int main() { int t; scanf("%d",&t); for(int I=1; I<=t; I++){ ans=0; int n,m,k; scanf("%d %d %d",&n,&m,&k); for(int i=1; i<=k; i++){ scanf("%lld %lld",&road[i].s,&road[i].e); } sort(road+1,road+1+k,comp); for(int i=1; i<=k; i++) a[i]=road[i].e; Mergesort(1,k); printf("Test case %d: %lld\n",I,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