답안 #1037182

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1037182 2024-07-28T10:04:42 Z Phuoc 수열 (APIO14_sequence) C++14
22 / 100
2000 ms 131072 KB
#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set> 

using namespace std;
 
#define ll long long
#define pb push_back
#define el '\n'
#define mpair make_pair
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define fi first
#define se second
 
/* Author: Pham Gia Phuoc */
 
const ll MOD = 998244353;
 
template <class T1, class T2>
    void add(T1 &a, T2 b){
        a += b;
        if(a >= MOD) a -= MOD;
    }
 
template <class T1, class T2>
    void sub(T1 &a, T2 b){
        a -= b;
        if(a < 0) a += MOD;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if(a > b){a = b; return true;} return false;
    }
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if(a < b){a = b; return true;} return false;
    }
 
/** END OF TEMPLATE. DRINK A CUP OF COFFEE BEFORE READING MY CODE **/
 
const int MAX = 100010;
const ll INF = (ll) 1e18 + 67LL;
const int oo = (int)(1e9 + 7);
const int NUM_BIT = 62;

#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)

int n, k;
long long a[MAX], sum[MAX];

void init(){
    cin >> n >> k;
    FOR(i, 1, n){
        cin >> a[i];
        sum[i] = sum[i - 1] + a[i];
    }
}

pair <int, int> trace[222][222][222];
long long dp[222][222][222];

long long Try(int L, int R, int K){
    if(K == 0) return 0;
    if(L == R) return 0;
    if(dp[L][R][K] != -1) return dp[L][R][K];
    
    long long res = -INF;
    long long sumTotal = sum[R] - sum[L - 1], sumLeft = 0;
    FOR(i, L, R - 1){
        sumLeft += a[i];
        FOR(k1, 0, K - 1){
            int k2 = K - k1 - 1;
            if(k2 < 0) return 0;
            long long splitCost = sumLeft * (sumTotal - sumLeft);
            long long left = Try(L, i, k1);
            long long right = Try(i + 1, R, k2);
            long long cur = left + right + splitCost;
            if(maximize(res, cur)) trace[L][R][K] = make_pair(i, k1);
        } 
    }

    return dp[L][R][K] = res;
}

vector <int> path;

void backTrack(int L, int R, int K){
    if(K == 0) return;
    if(L >= R) return;

    int t = trace[L][R][K].fi;
    int k1 = trace[L][R][K].se;
    int k2 = K - k1 - 1;

    path.push_back(t);
    backTrack(L, t, k1);
    backTrack(t + 1, R, k2);
}

void solve(){
    memset(dp, -1, sizeof dp);
    cout << Try(1, n, k) << el;
    backTrack(1, n, k);
    sort(path.begin(), path.end());
    for(int x:path) cout << x << ' ';
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define test "test"
    // freopen(test".inp", "r", stdin);
    // freopen(test".out", "w", stdout);
    srand(time(0));
    int t = 1;
    while(t--){
        init();
        solve();
    }
    
    return 0;
}


 
/*** ROAD TO VOI 2025 ***/
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 90900 KB contestant found the optimal answer: 108 == 108
2 Correct 11 ms 92764 KB contestant found the optimal answer: 999 == 999
3 Correct 12 ms 88752 KB contestant found the optimal answer: 0 == 0
4 Correct 12 ms 92764 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 11 ms 92764 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 11 ms 88836 KB contestant found the optimal answer: 1 == 1
7 Correct 11 ms 88668 KB contestant found the optimal answer: 1 == 1
8 Correct 12 ms 88668 KB contestant found the optimal answer: 1 == 1
9 Correct 12 ms 92764 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 11 ms 92764 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 12 ms 92764 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 14 ms 92764 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 12 ms 92764 KB contestant found the optimal answer: 140072 == 140072
14 Correct 11 ms 90836 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 11 ms 92824 KB contestant found the optimal answer: 805 == 805
16 Correct 11 ms 92864 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 11 ms 92884 KB contestant found the optimal answer: 999919994 == 999919994
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 107100 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 15 ms 107100 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 73 ms 107072 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 14 ms 107096 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 15 ms 107100 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 13 ms 107100 KB contestant found the optimal answer: 933702 == 933702
7 Correct 28 ms 107336 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 15 ms 107268 KB contestant found the optimal answer: 687136 == 687136
9 Correct 14 ms 107096 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 18 ms 107260 KB contestant found the optimal answer: 29000419931 == 29000419931
# 결과 실행 시간 메모리 Grader output
1 Runtime error 24 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 30 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 42 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2190 ms 131072 KB Time limit exceeded
2 Halted 0 ms 0 KB -