首页 > 其他分享 >cf

cf

时间:2024-02-29 15:56:11浏览次数:16  
标签:exchange cf number permutation test special he

C. Omkar and Baseball

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across \(n\) sessions follow the identity permutation (ie. in the first game he scores \(1\) point, in the second game he scores \(2\) points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up!

Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on \([1,2,3]\) can yield \([3,1,2]\) but it cannot yield \([3,2,1]\) since the \(2\) is in the same position.

Given a permutation of \(n\) integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed \(10^{18}\).

An array \(a\) is a subarray of an array \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input

Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.

The first line of each test case contains integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\))  — the length of the given permutation.

The second line of each test case contains \(n\) integers \(a_{1},a_{2},...,a_{n}\) (\(1 \leq a_{i} \leq n\))  — the initial permutation.

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

For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation.
2
5
1 2 3 4 5
7
3 2 4 5 1 6 7

0
2

Note

In the first permutation, it is already sorted so no exchanges are needed.

It can be shown that you need at least \(2\) exchanges to sort the second permutation.

\([3, 2, 4, 5, 1, 6, 7]\)

Perform special exchange on range (\(1, 5\))

\([4, 1, 2, 3, 5, 6, 7]\)

Perform special exchange on range (\(1, 4\))

\([1, 2, 3, 4, 5, 6, 7]\)

标签:exchange,cf,number,permutation,test,special,he
From: https://www.cnblogs.com/Elgina/p/18044464

相关文章

  • CF510C Fox And Names 题解
    CF510CFoxAndNames题解https://www.luogu.com.cn/problem/CF510C思路题意就是:确定一个小写字母的比较规则,使得给定的所有字符串在一开始就是按你确定的比较规则排序了的。可以发现:对于前后一对字符串,找到第一对不同的字符,是要这两个字符有合法的大小关系,就能满足题意。......
  • [思维] [树形数据结构] CF1379F1 Chess Strikes Back (easy version)
    注意到棋盘大小为$2n,2m$,共$2nm$个白格,同时国王数量为$nm$,尝试将$2$个国王捆绑在一块,即将棋盘均匀划分为若干个$2*2$大小的大格子。在此基础上观察,显然同一个大格子内的两个白格不能同时放置国王,同时大格子数量为$nm$,因此问题转化为判定能否使得所有大格子都有一个国王,......
  • CF514D R2D2 and Droid Army(二分,ST表)
    传送门解题思路直接二分能干掉的人数,然后check函数枚举所有区间,因为m很小,所以可以用m个ST表预处理每个区间对应每个属性的最大值。一是需要注意二分的写法,而是注意check(0)时候的特判。AC代码#include<iostream>#include<algorithm>#include<cmath>#include<cstdio>#......
  • CF383C Propagating tree 题解
    题目链接:CF或者洛谷比较朴素的题,注意到这个涉及到子树变化,我们考虑优先处理出\(dfs\)序,方便处理。注意到第一个问题较为繁琐,我们着重解决下第一个问题。在树上问题,我们这种间隔点常常使用\(deep\)进行区分。根的\(deep\)为奇数,那么对自己子树范围内的奇数\(deep\)......
  • 数组构建_cfECR162_C. Find B
    目录问题概述思路分析参考代码问题反思问题概述原题参考:C.FindB对于一个数组a,给出m次咨询,问对于每一次询问的区间是否可以构建出另外一个好的数组b,对于a的好数组的定义是a数组和b数组的元素和相同a数组和b数组的每一位不同b数组的每一位是正数思路分析对于第一个条件......
  • CF836F 做题笔记
    传送门非常好题目,使我原地旋转。首先数据这么小显然直接暴力求出每个\(A_i\)的取值范围。由于每个\(A_i\)只能有一个取值,所以源点先给所有\(A_i\)连一个限流为\(1\),费用为\(0\)的边。同时显然还要给每个值域点(不是\(A_i\))向汇点连限为\(inf\),费用为\(0\)的边......
  • CF1408H Rainbow Triples 题解
    Description给定长度为\(n\)的序列\(p\)找出尽可能多的三元组\((a_i,b_i,c_i)\)满足:\(1\lea_i<b_i<c_i\len\)\(p_{a_i}=p_{c_i}=0\),\(p_{b_i}\ne0\)\(p_{b_i}\)互不相同。所有的\(a_i,b_i,c_i\)互不相同。输出最多可以选出多少个三元组,多组数据。\(\sumn\le......
  • CF-929(已更新:B-E)
    CF-929开学以来打得最烂的一场(⊙﹏⊙)B两种操作:删除一个元素、把一个元素的权值增加1。求使得序列元素和整除于3的最小操作次数。分析如果序列和sum模3的余数为0,答案为0,若为2,可以进行第二种操作,答案为1,但是若为1,答案就不一定为2,因为若能进行第一种操作删去一个模3为1的元素,......
  • CF1209G2 Into Blocks (hard version) 题解
    Description给你\(n\),\(q\),\(n\)表示序列长度,\(q\)表示操作次数。我们需要达成这么一个目标状态:如果存在\(x\)这个元素,那么必须满足所有\(x\)元素都必须在序列中连续。然后你可以进行这么一种操作,将所有的\(x\)元素的变为任意你指定的\(y\)元素,并且花费\(cnt[x......
  • CF351D - Jeff and Removing Periods 题解
    首先做一点显然的转化:在进行第一次操作之后,可以将相同的数排在一起,这样一次就能删掉一种数。如果一开始就能删光一种数的话,那么次数就是区间颜色数,否则就是区间颜色数\(+1\)。所以现在原问题变成了两个问题:求区间内不同颜色数,判断区间内是否有某种颜色满足其出现位置构成等差数......