当前位置:网站首页>【AtCoder1984】Wide Swap (拓扑排序转化)
【AtCoder1984】Wide Swap (拓扑排序转化)
2022-06-11 07:23:00 【CaptainHarryChen】
题意
给一个(1~n)排列A,你可以做任意次操作,选择两个位置i,j,且 ∣ i − j ∣ ≥ K |i-j|\geq K ∣i−j∣≥K且 ∣ A i − A j ∣ = 1 |A_i-A_j|=1 ∣Ai−Aj∣=1,然后交换 A i A_i Ai, A j A_j Aj。求以此操作得到的最小字典序排列。
题解
用pos[i]表示值i所在位置,题目操作在pos上即为:选择相邻两个数,且这两个数差大于等于K,交换这两个数。
容易发现两个个性质:
- 在pos上,两个数的差小于等于K的数,顺序无法交换,其余的都可以交换。
- 原排列字典序最小,即对应pos上值为1尽量靠前,然后值为2的尽量靠前……(并不是pos字典序最小)
利用这两个性质,把固定的顺序的数连边,利用拓扑排序确定,我们很容易找出字典序最小的拓扑序。
为了减少拓扑序连边数量,pos上的数向“ 比他靠后,最近的一个固定顺序的数 ”连边
代码
#include<cstdio> #include<set> #include<queue> #include<algorithm> using namespace std; const int MAXN=500005; int N,K,A[MAXN]; int pos[MAXN]; vector<int> adj[MAXN]; set<int> L,R; //L存储pos上,比当前数小,绝对值差<K的下标;R存储比当前数大的下标 priority_queue<int,vector<int>,greater<int> > Q; vector<int> stk; int pri[MAXN]; int deg[MAXN]; int main() {
scanf("%d%d",&N,&K); for(int i=1;i<=N;i++) scanf("%d",&A[i]),pos[A[i]]=i; for(int i=1;i<=K;i++) R.insert(A[i]); set<int>::iterator x,y; for(int i=1;i<=N;i++) {
x=L.upper_bound(A[i]); y=R.upper_bound(A[i]); if(x!=L.end()) {
adj[i].push_back(pos[*x]); deg[pos[*x]]++; } if(y!=R.end()) {
adj[i].push_back(pos[*y]); deg[pos[*y]]++; } L.insert(A[i]); if(i-K+1>0) L.erase(A[i-K+1]); if(i+K<=N) R.insert(A[i+K]); R.erase(A[i]); } for(int i=1;i<=N;i++) if(deg[i]==0) Q.push(i); while(!Q.empty()) {
int u=Q.top(); Q.pop(); stk.push_back(u); for(int i=0;i<(int)adj[u].size();i++) {
deg[adj[u][i]]--; if(deg[adj[u][i]]==0) Q.push(adj[u][i]); } } for(int i=0;i<N;i++) pri[stk[i]]=i+1; for(int i=1;i<=N;i++) printf("%d\n",pri[i]); return 0; } 边栏推荐
- 2022年熔化焊接与热切割考试练习题及答案
- Summary of classic interview questions
- 【CF#693 (Div. 3)】B. Fair Division
- 資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員
- Installation de SQL Server 2008 (avec mot de passe), création d'une base de données, test de projet de formulaire C
- [compilation principle] 05- syntax guided semantic computing -- Semantic Computing Based on translation mode
- Gobang interface of mobile console (C language)
- [analysis of STL source code] summary notes (3): vector introduction
- CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
- [STL source code analysis] summary notes (12): functors and adapters
猜你喜欢

Classification of MNIST datasets with keras

2022 melting welding and thermal cutting exam exercises and answers

Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation

二、用户登录和注册

Mobile console Gobang (first draft of detailed design)

Installation de SQL Server 2008 (avec mot de passe), création d'une base de données, test de projet de formulaire C

10 advanced concepts that must be understood in learning SQL

Leetcode-141. Linked List Cycle

Software testing weekly (issue 75): only when you look down, can you see your true self.

No response from win10 explorer when dragging files
随机推荐
Analyse du contrat du modèle de taux composé
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list
1190. invert the substring between each pair of parentheses
【LeetCode】-- 17. Letter combination of telephone number
Pointer to a two-dimensional array
Ffmpeg extraction package format extracts AAC and customizes adtc header to realize arbitrary frame decoding
Sdl-2 thread logic
[STL source code analysis] summary note (2): overview of containers
顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
Calculate the day of the week for a specific month, year and day
[analysis of STL source code] summary notes (3): vector introduction
Atomicinteger atomic operation class
Use definite integral to calculate triangle area
P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
Ffmpe a small demo to understand 80% of common APIs
Menu double linkage effect in uniapp
1442. number of triples forming two exclusive or equal arrays
MS office level II wrong question record [8]
A case in which the MySQL administrator password cannot take effect
No response from win10 explorer when dragging files