\(\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. vue中使用window.resize并去抖动优化

    this.clientWidth = document.documentElement.clientWidth window.onresize = () => { this.clientWidt ...

  2. JAVA之G1垃圾回收器

    概述 G1 GC,全称Garbage-First Garbage Collector,通过-XX:+UseG1GC参数来启用,作为体验版随着JDK 6u14版本面世,在JDK 7u4版本发行时被正式推 ...

  3. 面试官: Flink双流JOIN了解吗? 简单说说其实现原理

    摘要:今天和大家聊聊Flink双流Join问题.这是一个高频面试点,也是工作中常遇到的一种真实场景. 本文分享自华为云社区<万字直通面试:Flink双流JOIN>,作者:大数据兵工厂 . ...

  4. 《剑指offer》面试题33. 二叉搜索树的后序遍历序列

    问题描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果.如果是则返回 true,否则返回 false.假设输入的数组的任意两个数字都互不相同.   参考以下这颗二叉搜索树: 5 / \ ...

  5. C#图片转成流

    Bitmap b = new Bitmap(Server.MapPath(ppath)); Stream ms = new MemoryStream(); b.Save(ms, System.Draw ...

  6. [开发笔记usbTOcan]用树莓派搭建私有Git服务器

    0 | 思路 在开始编程前,先创建一个版本管理库,以前一直用SVN,但目前用Git的还是比较,正好利用这个机会学习GIt. 想过使用Github提供的免费服务器,但项目目前还没有做开源的准备,于是就有 ...

  7. Rust 连接 PostgreSQL 数据库

    这次,我们使用 postgres 这个 crate 来连接和操作 PostgreSQL 数据库. 创建好项目后,在 cargo.toml 里添加 postgres 的依赖: 首先,导入相关的类型,并创 ...

  8. 【自写信息搜集工具】ThunderSearch开发原理解析

    前段时间结合zoomeye的开发文档做了个简易的信息搜集工具ThunderSearch[项目地址 / 博客地址],这次来讲讲具体的实现原理和开发思路 首先要能看懂开发文档,https://www.zo ...

  9. go面试题-基础类

    go基础类 1. go优势 * 天生支持并发,性能高 * 单一的标准代码格式,比其它语言更具可读性 * 自动垃圾收集比java和python更有效,因为它与程序同时执行 go数据类型 int stri ...

  10. StringBuilder类介绍

    1 package cn.itcast.p2.stringbuffer.demo; 2 3 public class StringBuilderDemo { 4 public static void ...