当前位置:网站首页>[atcoder2306] rearranging (topology)
[atcoder2306] rearranging (topology)
2022-06-11 07:37:00 【CaptainHarryChen】
The question
It's on the blackboard n Number ,A First, according to their own wishes n Rearrange the numbers ( It can be the original order ), And then let B Do the following :
Select a pair of adjacent and coprime numbers , Exchange their positions .( This operation B It can be done countless times .)
A Want the dictionary order of this sequence to be as small as possible , and B Want the dictionary order of this sequence to be as large as possible .
When both of them adopt the optimal strategy , What does the resulting sequence look like .
Answer key
Discover non coprime numbers , Just at the beginning A When it's ready , The order is fixed , Can't change .
To minimize the lexicographic order , We count each number u, To the smallest number that has not been accessed and is not coprime with it v One side , Express u Guaranteed at v Before , To minimize the lexicographic order , It must be ensured that the penetration of each point is 1, Pictured
If you connect the edges like this , You can construct {3,2,4,6}
Obviously, it is better to connect the edges , To ensure the 2 It must be at the front 
The implementation , Which numbers are not coprime with each number in the preprocessing , then dfs, For each number, edge to the smaller number first , Build a tree oriented graph . Then we use the priority queue to find the topological order with the largest dictionary order .
Code
#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; } 边栏推荐
- JVM tuning
- MFC auxiliary CString string splicing
- [IOT] project management: how to build a better cross functional team?
- MySQL设置管理员密码无法生效的案例一则
- Arduino_ STM development record
- Building a full-featured NAS server with raspberry pie (05): playing with video and audio & sorting out movies
- 【Oracle 数据库】奶妈式教程day03 排序查询
- C language function stack frame
- [compilation principle] 05- syntax guided semantic computing -- Semantic Computing Based on translation mode
- Cartland number application
猜你喜欢
![[Oracle database] mammy tutorial day04 Sorting Query](/img/79/9db26aa2d9dbb5514427edf03004f4.png)
[Oracle database] mammy tutorial day04 Sorting Query

学 SQL 必须了解的10个高级概念

【AtCoder2306】Rearranging(拓扑)

【Oracle 数据库】奶妈式教程day04 排序查询

群晖DS918创建m.2 固态硬盘SSD读写缓存
![[analysis of STL source code] summary notes (3): vector introduction](/img/70/faa40c273f6b3a6b124fb870058489.jpg)
[analysis of STL source code] summary notes (3): vector introduction

Building a full-featured NAS server with raspberry pie (06): built-in file synchronization tool for penetration
![Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]](/img/f0/9d4609a53f398636b8062c625f7d3c.jpg)
Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
![2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]](/img/4d/a67a63d2c4eb80c98315c3057b01b9.jpg)
2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]

JVM learning record (VII) -- class loading process and parental delegation model
随机推荐
3年功能测试拿8K,被新来的反超,其实你在假装努力
【Oracle 数据库】奶妈式教程day04 排序查询
May 30-June 5, 2022 AI industry weekly (issue 100): three years
远程办公经验 | 社区征文
2022低压电工操作证考试题模拟考试平台操作
Tetris preliminary
【AtCoder2305】Decrementing(博弈)
2022.5.30-6.5 AI行业周刊(第100期):三年时光
20200727 T2 small w playing game [generating function (binomial inversion technique)]
[IOT] project management: how to build a better cross functional team?
Sqlzoo question brushing record-3
MFC custom string linked list
QT interface nested movement based on qscrollarea
Rabin-Miller素数测试
. Net C Foundation (6): namespace - scope with name
[compilation principle] 05- syntax guided semantic computing -- Semantic Computing Based on translation mode
Ffmpe a small demo to understand 80% of common APIs
20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
如果要存 IP 地址,用什么数据类型比较好?99%人都会答错!
Summary of written test questions of shopee 2021 autumn recruitment