Codeforces Round #813 (Div. 2) 题解 (A~C)

A. Wonderful Permutation

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Problem Statement

You are given a permutation p 1 , p 2 , … , p n p_1,p_2,…,p_n p1,p2,,pn of length n and a positive integer k ≤ n k≤n kn.

In one operation you can choose two indices i i i and j j j ( 1 ≤ i < j ≤ n ) (1≤i<j≤n) (1i<jn) and swap p i p_i pi with p j p_j pj.

Find the minimum number of operations needed to make the sum p 1 + p 2 + … + p k p_1+p_2+…+p_k p1+p2++pk as small as possible.

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

题面翻译

给出一个长度为 n n n的排列 p 1 , p 2 , . . . , p n p_1,p_2,...,p_n p1,p2,...,pn和一个正整数 k k k

在每一次操作中,你可以交换任意的 a i , a j ( 1 ≤ i , j ≤ n ) a_i,a_j(1 \le i,j \le n) ai,aj(1i,jn)

求当 p 1 + p 2 + . . . + p k p_1+p_2+...+p_k p1+p2+...+pk最小时,最少的操作次数。

定义:排列是指有 1 1 1 n n n组成的任意排序。例如, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4]是一个排列,但 [ 1 , 2 , 2 ] [1,2,2] [1,2,2]不是排列( 2 2 2在数组中出现两次), [ 1 , 3 , 4 ] [1,3,4] [1,3,4]也不是排列( n = 3 n=3 n=3 4 4 4也在数组中)。

Input

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

The first line of each test case contains two integers n n n and k ( 1 ≤ k ≤ n ≤ 100 ) k (1≤k≤n≤100) k(1kn100).

The second line of each test case contains n n n integers p 1 , p 2 , … , p n ( 1 ≤ p i ≤ n ) p_1,p_2,…,p_n (1≤p_i≤n) p1,p2,,pn(1pin). It is guaranteed that the given numbers form a permutation of length n n n.

Output

For each test case print one integer — the minimum number of operations needed to make the sum p 1 + p 2 + … + p k p_1+p_2+…+p_k p1+p2++pk

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值