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 <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#define X first
#define Y second
#define PB push_back
using namespace std;
const int N = 1e5 + 500;
const int OFF = (1 << 17);
typedef long long ll;
typedef pair < int, int > pii;
ll LO, HI = (ll)1e12, COST;
inline ll get_cost(pii &tmp){
return 100000LL * tmp.X + COST * tmp.Y;
}
inline pii bolji(pii A, pii B){
if(get_cost(A) < get_cost(B))
return A;
return B;
}
int n, d, tme[N], L;
int par[N];
vector < int > q[N];
int pr(int x){
if(par[x] == x) return x;
return par[x] = pr(par[x]);
}
pii dp[N];
int dod[N];
pii probaj(){
for(int i = 0;i <= n;i++) dod[i] = 0, dp[i] = {0,0};
int mini = 0, pos = 0; dp[0] = {0, 0};
for(int i = 1;i <= n;i++){
for(int j : q[i]){
//printf("%d %d += 1\n", 0, j - 1);
if(j > mini)
dod[j]++, pos++;
while(mini + 1 < i &&
100000LL * dp[mini].X + COST * dp[mini].Y >=
100000LL * (dp[mini + 1].X - dod[mini + 1]) + COST * dp[mini + 1].Y){
pos -= dod[++mini];
}
}
//printf("mini = %d\n", mini);
dp[i] = dp[mini]; dp[i].X += pos, dp[i].Y++;
while(mini + 1 <= i &&
100000LL * dp[mini].X + COST * dp[mini].Y >=
100000LL * (dp[mini + 1].X - dod[mini + 1]) + COST * dp[mini + 1].Y){
pos -= dod[++mini];
}
//printf("dp[ %d ] = %d %d\n", i, dp[i].X, dp[i].Y);
}
dp[mini].X += pos;
return dp[mini];
}
int main(){
scanf("%d%d%d", &n, &d, &L);
stack < int > Q;
for(int i = 1;i <= n;i++){
scanf("%d", tme + i);
Q.push(i);
while(!Q.empty() && tme[Q.top()] + i - Q.top() > L)
Q.pop();
if(!Q.empty()){
// printf("%d %d\n", Q.top(), i);
q[i].PB(Q.top());
}
}
for(int i = 0;i < 45;i++){
COST = (LO + HI) / 2;
int odg = probaj().Y;
if(odg == d) break;
// printf("LO = %lld HI = %lld\n", LO, HI);
if(odg < d)
HI = COST;
else
LO = COST;
}
//printf("COST = %lld\n", COST);
pii fin = probaj();
//printf("%d %d\n", fin.X, fin.Y);
printf("%lld\n", (get_cost(fin) - COST * d) / 100000LL);
return 0;
}
Compilation message (stderr)
prison.cpp: In function 'int main()':
prison.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d%d%d", &n, &d, &L);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
prison.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d", tme + i);
| ~~~~~^~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |