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 |
给点测试数据把,不知道错哪了我的思路,线性扫秒,保存当前的最大和最小,以及他们的下标,并计算出当前距离,如果遇到一个比max大的,那么将起赋为最小同时改变下标,动态的更新最长距离 #include<iostream> using namespace std; int a[50001]; int main() { int n,i,max,min,sum,len,ti,tj; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) { scanf("%d",&a[i]); } max=-999999999; min=999999999; ti=1;tj=1; len=0; sum=0; for(i=1;i<=n;i++) { // cout<<"case:"<<i<<endl; if(a[i]>max) { max=a[i]; tj=i; } if(a[i]<min) { min=a[i]; ti=i; } len=tj-ti; if(len>sum)sum=len; if(a[i]<max) { min=a[i]; ti=i; max=a[i];tj=i; } // cout<<"i="<<ti<<" j="<<tj<<endl; // cout<<"min="<<min<<" max="<<max<<endl; // system("pause"); } if(sum==0)printf("-1\n"); else printf("%d\n",sum); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator