#include <bits/stdc++.h>
#define ll long long
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) ((1LL) << (i))
using namespace std;
const long mod = 1e9+7;
const long Nmax = 1e5+7;
int fr, to, cost, d, n;
int t[Nmax], b[Nmax];
ll dp[Nmax], inf;
vector<int> nen, num;
pair<ll, int> st[Nmax * 4];
pair<ll, int> maximize(pair<ll, int> left, pair<ll, int> right)
{
if (left.first > right.first) return left;
else return right;
}
void update(int id, int l, int r, int i, ll v, int k)
{
if (l > i || r < i) return;
if (l == r)
{
st[id] = maximize(st[id], {v, k});
// cout << st[id].first << " ";
return;
}
int mid = (l + r) >> 1;
update(id * 2, l, mid, i, v, k);
update(id * 2 + 1, mid + 1, r, i, v, k);
st[id] = maximize(st[id * 2], st[id * 2 + 1]);
}
pair<ll, int> get(int id, int l, int r, int u, int v)
{
if (l > v || r < u) return {-inf, -1};
if (l >= u && r <= v) return st[id];
int mid = (l + r) >> 1;
return maximize(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("name.inp","r",stdin);
//freopen("name.out","w",stdout);
cin >> fr >> to >> d >> cost >> n;
n += 2;
t[1] = fr;
t[n] = to;
nen.push_back(fr % d);
nen.push_back(to % d);
for (int i = 2; i <= n - 1; i++)
{
cin >> t[i] >> b[i];
nen.push_back(t[i] % d);
}
sort(nen.begin(), nen.end());
num.push_back(nen[0]);
for (int i = 1; i < nen.size(); i++) if (nen[i] != nen[i - 1]) num.push_back(nen[i]);
// for (int i = 0; i < num.size(); i++) cout << num[i] << " ";
// cout << "\n";
memset(dp, -0x3f, sizeof dp);
for (int i = 1; i < 4 * Nmax; i++) st[i] = {dp[0], -1};
inf = -dp[0];
inf += 5;
// cout << inf << "\n";;
dp[1] = 0;
for (int i = 1; i <= n; i++)
{
if (i == 1)
{
int modun = t[i] % d;
int pos = lower_bound(num.begin(), num.end(), modun) - num.begin() + 1;
// cout << pos << " ";
update(1, 1, num.size(), pos, dp[i] + 1LL * t[i]/d * cost, i);
}
else
{
int modun = t[i] % d;
// cout << modun << " ";
int pos = lower_bound(num.begin(), num.end(), modun) - num.begin() + 1;
int j = get(1, 1, num.size(), 1, pos - 1).second;
// cout << i << " " << j << ".";
if (j != -1) dp[i] = max(dp[i], dp[j] - 1LL * (t[i]/d - t[j]/d + 1) * cost + b[i]);
j = get(1, 1, num.size(), pos, num.size()).second;
// cout << i << " " << j << "\n";
if (j != -1) dp[i] = max(dp[i], dp[j] - 1LL * (t[i]/d - t[j]/d) * cost + b[i]);
update(1, 1, num.size(), pos, dp[i] + 1LL * t[i]/d * cost, i);
}
}
// cout << "\n";
// for (int i = 1; i <= n; i++) cout << dp[i] << " ";
// cout << "\n";
cout << dp[n];
}
Compilation message
koala.cpp: In function 'int main()':
koala.cpp:66:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int i = 1; i < nen.size(); i++) if (nen[i] != nen[i - 1]) num.push_back(nen[i]);
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7384 KB |
Output is correct |
4 |
Correct |
3 ms |
7384 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7416 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
3 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
10472 KB |
Output is correct |
2 |
Correct |
41 ms |
10276 KB |
Output is correct |
3 |
Correct |
16 ms |
9440 KB |
Output is correct |
4 |
Correct |
41 ms |
10524 KB |
Output is correct |
5 |
Correct |
24 ms |
9304 KB |
Output is correct |
6 |
Correct |
16 ms |
8536 KB |
Output is correct |
7 |
Correct |
20 ms |
9568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
10964 KB |
Output is correct |
2 |
Correct |
86 ms |
10872 KB |
Output is correct |
3 |
Correct |
81 ms |
10980 KB |
Output is correct |
4 |
Correct |
86 ms |
10960 KB |
Output is correct |
5 |
Correct |
56 ms |
9536 KB |
Output is correct |
6 |
Correct |
44 ms |
9136 KB |
Output is correct |