题目链接: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. 记一次使用Asp.Net Core WebApi 5.0+Dapper+Mysql+Redis+Docker的开发过程

    #前言 我可能有三年没怎么碰C#了,目前的工作是在全职搞前端,最近有时间抽空看了一下Asp.net Core,Core版本号都到了5.0了,也越来越好用了,下面将记录一下这几天以来使用Asp.Net ...

  2. 你还不知道mysql中空值和null值的区别吗?

    前言 最近发现带的小伙伴写sql对于空值的判断方法不正确,导致程序里面的数据产生错误,在此进行一下整理,方便大家以后正确的判断空值.以下带来示例给大家进行讲解. 建表 create table tes ...

  3. 【Redis3.0.x】持久化

    Redis3.0.x 持久化 概述 Redis 提供了两种不同的持久化方式: RDB(Redis DataBase)持久化,可以在指定的时间间隔内生成数据集的时间点快照. AOF(Append Onl ...

  4. 拍摄、剪辑vlog的思路

    这篇文章是看了很多狂阿弥_ 的作品后 产生的一些小小总结.这些技法只是锦上添花,阿弥作品真正好的地方在于他细腻的情感,真实的对白,和打动人心的满分作文. 优秀的Vlog ,在于它和观众产生的情感共鸣. ...

  5. ClickHouse安装使用(单机、集群、高可用)

    Clickhouse版本:20.3.6.40-2 安装包地址:https://repo.yandex.ru/clickhouse/rpm/stable/x86_64/ 一.单机版 1.安装依赖 yum ...

  6. xtrabackup 备份与恢复

    书上摘抄 ---深入浅出mysql 448页  grant reload on *.* to 'backup'@'localhost' identified by '123456'; grant re ...

  7. 使用msys2在window下构建和使用Linux的软件

    目录 前言 安装 使用 总结 前言 在window下构建Linux编译环境是很常见的,以前用过mingw弄过差不多的环境. 但是使用msys2后就根本停不下来咯,太好用咯. 安装 去官网下载吧,安装跟 ...

  8. oracle RAC和RACOneNode之间的转换

    Convert RAC TO RACOneNode 1.查看资源状态 [grid@rac01 ~]$ crsctl status res -t 从这里看到,数据库的名字叫racdb 2.查看实例 [o ...

  9. 自翻------Office 2013 RT 使用说明

    Office Home and Student 2013 RT Preview的更新 介绍 Microsoft已发布Microsoft Office Home and Student 2013 RT ...

  10. 【Android初级】使用Gallery实现照片拖动的特效(附源码)

    今天要分享一个非常简单的功能: 使用Android原生控件Gallery实现照片拖动的特效 实现思路如下: 在布局文件中定义一个Gallery控件 由于要显示多张图,为了方便,我直接引用了Androi ...