Codeforces Round #243 (Div. 2) C. Sereja and Swaps

OpenSoucre發表於2014-04-29

由於n比較小,直接暴力解決

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include<iterator>
using namespace std;

int main(){
    int n,k;
    cin >> n >> k;
    vector<int> a(n);
    for(int  i = 0 ; i < n;  ++ i) cin >> a[i];
    int sum = accumulate(a.begin(),a.end(),0);
    for(int i = 0 ; i < n; ++ i){
        for(int j = i ; j < n ; ++ j ){
            vector<int> tmp(a.begin()+i,a.begin()+j+1);
            sort(tmp.begin(),tmp.end());
            if(j-i+1!=n){
                vector<int> b(a);
                b.erase(b.begin()+i,b.begin()+j+1);
                sort(b.begin(),b.end(),greater<int>());
                for(int x =0, y = 0; b[x]>tmp[y] && x < k && x < b.size(); ++x ,++y){
                    tmp[y] = b[x];
                }
            }
            sum = max(sum,accumulate(tmp.begin(),tmp.end(),0));
        }
    }
    cout<<sum<<endl;
}

 

相關文章