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 |
我也用了两次sort,为啥TLE了?郁闷,谁帮我看下代码In Reply To:hehe,两次快排不会超时!爽inging! Posted by:hujk2008 at 2004-11-05 12:43:34 我也用了两次sort,为啥TLE了?郁闷,谁帮我看下代码 #include<iostream> #include <algorithm> using namespace std; #define M 131075 struct stone { int x,y; }; stone stones[M]; bool cmp(stone a,stone b) { if(a.x<b.x) return true; else if(a.x==b.x&&a.y<b.y) return true; else return false; } bool cmp2(stone a,stone b) { if(a.y<b.y) return true; else if(a.y==b.y&&a.x<b.x) return true; else return false; } int main() { freopen("in.txt","r",stdin); int t,m,n,k,i,j,r,c,pos,tmp; cin>>t; while(t--) { cin>>m>>n>>k; pos=0; for(i=0;i<k;i++) { cin>>r>>c; stones[i].x=r-1; stones[i].y=c-1; } sort(stones,stones+k,cmp); pos+=stones[0].x; if(stones[0].y>=2) pos++; for(i=0;i<k-1;i++) { if(stones[i+1].x==stones[i].x&&stones[i+1].y-stones[i].y>=3) pos++; else if(stones[i+1].x>stones[i].x) { pos+=stones[i+1].x-stones[i].x-1; if(stones[i+1].y>=2) pos++; if(n-1-stones[i].y>=2) pos++; } } pos+=m-stones[k-1].x-1; if(n-1-stones[k-1].y>=2) pos++; sort(stones,stones+k,cmp2); pos+=stones[0].y; if(stones[0].x>=2) pos++; for(i=0;i<k-1;i++) { if(stones[i+1].y==stones[i].y&&stones[i+1].x-stones[i].x>=3) pos++; else if(stones[i+1].y>stones[i].y) { pos+=stones[i+1].y-stones[i].y-1; if(stones[i+1].x>=2) pos++; if(m-1-stones[i].x>=2) pos++; } } pos+=n-stones[k-1].y-1; if(m-1-stones[k-1].x>=2) pos++; cout<<pos<<endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator