C. Min Max Sort

很不错的一道题目,不过脑电波和出题人每对上,\(qwq。\)

正难则反。

我们考虑最后一步是怎么操作的。

最后一步一定是对\(1\)和\(n\)进行操作

那么上一步呢?

上一步应该是对\(2\)和\(n-1\)

以此类推

第一步应该是对\(\frac{n}{2}\)和\(\frac{n}{2}+1\)

我们的答案应该是上一步之前的所有操作次数加上最后一步的操作次数。

然后对于\(i \in [1, \frac{n}{2}]\)并不是所有\(i\)都需要进行操作的。

如果本身\(i\)到\(n-i+1\)已经是有序的就不需要进行操作了。

如何判断是不是有序的呢?

这里预处理出来一个\(f_i\)表示,以\(i\)结尾的最长连续上升序列的长度。

如果\(f[n-i+1]<n-i+1-i+1\)说明这个\(i\)到\(n-i+1\)不是有序的则需要进行一次操作


#include <bits/stdc++.h> #define int long long
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define fep(i, a, b) for(int i = (a); i >= (b); --i)
#define _for(i, a, b) for(int i=(a); i<(b); ++i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
#define vi vector<int> using namespace std;
const int maxn = 2e5 + 10;
int n,a[maxn],f[maxn]; void solve() {
cin>>n;
int lst=0;
rep(i,1,n){
f[i]=0;
cin>>a[i];
}
rep(i,1,n) {
f[a[i]]=f[a[i]-1]+1;
}
int ans=0;
fep(i,n/2,1){
if(f[n-i+1]<n-i+1-i+1){
ans++;
}
}
cout<<ans<<endl;
} signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("C:\\Users\\24283\\CLionProjects\\untitled2\\1.in", "r", stdin);
int _;
cin >> _;
while (_--)
solve();
return 0;
}

16 Educational Codeforces Round 142 (Rated for Div. 2)C. Min Max Sort(递归、思维、dp)的更多相关文章

  1. Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)

    Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...

  2. Educational Codeforces Round 142 (Rated for Div. 2) A-D

    比赛链接 A 题解 知识点:贪心. 代码 #include <bits/stdc++.h> using namespace std; using ll = long long; bool ...

  3. Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp

    D. Yet Another Subarray Problem You are given an array \(a_1, a_2, \dots , a_n\) and two integers \( ...

  4. Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again(DP)

    链接: https://codeforces.com/contest/1221/problem/D 题意: You have a fence consisting of n vertical boar ...

  5. Educational Codeforces Round 78 (Rated for Div. 2) B - A and B(思维)

  6. Educational Codeforces Round 72 (Rated for Div. 2)E(线段树,思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;#define BUF_SIZE 100000 ...

  7. Educational Codeforces Round 96 (Rated for Div. 2) E. String Reversal 题解(思维+逆序对)

    题目链接 题目大意 给你一个长度为n的字符串,可以交换相邻两个元素,使得这个字符串翻转,求最少多少种次数改变 题目思路 如果要求数组排序所需要的冒泡次数,那其实就是逆序对 这个也差不多,但是如果是相同 ...

  8. Educational Codeforces Round 88 (Rated for Div. 2) E、Modular Stability 逆元+思维

    题目链接:E.Modular Stability 题意: 给你一个n数,一个k,在1,2,3...n里挑选k个数,使得对于任意非负整数x,对于这k个数的任何排列顺序,然后用x对这个排列一次取模,如果最 ...

  9. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

随机推荐

  1. C语言输出键盘

    使用printf()函数输出样式 #include <stdio.h> int main() { printf("┌───┬───┬───┬───┬───┬───┬───┬─── ...

  2. 【二】gym初次入门一学就会---代码详细解析简明教程----平衡杆案例

    相关文章: [一]gym环境安装以及安装遇到的错误解决 [二]gym初次入门一学就会-简明教程 [三]gym简单画图 [四]gym搭建自己的环境,全网最详细版本,3分钟你就学会了! [五]gym搭建自 ...

  3. C/C++ 通过HTTP实现文件上传下载

    WinInet(Windows Internet)是 Microsoft Windows 操作系统中的一个 API 集,用于提供对 Internet 相关功能的支持.它包括了一系列的函数,使得 Win ...

  4. Swift中UITableViewDiffableDataSource的使用

    在 iOS 13 中 Apple 为 UITableView 和 UICollectionView 引入了 DiffableDataSource, 让开发者可以更简单高效的实现 UITableView ...

  5. 中兴BE7200Pro+的WIFI 7路由器开箱

    上一个讨论的帖子:https://www.chiphell.com/thread-2573626-1-1.html . 对应小米WIFI 7路由器BE6500 Pro开箱的帖子:https://www ...

  6. Hadoop-Operation category READ is not supported in state standby 故障解决

    在查询hdfs时或者执行程序向hdfs写入数据时遇到报错:Operation category READ is not supported in state standby 意思是:该主机状态为待机, ...

  7. NVM Feature— Reservation(NVME 学习笔记五)

    8.8 Reservations 预订 NVMe的reservation预订功能,用于让两个或多个主机能够协调配合的访问共享namespace.使用这些功能的协议和方式超出了本规格说明书的范围.对这些 ...

  8. BasicSample项目说明

    整个示例项目,两个Fragment,ProductListFragment和ProductFragment,一个MainActivity.在MainActivity里面展示的是ProductListF ...

  9. 【分布式】load balance 04-java 从零手写实现负载均衡

    负载均衡系列专题 01-负载均衡基础知识 02-一致性 hash 原理 03-一致性哈希算法 java 实现 04-负载均衡算法 java 实现 本节我们来看一下如何实现一负载均衡框架. 源码 核心接 ...

  10. Springboot集成Swagger实战

    1.介绍 本文将通过实战介绍Springboot如何集成swagger2,以用户管理模块为例,实现项目接口文档的在线管理. 项目源码 本文只列出核心部分,详细请看源码: https://gitee.c ...