This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define inf (int)1e9
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)), v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if (x < 0)
x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if (x < 0)
x += mod;
}
int n, k, a[maxn];
int l[maxn], r[maxn], f[2][maxn];
void L(int a[], int n, int ans[])
{
stack<int> st;
fo(i, 1, n)
{
while (st.size() and a[st.top()] <= a[i])
st.pop();
ans[i] = (st.empty() ? 0 : st.top());
st.push(i);
}
}
void R(int a[], int n, int ans[])
{
stack<int> st;
fod(i, n, 1)
{
while (st.size() and a[st.top()] <= a[i])
st.pop();
ans[i] = (st.empty() ? n + 1 : st.top());
st.push(i);
}
}
int rmq[maxn][18];
int get(int l,int r)
{
if(l > r) return inf;
int lg = __lg(r - l + 1);
return min(rmq[l][lg],rmq[r - (1 << lg) + 1][lg]);
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
fo(i, 1, n) cin >> a[i];
L(a, n, l);
R(a, n, r);
int maxx = 0;
fo(i,1,n) maximize(maxx,a[i]),f[1][i] = maxx;
fo(j, 2, k)
{
priority_queue<pii,vii,greater<pii>> q;
fo(i,1,n) rmq[i][0] = f[j & 1 ^ 1][i];
fo(k,1,17) fo(i,1,n)
rmq[i][k] = min(rmq[i][k - 1],rmq[i + (1 << k - 1)][k - 1]);
fo(i, 1, n)
{
f[j & 1][i] = get(max(l[i],1), i - 1) + a[i];
while(ss(q) and q.top().se < i) q.pop();
if(ss(q))
minimize(f[j & 1][i],q.top().fi);
q.push({f[j & 1][i],r[i] - 1});
}
}
int ans = inf;
fod(i, n, 1) if (r[i] == n + 1) minimize(ans, f[k & 1][i]);
cout << ans;
}
Compilation message (stderr)
blocks.cpp:90:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
90 | main()
| ^~~~
blocks.cpp: In function 'int main()':
blocks.cpp:109:35: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
109 | fo(i,1,n) rmq[i][0] = f[j & 1 ^ 1][i];
| ~~^~~
blocks.cpp:111:55: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
111 | rmq[i][k] = min(rmq[i][k - 1],rmq[i + (1 << k - 1)][k - 1]);
| ~~^~~
blocks.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |