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 |
为什么贪心不可以#include<iostream> using namespace std; __int64 a[2002]; __int64 dfs(__int64 i,__int64 j) { if(i==j)return 1; if(a[i]<a[j])return 1; if(a[i]>a[j])return 0; return dfs(i+1,j-1); } int main() { __int64 n,i,j,age,left,right,s; while(cin>>n) { for(i=1;i<=n;i++) { cin>>a[i]; } if(n==1){cout<<a[n]<<endl;continue;} age=1; left=1; right=n; s=0; for(i=1;i<=n;i++) { if(dfs(left,right)) { s+=a[left]*age; left++; } else {s+=a[right]*age;right--;} age++; } cout<<s<<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