AtCoder Beginner Contest 430
D - Neighbor Distance
set模拟
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define pii pair<int,int>
#define ll long long
#define pb push_back
#define ft first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define int long long
const int N = 500010;
int a[N];
void solve(){
int n; cin >> n;
int ans = 0;
set<pii> s; s.insert({0, 0}); s.insert({1e18, -1});
for(int i = 1; i <= n; i ++){
int x; cin >> x;
if(i == 1){a[0] = x; ans += x;}
auto t1 = s.lower_bound({x, -1});auto t = t1; t --;
int tf = (*t).ft;int t1f = (*t1).ft;
int ts = (*t).se; int t1s = (*t1).se;
if(ts != -1){
if(a[ts] > x - tf){
ans += x - tf - a[ts];
a[ts] = x - tf;
}
}
if(t1s != -1){
if(a[t1s] > t1f - x){
ans += t1f - x - a[t1s];
a[t1s] = t1f -x;
}
}
a[i] = min(x - tf, t1f - x);
ans += a[i];
s.insert({x,i});
cout << ans << '\n';
}
}
signed main(){
std::ios::sync_with_stdio(false);
int T=1;//cin>>T;
while(T--){
solve();
}
}
F - Back and Forth Filling
每个可能的位置是[ls+1, n-rs]
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define pii pair<int,int>
#define ll long long
#define pb push_back
#define ft first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define int long long
const int N = 500010;
int a[N];
void solve(){
int n; cin >> n;
int ans = 0;
set<pii> s; s.insert({0, 0}); s.insert({1e18, -1});
for(int i = 1; i <= n; i ++){
int x; cin >> x;
if(i == 1){a[0] = x; ans += x;}
auto t1 = s.lower_bound({x, -1});auto t = t1; t --;
int tf = (*t).ft;int t1f = (*t1).ft;
int ts = (*t).se; int t1s = (*t1).se;
if(ts != -1){
if(a[ts] > x - tf){
ans += x - tf - a[ts];
a[ts] = x - tf;
}
}
if(t1s != -1){
if(a[t1s] > t1f - x){
ans += t1f - x - a[t1s];
a[t1s] = t1f -x;
}
}
a[i] = min(x - tf, t1f - x);
ans += a[i];
s.insert({x,i});
cout << ans << '\n';
}
}
signed main(){
std::ios::sync_with_stdio(false);
int T=1;//cin>>T;
while(T--){
solve();
}
}

浙公网安备 33010602011771号