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

注意切割的代价是long long

Posted by when at 2017-01-30 16:18:12 on Problem 3253
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;

const int MAXN=200005;
long long a[MAXN];
//原来的返回值是int,wrong了好多次
long long solve(int N){
    long long ans=0;

    while(N>0){
        int min1=0,min2=1;

        if(a[min2]<a[min1])
            swap(a[min1],a[min2]);

        for(int i=2;i<=N;i++){
            if(a[i]<a[min2])
                swap(a[i],a[min2]);
            if(a[min2]<a[min1])
                swap(a[min1],a[min2]);
        }

        ans+=(a[min1]+a[min2]);
        a[min1]+=a[min2];
        swap(a[min2],a[N]);
        N--;
    }
    return ans;
}

int main(){
    int N;
    cin>>N;
    for(int i=0;i<N;i++)
        cin>>a[i];
    cout<<solve(N-1)<<endl;

}

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