//Dedicated to my love, ivaziva
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(a) ((int)a.size())
const int N=1e5+5;
int n, q;
int w[N], a[N], b[N], e[N];
ll dp[N][2];
vector<ll> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) {
n=sz(W), q=sz(E);
for (int i=1; i<=n; i++)
w[i]=W[i-1], a[i]=A[i-1], b[i]=B[i-1];
for (int i=1; i<=q; i++)
e[i]=E[i-1];
if (q<=5&&n<=2000&&W==vector<int>(n, 1)) {
if (n%2==0) {
ll ans=0;
for (int i=1; i<=n; i++)
ans+=b[i];
return (vector<ll>(q, ans));
}
else {
ll sumb=0, ans=(ll)(1e18);
for (int i=1; i<=n; i++)
sumb+=b[i];
for (int i=1; i<=n; i++)
ans=min(ans, a[i]+sumb-b[i]);
return (vector<ll>(q, ans));
}
}
vector<int> sub2;
for (int i=0; i<n; i++) sub2.pb(i+1);
if (q<=5&&W==sub2) {
if (n%2==0) {
ll ans=0;
for (int i=1; i<=n; i++)
ans+=b[i];
return (vector<ll>(q, ans));
}
else {
vector<ll> ret;
for (int i=1; i<=q; i++) {
if (e[i]>=2) {
ll sumb=0, ans=(ll)(1e18);
for (int i=1; i<=n; i++)
sumb+=b[i];
for (int i=1; i<=n; i++)
ans=min(ans, a[i]+sumb-b[i]);
ret.pb(ans);
}
else {
ll sumb=0, ans=(ll)(1e18);
for (int i=1; i<=n; i++)
sumb+=b[i];
for (int i=1; i<=n; i++)
if (i%2==1)
ans=min(ans, a[i]+sumb-b[i]);
ret.pb(ans);
}
}
return ret;
}
}
if (q<=5&&A==vector<int>(n, 2)&&B==vector<int>(n, 1)) {
sort(w+1, w+n+1);
vector<ll> ret;
for (int j=1; j<=q; j++) {
ll ans=n, curr=1;
for (int i=1; i<n; i++) {
if (w[i+1]-w[i]>e[j]) {
ans+=curr%2;
curr=1;
continue;
}
curr++;
}
ans+=curr%2;
ret.pb(ans);
}
return ret;
}
if (q<=5) {
pair<ll, pair<ll, ll>> vec[n+1];
for (int i=1; i<=n; i++)
vec[i]=make_pair(w[i], make_pair(a[i], b[i]));
sort(vec+1, vec+n+1);
vector<ll> ret;
for (int D:E) {
ll dp[n+1];
dp[0]=0;
for (int i=1; i<=n; i++) {
dp[i]=dp[i-1]+vec[i].second.first;
ll sum=0;
for (int j=i-1; j>=max(1, i-2); j--) {
if (vec[i].first-vec[j].first<=D) {
if (j+1<i)
sum+=vec[j+1].second.first;
dp[i]=min(dp[i], dp[j-1]+vec[i].second.second+vec[j].second.second+sum);
}
}
}
ret.pb(dp[n]);
}
return ret;
}
return vector<ll>(q, -1);
}
// int32_t main () {
// ios::sync_with_stdio(false), cin.tie(0);
// int N_;
// cin >> N_;
// vector<int> W(N_), A(N_), B(N_);
// for (int i=0; i<N_; i++)
// cin >> W[i] >> A[i] >> B[i];
// int Q_;
// cin >> Q_;
// vector<int> E(Q_);
// for (int i=0; i<Q_; i++)
// cin >> E[i];
// vector<ll> ans=calculate_costs(W, A, B, E);
// cout << "-------------------\n";
// cout << "Answer:\n";
// for (ll x:ans) {
// cout << x << ' ';
// }
// cout << '\n';
// return 0;
// }
/*
5
15 5 1
12 4 2
2 5 2
10 6 3
21 3 2
3
5 9 1
*/
# | 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... |