跳到正文

个人记录

排列

Satiatish约 10 分钟
原文

这是 2025年10月22日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年10月19日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 10 分钟
获赞
1

版本

  1. v2当前
  2. v1

写作原因

单纯想写。

题意简化

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

STP1

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

但由于bb必在aa、cc之间,而具体相对位置却不定:

  • {a,b,ca,b,c}可以;
  • {c,b,ac,b,a}可以。

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

C++
#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·)

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

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

画出来图长这样:

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

reStructuredtext
5 7 3 4 2 1 9 8 6

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

STP3

对于一个三元组{a,b,ca,b,c},我们假设aa、cc为′e′'e',bb为′w′'w',如果根本不加限制,它的拓扑相对顺序会长成这样:
{eweeewwee\left\{\begin{matrix} ewe \\eew \\wee \end{matrix}\right.
对于第三种情况,只要像方法一那样建图就能排除;
对于为什么要考虑第二种情况,是因为有些三元组这样排列会使整体更优;
建图还是那么建,只是中间′w′'w'的入度设为1就行。
建图代码:

C++
    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});
	}

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

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

  • 该三元组仅有一个′e′'e'已确定:uu填在′e′'e'的同侧可以产生一个贡献;
  • 该三元组仅有当前的uu未确定:uu任填,贡献与其无关;

二、uu为′e′'e':

  • 该三元组无确定元素:uu任填,为后续答案更新作参考。
  • 该三元组仅有另一个′e′'e'已确定:uu填在与′e′'e'的对侧可以产生一个贡献;
  • 该三元组仅有当前的uu未确定:uu任填,贡献与其无关。

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

C++
#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^)