| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1309833 | Rares | Radio Towers (IOI22_towers) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=3e5+10;
const ll INF=LLONG_MAX;
int n,m;
vector <int> pos[MAXN];
vector <pair <ll,ll>> c[MAXN];
ll query (ll i, ll j){
return (upper_bound (c[i].begin (), c[i].end (), make_pair (j,INF))-1)->second;
}
vector <ll> dpi,dpd;
ll max_weights(int N, int M, vector <int> x, vector <int> y, vector <int> w){
n=N,m=M;
for (int i=0;i<m;++i){
x[i]++;
y[i]++;
c[x[i]].push_back ({y[i],w[i]});
}
for (int i=0;i<=n+1;++i){
c[i].push_back ({0,0});
sort (c[i].begin (),c[i].end ());
for (int j=1;j<c[i].size ();++j){
c[i][j].second+=c[i][j-1].second;
}
}
for (int i=1;i<=n;++i){
vector <int> aux;
for (auto x:c[i-1]) aux.push_back (x.first);
for (auto x:c[i+1]) aux.push_back (x.first);
sort (aux.begin (),aux.end ());
for (int j=0;j<aux.size ();++j){
if (j==0 or aux[j]!=aux[j-1]) pos[i].push_back (aux[j]);
}
}
pos[0].push_back (0);
pos[n+1].push_back (0);
dpi.resize (1,0);
dpd.resize (1,0);
for (int i=1;i<=n+1;++i){
vector <ll> ndpi(pos[i].size (),0);
ll pmax=0,k=0;
for (int j=0;j<pos[i].size ();++j){
while (k<pos[i-1].size () and pos[i-1][k]<=pos[i][j]){
pmax=max (pmax, dpi[k]-query (i-1,pos[i-1][k]));
k++;
}
ndpi[j]=max (pmax+query (i-1,pos[i][j]),dpd[0]);
}
vector <ll> ndpd (pos[i].size (),0);
ll smax=0;
k=pos[i-1].size ()-1;
for (int j=pos[i].size ()-1;j>=0;--j){
while (k>=0 and pos[i-1][k]>=pos[i][j]){
smax=max (smax, max (dpi[k],dpd[k])+query (i,pos[i-1][k]));
k--;
}
ndpd[j]=smax-query (i,pos[i][j]);
}
swap (dpi,ndpi);
swap (dpd,ndpd);
}
return max (dpi[0],dpd[0]);
}
