\(\mathcal{Description}\)

  Link.

  给定 \(0\sim n-1\) 的排列 \(p_{0..n-1}\),每次操作给出 \(i\),交换 \(p_i\) 和 \(p_{(i+p_i)\bmod n}\)。构造一种使排列升序的操作序列。

  \(n\le100\)。

\(\mathcal{Solution}\)

  反正兔子就一个样例观察法,一个暴力伪解拍上去就 AC 了。(

  先讲讲我的伪解,观察样例解释:

  • First, announce \(i=6\). We swap \(P_6(=5)\) and \(P_{(6+5)\bmod8}=P_3(=6)\), turning \(P\) into \(7,1,2,5,4,0,6,3\).

  • Then, announce \(i=0\). We swap \(P_0(=7)\) and \(P_{(0+7)\bmod8}=P_7(=3)\), turning \(P\) into \(3,1,2,5,4,0,6,7\).

  • Then, announce \(i=3\). We swap \(P_3(=5)\) and \(P_{(3+5)\bmod8}=P_0(=3)\), turning \(P\) into \(5,1,2,3,4,0,6,7\).

  • Finally, announce \(i=0\). We swap \(P_0(=5)\) and \(P_{(0+5)\bmod8}=P_5(=0)\), turning \(P\) into \(0,1,2,3,4,5,6,7\).

  

  发现三次 \(p_i=5\),一次 \(p_i=7\)。考虑到样例的迷惑性,我们尝试让对 \(p_i=5\) 的操作挨在一起。交换第一步和第二步,发现操作序列仍合法。

  接下来,我们强行解释该操作序列的内在逻辑:

  • 希望 \(p_7=7\),反复操作 \(p_i=7\) 直到 \(p_7=7\);
  • 希望 \(p_7=7\land p_6=6\),反复操作 \(p_i=6\)(样例中不需要操作),直到不满足 \(p_7=7\) 回到第一步,或满足 \(p_6=6\);
  • 希望 \(p_5=5\land p_6=6\land p_7=7\),操作同上。
  • ……

  综上,交换策略为

选择不满足 \(p_i=i\) 的最大的 \(p_i\) 进行交换直到序列升序。

  然后就 AC 了,复杂度不知道。(


  正解是先操作使得 \(p=\{n-1,n-2,\cdots,0\}\) 然后逆序。操作方法考察从后往前的每个 \(i\),不断操作 \(i\) 直至 \(p_i=n-i-1\),可证至多操作 \(\mathcal O(n)\) 次,总复杂度 \(\mathcal O(n^2)\)。

\(\mathcal{Code}\)

  伪解:

/* Clearink */

#include <cstdio>
#include <vector> #define rep( i, l, r ) for ( int i = l, rpbound##i = r; i <= rpbound##i; ++i )
#define per( i, r, l ) for ( int i = r, rpbound##i = l; i >= rpbound##i; --i ) const int MAXN = 100;
int n, p[MAXN + 5];
std::vector<int> ans; inline void iswp ( int& a, int& b ) { a ^= b ^= a ^= b; } int main () {
scanf ( "%d", &n );
rep ( i, 0, n - 1 ) scanf ( "%d", &p[i] );
while ( true ) {
int irr = -1;
rep ( i, 0, n - 1 ) if ( i ^ p[i] && ( !~irr || p[i] > p[irr] ) ) irr = i;
if ( !~irr ) break;
ans.push_back ( irr );
iswp ( p[irr], p[( irr + p[irr] ) % n] );
}
printf ( "%d\n", ( int ) ans.size () );
for ( int i: ans ) printf ( "%d\n", i );
return 0;
}

  正解:

/* Clearink */

#include <cstdio>
#include <vector> #define rep( i, l, r ) for ( int i = l, rpbound##i = r; i <= rpbound##i; ++i )
#define per( i, r, l ) for ( int i = r, rpbound##i = l; i >= rpbound##i; --i ) const int MAXN = 100;
int n, p[MAXN + 5];
std::vector<int> ans; inline void iswp ( int& a, int& b ) { a ^= b ^= a ^= b; }
inline void oper ( const int i ) {
ans.push_back ( i ), iswp ( p[i], p[( i + p[i] ) % n] );
} int main () {
scanf ( "%d", &n );
rep ( i, 0, n - 1 ) scanf ( "%d", &p[i] );
per ( i, n - 1, 1 ) for ( ; p[i] != n - i - 1; oper ( i ) );
per ( i, n - 2, 0 ) {
rep ( j, i + 1, n - 2 ) oper ( j );
oper ( i ), oper ( i );
}
printf ( "%d\n", ( int ) ans.size () );
for ( int i: ans ) printf ( "%d\n", i );
return 0;
}

Solution -「ARC 110F」Esoswap的更多相关文章

  1. Solution -「ARC 104E」Random LIS

    \(\mathcal{Description}\)   Link.   给定整数序列 \(\{a_n\}\),对于整数序列 \(\{b_n\}\),\(b_i\) 在 \([1,a_i]\) 中等概率 ...

  2. Solution -「ARC 101D」「AT4353」Robots and Exits

    \(\mathcal{Description}\)   Link.   有 \(n\) 个小球,坐标为 \(x_{1..n}\):还有 \(m\) 个洞,坐标为 \(y_{1..m}\),保证上述坐标 ...

  3. Solution -「ARC 110D」Binomial Coefficient is Fun

    \(\mathcal{Description}\)   Link.   给定非负整数序列 \(\{a_n\}\),设 \(\{b_n\}\) 是一个非负整数序列且 \(\sum_{i=1}^nb_i\ ...

  4. Solution -「ARC 124E」Pass to Next

    \(\mathcal{Description}\)   Link.   有 \(n\) 个人站成一个环,初始时第 \(i\) 个人手里有 \(a_i\) 个球.第 \(i\) 个人可以将自己手中任意数 ...

  5. Solution -「ARC 126E」Infinite Operations

    \(\mathcal{Description}\)   Link.   给定序列 \(\{a_n\}\),定义一次操作为: 选择 \(a_i<a_j\),以及一个 \(x\in\mathbb R ...

  6. Solution -「ARC 126F」Affine Sort

    \(\mathcal{Description}\)   Link.   给定 \(\{x_n\}\),令 \[f(k)=\left|\{(a,b,c)\mid a,b\in[0,c),c\in[1,k ...

  7. Solution -「ARC 125F」Tree Degree Subset Sum

    \(\mathcal{Description}\)   Link.   给定含有 \(n\) 个结点的树,求非负整数对 \((x,y)\) 的数量,满足存在 \(\exist S\subseteq V ...

  8. Solution -「ARC 125E」Snack

    \(\mathcal{Description}\)   Link.   把 \(n\) 种零食分给 \(m\) 个人,第 \(i\) 种零食有 \(a_i\) 个:第 \(i\) 个人得到同种零食数量 ...

  9. Solution -「ARC 058C」「AT 1975」Iroha and Haiku

    \(\mathcal{Description}\)   Link.   称一个正整数序列为"俳(pái)句",当且仅当序列中存在连续一段和为 \(x\),紧接着连续一段和为 \(y ...

随机推荐

  1. Ant 调用 Shell/CMD 命令

    Ant中调用Makefile,使用shell中的make命令 <?xml version="1.0" encoding="utf-8" ?> < ...

  2. 使用Crossplane构建专属PaaS体验:Kubernetes,OAM和CoreWorkflows

    关键点:•许多组织的目标是构建自己的云平台,这些平台通常由内部部署架构和云供应商共建完成.•虽然Kubernetes没有提供开箱即用的完整PaaS平台式服务,但其具备完整的API,清晰的技术抽取和易理 ...

  3. 【记录一个问题】铁威马NAS,升级系统后,所有安装的配置项都丢失了

    因为铁威马的系统功能真的是弱,所以写了一些家庭照片处理的系统. 上上周升级了系统后,丢失了以下内容: anaconda 2.及其conda下python3环境的各种库 3.nginx的转发配置 铁威马 ...

  4. Cesium中级教程4 - 空间数据可视化(二)

    Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ Viewer中的Entity功能 让我们看看Viewer为操作e ...

  5. vue学习13-自定义组件

    1 <!DOCTYPE html> 2 <html lang='en'> 3 <head> 4 <meta charset='UTF-8'> 5 < ...

  6. 多线程-创建线程第二种方式-实现Runnable接口-细节和好处

    1 package multithread2; 2 3 /* 4 * 创建线程的第一种方法:继承Thread类 5 * 6 * 创建线程的第二种方式:实现Runnable接口 7 * 8 * 1,定义 ...

  7. Java中四种访问权限总结

    一.Java中有四种访问权限, 其中三种有访问权限修饰符,分别为private.public.protected,还有一种不带任何修饰符(default). 1. private: Java语言中对访 ...

  8. 新一代Python包管理工具来了

    1 简介 说起Python的包管理工具,大家第一时间想到的肯定是pip.conda等经典工具.但最近我发现了一款新颖的Python包管理工具--pdm,它受到PEP582(https://www.py ...

  9. 不使用pvc的方式在K8S中部署apisix-gateway

    不使用pvc的方式在K8S中部署apisix-gateway 简介 我的apisix使用etcd作为数据存储服务器,官方的使用pvc方式或者docker-compose的方式,对于新手不太友好,本篇是 ...

  10. Android状态栏微技巧,带你真正理解沉浸式模式【转】

    感谢! 本文转自大佬郭霖:http://blog.csdn.net/guolin_blog/article/details/51763825 转载请注明出处:http://blog.csdn.net/ ...