Codeforces 第 813 轮(第 2 部分)A~C

分类:编程技术 时间:2024-02-20 17:28 浏览:0 评论:0
0

文章Codeforces Round #813 (Div. 2) A~C,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

A. Wonderful Permutation

  

You are given a permutation p1,p2,…,pnp1,p2,…,pn of length nn and a positive integer k≤nk≤n.

In one operation you can choose two indices ii and jj (1≤i

Find the minimum number of operations needed to make the sum p1+p2+…+pkp1+p2+…+pk as small as possible.

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

Input

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

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

The second line of each test case contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). It is guaranteed that the given numbers form a permutation of length nn.

Output

For each test case print one integer — the minimum number of operations needed to make the sum p1+p2+…+pkp1+p2+…+pk as small as possible.

A

You are given a permutation p1,p2,…,pnp1,p2,…,pn of length nn and a positive integer k≤nk≤n.

In one operation you can choose two indices ii and jj (1≤i

Find the minimum number of operations needed to make the sum p1+p2+…+pkp1+p2+…+pk as small as possible.

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

Input

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

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

The second line of each test case contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). It is guaranteed that the given numbers form a permutation of length nn.

Output

For each test case print one integer — the minimum number of operations needed to make the sum p1+p2+…+pkp1+p2+…+pk as small as possible.

定义第k大的数为下,直接找前k个数中小于x的数即可,我写的倒是有一些麻烦。

#include#include#include#include#include#include#include#include#include#include#include  #includeusing namespace std;#define int long long#define ull unsigned long long#define unmap unordered_map#define endl '\n'#define ls (p << 1)#define rs (p << 1 | 1)#define s_n (int)s.size()#define two int a,b,c;cin>>a>>b>>c;add(a,b,c);add(b,a,c);#define one int a,b,c;cin>>a>>b>>c;add(a,b,c);const int maxn=2e5+5,mod=1e9+7;typedef pair PII;int a[110],b[110],n,k;void solve() {cin>>n>>k;    for(int i=1;i<=n;i++) {        cin>>a[i];        b[i]=a[i];    }    sort(b+1,b+1+n);    unmapmp;    for(int i=1;i<=k;i++) {        mp[b[i]]=1;    }    int ans=0;    for(int i=1;i<=k;i++) {        if(!mp[a[i]]) ans++;    }    cout<>ONE_PIECE;while(ONE_PIECE--) {solve();}return 0;}

 B. Woeful Permutation

You are given a positive integer nn.

Find any permutation pp of length nn such that the sum lcm(1,p1)+lcm(2,p2)+…+lcm(n,pn)lcm⁡(1,p1)+lcm⁡(2,p2)+…+lcm⁡(n,pn) is as large as possible.

Here lcm(x,y)lcm⁡(x,y) denotes the least common multiple (LCM) of integers xx and yy.

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

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001≤t≤1000). Description of the test cases follows.

The only line for each test case contains a single integer nn (1≤n≤1051≤n≤105).

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case print nn integers p1p1, p2p2, ……, pnpn — the permutation with the maximum possible value of lcm(1,p1)+lcm(2,p2)+…+lcm(n,pn)lcm⁡(1,p1)+lcm⁡(2,p2)+…+lcm⁡(n,pn).

If there are multiple answers, print any of them.

 

可以发现,只有每一对都是奇数和偶数想乘相加后才会是最大值,因此从小到大奇数和偶数错开排列即可。

注意n为奇数时让1与1相乘才能获得最大值

#include#include#include#include#include#include#include#include#include#include#include  #includeusing namespace std;#define int long long#define ull unsigned long long#define unmap unordered_map#define endl '\n'#define ls (p << 1)#define rs (p << 1 | 1)#define s_n (int)s.size()#define two int a,b,c;cin>>a>>b>>c;add(a,b,c);add(b,a,c);#define one int a,b,c;cin>>a>>b>>c;add(a,b,c);const int maxn=2e5+5,mod=1e9+7;typedef pair PII;int n,a[maxn];void solve() {cin>>n;    queueq,p;    for(int i=1;i<=n;i++) {        if(i&1) q.push(i);        else p.push(i);    }    if(n%2==0) {        for(int i=1;i<=n;i++) {        if(i&1) {            int op=p.front();            cout<>ONE_PIECE;while(ONE_PIECE--) {solve();}return 0;}

 C. Sort Zero

You are given an array of nn positive integers a1,a2,…,ana1,a2,…,an.

In one operation you do the following:

Choose any integer xx.For all ii such that ai=xai=x, do ai:=0ai:=0 (assign 00 to aiai).

Find the minimum number of operations required to sort the array in non-decreasing order.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105).

The second line of each test case contains nn positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n).

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.

二分答案,找到最靠后的一个数ax使得x到n满足非递减且前边全是0即可。

#include #include #include #include #include #include #include #include #include #include #include #include using namespace std;#define int long long#define ull unsigned long long#define unmap unordered_map#define endl '\n'#define ls (p << 1)#define rs (p << 1 | 1)#define s_n (int)s.size()const int maxn = 2e5 + 5, mod = 1e9 + 7;typedef pair PII;int n, a[maxn],b[maxn];bool check(int x) {    unmapmp;    for(int i=1;ib[i+1]) return false;    }    return true;}void solve(){    cin>>n;    for(int i=1;i<=n;i++) {        cin>>a[i];    }    int l=1,r=n,k=0;    while(l<=r) {        int mid=(l+r)/2;        if(check(mid)) {            k=mid;            r=mid-1;        }else {            l=mid+1;        }    }    int ans=0;    unmapmm;    for(int i=1;i> ONE_PIECE;        while (ONE_PIECE--)        {            solve();        }        return 0;    }

 



1. 本站所有资源来源于用户上传或网络,仅作为参考研究使用,如有侵权请邮件联系站长!
2. 本站积分货币获取途径以及用途的解读,想在本站混的好,请务必认真阅读!
3. 本站强烈打击盗版/破解等有损他人权益和违法作为,请各位会员支持正版!
4. 编程技术 > Codeforces 第 813 轮(第 2 部分)A~C

用户评论