This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include "holiday.h"
#include <bits/stdc++.h>
using namespace std;
void File() {
#define file "test"
freopen(file".in", "r", stdin);
freopen(file".out", "w", stdout);
}
set<pair<int, int>> chosen;
set<pair<int, int>, greater<>> have;
long long curSum = 0;
int lt = 1;
int rt = 0;
int n, d, start;
int du;
int arr[100001];
long long ans = -1e18;
int cnt = 0;
void trade()
{
pair<int, int> temp1 = *have.begin();
pair<int, int> temp2 = *(--chosen.end());
curSum = curSum - temp2.first + temp1.first;
have.erase(temp1);
have.insert(temp2);
chosen.erase(temp2);
chosen.insert(temp1);
}
void giveToHave()
{
du++;
pair <int, int> temp = *chosen.begin();
have.insert(temp);
curSum -= temp.first;
chosen.erase(temp);
}
void giveToChosen()
{
du--;
pair <int, int> temp = *have.begin();
chosen.insert(temp);
curSum += temp.first;
have.erase(temp);
}
void Add(int x)
{
pair <int, int> temp = *chosen.begin();
if (du > 0)
{
du--;
curSum += arr[x];
chosen.insert(make_pair(arr[x], x));
}
else if (arr[x] > temp.first)
{
curSum -= temp.first;
have.insert(temp);
chosen.erase(temp);
chosen.insert(make_pair(arr[x], x));
curSum += arr[x];
}
else have.insert(make_pair(arr[x], x));
}
void Del(int x)
{
if (chosen.find(make_pair(arr[x], x)) != chosen.end())
{
chosen.erase(make_pair(arr[x], x));
curSum -= arr[x];
du++;
if (!have.empty() && du > 0) giveToChosen();
}
else have.erase(make_pair(arr[x], x));
}
int calcDis(int l, int r)
{
if (l > r) return 0;
if (start <= l) return(l - start + (r - l));
if (start >= r) return(start - r + (r - l));
return (min(start - l, r - start) + (r - l));
}
long long cost(int l, int r)
{
while (lt != l || rt != r)
{
while (rt < r)
{
du += calcDis(lt, rt) - calcDis(lt, rt + 1);
rt++;
Add(rt);
while (!have.empty() && !chosen.empty() && have.begin()->first > (--chosen.end())->first) trade();
while (du < 0 && !chosen.empty()) giveToHave();
while (du > 0 && !have.empty()) giveToChosen();
}
while (rt > lt && rt > r)
{
du += calcDis(lt, rt) - calcDis(lt, rt - 1);
Del(rt);
rt--;
while (!have.empty() && !chosen.empty() && have.begin()->first > (--chosen.end())->first) trade();
while (du < 0 && !chosen.empty()) giveToHave();
while (du > 0 && !have.empty()) giveToChosen();
}
while (lt < rt && lt < l)
{
du += calcDis(lt, rt) - calcDis(lt + 1, rt);
Del(lt);
lt++;
while (!have.empty() && !chosen.empty() && have.begin()->first > (--chosen.end())->first) trade();
while (du < 0 && !chosen.empty()) giveToHave();
while (du > 0 && !have.empty()) giveToChosen();
}
while (lt > l)
{
du += calcDis(lt, rt) - calcDis(lt - 1, rt);
lt--;
Add(lt);
while (!have.empty() && !chosen.empty() && have.begin()->first > (--chosen.end())->first) trade();
while (du < 0 && !chosen.empty()) giveToHave();
while (du > 0 && !have.empty()) giveToChosen();
}
}
return curSum;
}
void divide(int l, int r, int gL, int gR)
{
if (l > r) return;
int mid = (l + r) / 2;
long long bestCost = -1e18;
int bestPos = gL;
for (int i = gL; i <= min(mid, gR); i++)
{
if (cost(i, mid) > bestCost)
{
bestCost = cost(i, mid);
bestPos = i;
}
}
ans = max(ans, bestCost);
divide(l, mid - 1, gL, bestPos);
divide(mid + 1, r, bestPos, gR);
}
long long findMaxAttraction(int _n, int _start, int _d, int *attraction)
{
n = _n;
d = _d;
start = _start + 1;
for (int i = 1; i <= n; i++) arr[i] = attraction[i - 1];
du = d;
divide(1, n, 1, n);
return ans;
}
/*
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
File();
cin >> n >> start >> d;
for (int i = 1; i <= n; i++) cin >> arr[i];
cout << findMaxAttraction(n, start, d, arr);
return 0;
}*/
Compilation message (stderr)
holiday.cpp: In function 'void File()':
holiday.cpp:10:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | freopen(file".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
holiday.cpp:11:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | freopen(file".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |