#include<bits/stdc++.h>
using namespace std;
const int Nmax = 1e5;
const int MOD = 1000000007;
int numhouse, sta, en;
long long maxdis, fee;
long long dir[Nmax + 3], val[Nmax + 3], p[Nmax + 3];
map<int, int> newdir;
long long dp[Nmax + 3];
vector<int> values;
struct SegmentTree{
long long st[(1 << 19) + 3];
void update(int id, int l, int r, int i, long long v){
if(i < l || i > r) return;
if(l == r){
st[id] = v;
return;
}
int mid = (l + r) / 2;
update(id * 2, l, mid, i, v);
update(id * 2 + 1, mid + 1, r, i, v);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
long long get(int id, int l, int r, int u, int v){
if(v < l || r < u) return -1e18;
if(u <= l && r <= v) return st[id];
int mid = (l + r) / 2;
return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
} segtree;
void in(){
cin >> sta >> en >> maxdis >> fee >> numhouse;
dir[0] = sta;
p[0] = sta % maxdis;
values.push_back(p[0]);
dir[numhouse + 1] = en;
p[numhouse + 1] = en % maxdis;
values.push_back(p[numhouse + 1]);
for(int i = 1; i <= numhouse; i++){
cin >> dir[i] >> val[i];
p[i] = dir[i] % maxdis;
values.push_back(p[i]);
}
sort(values.begin(), values.end());
values.erase(unique(values.begin(), values.end()), values.end());
for(int i = 0; i < values.size(); i++) {
newdir[values[i]] = i + 1;
segtree.update(1, 1, values.size(), i + 1, -1e18);
}
}
void sol(){
//dp[i] = max(dp[i], dp[j] - (distance(j, i) / maxdis) * fee + val[i])
// distance(j, i) la khoang cach cua nha j va nha i (j <= i) = dir[i] - dir[j]
dp[0] = 0;
for(int i = 1; i <= numhouse + 1; i++){
dp[i] = val[i] -((dir[i] - dir[0] + maxdis - 1) / maxdis) * fee;
dp[i] = max(dp[i], segtree.get(1, 1, values.size(), newdir[p[i]], values.size()) - (dir[i] / maxdis) * fee + val[i]);
dp[i] = max(dp[i], segtree.get(1, 1, values.size(), 1, newdir[p[i]] - 1) - (dir[i] / maxdis + 1) * fee + val[i]);
segtree.update(1, 1, values.size(), newdir[p[i]], dp[i] + (dir[i] / maxdis) * fee);
}
cout << dp[numhouse + 1];
}
int main(){
//freopen(" ", "r", stdin);
//freopen(" ", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--){
in();
sol();
}
return 0;
}
Compilation message
koala.cpp: In function 'void in()':
koala.cpp:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0; i < values.size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
400 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
5872 KB |
Output is correct |
2 |
Correct |
30 ms |
5552 KB |
Output is correct |
3 |
Correct |
13 ms |
3904 KB |
Output is correct |
4 |
Correct |
30 ms |
5768 KB |
Output is correct |
5 |
Correct |
17 ms |
3728 KB |
Output is correct |
6 |
Correct |
11 ms |
2408 KB |
Output is correct |
7 |
Correct |
15 ms |
4844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
9740 KB |
Output is correct |
2 |
Correct |
118 ms |
12304 KB |
Output is correct |
3 |
Correct |
121 ms |
12496 KB |
Output is correct |
4 |
Correct |
111 ms |
12504 KB |
Output is correct |
5 |
Correct |
71 ms |
7488 KB |
Output is correct |
6 |
Correct |
51 ms |
5840 KB |
Output is correct |