이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;
typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> OST;
const ll NMAX = 100001;
const ll INF = (1LL << 60);
const ll HALF = (1LL << 59);
const ll MOD = 1000000007;
const ll BLOCK = 318;
const ll base = 31;
const ll nr_of_bits = 21;
const ll LIMIT = 1000;
ll dp[NMAX][101]; ///aici mai putem scoate chestii
ll n, k;
priority_queue <pii> pq[101];
ll v[NMAX];
ll stiva[NMAX];
ll vf;
ll f[NMAX];
int main() {
ll i;
cin >> n >> k;
for(i = 1; i <= n; i++){
cin >> v[i];
}
for(i = 0; i <= n; i++){
for(ll j = 0; j <= k; j++)
dp[i][j] = HALF;
}
dp[0][0] = 0;
for(i = 1; i <= n; i++){
while(vf && v[stiva[vf]] < v[i]){
f[stiva[vf]]--;
vf--;
}
stiva[++vf] = i;
f[i]++;
for(ll j = 1; j <= min(i, k); j++){
ll val = dp[stiva[vf - 1]][j - 1] + v[i];
if(stiva[vf - 1] <= j - 1){
val = dp[j - 1][j - 1] + v[i];
}
pq[j].push({-val, i});
}
for(ll j = 1; j <= min(i, k); j++){
while(pq[j].size() && !f[pq[j].top().second])
pq[j].pop();
ll cost = -pq[j].top().first;
dp[i][j] = min(dp[i][j], cost);
}
}
cout << dp[n][k];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |