Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么贪心不可以

Posted by foreverlin at 2008-11-25 15:09:13 on Problem 3186
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator