当前位置:网站首页>[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; } 边栏推荐
- nosqlzoo刷题-1
- 2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings
- Compound RateModel合约解析
- I/o multiplexing - select/poll/epoll
- Paging of the flask page
- 远程办公经验 | 社区征文
- 10 advanced concepts that must be understood in learning SQL
- wordcloud的使用
- C language volatile
- @Jsonproperty annotation
猜你喜欢

Summary of classic interview questions

You got 8K in the 3-year function test, but you were actually pretending to work hard
![[STL source code analysis] summary notes (5): a good helper for understanding iterators --list](/img/a7/c54bfb6a03c04e4ffeafdfcf8cedc2.jpg)
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list

Nim product

零基础自学SQL课程 | UNION 联合查询

【CodeForces1019E】Raining season(边分治+斜率优化)

Zero foundation self-study SQL course | outer join external connection

Directrix of ellipse

2022.5.30-6.5 AI行业周刊(第100期):三年时光

Flask页面的分页
随机推荐
Bidirectional linked list simple template (pointer version)
What is the difference between gaussdb for redis and redis?
Implementation of queue (C language)
黑群晖DSM7.0.1物理机安装教程
C language to write a calculator calculation logic
Paging of the flask page
2、 User login and registration
【CodeForces908H】New Year and Boolean Bridges (FWT)
MySQL设置管理员密码无法生效的案例一则
Uoj 551 [unr 4] campus stroll [good polynomial questions (FOG)]
Several transaction modes of Seata
Arduino_ STM development record
QT picture adaptive display control
如果要存 IP 地址,用什么数据类型比较好?99%人都会答错!
C language to achieve minesweeping games
[analysis of STL source code] summary notes (6): Design of iterator and magical traits
【Oracle 数据库】奶妈式教程day03 排序查询
A correction book full of sad tears
After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry
SQLZOO刷题记录-3