首页 > 其他分享 >B. Quick Sort【Codeforces Round #842 (Div. 2)】

B. Quick Sort【Codeforces Round #842 (Div. 2)】

时间:2023-01-06 13:22:31浏览次数:64  
标签:Sort case 842 int Codeforces 取整 choose permutation test

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. 不要盲目模拟过程

标签:Sort,case,842,int,Codeforces,取整,choose,permutation,test
From: https://www.cnblogs.com/J-12045/p/17030152.html

相关文章

  • python-内建函数-排序函数sorted函数
    1.排序函数sorted()函数:对所有可迭代的对象进行排序操作语法格式:sorted(iterable,*,key=None,reverse=False)key:指定带有单个参数的函数,用于从interable的......
  • 关于C语言库函数qsort的学习
    #include<stdio.h>#include<stdlib.h>#include<string.h>structStu{charname[20];intage;};//voidqsort(void*base,//size_tnum,//size_twidth,//int(*cmp......
  • Codeforces Contest 1616
    A.IntegerDiversity直接用个map贪心,如果有相同的就反向即可。B.MirrorintheString这道题洛谷的翻译锅了,所以建议去看原题。考虑这样一个字符串baacc,那么答案显......
  • 「Codeforces」寒假训练 2023 #2
    A.YetAnotherPalindromeProblem原题链接#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e5+10;intt;intn;inta[N];i......
  • 深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)
    导读本文主要介绍如何使用Yolo-V5+DeepSORT实现多目标检测与跟踪。(公众号:OpenCV与AI深度学习)背景介绍   目标跟踪是一种利用检测到对象的空间和时间特征在整个视频......
  • Codeforces Round #839 (Div. 3)题解
    C.DifferentDifferences(贪心)题意​ 给定\(k\),\(n\)\((2\lek\len\le40)\)。从\([1-n]\)中不重复地任选\(k\)个数组成一个数组,使这个数组的差分数组中不同的......
  • Educational Codeforces Round 11
    EducationalCodeforcesRound11https://codeforces.com/contest/660A.Co-primeArray\(1\)与任何数的\(gcd\)都为\(1\),直接在不符合条件的两点间塞就可以了#inc......
  • 1.4 vp Codeforces Round #838 (Div. 2)
    A-DivideandConquer题意:给出序列a,设b为a中元素总和。你可以选择a中任意元素,将它除以二(向下取整)。问最少需要多少次可以使b为偶数思路:将a划分为奇偶两个集合。a中偶数......
  • LOJ #2842. 「JOISC 2018 Day 4」野猪
    题面传送门考试的时候只想到处理\(O(1)\)的边没想到维护\(O(1)\)的路径。首先如果没有可以退一步的限制显然就是相邻两点的最短路之和。退一步的限制想到点边互换。与处......
  • Codeforces Hello 2023 CF 1779 A~F 题解
    点我看题A.HallofFame先把不用操作就合法的情况判掉。然后发现交换LL,RR,RL都是没用的,只有交换LR是有用的,这样可以照亮相邻的两个位置。所以我们就是要找到一个位置i,......