#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
const int MAXN = 2000013;
const long long LLINF = 3e18;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, D, T;
int arr[MAXN], lt[MAXN];
int fen[MAXN];
ll dp[MAXN];
int cost[MAXN], taken[MAXN];
int ans;
void update(int idx, int v)
{
for (int e = idx + 1; e <= N; e += e & (-e))
{
ckmax(fen[e], v);
}
}
int query(int idx)
{
int res = -1;
for (int e = idx + 1; e; e -= e & (-e))
{
ckmax(res, fen[e]);
}
return res;
}
void check(int c)
{
// cerr << "CHECKING " << c << endl;
//assign a penalty of c for starting a new range at c.
//minimum # of guys rebelling.
dp[0] = 0; taken[0] = 0;
fill(cost, cost + N + 1, 0);
FOR(i, 0, N)
{
dp[i + 1] = LLINF;
int v = lt[i];
FOR(j, 0, v + 1)
{
cost[j]++;
}
FOR(j, 0, i + 1)
{
if (dp[i + 1] > dp[j] + cost[j] + c)
{
dp[i + 1] = dp[j] + cost[j] + c;
taken[i + 1] = taken[j] + 1;
}
}
// cerr << dp[i + 1] << ' ' << taken[i + 1] << endl;
}
// cerr << "DP " << dp[N] << " TAKEN " << c << endl;
return;
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
cin >> N >> D >> T; D++;
FOR(i, 0, N)
{
cin >> arr[i];
}
fill(fen, fen + N + 1, -1);
FOR(i, 0, N)
{
int c = arr[i];
if (c <= T)
{
//i...i+(T-c) are getting infected.
update(max(0, N - 1 - (i + (T - c))), i);
}
lt[i] = query(N - 1 - i);
// cerr << lt[i] << ' ';
}
int lo = 0, hi = N + 1;
while(hi > lo)
{
int mid = (hi + lo + 1) >> 1;
check(mid);
if (taken[N] >= D) lo = mid;
else hi = mid - 1;
}
// cerr << "LO = " << lo << endl;
check(lo);
// cerr << "TAKED " << taken[N] << endl;
ll cost1 = dp[N] - lo * taken[N]; int cnt1 = taken[N];
assert(cnt1 >= D);
if (cnt1 == D)
{
ans = cost1;
}
else
{
hi++;
check(hi);
ll cost2 = dp[N] - hi * taken[N]; int cnt2 = taken[N];
assert(cnt2 < D);
// cerr << cost1 << ' ' << cnt1 << ' ' << cost2 << ' ' << cnt2 << endl;
ans = cost2 + (cost1 - cost2) * (D - cnt2) / (cnt1 - cnt2);
}
cout << ans << '\n';
//you are going to have some new blocks starting with -1....x. and then -1...x and then -1...x
// cerr << endl;
return 0;
}
Compilation message
/usr/bin/ld: /tmp/ccDmgnIl.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cc09nqUk.o:swaps.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccDmgnIl.o: in function `main':
grader.cpp:(.text.startup+0x67): undefined reference to `solve(int, int)'
collect2: error: ld returned 1 exit status