제출 #754152

#제출 시각아이디문제언어결과실행 시간메모리
754152OlympiaK개의 묶음 (IZhO14_blocks)C++17
53 / 100
1084 ms468 KiB
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <map>
#include <set>
using namespace std;
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, K;
    cin >> N >> K;
    int64_t arr[N];
    for (int i = 0; i < N; i++) {
        cin >> arr[i];
    }
    int64_t INF = 1e17;
    vector<int64_t> prev(N + 1), cur(N + 1);
    prev.assign(N + 1, INF), cur.assign(N + 1, INF);
    for (int j = 1; j <= N; j++) {
        prev[j] = ((j == 1) ? arr[0] : max(prev[j - 1], arr[j - 1]));
    }
    for (int i = 2; i <= K; i++) {
    	vector<pair<int,pair<int,int>>> ranges;
        for (int j = 1; j <= N; j++) {
        	cur[j] = INF;
            if (j == 1) {
            	cur[1] = prev[0] + arr[0];
            	ranges.push_back(make_pair(arr[0], make_pair(0, 0)));
            } else {
            	while (!ranges.empty() and ranges.back().first <= arr[j - 1]) {
            		ranges.pop_back();
            	}
            	if (ranges.empty()) {
            		ranges.push_back(make_pair(arr[j - 1], make_pair(0, j - 1)));
            	} else {
            		ranges.push_back(make_pair(arr[j - 1], make_pair(ranges.back().second.second + 1, j - 1)));
            	}
                for (auto& p: ranges) {
                	for (int x = p.second.first; x <= p.second.second; x++) {
                		cur[j] = min(cur[j], p.first + prev[x]);
                	}
                }
            }
        }
        swap(cur, prev);
    }
    cout << prev[N];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...