当前位置:网站首页>【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; } 边栏推荐
- 1734. arrangement after decoding XOR
- Matplotlib, set coordinate scale size, font / set legend size and font / set vertical and horizontal coordinate name, font and size
- QT 基于QScrollArea的界面嵌套移动
- 1、 Sqlserver2008 installation (with password), database creation, C form project test
- What is the difference between gaussdb for redis and redis?
- P1390 sum of common divisors (Mobius inversion)
- R语言并行计算实战教程
- [STL source code analysis] summary notes (10): hashtable exploration
- MS office level II wrong question record [6]
- Leetcode-9. Palindrome Numbber
猜你喜欢

Sdl-2 thread logic

Decimal to binary

教育专家王中泽老师:家庭教育重在自己成长

RTMP protocol

QT 基于QScrollArea的界面嵌套移动
![pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])](/img/1c/4013479ce1fc5b0ff2ebeb754f05a9.png)
pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])

Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please

Create a form whose client area is 800 pixels by 600 pixels

Menu double linkage effect in uniapp

教育专家王中泽老师一招解决学生问题
随机推荐
Mybags puls will report an error invalid bound statement (not found) when writing an SQL statement in the XML file:
P3172 [cqoi2015] data selection (Mobius inversion + Du Jiao sieve)
Ffmpe a small demo to understand 80% of common APIs
Mistakes in Niuke JS exercise
1269. number of options left in place
R language Parallel Computing practice tutorial
[STL source code analysis] summary notes (12): functors and adapters
商汤科技积极复工,将大力投入数字哨兵的产能和部署
Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
QT interface nested movement based on qscrollarea
Software testing weekly (issue 75): only when you look down, can you see your true self.
C language to write a calculator MVC (very interesting code architecture callback and constructor mode and the use of pointer functions)
Android and IOS reverse analysis / security detection / penetration testing framework
Compound RateModel合約解析
Janus feature draft
Android和iOS逆向分析/安全检测/渗透测试框架
Education expert wangzhongze shared his experience for many years: family education is not a vassal
【CF #277.5 (Div. 2)】B. BerSU Ball
1734. arrangement after decoding XOR
【LeetCode】-- 17. Letter combination of telephone number