[AT1982] [AGC001 D] Arrays and Palindrome

版权声明:欢迎转载蒟蒻博客,但请注明出处: https://blog.csdn.net/LPA20020220/article/details/88072531

洛谷传送门

Atcoder传送门

题目大意

给你一个所有元素为正整数且和为 N N ,长度为 M M 的序列 A A , 要你求出 A A 的一个排列 A A' 和一个所有元素为正整数且和为 N N 的序列 B B , 满足符合前 A 1 A'_1 个字符, 之后的 A 2 A'_2 个字符…组成的都是回文串,前 B 1 B_1 个字符, 之后的 B 2 B_2 个字符…组成的都是回文串, 且这样的字符串只可能由一种字符构成。

输入输出格式

输入格式

第一行两个正整数 N , M N,M

第二行 M M 个元素分别代表 A 1 , A 2 , . . . , A M A_1,A_2,...,A_M

输出格式

第一行 M M 个正整数分别表示 A 1 , A 2 , . . . , A M A'_1,A'_2,...,A'_M

第二行一个正整数 K K 表示序列 B B 的长度。

第三行 K K 个正整数分别表示 B 1 , B 2 , . . . , B K B_1,B_2,...,B_K

输入输出样例

输入样例#1:

3 2
2 1

输出样例#1:

1 2
1
3

输入样例#2:

6 1
6

输出样例#2:

6
3
1 2 3

输入样例#3:

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

输出样例#3:

Impossible

解题分析

很容易得到一个图论模型: 把对称的点连起来, 如果最终连通块只有一个那么就是正确的。

这样做以后发现最多只有两个长度为奇数的回文串限制(否则最终连的边数不够 N 1 N-1 条)。

那么就好办了, 直接搞错开一格就可以连上不同连通块了。

注意特判一堆情况。

代码如下:

扫描二维码关注公众号,回复: 5380606 查看本文章
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstdlib>
#include <vector>
#define R register
#define IN inline
#define gc getchar()
#define ll long long
#define MX 105
template <class T>
IN void in(T &x)
{
	x = 0; R char c = gc;
	for (; !isdigit(c); c = gc);
	for (;  isdigit(c); c = gc)
	x = (x << 1) + (x << 3) + c - 48;
}
int n, sum;
std::vector <int> eve, odd;
int main(void)
{
	in(sum), in(n);
	int foo;
	for (R int i = 1; i <= n; ++i)
	{
		in(foo);
		if (foo & 1) odd.push_back(foo);
		else eve.push_back(foo);
	}
	if (n == 1)
	{
		if (sum == 1) printf("1\n1\n1");
		else printf("%d\n2\n%d %d", sum, 1, sum - 1);
		return 0;
	}
	if (odd.size() > 2) return puts("Impossible"), 0;
	else if (odd.size() == 2)
	{
		if (odd[0] > odd[1]) std::swap(odd[0], odd[1]);
		printf("%d ", odd[0]);
		for (auto i : eve) printf("%d ", i);
		printf("%d\n", odd[1]);
		odd[0]--, odd[1]++;
		if (!odd[0]) printf("%d\n", n - 1);
		else printf("%d\n%d ", n, odd[0]);
		for (auto i : eve) printf("%d ", i);
		printf("%d ", odd[1]);
	}
	else if (odd.size() == 1)
	{
		printf("%d ", odd[0]);
		for (auto i : eve) printf("%d ", i);
		puts("");
		odd[0]++, eve[eve.size() - 1]--;
		printf("%d\n", n);
		printf("%d ", odd[0]);
		for (auto i : eve) printf("%d ", i);
	}
	else
	{
		for (auto i : eve) printf("%d ", i);
		eve[0]++, eve[eve.size() - 1]--;
		printf("\n%d\n", n);
		for (auto i : eve) printf("%d ", i);
	}
}

猜你喜欢

转载自blog.csdn.net/LPA20020220/article/details/88072531