首页 > 其他分享 >[AGC010E] Rearranging

[AGC010E] Rearranging

时间:2024-01-17 14:34:29浏览次数:15  
标签:Aoki integers AGC010E sequence int will Rearranging Takahashi

There are $N$ integers written on a blackboard. The $i$-th integer is $A_i$.

Takahashi and Aoki will arrange these integers in a row, as follows:

  • First, Takahashi will arrange the integers as he wishes.
  • Then, Aoki will repeatedly swap two adjacent integers that are coprime, as many times as he wishes.

We will assume that Takahashi acts optimally so that the eventual sequence will be lexicographically as small as possible, and we will also assume that Aoki acts optimally so that the eventual sequence will be lexicographically as large as possible. Find the eventual sequence that will be produced.

Constraints

  • $1 ≦ N ≦ 2000$
  • $1 ≦ A_i ≦ 10^8$

Input

The input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ … $A_N$

Output

Print the eventual sequence that will be produced, in a line.


Sample Input 1

5
1 2 3 4 5

Sample Output 1

5 3 2 4 1

If Takahashi arranges the given integers in the order $(1,2,3,4,5)$, they will become $(5,3,2,4,1)$ after Aoki optimally manipulates them.


Sample Input 2

4
2 3 4 6

Sample Output 2

2 4 6 3

先把 \(a\) 排序。

给一个序列,如何判断另一个序列是否可以有其转换出来。

对于两个不互质的数 \(u,v\),他们在序列中的相对顺序不会改变。只要满足这个要求,排列就可以操作出来。

那么后手的最优策略就有了。建图然后用堆跑出字典序最大的拓扑序。

而先手就是要给出这个 DAG。建出图后,先手是要给一个无向图定向为 DAG,使得其字典序最大的拓扑序最小。

首先每个连通块是独立的。而且连通块中唯一的无入度的点一定是最小的那个点。现在在考虑一个点的所有出边怎么定向。

从小到大枚举出边,如果终点到达过,可以考虑下一个。否则就从这里深搜。为什么是深搜而不是广搜呢?因为有可能可以从某个点定向定回来这个出边。

#include<bits/stdc++.h>
using namespace std;
const int N=2005;
int n,a[N],p[N][N],in[N],vs[N];
priority_queue<int>q;
int gcd(int x,int y)
{
	if(!y)
		return x;
	return gcd(y,x%y);
}
void dfs(int x)
{
	vs[x]=1;
	for(int v=1;v<=n;v++)
	{
		if(!p[x][v])
			continue;
		if(vs[v])
			p[x][v]=p[v][x]=0;
		else
			p[v][x]=0,dfs(v);
	}
}
void topo()
{
	for(int i=1;i<=n;i++)
		if(!in[i])
			q.push(i);
	while(!q.empty())
	{
		int k=q.top();
		q.pop();
		printf("%d ",a[k]);
		for(int i=1;i<=n;i++)
		{
			if(p[k][i])
			{
				--in[i];
				if(!in[i])
					q.push(i);
			}
		}
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",a+i);
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(i^j&&gcd(a[i],a[j])^1)
				p[i][j]=p[j][i]=1;
	for(int i=1;i<=n;i++)
		if(!vs[i])
			dfs(i);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(p[i][j])
				in[j]++;
	topo();
}

标签:Aoki,integers,AGC010E,sequence,int,will,Rearranging,Takahashi
From: https://www.cnblogs.com/mekoszc/p/17969949

相关文章

  • [ABC317G] Rearranging
    ProblemStatementThereisagridwith$N$rowsand$M$columns.Thesquareatthe$i$-throwfromthetopandthe$j$-thcolumnfromtheleftcontainstheinteger$A_{i,j}$.Here,thesquarescontain$M$occurrencesofeachof$1,\ldots,N$,foratotalo......
  • [ABC317G] Rearranging 题解
    取自我的洛谷博客:https://www.luogu.com.cn/blog/SunnyYuan/solution-at-abc317-g借鉴了官方题解思路。思路首先我们要建立一个二分图。对于输入的\(a_{i,j}\),我们可以连接左侧的\(i\)和右侧的\(a_{i,j}\)。比如样例\(1\):注意:左边的\(1,2,3\)和右边的\(1,2......
  • AtCoder Beginner Contest 247 Ex Rearranging Problem
    洛谷传送门AtCoder传送门考虑我们如何判定一个排列是否能成为最终答案。连边\(i\top_i\),设环数为\(k\),那么最少交换次数为\(n-k\)。那么充要条件是,每个环所有点的\(c_i\)相同,并且\(n-k\leK\)且\(2\mid(K-(n-k))\)。\(K\)和\(n-k\)奇偶性相同是因为,......
  • 1846.maximum element after decreasing and rearranging 减小和重新排列数组后的最大
    问题描述1846.减小和重新排列数组后的最大元素解题思路由于题目允许我们重新排列数组中的元素任意次,因此首先将数组排序,根据arr中第一个元素必须为1,以及相邻两元素的差......
  • 【AGC010E】Rearranging(博弈,图论,拓扑排序)
    考场上想了很久才想到做法,然后考完后又想了很久,加上看了一下一些大佬的博客和Atcoder的官方题解,才完整地证明了整个做法的正确性。综合了一下,在这里详细阐述:首先发现如......