当前位置:网站首页>【AtCoder2306】Rearranging(拓扑)
【AtCoder2306】Rearranging(拓扑)
2022-06-11 07:23:00 【CaptainHarryChen】
题意
黑板上有n个数,A首先按照自己的意愿将n个数重新排列(可以是原来的顺序),然后让B进行如下操作:
选择一对相邻且互质的数,交换它们的位置.(这个操作B可以进行无数次.)
A想要这个序列的字典序尽可能小,而B想要这个序列的字典序尽可能大。
两人都采取最优策略的情况下,最后形成的序列是什么样子的.
题解
发现不互质的数,只要在一开始A放好后,顺序就固定下来,无法改变。
为了使得字典序最小,我们对每个数u,向最小的没被访问过的与它不互质的数v连一条边,表示u保证在v之前,为了使得字典序最小,必须保证每个点的入度为1,如图
如果这样连边,可以构造出{3,2,4,6}
显然没有这样连边更优,保证了2一定在最前面
实现时,预处理处每个数与哪些数不互质,然后dfs,对每个数优先向小的数连边,建成树状的有向图。然后利用优先队列求字典序最大的拓扑序。
代码
#include<cstdio> #include<cstdlib> #include<vector> #include<queue> #include<algorithm> using namespace std; const int MAXN=2005; int gcd(int a,int b) {
if(b==0) return a; return gcd(b,a%b); } int n,A[MAXN]; bool vis[MAXN]; vector<int> adj[MAXN],adj2[MAXN]; int deg[MAXN],ans[MAXN]; priority_queue<int> Q; void dfs(int u) {
vis[u]=true; for(int i=0;i<(int)adj[u].size();i++) {
int v=adj[u][i]; if(vis[v]) continue; adj2[u].push_back(v); deg[v]++; dfs(v); } } 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) adj[i].push_back(j); for(int i=1;i<=n;i++) if(!vis[i]) dfs(i); for(int i=1;i<=n;i++) if(deg[i]==0) Q.push(i); int it=0; while(!Q.empty()) {
int u=Q.top(); ans[++it]=A[u]; Q.pop(); for(int i=0;i<(int)adj2[u].size();i++) {
int v=adj2[u][i]; deg[v]--; if(deg[v]==0) Q.push(v); } } for(int i=1;i<n;i++) printf("%d ",ans[i]); printf("%d\n",ans[n]); return 0; } 边栏推荐
- Use definite integral to calculate triangle area
- QObject usage skills -- control function class
- 【编译原理】05-语法制导的语义计算——基于翻译模式的语义计算
- Concurrent tool class
- Decimal to binary
- Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
- 【AtCoder1984】Wide Swap (拓扑排序转化)
- big.js--使用/实例
- 421. maximum XOR value of two numbers in the array
- [STL source code analysis] summary notes (12): functors and adapters
猜你喜欢

C memory alignment

CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码

【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用

软件测试周刊(第75期):唯有平视,才能看见真实的自己。

黑群晖DSM7.0.1物理机安装教程

Sdl-2 thread logic
![[STL source code analysis] summary notes (12): functors and adapters](/img/6d/a3a9cde2c8792579af7505c2226914.jpg)
[STL source code analysis] summary notes (12): functors and adapters

QObject usage skills -- control function class

2022年熔化焊接与热切割考试练习题及答案

QT picture adaptive display control
随机推荐
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list
二、用户登录和注册
【CF#697 (Div. 3)】 A - Odd Divisor
MS office level II wrong question record [4]
SQLZOO刷题记录-3
Adventure of small X
【Oracle 数据库】奶妈式教程day04 排序查询
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
Sdl-4 PCM playback
Miscellany C language
【CF#654 (Div. 2)】A. Magical Sticks
Concurrent tool class
2022.5.30-6.5 AI行业周刊(第100期):三年时光
Leetcode-141. Linked List Cycle
Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
2022 low voltage electrician operation certificate test question simulation test platform operation
Typora set markdown syntax inline mode
Summary of classic interview questions
[Oracle database] mammy tutorial day03 Sorting Query
Qstring to hexadecimal qstring