QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#69006 | #4590. Happy Travelling | duongnc000 | WA | 74ms | 17720kb | C++20 | 3.3kb | 2022-12-22 13:21:59 | 2022-12-22 13:22:02 |
Judging History
answer
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
#define int long long
#define taskname "bai3"
#define all(x) x.begin(), x.end()
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
using namespace std;
const int mxN = 1e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, k, d, a[mxN], b[mxN], st[4 * mxN], dp[mxN];
vector<pii> segments;
set<pii> s[mxN];
bool sortbysec(pii x, pii y) {
if(x.ss == y.ss) return (x.ff < y.ff);
return (x.ss < y.ss);
}
void build(int l, int r, int idx) {
if(l == r) {
st[idx] = -oo;
return;
}
int mid = (l + r) >> 1;
build(l, mid, idx * 2);
build(mid + 1, r, idx * 2 + 1);
st[idx] = max(st[idx * 2], st[idx * 2 + 1]);
}
void update1(int l, int r, int idx, int pos, int val) {
if(l == r) {
st[idx] = val;
return;
}
int mid = (l + r) >> 1;
if(pos <= mid) update1(l, mid, idx * 2, pos, val);
else update1(mid + 1, r, idx * 2 + 1, pos, val);
st[idx] = max(st[idx * 2], st[idx * 2 + 1]);
}
int get(int l, int r, int idx, int u, int v) {
if(v < u) return -oo;
if(r < u || v < l) return -oo;
if(u <= l && r <= v) {
//cout << l << " " << r << " " << idx << " " << st[idx] << endl;
return st[idx];
}
int mid = (l + r) >> 1;
return max(get(l, mid, idx * 2, u, v), get(mid + 1, r, idx * 2 + 1, u, v));
}
void solve() {
cin >> n >> k >> d;
build(1, k, 1);
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i < n; ++i) {
cin >> b[i];
segments.pb({i, i + b[i]});
}
sort(all(segments), sortbysec);
dp[1] = a[1]; s[1].insert({dp[1], 1});
update1(1, k, 1, (1 % k) + 1, dp[1]);
int idx = 0;
for(int i = 2; i <= n; ++i) {
while(idx + 1 < n && segments[idx].ss < i) {
int i_ = segments[idx].ff;
int tmp = i_ % k, tmp1 = i_ / k;
s[tmp].erase({dp[i_] + tmp1 * d, i_});
pii lst = {-oo, oo};
if(!s[tmp].empty()) lst = *prev(s[tmp].end());
update1(1, k, 1, tmp + 1, lst.ff);
++idx;
}
int tmp = i % k, tmp1 = i / k;
if(tmp1 < 1) {
int get1 = get(1, k, 1, 1, tmp + 1);
//cout << get1 << endl;
get1 -= tmp1 * d;
dp[i] = a[i] + get1;
}
else {
int get1 = get(1, k, 1, 1, tmp + 1), get2 = get(1, k, 1, tmp + 2, k);
//cout << "tmp: " << tmp << endl;
//cout << "get1: " << get1 << " " << get2 << endl;
get1 -= tmp1 * d;
get2 -= (tmp1 - 1) * d;
dp[i] = a[i] + max(get1, get2);
}
//cout << "dp: " << i << " " << dp[i] << endl;
s[tmp].insert({dp[i] + tmp1 * d, i});
pii lst = *prev(s[tmp].end());
update1(1, k, 1, tmp + 1, lst.ff);
}
cout << dp[n] << endl;
}
signed main() {
#ifdef CDuongg
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 8256kb
input:
6 2 1 8 -7 -8 9 0 2 5 3 3 2 1
output:
18
result:
ok single line: '18'
Test #2:
score: 0
Accepted
time: 2ms
memory: 8212kb
input:
8 8 8 10 -5 -5 -5 -5 -5 -5 10 5 2 5 3 2 1 1
output:
15
result:
ok single line: '15'
Test #3:
score: 0
Accepted
time: 1ms
memory: 8256kb
input:
13 2 2 -5 -4 -4 -1 7 -6 -5 -4 -3 -2 -1 5 -7 3 10 9 8 7 6 5 4 3 2 1 1
output:
-9
result:
ok single line: '-9'
Test #4:
score: 0
Accepted
time: 5ms
memory: 8016kb
input:
2 1 0 -10000 10000 1
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 74ms
memory: 15760kb
input:
98987 4 3 -8225 -8961 -5537 -5621 -8143 -5214 -5538 -6912 -6601 -8839 -7872 -7867 -9553 -9793 -7333 -7360 -5820 -7459 -8824 -9716 -9757 -5846 -5300 -5912 -7953 -8360 -7609 -5937 -5525 -9748 -7326 -8311 -9979 -9292 -8542 -7589 -7939 -5914 -7985 -9999 -9212 -8274 -8084 -6620 -5991 -7826 -6327 -5228 -6...
output:
-84108
result:
ok single line: '-84108'
Test #6:
score: 0
Accepted
time: 57ms
memory: 15940kb
input:
98467 2 3 -5677 -9080 -6665 -5838 -5755 -8938 -6286 -5160 -7147 -8370 -8214 -6088 -9763 -5183 -7123 -7264 -5298 -8855 -6381 -6592 -9216 -8429 -9598 -7443 -7393 -8712 -5545 -6778 -6010 -5717 -9102 -7968 -6140 -9592 -7917 -5217 -5015 -7798 -9339 -5678 -7073 -7607 -7961 -6185 -9941 -6421 -8779 -5388 -8...
output:
-150169
result:
ok single line: '-150169'
Test #7:
score: 0
Accepted
time: 70ms
memory: 15560kb
input:
96173 2 1 -6463 -9099 -6269 -6169 -8273 -9839 -9929 -5447 -5908 -6884 -6908 -8359 -6477 -9414 -9207 -8180 -6264 -9293 -8981 -6557 -9260 -9700 -6785 -7121 -8382 -9712 -5178 -5821 -9107 -9004 -7472 -9306 -9311 -7160 -7965 -5394 -8048 -7415 -5233 -7746 -7390 -5298 -7721 -7915 -9646 -5371 -5712 -6234 -5...
output:
-45780
result:
ok single line: '-45780'
Test #8:
score: -100
Wrong Answer
time: 57ms
memory: 17720kb
input:
96905 1 1 -7933 -5685 -6201 -5415 -7294 -9904 -8968 -8433 -6287 -6727 -5933 -5418 -8953 -6743 -7057 -7087 -7244 -5302 -5130 -8934 -5053 -9811 -8695 -5989 -7785 -5991 -8351 -9463 -7037 -8867 -8956 -8194 -5272 -6270 -7737 -7793 -8851 -5839 -5178 -7009 -8018 -7375 -6091 -9226 -7117 -9522 -9248 -6036 -8...
output:
-78022
result:
wrong answer 1st lines differ - expected: '-72336', found: '-78022'