이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "peru.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vpii;
typedef vector <vi> vvi;
const int N = 4e5 + 5, M = 19, mod = 1e9 + 7;
const ll infll = (ld)1e18 + 7;
namespace {
deque <pair <int, ll>> dq;
multiset <ll> mtsdp;
ll dp[N];
int sparse[M][N];
int rmq(int l, int r){
int z = __lg(r - l + 1);
return max(sparse[z][l], sparse[z][r - (1 << z) + 1]);
}
int convert(int n){
int ans = 0;
For(i, 0, n){
ans = (ll)ans * 23 % mod;
ans += dp[i] % mod;
if (ans >= mod){
ans -= mod;
}
}
return ans;
}
}
int solve(int n, int k, int* a){
For(i, 0, n){
sparse[0][i] = a[i];
}
For(j, 1, M){
ForE(i, 0, n - (1 << j)){
sparse[j][i] = max(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
}
}
if (k == 1){
For(i, 0, n){
dp[i] = (i == 0 ? 0 : dp[i - 1]) + a[i];
}
return convert(n);
}
For(i, 0, n){
int val = 0;
while (!dq.empty() and a[dq.front().fi] <= a[i]){
mtsdp.erase(mtsdp.lower_bound(dq.front().se));
val = a[dq.front().fi];
dq.pop_front();
}
if (!dq.empty()){
mtsdp.erase(mtsdp.lower_bound(dq.front().se));
dq.front().se -= val;
dq.front().se += a[i];
mtsdp.insert(dq.front().se);
}
dp[i] = infll;
if (!mtsdp.empty()){
dp[i] = *mtsdp.begin();
}
dp[i] = min(dp[i], rmq(max(0, i - k + 1), i) + (i >= k ? dp[i - k] : 0));
dq.push_front(make_pair(i, dp[i]));
mtsdp.insert(dp[i]);
while (!dq.empty() and dq.back().fi == i - k + 1){
mtsdp.erase(mtsdp.lower_bound(dq.back().se));
dq.pop_back();
}
}
// For(i, 0, n){
// cout << dp[i] << ' ';
// } cout << endl;
return convert(n);
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |