排列
排列
写作原因
单纯想写。
题意简化
有一个
现在排列未知,请求出一种排列使之至少满足
STP1
最初想法:这些三元组有位置相对关系,所以我们可以把相对位置关系转化为有向图(
但由于
- {
a,b,c }可以; - {
c,b,a }可以。
所以优先确定一个作为最左侧的贪心是不成立的。(·_·)
于是又有了第二个想法:从两端向中间求出排列。
比如先将
因此建立
这就是我的赛事想法,骗到了
代码:
#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
对于一个三元组{
对于第三种情况,只要像方法一那样建图就能排除;
对于为什么要考虑第二种情况,是因为有些三元组这样排列会使整体更优;
建图还是那么建,只是中间
建图代码:
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});
}
关于
当我们拓扑到一个
对于当前三元组:
一、
- 该三元组仅有一个
'e' 已确定:u 填在'e' 的同侧可以产生一个贡献; - 该三元组仅有当前的
u 未确定:u 任填,贡献与其无关;
二、
- 该三元组无确定元素:
u 任填,为后续答案更新作参考。 - 该三元组仅有另一个
'e' 已确定:u 填在与'e' 的对侧可以产生一个贡献; - 该三元组仅有当前的
u 未确定:u 任填,贡献与其无关。
定义数组
由于每次选择的一侧,另一侧的都不会被满足,但选择的都是较多的一侧,因此对于每一个
代码:
#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^)