#include "boxes.h"
#include<bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<ll>;
ll n, k, l;
vi pos, dpl, dpr;
ll getl(ll x) {
return min(2ll*x, l);
}
ll getr(ll x) {
return min(2ll*(l-x), l);
}
long long delivery(int N, int K, int L, int P[]) {
n=N, k = K, l = L;
ll ans = LLONG_MAX;
pos.resize(n+2);
dpl.resize(n+2);
dpr.resize(n+2);
for(int i = 0; i < n; i++)
pos[i+1] = P[i];
for(int i = 1; i <= n; i++) {
if((i-1)%k==0) {
dpl[i] = dpl[i-1]+pos[i]+min(pos[i-1], l-pos[i-1]);
} else {
dpl[i] = dpl[i-1]-pos[i-1]+pos[i];
}
}
// for(int i = 0; i <= n; i++) {
// cout << dpl[i] << " ";
// }
// cout << "l\n";
for(int i = 1; i <= n; i++) {
// if((i-1)%k!=0) {
dpl[i] = dpl[i]+min(pos[i], l-pos[i]);
// }
}
// cout << "r\n";
for(int i = n; i; i--) {
if((n-i)%k==0) {
dpr[i] = dpr[i+1]+((l-pos[i])%l)+min(pos[i+1], l-pos[i+1]);
} else {
dpr[i] = dpr[i+1]-((l-pos[i+1])%l)+((l-pos[i])%l);
}
}
// for(int i = n; i; i--) {
// if((n-i)%k!=0) {
// dpr[i] = dpr[i]+min(pos[i], l-pos[i]);
// }
// }
// for(int i = 0; i <= n; i++) {
// cout << dpr[i] << " ";
// }
for(int i = 0; i <= n; i++) {
// cout << dpr[i] << " ";
ans = min(ans, dpl[i]+dpr[i+1]);
// cout << i << " " << dpl[i] << " " << dpr[i+1] << "\n";
}
return ans;
}
Compilation message
boxes.cpp: In function 'long long int delivery(int, int, int, int*)':
boxes.cpp:41:14: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
for(int i = n; i; i--) {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |