跳到正文

算法·理论

寒假训练——贪心构造做题记录

MaxFwl约 38 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 38 分钟
获赞
1

版本

  1. v1当前

前言

这几天在补 whk,做一点轻松的题目

尝试不看题解自己先写

完成进度(10/10)

贪心(Finished)

P5521 [yLOI2019] 梅深不见冬

等价于问一个最优的访问子树的顺序,使得花费最少

先考虑当前节点只有两个孩子的情况

容易发现放满它的孩子的花费变低,在这个节点上放梅花的花费不会增加

令 ansians_i 表示要放满结点 ii 的最小花费,令 ai=ansi−wia_i = ans_i - w_i

我们考虑放满所有孩子的花费:

先放 ii 的花费为:

max⁡(ansi,ansj+wi)=max(ai+wi,aj+wj+wi)=max(ai,aj+wj)+wi\max(ans_i,ans_j+w_i) = max(a_i + w_i, a_j+w_j+w_i) = max(a_i, a_j + w_j) + w_i

先放 jj 的花费为:

max⁡(ansj,ansi+wj)=max(aj+wj,ai+wi+wj)=max(aj,ai+wi)+wj\max(ans_j, ans_i+w_j) = max(a_j + w_j, a_i + w_i + w_j) = max(a_j, a_i + w_i) + w_j

可以发现 ai>aja_i > a_j 时,先放 ii 不劣于先放 jj

进一步发现这个结论在所有情况下的普适性

于是考虑对孩子排序,然后模拟计算

O(n×logn)O(n \times logn),附代码:

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
int n, w[N], ans[N];
vector<int> g[N];

bool cmp(int a, int b){
	return ans[a] - w[a] > ans[b] - w[b];
}

void dfs(int u){
	for (auto to : g[u])
		dfs(to);

	sort(g[u].begin(), g[u].end(), cmp);

	int res = 0;
	for (auto to : g[u]){
		if (res >= ans[to])
			res -= w[to];
		else{
			ans[u] += ans[to] - res;
			res = ans[to] - w[to];
		}
	}

	ans[u] += max(0, w[u] - res);
}

int main(){
	cin >> n;
	for (int i = 2, fa; i <= n; i++){
		cin >> fa;
		g[fa].push_back(i);
	}

	for (int i = 1; i <= n; i++)
		cin >> w[i];

	dfs(1);

	for (int i = 1; i <= n; i++)
		cout << ans[i] << " " ;
	cout << endl;

	return 0;
}

这题是典型的邻项交换法

P1792 [国家集训队] 种树

很巧妙的题目,感觉像后悔贪心,想不出来,看了题解

这题重点在如何“后悔”

我们考虑每次贪心地选择权值 aia_i 最大的土地后,将左边和右边现存的土地合并,三块土地的权值合并为 aprei+anxti−aia_{pre_i}+ a_{nxt_i}-a_i

然后再将左右的土地删除,这里可以使用双向链表

妙处在于,这样做不仅保证了不会有相邻土地被种树的正确性,还保证你能有效地“后悔”——把中间格子种树改为两边格子种树

细想一下,若有格子被多次合并,这个做法也依然正确

剩余部分就与正常的后悔贪心一样,用大根堆维护

附代码:

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10;
int n, m, ans = 0, w[N], vis[N], pre[N], nxt[N];

struct node{
	int val, id;
	bool operator < (const node& b) const{
		return val < b.val;
	}
};

priority_queue<node> pq;

void Update(int x){
	vis[x] = 1;
	nxt[pre[x]] = nxt[x];
	pre[nxt[x]] = pre[x];

	nxt[x] = pre[x] = 0;
}

int main(){
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> w[i];

	if ((n >> 1) < m)
		return puts("Error!"), 0;

	for (int i = 1; i < n; i++)
		nxt[i] = i + 1;
	nxt[n] =  1;

	for (int i = 2; i <= n; i++)
		pre[i] = i - 1;
	pre[1] = n;

	for (int i = 1; i <= n; i++)
		pq.push({w[i], i});

	for (int i = 1; i <= m; i++){
		while (vis[pq.top().id])
			pq.pop();

		int val = pq.top().val;
		int id = pq.top().id;
		pq.pop();

		ans += val;
		int l = pre[id], r = nxt[id];
		w[id] = w[l] + w[r] - val;
		pq.push({w[id], id});
		Update(l);
		Update(r);
	}

	cout << ans << endl;

	return 0;
}

AT_agc018_c Coins

常见的套路是将三元组转化为二元组

定义这样的二元组 (ei,fi)(e_i, f_i),使得 ei=bi−aie_i = b_i - a_i 且 fi=ci−aif_i = c_i - a_i

问题转化为选 yy 个获得 ee 收益,选 zz 个获得 ff 收益,最大化总收益,然后想不下去了

考虑邻项交换法

发现的性质是,当 ei−fi>ej−fje_i-f_i > e_j-f_j 时,ii 选 ee 且jj 选 ff 优于 ii 选 ff 且jj 选 ee

于是排序

对排序后的数组预处理每个前缀前 yy 大的 ee 之和 ,每个后缀前 zz 大的 ff 之和

枚举分割点 ii ,计算答案即可,附代码:

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

const int N = 1e5 + 10;
int n, x, y, z, ans, l[N], r[N];
struct node{
	int e, f;
}p[N];

priority_queue<int, vector<int>, greater<int> > pq;

bool cmp(node x, node y){
	return x.e - x.f > y.e - y.f;
}

signed main(){
	cin >> x >> y >> z;
	n = x + y + z;
	for (int i = 1, a, b, c; i <= n; i++){
		cin >> a >> b >> c;
		ans += a;
		p[i] = {b-a, c-a};
	}

	sort(p + 1, p + n + 1, cmp);

	for (int i = 1; i <= n; i++){
		l[i] = l[i-1] + p[i].e;
		pq.push(p[i].e);

		if (pq.size() > y){
			l[i] -= pq.top();
			pq.pop();
		}
	}

	while (!pq.empty())
		pq.pop();
	for (int i = n; i >= 1; i--){
		r[i] = r[i+1] + p[i].f;
		pq.push(p[i].f);

		if (pq.size() > z){
			r[i] -= pq.top();
			pq.pop();
		}
	}

	int maxn = -9e18;
	for (int i = y; i <= n-z; i++)
		maxn = max(maxn, l[i] + r[i+1]);

	ans += maxn;
	cout << ans << endl;

	return 0;
}

CF521D Shop

发现不需要输出精确计算值,考虑一些误差比较大的乱搞算法

我们发现可以把=强转为+,将+强转为*

对于同一个位置的赋值,只保留最大的一个并强转为加法

对于同一个位置的加法,将加的值从大到小排序,再以依次强转为乘

由于在选择加法操作时,必然先选择加的值大的操作,所以这样的贪心是正确的

最后把所有转化后的乘法操作从大到小排序,选出 mm 个操作

对于一个子序列,最优的操作方式必然是先=,再+,最后*

所以再对这 mm 个操作按这个要求排序即可,附代码:

C++
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;

typedef long long ll;
const int N = 1e5 + 10;
int k, n, m, a[N], op[N], p[N], b[N];

pair<int, int> fz[N];
vector<pair<int, int>> add[N];
vector<pair<long double, int>> mul;

bool cmp(pair<long double, int> a, pair<long double, int> b){
	return op[a.se] < op[b.se];
}

int main(){
    cin >> k >> n >> m;
    for (int i = 1; i <= k; i++)
    	cin >> a[i];
    for (int i = 1; i <= n; i++){
		cin >> op[i] >> p[i] >> b[i];
		switch(op[i]){
			case 1:
				fz[p[i]] = max(fz[p[i]], mp(b[i], i));
				break;
			case 2:
				add[p[i]].push_back(mp(b[i], i));
				break;
			case 3:
				mul.push_back(mp(b[i], i));
		}
	}

    for (int i = 1; i <= k; i++)
    	if (fz[i].fi > a[i])
    		add[i].push_back(mp(fz[i].fi - a[i], fz[i].se));

    for (int i = 1; i <= k; i++){
		sort(add[i].begin(), add[i].end(), greater<pair<int, int> >());
		ll now = a[i];

		for (auto x : add[i]){
			mul.push_back(mp(1.0L * (now+x.fi) / now, x.se));
			now += x.fi;
		}
	}

	sort(mul.begin(), mul.end(), greater<pair<long double, int> >());
	int t = min(m, (int)mul.size());
	sort(mul.begin(), mul.begin() + t, cmp);

	cout << t << endl;
	for (int i = 0; i < t; i++)
		cout << mul[i].se << " " ;
	cout << endl;

    return 0;
}

P8170 [eJOI2021] Waterfront

查了一些资料,深入学习了一下这类题型

比较好想的写法是二分答案并贪心,但细想发现有太多细节要考虑,复杂度也不优

我们进一步思考,发现可以假设所有树都涨完了,然后开始砍

砍树的贪心思路很好想,就是先砍高的树

为了不出现将高度小于 xx 的树砍掉,从而不合法

我们定义函数 Calc(h,d,c)Calc(h, d, c) 为一颗高度为 hh,每天涨 dd ,砍了 cc 次,下次砍的最早时间

这很好求,细致思考即可

此外,为了不让一天砍超过 kk 次,我们计数当前每天砍的次数,若一天已经砍满,那么算出 Calc(h,d,c)Calc(h, d, c) 后,还要跳到下一个还剩余砍树次数的天数

维护序列连通性,容易想到使用并查集

附代码:

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 1e4 + 10;
int n, m, k, x, cnt[N], H[N], a[N];

int Calc(int h, int d, int c){
	if (d == 0)
		return h - c*x < x ? m+1 : 1;
	return max(1, ((c+1)*x-h - 1) / d + 1);
}

struct node{
	int id, val, day, cut;
	node() {}
	node(int id_, int val_, int day_, int cut_){
		id = id_;
		val = val_;
		day = day_;
		cut = cut_;
	}

	bool operator < (const node& b) const{
		return val < b.val;
	}
};

priority_queue<node> pq;

int fa[N];
void Get_Pre(){
	for (int i = 1; i <= m+1; i++){
		fa[i] = i;

		if (i != m+1)
			cnt[i] = k;
	}
}

int find(int x){
	if (fa[x] == x)
		return x;
	return fa[x] = find(fa[x]);
}

int main(){
    cin >> n >> m >> k >> x;
    for (int i = 1; i <= n; i++)
    	cin >> H[i] >> a[i];
    Get_Pre();

    for (int i = 1; i <= n; i++)
    	pq.push(node(i, H[i] + a[i]*m, min(m+1, Calc(H[i], a[i], 0)), 0));

    while (!pq.empty()){
		int id = pq.top().id;
		int val = pq.top().val;
		int day = pq.top().day;
		int cut = pq.top().cut;
		pq.pop();
		day = find(day);

		if (day == m+1){
			cout << val << endl;
			return 0;
		}

		cnt[day]--;
		if (!cnt[day])
			fa[day] = find(day+1);

		val -= x;
		cut++;
		day = min(m+1, Calc(H[id], a[id], cut));

		pq.push(node(id, val, day, cut));
	}

    return 0;
}

P8793 [蓝桥杯 2022 国 A] owo

串内的 owo 数量很好统计,我们考虑如何最大化拼接后产生的 owo 数量

由于题目限制,字符串中只会出现 o 和 w,考虑分类讨论拼接后可以贡献新的 owo 的前后缀

发现当前缀是 o、wo,后缀是o、ow 时都可以拼接

而如果有前缀或后缀是 ww 时,相当于这个字符串前端或后端被封闭了,无法贡献新的 owo

最后考虑一种特殊情况——这个字符串是 w,这种情况下若要贡献新的 owo,必须前后两个串都符合要求,显然不优

我们贪心地先将两个前后缀都能做贡献的字符串拼接,这样不仅能做贡献,拼接后的大字符串依旧是前后缀都能做贡献的,显然是最优的

接着是将一个前后缀都能做贡献的字符串和一个一端被封闭的字符串拼接,在做出贡献之余拼接后的大字符串依旧有前或后缀能做贡献

接着是将两个一端封闭的字符串拼接

然后将 将两个前后缀都能做贡献的字符串拼接 这一步中剩余的 o-o 、wo-ow 串合并

最后是最劣的 w 的合并

O(∑∣si∣)O(\sum |s_i|),附代码:

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 10;
int n, m, ans = 0, cnt[9], t[9];
char s[N];

int Merge(){
    int k = 0, res = 0;
	for (int i = 0; i < 9; i++)
		t[i] = cnt[i];

	if (t[0] || t[4])
		res += t[1] + t[3], t[1] = t[3] = 0;

	if (t[1] && (t[2] || t[7]))
		res += t[1], t[1] = 0;
	if (t[3] && (t[5] || t[6]))
		res += t[3], t[3] = 0;

	if (t[0] && t[5])
		k = min(t[0], t[4]), res += k << 1, t[0] -= k, t[4] -= k;
	k = min(t[0], t[5]), res += k, t[0] -= k, t[5] -= k, t[2] += k;

	if (t[2] && t[4])
		k = min(t[0], t[4]), res += k << 1, t[0] -= k, t[4] -= k;
	k = min(t[2], t[4]), res += k, t[2] -= k, t[4] -= k, t[5] += k;

	k = min(t[0], t[5]), res += k, t[0] -= k, t[5] -= k, t[2] += k;

	if (t[0] && t[7])
		k = min(t[0], t[4]), res += k << 1, t[0] -= k, t[4] -= k;
	k = min(t[0], t[7]), res += k, t[0] -= k, t[7] -= k, t[6] += k;

	if (t[4] && t[6])
		k = min(t[0], t[4]), res += k << 1, t[0] -= k, t[4] -= k;
	k = min(t[4], t[6]), res += k, t[4] -= k, t[6] -= k, t[7] += k;

	k = min(t[0], t[7]), res += k, t[0] -= k, t[7] -= k, t[6] += k;

	k = min(t[2], t[7]), res += k, t[2] -= k, t[7] -= k;
	k = min(t[5], t[6]), res += k, t[5] -= k, t[6] -= k;

	if (t[1])
		res += t[1] - 1, t[1] = 1;
	if (t[3])
		res += t[3] - 1, t[3] = 1;

	k = min(t[0], t[4]);
	if (k && t[0] == t[4])
		res--, t[1]<t[3]?t[1]++:t[3]++;

	t[0] -= k, t[4] -= k, res += k << 1;

	int preffix = t[3] + t[6], suffix = t[1] + t[2];

	k = min({preffix, suffix, t[8]});
	preffix -= k, suffix -= k, t[8] -= k, res += k;

	if (!(k | preffix | suffix))
		t[0] = max(t[0] - 1, 0);

	res += min(t[0], t[8]);

	return res;
}

int main(){
    cin >> n;

    for (int i = 1; i <= n; i++){
    	scanf("%s", s);
    	m = strlen(s);
    	for (int i = 0; i <= m-3; i++)
    		if (s[i] == 'o' && s[i+1] == 'w' && s[i+2] == 'o')
    			ans++;

    	if (m == 1 && *s == 'w')
    		cnt[8]++;
    	else if (*s == 'o' && s[m-1] == 'o')
    		cnt[0]++;
    	else if (*s == 'o' && s[m-2] == 'o' && s[m-1] == 'w')
    		cnt[1]++;
    	else if (*s == 'o' && s[m-2] == 'w' && s[m-1] == 'w')
		    cnt[2]++;
    	else if (*s == 'w' && s[1] == 'o' && s[m-1] == 'o')
    		cnt[3]++;
    	else if (*s == 'w' && s[1] == 'o' && s[m-2] == 'o' && s[m-1] == 'w')
			cnt[4]++;
		else if (*s == 'w' && s[1] == 'o' && s[m-2] == 'w' && s[m-1] == 'w')
			cnt[5]++;
    	else if (*s == 'w' && s[1] == 'w' && s[m-1] == 'o')
    		cnt[6]++;
    	else if (*s == 'w' && s[1] == 'w' && s[m-2] == 'o' && s[m-1] == 'w')
    		cnt[7]++;

		cout << ans + Merge() << endl;
	}

	return 0;
}

构造(Finished)

AT_joisc2017_e 壊れた機器 (Broken Device)

这题感觉思维难度很大啊,想了好久才想通

将题目转化为设计一种构造方法:

构造一个长度为 150150 的01序列

在部分位置(≤40\le 40)被干扰的情况下(对方并不知道哪些位置被干扰)

传递一个 ≤1e18 \le 1e18 的大数

于是思考一种合法的对应,首先将全 00 的一段设为被干扰段

一位一段显然会收到干扰,不可行

两位一段看上去可行,但极端情况下,由于两位在一位被干扰的情况下不能有效传递信息

所以表示的上限最小为 375−40=335<5e16<1e183^{75-40} = 3^{35} < 5e16 < 1e18

考虑三位一段的对应,为了不浪费位置,可以将不被干扰的段储存两位二进制的信息,有一位被干扰的段存一位,其他不存

在这样的情况下,一个 ≤1e18 \le 1e18 的数必能被存下

考虑对应,先考虑有干扰的特殊情况:

我们令 100 、011 表示一位二进制 1 ,不管什么情况,这两种表示法总有一种能被表示

如何保存一位 0 较麻烦,我们考虑先构造其他方案,并且尽量在构造时解决这个问题

对于每个不受干扰的段:

001 -> 00、110 -> 01、101 -> 10、111 -> 11

再回过头来考虑保存一位 0,发现只剩余 010 一种表示法

若中间位置未损坏时,明显可行

若中间位置损坏时,观察我们对于每个不受干扰的段设计的构造,发现表示二位的末位为 0 的二进制的表示法,中间一位都为 0

所以,若中间位置损坏时,假设它不受干扰,直接构造即可

有了构造方式,解密就很简单了,算是一道很好玩的通信题

附代码:

C++
#include "Broken_device_lib.h"
#define ll long long

void Anna(int N, ll X, int K, int P[]){
	int cnt = 0, b[3];
	ll now = X;

	for (int i = 0; i < N/3; i++){
		cnt = b[0] = b[1] = b[2] = 0;
		for (int j = 0; j < K; j++){
			if (P[j] == i*3)
				b[0] = 1, cnt++;
			else if (P[j] == i*3 + 1)
				b[1] = 1, cnt++;
			else if (P[j] == i*3 + 2)
				b[2] = 1, cnt++;
		}

		if (cnt >= 2){
			Set(i*3, 0);
			Set(i*3 + 1, 0);
			Set(i*3 + 2, 0);
			continue;
		}

		if (cnt == 1){
			int t = now % 2;

			if (!t){
				if (b[1] != 1){
					Set(i*3 + 1, 1);
					Set(i*3, 0);
					Set(i*3 + 2, 0);
					now >>= 1;
				}else{
					t = now % 4;
					if (!t){
						Set(i*3 + 2, 1);
						Set(i*3, 0);
						Set(i*3 + 1, 0);
					}else if (t == 2){
						Set(i*3, 1);
						Set(i*3 + 2, 1);
						Set(i*3 + 1, 0);
					}

					now >>= 2;
				}
			}else{
				if (b[0] != 1){
					Set(i*3, 1);
					Set(i*3 + 1, 0);
					Set(i*3 + 2, 0);
				}else{
					Set(i*3 + 1, 1);
					Set(i*3 + 2, 1);
					Set(i*3, 0);
				}

				now >>= 1;
			}
		}

		if (cnt == 0){
			int t = now % 4;
			if (t == 0){
				Set(i*3 + 2, 1);
				Set(i*3, 0);
				Set(i*3 + 1, 0);
			}else if (t == 1){
				Set(i*3, 1);
				Set(i*3 + 1, 1);
				Set(i*3 + 2, 0);
			}else if (t == 2){
				Set(i*3, 1);
				Set(i*3 + 2, 1);
				Set(i*3 + 1, 0);
			}else{
				Set(i*3, 1);
				Set(i*3 + 1, 1);
				Set(i*3 + 2, 1);
			}

			now >>= 2;
		}
	}
}

ll Bruno(int N, int A[]){
	ll res = 0;

	for (int i = N/3 - 1; i >= 0; i--){
		int t = (A[i*3+2] << 2) + (A[i*3+1] << 1) + A[i*3];
		if (t == 0)
			;
		else if (t == 1)
			res <<= 1, res |= 1;
		else if (t == 2)
			res <<= 1;
		else if (t == 3)
			res <<= 2, res |= 1;
		else if (t == 4)
			res <<= 2;
		else if (t == 5)
			res <<= 2, res |= 2;
		else if (t == 6)
			res <<= 1, res |= 1;
		else	res <<= 2, res |= 3;
	}

	return res;
}

AT_agc012_c Tautonym Puzzle

神仙题

其实数据范围有提示,L=2⋅VL = 2 \cdot V,考虑不管怎样都先在序列末尾构造一个 1 2 3 4 5 6 7 ... 98 99 100 的序列

考虑构造前半部分,发现好序列个数就等价于前半部分最长上升子序列个数

将 1−1001-100 从小到大添加到前半部分,发现若将数添加在序列开头,则 LIS 个数增加一

若将数添加前半部分末尾,则 LIS 个数增加一倍再加一

将构造转化为对 N≤1e12N \le 1e12 做二进制拆分

附代码:

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

const int N = 210;
int n, cnt = 0, fr[N], bk[N];

void Solve(int x){
	if (!x)
		return;

	if (!(x & 1))
		fr[++fr[0]] = cnt++, Solve(x - 1);
	else	bk[++bk[0]] = cnt++, Solve(x >> 1);
}

signed main(){
	cin >> n;

	Solve(n);
	cout << cnt + 100 << endl;

	for (int i = 1; i <= fr[0]; i++)
		cout << cnt - fr[i] << " " ;

	for (int i = bk[0]; i >= 1; i--)
		cout << cnt - bk[i] << " " ;

	for (int i = 1; i <= 100; i++)
		cout << i << " " ;

	return 0;
}

P8326 [COCI2021-2022#5] Fliper

神奇题目,首先要了解无向图上欧拉回路的性质

熟练掌握Hierholzer 算法后,再来分析这道题目

将此题转化为图论问题,按照挡板的到达关系建图

考虑只要染两种颜色的特殊情况

由于我们考虑的是环内颜色,其实可以将每个环缩点,将每个挡板设为一条边,连接它正反两个面所属的环,边权为挡板的颜色

我们于是把题目转化为黑白染色问题,进一步分析,可以将无向图转化为有向图,设黑边指向当前点、白边从当前点出发

重新审视题目,发现求的是一种染色方案,使得每个点出入度相等

分析题目,无解的情况必然是有点总度数  mod 8≠0\bmod 8 \ne 0

有解的情况便是欧拉回路 Hierholzer 算法的经典应用

考虑如何处理染四种颜色

我们发现,对于染白的与染黑的边,我们可以再分别跑一遍欧拉回路,考虑位运算处理最终解

附代码:

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 5e5 + 10;
char typ;
int n, m, tp = 0, circle = 0, flg = 0;
int st[N<<1], bel[N<<1], cnt[N<<1], col[N], ans[N];
int vis1[N<<1], vis2[N<<1], vis3[N<<1];

struct node{
	int id, typ, x, y;
}pt[N];

struct e{
	int to, nxt;
};

struct Graph{
	int tot, head[N<<1], deg[N<<1];
	e edge[N<<1];

	Graph() {tot = 0;}

	void add(int a, int b){
		edge[++tot].to = b;
		deg[a]++;
		edge[tot].nxt = head[a];
		head[a] = tot;
	}
}g1, g2;

void Init(int x, int y){
	flg = 0;

	for (int i = 0; i <= x; i++){
		cnt[i] = g2.head[i];
		vis2[i] = 0;
	}

	for (int i = 1; i <= y; i++)
		vis3[i] = 0;
}

bool cmp1(node a, node b){
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}

bool cmp2(node a, node b){
	if (a.y != b.y)
		return a.y < b.y;
	return a.x < b.x;
}

void dfs(int u, int fno){
	if (vis1[u]){
		++circle;
		while (tp)
			bel[st[tp--]] = circle;
		return;
	}

	vis1[u] = 1;
	st[++tp] = u;
	for (int i = g1.head[u]; i; i = g1.edge[i].nxt){
		int to = g1.edge[i].to;
		if (to != fno)
			dfs(to, u);
	}
}

void Hierholzer1(int u){
	vis2[u] = 1;

	for (int i = cnt[u]; i; i = g2.edge[cnt[u]].nxt){
		int id = (i+1) / 2;
		cnt[u] = i;

		if (!vis3[id]){
			vis3[id] = 1;
			Hierholzer1(g2.edge[i].to);
			col[id] = flg;
			flg ^= 1;
		}
	}
}

void Hierholzer2(int u, int x){
	vis2[u] = 1;

	for (int i = cnt[u]; i; i = g2.edge[cnt[u]].nxt){
		int id = (i+1) / 2;
		cnt[u] = i;

		if (col[id] == x && !vis3[id]){
			vis3[id] = 1;
			Hierholzer2(g2.edge[i].to, x);
			ans[id] = ((x << 1) | flg) + 1;
			flg ^= 1;
		}
	}
}

int main(){
	cin >> n;
	m = n << 1;
	for (int i = 1; i <= n; i++){
		cin >> pt[i].x >> pt[i].y >> typ;
		if (typ == '/')
			pt[i].typ = 0;
		else	pt[i].typ = 1;
		pt[i].id = i;
	}

	sort(pt + 1, pt + n + 1, cmp1);
	for (int i = 1; i <= n; ){
		int nxt = i;
		while (nxt < n && pt[i].x == pt[nxt+1].x)
			++nxt;

		for (int j = i; j < nxt; j++){
			g1.add(pt[j].id + n, pt[j+1].id);
			g1.add(pt[j+1].id, pt[j].id + n);
		}

		i = nxt + 1;
	}

	sort(pt + 1, pt + n + 1, cmp2);
	for (int i = 1; i <= n; ){
		int nxt = i;
		while (nxt < n && pt[i].y == pt[nxt+1].y)
			++nxt;

		for (int j = i; j < nxt; j++){
			if (!pt[j].typ){
				if (!pt[j+1].typ){
					g1.add(pt[j].id, pt[j+1].id + n);
					g1.add(pt[j+1].id + n, pt[j].id);
				}else{
					g1.add(pt[j].id, pt[j+1].id);
					g1.add(pt[j+1].id, pt[j].id);
				}
			}else{
				if (!pt[j+1].typ){
					g1.add(pt[j].id + n, pt[j+1].id + n);
					g1.add(pt[j+1].id + n, pt[j].id + n);
				}else{
					g1.add(pt[j].id + n, pt[j+1].id);
					g1.add(pt[j+1].id, pt[j].id + n);
				}
			}
		}

		i = nxt + 1;
	}

	for (int i = 1; i <= m; i++)
		if (!vis1[i]){
			tp = 0;
			dfs(i, 0);
		}

	for (int i = 1; i <= n; i++){
		g2.add(bel[i], bel[i+n]);
		g2.add(bel[i+n], bel[i]);
	}

	for (int i = 1; i <= circle; i++)
		if (g2.deg[i] % 8)
			return puts("-1"), 0;

	while (g2.deg[0] % 8)
		g2.add(0, 0);

	Init(circle, n);
	for (int i = 0; i <= circle; i++)
		if (!vis2[i])
			Hierholzer1(i);

	Init(circle, n);
	for (int i = 0; i <= circle; i++)
		if (!vis2[i])
			Hierholzer2(i, 0);

	Init(circle, n);
	for (int i = 0; i <= circle; i++)
		if (!vis2[i])
			Hierholzer2(i, 1);

	for (int i = 1; i <= n; i++)
		cout << ans[i] << " " ;
	cout << endl;

	return 0;
}

AT_agc001_d Arrays and Palindrome

回文的性质可以使许多位置的字符必定为一类,性质类似并查集

考虑转化为图上模型,并集化为连边,分类讨论,找找规律

以下黑边表示 aa 序列,蓝边代表 bb 序列

  • 开头的回文段

这里是缺省信息

  • 中间的回文段

这里是缺省信息

  • 末尾的回文段

这里是缺省信息

容易找到的规律是,对于合法的连接,中间段长为偶数,这是构造 aa 序列的标准

于是当且仅当 aa 序列中存在 0,1,20,1,2 个奇数,存在合法构造方案

对于构造 bb 序列,中间段对应的 bb 序列与其在 aa 序列中的对应权值相等

开头加一,末尾减一,即可

最后特判 m=1m = 1 的情况即可,附代码:

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 1010;
int n, m, tot = 0, a[N], cnt[2], ans[N];

int main(){
	cin >> n >> m;
	for (int i = 1; i <= m; i++){
		cin >> a[i];
		cnt[a[i]&1]++;
	}

	if (m == 1){
		if (a[1] == 1)
			cout << "1\n1\n1\n" ;
		else	cout << a[1] << endl << 2 << endl << 1 << " " << a[1]-1 << endl;

		return 0;
	}
	if (cnt[1] >= 3)
		return puts("Impossible"), 0;

	sort(a + 1, a + m + 1, [](int x, int y){return x%2 > y%2;});

	cout << a[1] << " " ;
	for (int i = 3; i <= m; i++)
		cout << a[i] << " " ;
	cout << a[2] << endl;

	ans[++tot] = a[1] + 1;
	for (int i = 3; i <= m; i++)
		ans[++tot] = a[i];
	if (a[2] >= 2)
		ans[++tot] = a[2] - 1;

	cout << tot << endl;
	for (int i = 1; i <= tot; i++)
		cout << ans[i] << " " ;
	cout << endl;

	return 0;
}
原文