B. Quick Sort

You are given a permutation【排列】† \(p\) of length \(n\) and a positive integer \(k≤n\).

In one operation, you:

Choose \(k\) distinct elements【不连续的元素】 \(p_{i1},p_{i2},…,p_{ik}\).

Remove them and then add them sorted in increasing order to the end of the permutation.

For example, if \(p=[2,5,1,3,4]\) and \(k=2\) and you choose \(5\) and \(3\) as the elements for the operation, then \([2,5,1,3,4]→[2,1,4,3,5]\).

Find the minimum number of operations needed to sort the permutation in increasing order【递增次序】. It can be proven that it is always possible to do so.

† A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (2 appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).

Input

The first line contains a single integer \(t (1≤t≤10^4)\) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers \(n\) and \(k (2≤n≤10^5, 1≤k≤n)\).

The second line of each test case contains \(n\) integers \(p_1,p_2,…,p_n (1≤pi≤n)\). It is guaranteed that \(p\) is a permutation.

It is guaranteed that the sum of n over all test cases does not exceed \(10^5\).

Output

For each test case output a single integer — the minimum number of operations needed to sort the permutation. It can be proven that it is always possible to do so.

Example

input

4

3 2

1 2 3

3 1

3 1 2

4 2

1 3 2 4

4 2

2 3 1 4

output

0

1

1

2

Note

In the first test case, the permutation is already sorted.

In the second test case, you can choose element \(3\), and the permutation will become sorted as follows: \([3,1,2]→[1,2,3]\).

In the third test case, you can choose elements \(3\) and \(4\), and the permutation will become sorted as follows: \([1,3,2,4]→[1,2,3,4]\).

In the fourth test case, it can be shown that it is impossible to sort the permutation in \(1\) operation. However, if you choose elements \(2\) and \(1\) in the first operation, and choose elements \(3\) and \(4\) in the second operation, the permutation will become sorted as follows: \([2,3,1,4]→[3,4,1,2]→[1,2,3,4]\).

原题链接

简述题意

  • 给出一个长度为\(n\)的不连续排列,通过每次移动\(k\)个数并排序放在排列的最后面,确保一定次数内一定可以使得排列正序,问最小操作数为几?

思路

  1. 如果需要最小化操作数,那么不需要移动的元素个数应该最大化,即找到{1,2,...}的maximal subsequence【最大子序列】的元素个数
  2. 我们可以在遍历过程中维护相对顺序来找到最大子序列的元素个数
  3. 结果是遍历一遍记录不满足相对顺序的个数除以每次可移动的个数向上取整
  4. 需要注意是:向上取整要先乘1.0,否则结果会先向下取整再向上取整

代码

点击查看代码
#include<iostream>
#include<cmath> #define endl '\n'
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int k,t,n;
int a[N];
LL ans; int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin >> t; while(t -- ){
ans = 0;
cin >> n >> k;
for(int i = 1; i <= n; i ++)cin >> a[i];
int t = 0,m = 0;
for(int i = 1; i <= n; i ++){
if(a[i] != i - t){
m ++; //不满足相对顺序的数的个数
t ++; //维护相对顺序
}
}
cout << ceil(m * 1.0 / k) << endl; //注意:向上取整要先乘1.0,否则结果会先向下取整再向上取整
}
}

标准答案

点击查看代码
#include <bits/stdc++.h>	

#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define gsize(x) (int)((x).size()) const char nl = '\n'; //简写换行
typedef long long ll;
typedef long double ld; using namespace std; void solve() {
int n, k;
cin >> n >> k;
vector<int> p(n); //动态数组
for (int i = 0; i < n; i++) cin >> p[i]; int c_v = 1;
for (int i = 0; i < n; i++) {
if (p[i] == c_v) c_v++;
} cout << (n - c_v + k) / k << nl;
} int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T;
cin >> T;
while (T--) solve();
}

解题历程

  1. 考虑了相对顺序但是结果计算方式错误
  2. AC(46 ms,3900 KB) 【00:57 - 01:29】 //二次思考

经验总结

  1. 向上取整要先乘1.0,否则结果会先向下取整再向上取整
  2. 注意如何维护相对顺序
  3. 不要盲目模拟过程

B. Quick Sort【Codeforces Round #842 (Div. 2)】的更多相关文章

  1. A. Greatest Convex【Codeforces Round #842 (Div. 2)】

    A. Greatest Convex You are given an integer \(k\). Find the largest integer \(x\), where \(1≤x<k\ ...

  2. 【Codeforces Round #406 (Div. 2)】题解

    The Monster 签到题,算一下b+=a和d+=c,然后卡一下次数就可以了. Not Afraid 只要一组出现一对相反数就是安全的. Berzerk 题意:[1,n],两个人轮流走,谁能走到1 ...

  3. 【Codeforces Round #411 (Div. 1)】Codeforces 804C Ice cream coloring (DFS)

    传送门 分析 这道题做了好长时间,题意就很难理解. 我们注意到这句话Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a ...

  4. 【Codeforces Round#279 Div.2】B. Queue

    这题看别人的.就是那么诚实.http://www.cnblogs.com/zhyfzy/p/4117481.html B. Queue During the lunch break all n Ber ...

  5. 【Codeforces Round #405 ( Div 2)】题解

    Bear and Big Brother 签到题,直接模拟就可以了. Bear and Friendship Condition 满足只能是每个朋友圈中每个人和其他人都是朋友,这样的边数的确定的. 然 ...

  6. 【Codeforces Round #404 (Div. 2)】题解

    A. Anton and Polyhedrons 直接统计+答案就可以了. #include<cstdio> #include<cstring> #include<alg ...

  7. 【Codeforces Round #518 (Div. 2)】

    A:https://www.cnblogs.com/myx12345/p/9847588.html B:https://www.cnblogs.com/myx12345/p/9847590.html ...

  8. 【Codeforces Round #506 (Div. 3) 】

    A:https://www.cnblogs.com/myx12345/p/9844334.html B:https://www.cnblogs.com/myx12345/p/9844368.html ...

  9. 【Codeforces Round #503 (Div. 2)】

    A:https://www.cnblogs.com/myx12345/p/9843198.html B:https://www.cnblogs.com/myx12345/p/9843245.html ...

随机推荐

  1. 驱动开发:内核测试模式过DSE签名

    微软在x64系统中推出了DSE保护机制,DSE全称(Driver Signature Enforcement),该保护机制的核心就是任何驱动程序或者是第三方驱动如果想要在正常模式下被加载则必须要经过微 ...

  2. 最全iOS 上架指南

    一.上架基本需求资料 1.苹果开发者账号(公司已有可以不用申请,需要开通开发者功能,每年 99 美元) 2.开发好的APP 二.证书 上架版本需要使用正式的证书 1.创建证书 Apple Develo ...

  3. 一个实用的 vite + vue3 组件库脚手架工具,提升开发效率

    无论是 vue2 全家桶还是 vue3 + vite + TypeScript,组件库的使用几乎大家都会,但自己开发一个独立组件库就不是每个人都掌握的,因为搭建组件库的基础开发环境,就会让很多同学望而 ...

  4. Vue 实现小小记事本

    1.实现效果 用户输入后按回车,输入的内容自动保存,下方会显示记录的条数,鼠标移动到文字所在div上,会显示删除按钮,点击按钮,相应记录会被删除,下方的记录条数会相应变化,点击clear,所有记录会被 ...

  5. .Net Core 3.0 对 MongoDB 的多条件(两种)查询操作

    前言   在日常开发中,偶尔会用到 MongoDB 的数据操作,也花费了一些时间调试,因此在此处记录一下,共同进步. 废话少说,出招吧! 正文 2.1 准备工作 首先需要引入 .Net 平台链接 Mo ...

  6. CSS动画-transition/animation

    HTML系列: 人人都懂的HTML基础知识-HTML教程(1) HTML元素大全(1) HTML元素大全(2)-表单 CSS系列: CSS基础知识筑基 常用CSS样式属性 CSS选择器大全48式 CS ...

  7. C#之GCHandle

    转载 略谈GCHandle C# - Marshal.StructureToPtr方法简介 Marshal类 两个方法StructureToPtr和PtrToStructure实现序列化 字节 数组 ...

  8. perl文件操作

    Perl 文件操作 Perl 使用一种叫做文件句柄类型的变量来操作文件. 从文件读取或者写入数据需要使用文件句柄. 文件句柄(file handle)是一个I/O连接的名称. Perl提供了三种文件句 ...

  9. Node.js的学习(一)node.js 的介绍

    一.简介 1.1.什么是 node.js ? node.js  一种 JavaScript 的运行环境,能够使得javascript能够脱离浏览器运行.以前 js 只能在浏览器基础上运行,能够操作的也 ...

  10. Java-数组工具类Arrays

    java.util.Arrays是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见的操作. toSting方法 public static String toString(数组):将参 ...