题目链接:https://codeforces.com/contest/1427/problem/D

题意

给出一个大小为 \(n\) 的排列,每次操作可以将 \(n\) 个数分为 \(1 \sim n\) 个非空连续份,然后将对称的份两两交换,试给出在 \(n\) 次操作内将排列排为升序的操作过程。

题解

  1. 找到值相差为 \(1\) 的逆序对:\(i<j\),\(a_i = a_j + 1\)
  2. 将已为升序的数视为一个整体,找到 \(t\) 满足 \(i \le t < j\),\(a_t > a_{t+1}\)
  3. 分为 \(4\) 份,\(D_1=[a_1,a_2,\dots,a_{i-1}],\ D_2=[a_i,a_{i+1},\dots, a_t],\ D_3=[a_{t+1},a_{t+2},\dots, a_j],\ D_4=[a_{j+1},a_{j+2},\dots, a_n]\)
  4. 将对称组交换,转至步骤 \(1\) 。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), pos(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
--a[i];
}
vector<vector<int>> ans;
while (not is_sorted(a.begin(), a.end())) {
for (int i = 0; i < n; i++) {
pos[a[i]] = i;
}
//1
for (int i = 1; i < n; i++) {
if (pos[i] < pos[i - 1]) {
//2
int l = pos[i];
int r = pos[i - 1];
int mid = l;
while (a[mid + 1] == a[mid] + 1) ++mid;
//3
ans.push_back({l, mid - l + 1, r - mid, n - r - 1});
//4
vector<int> b;
for (int i = r + 1; i < n; i++) b.push_back(a[i]);
for (int i = mid + 1; i < r + 1; i++) b.push_back(a[i]);
for (int i = l; i < mid + 1; i++) b.push_back(a[i]);
for (int i = 0; i < l; i++) b.push_back(a[i]);
a.swap(b);
break;
}
}
}
cout << ans.size() << "\n";
for (auto &v : ans) {
//每份非空
while (v.back() == 0) v.pop_back();
while (v.front() == 0) v.erase(v.begin());
cout << v.size() << "\n";
for (int i = 0; i < int(v.size()); i++) {
cout << v[i] << " \n"[i == int(v.size()) - 1];
}
}
return 0;
}

Codeforces Global Round 11 D. Unshuffling a Deck(构造/相邻逆序对)的更多相关文章

  1. Codeforces Global Round 11 个人题解(B题)

    Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...

  2. Codeforces Global Round 11 A~D题解

    A.Avoiding Zero 题目链接:https://codeforces.ml/contest/1427 题目大意:给定一个数组a1,a2...,an,要求找出一个a重排后的数组b1,b2,.. ...

  3. Codeforces Global Round 11【ABCD】

    比赛链接:https://codeforces.com/contest/1427 A. Avoiding Zero 题意 将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和. 题解 计算正 ...

  4. Codeforces Global Round 11 C. The Hard Work of Paparazzi(dp/最长上升子序列)

    题目链接:https://codeforces.com/contest/1427/problem/C 题意 \(r\) 行与 \(r\) 列相交形成了 \(r \times r\) 个点,初始时刻记者 ...

  5. Codeforces Global Round 11 B. Chess Cheater(贪心)

    题目链接:https://codeforces.com/contest/1427/problem/B 题意 给出一个长为 \(n\) 由 W, L 组成的字符串,如果一个 W 左侧为 W,则它提供 2 ...

  6. Codeforces Global Round 11 A. Avoiding Zero(前缀和)

    题目链接:https://codeforces.com/contest/1427/problem/A 题意 将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和. 题解 计算正.负前缀和,如 ...

  7. Codeforces Global Round 11 C. The Hard Work of Paparazzi (DP)

    题意:有\(r\)X\(r\)的网格图,有\(n\)位名人,会在\(t_i\)时出现在\((x_i,y_i)\),如果过了\(t_i\)名人就会消失,从某一点走到另外一点需要花费的时间是它们之间的曼哈 ...

  8. Codeforces Global Round 11 B. Chess Cheater (贪心,结构体排序)

    题意:你和朋友进行了\(n\)个回合的棋艺切磋,没有平局,每次要么输要么赢,每次赢可以得一分,假如前一局也赢了,那么可以得两分,结果已成定局,但是你确可以作弊,最多修改\(k\)个回合的结果,问你作弊 ...

  9. Codeforces Global Round 8 C. Even Picture(构造)

    题目链接:https://codeforces.com/contest/1368/problem/C 题意 构造一个只含有灰.白块的网格,要求: 所有灰块为一个连通块 每个灰块与偶数个灰块相邻 恰有 ...

随机推荐

  1. 【JDBC核心】获取数据库连接

    获取数据库连接 要素一:Driver 接口实现类 Driver 接口: java.sql.Driver 接口是所有 JDBC 驱动程序需要实现的接口.这个接口是提供给数据库厂商使用的,不同数据库厂商提 ...

  2. 实现一个简单的 Linux Shell(C++)

    Implement a simple command interpreter in Linux. The interpreter should: support both internal and e ...

  3. python_字典(dict)

    dict 一.结构: info = { "key":"value", "key":"value" } print(inf ...

  4. pyi文件是干嘛的?(一文读懂Python的存根文件和类型检查)

    参考资料: https://blog.csdn.net/weixin_40908748/article/details/106252884 https://www.python.org/dev/pep ...

  5. 修改hosts文件后不生效,该怎么办

    对于web开发来说,经常需要修改hosts文件,用来将域名与ip对应匹配.但是有时候发现hosts文件明明已经改了,但就是不生效,页面还会跳到某个丧心病狂的私人小站.hosts文件不生效有很多种原因, ...

  6. MySQL 使用sql添加和创建用户

    用户管理 SQL 命令操作 用户表:mysql.user 本质:对mysql.user 表进行增删改查 -- ============== 用户管理 ============= -- 创建用户 -- ...

  7. 【Linux】rsync模板配置问题

    ------------------------------------------------------------------------------------------------- | ...

  8. 【Docker】runtime create failed: container_linux.go:345: 解决

    ------------------------------------------------------------------------------------------------- | ...

  9. CTFHub - Web(六)

    命令注入: 1.进入页面,测试127.0.0.1, 关键代码: <?php $res = FALSE; if (isset($_GET['ip']) && $_GET['ip'] ...

  10. linux系统中set、env、export关系

    set 用来显示shell变量(包括环境变量.用户变量和函数名及其定义),同时可以设置shell选项来开启调试.变量扩展.路径扩展等开关env 用来显示和设置环境变量export 用来显示和设置导出到 ...