排列
写作原因
单纯想写。
题意简化
有一个个元素的排列,给出个形如{}的三元组描述该排列,要求必须在与之间(不一定连续)。
现在排列未知,请求出一种排列使之至少满足个三元组。
STP1
最初想法:这些三元组有位置相对关系,所以我们可以把相对位置关系转化为有向图(在左侧即指向),建立指向、指向的图来跑拓扑,求出答案。
但由于必在、之间,而具体相对位置却不定:
- {}可以;
- {}可以。
所以优先确定一个作为最左侧的贪心是不成立的。(·_·)
于是又有了第二个想法:从两端向中间求出排列。
比如先将放在排列靠左的位置,再将放在排列靠右的位置,这样只需要扔在中间的那些位置就能满足当前三元组了。因此与在拓扑时要比更先出队并加入答案排列。
因此建立指向、指向的图来跑拓扑,每一个出队的值按照一左一右的顺序填入答案排列。
这就是我的赛事想法,骗到了。
代码:
#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
对于一个三元组{},我们假设、为,为,如果根本不加限制,它的拓扑相对顺序会长成这样:
对于第三种情况,只要像方法一那样建图就能排除;
对于为什么要考虑第二种情况,是因为有些三元组这样排列会使整体更优;
建图还是那么建,只是中间的入度设为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});
}关于数组干什么的,马上告知。
当我们拓扑到一个时,枚举它出现过的所有三元组,数组也就是用来存储这个的。第一维是该三元组的编号,第二维是在该三元组中出现的位置。
对于当前三元组:
一、 为(该三元组至少有一个的位置已确定):
- 该三元组仅有一个已确定:填在的同侧可以产生一个贡献;
- 该三元组仅有当前的未确定:任填,贡献与其无关;
二、为:
- 该三元组无确定元素:任填,为后续答案更新作参考。
- 该三元组仅有另一个已确定:填在与的对侧可以产生一个贡献;
- 该三元组仅有当前的未确定:任填,贡献与其无关。
定义数组,当填在左侧可以产生贡献时,++;当填在右侧可以产生贡献时,++;最后判断与哪个大,就将填在相应那一侧。
由于每次选择的一侧,另一侧的都不会被满足,但选择的都是较多的一侧,因此对于每一个,都有满足数量大于不满足数量,故最后一定有超过个三元组满足。
代码:
#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^)
