Opencup XII, Grand Prix of Tokyo F.Robots

发布时间 2023-04-23 04:22:07作者: tiany7

F.Robots

来复健,但是没料到是GP场,就不该来,应该去vp

这道题的题意是给两个数组a和b,对于每个b,会有最近的依然active的a来配对,问最小的距离是多少,并且给出一种删b的方案

样例给了:

3
1 2 3
11 12 13

答案是30,我们先删掉13,然后删掉12,最后删掉11, 为什么不从前往后呢?

因为我们没法保证所有的a[i] <> b[i],会出现一种情况,当前的b抢走了前面的a,或者前面的b抢走了当前的a,为了保证一致性,我们可以分类讨论

我们固定a[i] < b[i] 为唯一的ordering,那么在我们新iterate到的节点,我们看看这个节点的a[i]会不会被前面抢走,如果会的话,先把前面爆了,然后再塞入当前的b,这样就保证了一致性,如果不会被爆,那么我们就把第i个节点爆了,从而保证无后效性。

那么怎么维护呢?我们可以用一个栈来维护。

#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
constexpr int limit =  (4e5  + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}

int n;
int a[limit];
int b[limit];
bitset<limit>vis;
struct node{
    int i, aa, bb;
};
list<node>rp;
void solve(){
    cin>>n;
    rep(i,1,n)cin>>a[i];
    rep(i,1,n)cin>>b[i];
    ll ans = 0;
    vector<int>res;
    rep(i,1, n){
        if(a[i] < b[i]){
            rp.push_back({int(i), a[i], b[i]});
        }else if(a[i] == b[i]){
            res.push_back(i);
        }else{
            if(rp.size() and llabs(rp.back().bb - rp.back().aa) < llabs(rp.back().bb - a[i])){
                while(rp.size() and llabs(rp.back().bb - rp.back().aa) < llabs(rp.back().bb - a[i])){
                    res.push_back(rp.back().i);
                    rp.pop_back();
                }
                rp.push_back({int(i), a[i], b[i]});
            }else{
                res.push_back(i);
            }
        }
    }
    for(auto &i : rp| views::reverse){
        res.push_back(i.i);
    }
    for(auto &i : res){
        ans += llabs(a[i] - b[i]);
    }
    cout<<ans<<endl;
    for(auto &i : res )cout<<i<<" ";
    cout<<endl;
};
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
//    int kase;
//    cin>>kase;
//    while (kase--)
        invoke(solve);
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
    return 0;
}