#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x << ' '; print(x); cerr << endl;
#else
#define dbg(x)
#endif
void print(int x) {cerr << x;}
void print(long long x) {cerr << x;}
void print(char x) {cerr << x;}
void print(string x) {cerr << x;}
void print(double x) {cerr << x;}
template <class T> void print(vector <T> x);
template <class T> void print(set <T> x);
template <class T> void print(multiset <T> x);
template <class T, class V> void print(pair <T, V> x);
template <class T, class V> void print(map <T, V> x);
template <class T> void print(vector <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T> void print(set <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T> void print(multiset <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T, class V> void print(pair <T, V> x) {cerr << "{"; print(x.first); cerr << ' '; print(x.second); cerr << "}";}
template <class T, class V> void print(map <T, V> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
#define ll long long
#define pb push_back
#define ppb pop_back
#define PII pair <int, int>
#define PLL pair <ll, ll>
#define all(v) (v).begin(), (v).end()
#define OK cerr << "OK\n";
#define MP make_pair
const int N0 = 1e5 + 5, inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;
int n, q;
struct ber{
ll w, a, b;
};
bool operator<(ber x, ber y){
return x.w <= y.w;
}
vector <ber> a;
vector <PII> Q;
struct DisjointSetUnion{
int p[N0], sz[N0];
void init(){
for(int i = 0; i < n; i++){
sz[i] = 1;
p[i] = i;
}
}
int find(int x){
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
bool same_comp(int x, int y){
return find(x) == find(y);
}
void unite(int x, int y){
x = find(x), y = find(y);
if(x == y) return;
if(sz[x] < sz[y]) swap(x, y);
p[y] = x;
sz[x] += sz[y];
}
} dsu;
vector <ll> calculate_costs(vector <int> W, vector <int> A, vector <int> B, vector <int> E){
n = W.size();
q = E.size();
bool subtask1 = 1;
for(int i = 0; i < n; i++){
a.pb({W[i], A[i], B[i]});
if(W[i] != 1) subtask1 = 0;
}
if(subtask1){
ll sum = 0, mn = inf64;
for(int i = 0; i < n; i++){
sum += a[i].b;
mn = min(mn, a[i].a - a[i].b);
}
if(n & 1) sum += mn;
return vector<ll>(q, sum);
}
for(int i = 0; i < q; i++){
Q.pb({E[i], i});
}
sort(all(a));
sort(all(Q));
vector <ll> answ(q);
for(int ii = 0; ii < q; ii++){
ll d = Q[ii].first;
int indx = Q[ii].second;
dsu.init();
for(int i = 0; i < n - 1; i++){
if(a[i + 1].w - a[i].w <= d) dsu.unite(i, i + 1);
}
vector <PII> comp;
int L = 0, R = 0;
for(int i = 1; i < n; i++){
if(dsu.find(i) != dsu.find(i - 1)){
comp.pb({L, R});
L = R = i;
}
else R++;
}
comp.pb({L, R});
ll cost = 0;
for(PII seg : comp){
int L = seg.first, R = seg.second;
ll segcost = 0;
for(int i = L; i <= R; i++){
segcost += a[i].b;
}
if((R - L + 1) % 2 == 0){
cost += segcost;
continue;
}
ll nsegcost = inf64;
for(int i = L; i <= R; i++){
if(i == L || i == R || a[i + 1].w - a[i - 1].w <= d){
nsegcost = min(nsegcost, segcost - a[i].b + a[i].a);
continue;
}
if((i - L) & 1) continue;
nsegcost = min(nsegcost, segcost - a[i].b + a[i].a);
}
cost += nsegcost;
}
answ[indx] = cost;
}
return answ;
}
# | 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... |
# | 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... |