#include"holiday.h"
#include<bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define pii pair<ll,ll>
using namespace std;
const int N = 3e5 + 5;
int a[N], val[N], cur = 0;
ll dp[3][2][N];
pii tree[4 * N];
void upd(int u, int id, int l, int r, int t) {
if(l > id || r < id) return;
if(l == r) {
tree[u].f += t;
tree[u].s += val[l] * t;
return;
}
int mid = (l + r) / 2;
upd(2 * u, id, l, mid, t);
upd(2 * u + 1, id, mid + 1, r, t);
tree[u].f = tree[2 * u].f + tree[2 * u + 1].f;
tree[u].s = tree[2 * u].s + tree[2 * u + 1].s;
}
ll get(int u, int l, int r, int k) {
if(!k) return 0;
if(l == r) {
return (ll) min(tree[u].f, (ll)k) * val[l];
}
int mid = (l + r) / 2;
if(tree[2 * u + 1].f > k) {
return get(2 * u + 1, mid + 1, r, k);
}
return tree[2 * u + 1].s + get(2 * u, l, mid, k - tree[2 * u + 1].f);
}
ll get(int x) {
if(x < 0) return -1e5;
return get(1, 1, cur, x);
}
void solve(int optl, int optr, int l, int r,int price, int t) {
if(l > r || optl > optr) return;
int mid = (l + r) / 2; //cout << l << "__" << r << endl;
ll opt = 0, optm = optl;
for(int i = optl; i <= optr; i++) {
upd(1, a[i], 1, cur, 1);
if(opt <= get(mid - (ll)i * price)) {
opt = get(mid - (ll)i * price);
optm = i;
}
}
dp[price][t][mid] = opt;
for(int i = optm; i <= optr; i++) upd(1, a[i], 1, cur, -1);
if(l != r) solve(optm, optr, mid + 1, r, price, t);
for(int i = optl; i < optm; i++) upd(1, a[i], 1, cur, -1);
if(l != r) solve(optl, optm, l, mid - 1, price, t);
}
long long findMaxAttraction(int n, int start, int d, int attraction[]) {
vector<int> x;
ll sum = 0;
for(int i = 0; i < n; i++) x.push_back(attraction[i]), sum += attraction[i];
sort(x.begin(), x.end());
cur =0 ;
map<int,int> id;
for(int i = 0; i < x.size(); i++) {
if(!i || x[i] != x[i - 1]) ++cur;
id[x[i]] = cur;
val[cur] = x[i];
}
for(int i = 0; i < n; i++) attraction[i] = id[attraction[i]];
for(int i = 0; i < n - start; i++) a[i] = attraction[i + start];
solve(0, n - start - 1, 1, d, 1, 0);
solve(0, n - start - 1, 1, d, 2, 0);
for(int i = start - 1; i >= 0; i--) a[start - 1 - i] = attraction[i];
solve(0, start - 1, 1, d, 1, 1);
solve(0, start - 1, 1, d, 2, 1);
for(int i = d; i >= 1; i--) {
dp[1][1][i] = dp[1][1][i - 1];
if(i == 1) dp[2][1][i] = 0;
else dp[2][1][i] = dp[2][1][i - 2];
}
ll ans = 0;
for(int i = 0; i <= d; i++) {
ans = max(ans, dp[1][0][i] + dp[2][1][d - i]);
ans = max(ans, dp[2][0][i] + dp[1][1][d - i]);
}
return ans;
}
Compilation message
holiday.cpp: In function 'long long int findMaxAttraction(int, int, int, int*)':
holiday.cpp:67:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i = 0; i < x.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
7780 KB |
Output is correct |
2 |
Correct |
53 ms |
7868 KB |
Output is correct |
3 |
Correct |
130 ms |
7772 KB |
Output is correct |
4 |
Correct |
124 ms |
7804 KB |
Output is correct |
5 |
Correct |
534 ms |
5532 KB |
Output is correct |
6 |
Correct |
203 ms |
6680 KB |
Output is correct |
7 |
Correct |
295 ms |
4016 KB |
Output is correct |
8 |
Correct |
362 ms |
3940 KB |
Output is correct |
9 |
Correct |
207 ms |
8444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
1228 KB |
Output is correct |
2 |
Correct |
3 ms |
972 KB |
Output is correct |
3 |
Correct |
26 ms |
1192 KB |
Output is correct |
4 |
Correct |
23 ms |
1100 KB |
Output is correct |
5 |
Correct |
21 ms |
1096 KB |
Output is correct |
6 |
Correct |
6 ms |
844 KB |
Output is correct |
7 |
Correct |
5 ms |
796 KB |
Output is correct |
8 |
Correct |
5 ms |
716 KB |
Output is correct |
9 |
Correct |
4 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
9420 KB |
Output is correct |
2 |
Correct |
1351 ms |
19368 KB |
Output is correct |
3 |
Correct |
504 ms |
5952 KB |
Output is correct |
4 |
Correct |
19 ms |
972 KB |
Output is correct |
5 |
Correct |
6 ms |
816 KB |
Output is correct |
6 |
Correct |
5 ms |
716 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
1431 ms |
12180 KB |
Output is correct |
9 |
Correct |
1479 ms |
12168 KB |
Output is correct |