排列

· · 个人记录

排列

写作原因

单纯想写。

题意简化

有一个n个元素的排列,给出m个形如{a,b,c}的三元组描述该排列,要求b必须在a与c之间(不一定连续)。
现在排列未知,请求出一种排列使之至少满足\frac{m}{2}个三元组。

STP1

最初想法:这些三元组有位置相对关系,所以我们可以把相对位置关系转化为有向图(x在y左侧即x指向y),建立a指向b、b指向c的图来跑拓扑,求出答案。

但由于b必在a、c之间,而具体相对位置却不定:

所以优先确定一个作为最左侧的贪心是不成立的。(·_·)
于是又有了第二个想法:从两端向中间求出排列。
比如先将a放在排列靠左的位置,再将c放在排列靠右的位置,这样b只需要扔在中间的那些位置就能满足当前三元组了。因此a与c在拓扑时要比b更先出队并加入答案排列。
因此建立a指向b、c指向b的图来跑拓扑,每一个出队的值按照一左一右的顺序填入答案排列。
这就是我的赛事想法,骗到了\operatorname{25pts}。
代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e5 + 10;
int n, ind[N];
int m, a, b, c;
vector <int> G[N];
queue <int> Q;
deque <int> A[2]; 

signed main() {
//  freopen("permutation.in", "r", stdin);
//  freopen("permutation.out", "w", stdout);

    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    cin >> n >> m;
    for(int i = 1; i <= m; i ++) {
        cin >> a >> b >> c;
        G[a].emplace_back(b);
        G[c].emplace_back(b);
        ind[b] += 2;
    }

    for(int i = 1; i <= n; i ++) {
        if(0 == ind[i]) Q.push(i);
    }

    int st = 0;
    while(Q.size()) {
        int u = Q.front(); Q.pop();
        if(0 == st) A[st].push_back(u);
        if(1 == st) A[st].push_front(u);
        st = 1 - st;

        for(auto v : G[u]) {
            ind[v] --;
            if(0 == ind[v]) Q.push(v);
        }
    }

    while(A[0].size()) cout << A[0].front() << " ", A[0].pop_front();
    while(A[1].size()) cout << A[1].front() << " ", A[1].pop_front();
    return 0;
}

STP2

显然这不是正解。
(OS:T4正解不可能这么简单就能想出来)(·o·)

建图是没错,拓扑是没错,从两头到中间填写答案排列也没错,只是不能无脑填。
赛后发现一组数据:

9 6
1 2 3
5 2 6
4 1 5
7 4 3
6 3 8
7 9 8

画出来图长这样:

显然填出来的答案排列为:

5 7 3 4 2 1 9 8 6

这里已经有3组不满了,再填得随意一点就WA了,因此填之前的判断是必须滴。

STP3

对于一个三元组{a,b,c},我们假设a、c为'e',b为'w',如果根本不加限制,它的拓扑相对顺序会长成这样:

ewe \\eew \\wee \end{matrix}\right.

对于第三种情况,只要像方法一那样建图就能排除;
对于为什么要考虑第二种情况,是因为有些三元组这样排列会使整体更优;
建图还是那么建,只是中间'w'的入度设为1就行。
建图代码:

    for(int i = 1; i <= m; i ++) {
        cin >> thr[i][0] >> thr[i][1] >> thr[i][2];
        G[thr[i][0]].emplace_back(thr[i][1]);
        G[thr[i][2]].emplace_back(thr[i][1]);
        ind[thr[i][1]] ++;
        // only need anyone of the left and the right out, the mid can in;
        apr[thr[i][0]].push_back({i, 0});
        apr[thr[i][1]].push_back({i, 1});
        apr[thr[i][2]].push_back({i, 2});
    }

关于\operatorname{apr}数组干什么的,马上告知。

当我们拓扑到一个u时,枚举它出现过的所有三元组,\operatorname{apr}数组也就是用来存储这个的。第一维是该三元组的编号,第二维是u在该三元组中出现的位置。
对于当前三元组:
一、 u为'w'(该三元组至少有一个'e'的位置已确定):

二、u为'e':

定义数组can,当u填在左侧可以产生贡献时,can_{0}++;当u填在右侧可以产生贡献时,can_{1}++;最后判断can_{0}与can_{1}哪个大,就将u填在相应那一侧。
由于每次选择的一侧,另一侧的都不会被满足,但选择的都是较多的一侧,因此对于每一个u,都有满足数量大于不满足数量,故最后一定有超过\frac{m}{2}个三元组满足。
代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e5 + 10;
int n, ind[N], on[N], can[2];
int m, thr[N][3];
int l, r, Ans[N];
vector <pair <int, int> > apr[N]; 
vector <int> G[N];
queue <int> Q;
/*
e w e;
e e w;
w e e(it's excluded when we connect them).
*/
signed main() {
//  freopen("permutation.in", "r", stdin);
//  freopen("permutation.out", "w", stdout);

    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    cin >> n >> m;
    for(int i = 1; i <= m; i ++) {
        cin >> thr[i][0] >> thr[i][1] >> thr[i][2];
        G[thr[i][0]].emplace_back(thr[i][1]);
        G[thr[i][2]].emplace_back(thr[i][1]);
        ind[thr[i][1]] ++;
        // only need anyone of the left and the right out, the mid can in;
        apr[thr[i][0]].push_back({i, 0});
        apr[thr[i][1]].push_back({i, 1});
        apr[thr[i][2]].push_back({i, 2});
    }

    for(int i = 1; i <= n; i ++) {
        on[i] = -1;
        if(0 == ind[i]) Q.push(i);
    }
    l = 0, r = n + 1;
    while(Q.size()) {
        int u = Q.front(); Q.pop();
        can[0] = can[1] = 0;
        for(auto i : apr[u]) {
            int I = i.first, J = i.second;

            if(1 == J) { // the u is 'w';
                if(-1 != on[thr[I][0]] && -1 != on[thr[I][2]]) can[0] ++, can[1] ++; // both are sure;
                else {
                    if(-1 == on[thr[I][0]]) can[on[thr[I][0]]] ++; // only the first one is sure;
                    if(-1 == on[thr[I][2]]) can[on[thr[I][2]]] ++; // only the last one is sure;
                }
            } else { // the 'u' is 'e';
                int K = 2 - J;

                if(-1 != on[thr[I][K]]) {
                    if(-1 != on[thr[I][1]]) can[0] ++, can[1] ++; // both are sure;
                    else { // only the next one is sure;
                        int w = 1 - on[thr[I][K]];
                        can[w] ++;
                    }
                } else can[0] ++, can[1] ++; // both are not sure;
            }
        }
        if(can[0] > can[1]) Ans[++ l] = u, on[u] = 0;
        else Ans[-- r] = u, on[u] = 1;

        for(auto v : G[u]) {
            ind[v] --;
            if(0 == ind[v]) Q.push(v);
        }
    }

    for(int i = 1; i <= n; i ++) cout << Ans[i] << " ";
    return 0;
}

谢谢。(^v^)