首页 > 其他分享 > Codeforces Round #813 (Div. 2) A~C

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

时间:2022-08-14 01:29:54浏览次数:171  
标签:p2 813 nn int Codeforces permutation test Div include

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<j≤n1≤i<j≤n) and swap pipi with pjpj.

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<j≤n1≤i<j≤n) and swap pipi with pjpj.

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<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#include<cmath>
#include<stack>
#include <iomanip> 
#include<unordered_map>
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()
#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<int,int> 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);
    unmap<int,int>mp;
    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<<ans<<endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int ONE_PIECE=1;
	cin>>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<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#include<cmath>
#include<stack>
#include <iomanip> 
#include<unordered_map>
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()
#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<int,int> PII;


int n,a[maxn];
void solve() {
	cin>>n;
    queue<int>q,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<<op<<' ';
            p.pop();
        } else {
            int op=q.front();
            cout<<op<<' ';
            q.pop();
        }
        }
    } else {
        cout<<1<<' ';
        q.pop();
        for(int i=2;i<=n;i++) {
        if(i&1) {
            int op=p.front();
            cout<<op<<' ';
            p.pop();
        } else {
            int op=q.front();
            cout<<op<<' ';
            q.pop();
        }
        }
    }
    cout<<endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int ONE_PIECE=1;
	cin>>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:

  1. Choose any integer xx.
  2. 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 <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <cmath>
#include <stack>
#include <iomanip>
#include <unordered_map>
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<int, int> PII;

int n, a[maxn],b[maxn];

bool check(int x) {
    unmap<int,int>mp;
    for(int i=1;i<x;i++) mp[a[i]]=1;
    int flag = 1;
    for(int i=x;i<=n;i++) {
        if(mp[a[i]]) b[i]=0;
        else b[i]=a[i];
    }
    for(int i=x;i<n;i++) {
        if(b[i]>b[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;
    unmap<int,int>mm;
    for(int i=1;i<k;i++) {
        if(!mm[a[i]]) {
            mm[a[i]]=1;
            ans++;
        }
    }
    cout<<ans<<endl;
}

    signed main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
        int ONE_PIECE = 1;
        cin >> ONE_PIECE;
        while (ONE_PIECE--)
        {
            solve();
        }
        return 0;
    }

 

标签:p2,813,nn,int,Codeforces,permutation,test,Div,include
From: https://www.cnblogs.com/cbmango/p/16584676.html

相关文章

  • Codeforces 121 E
    感觉我数据结构有些弱,最近开始练习难道为2300~2700的数据结构题。首先我们发现,luckynumber不会太多,最多就是\((2^1+2^2+2^3+2^4+1)=31\)个(最后加\(1\)是对于所有\(x>7777......
  • 20220813 夜间闲话
    今天下午有一场模拟比赛。毫不奇怪,我又跌到了谷底。幸好奇瑞不在,不然又要被骂了。dkd和lh为AK取得好成绩,为cqyz争光。不出所料,lhAK非常快。T1是一个很微妙的贪心(其实并......
  • DFS记忆化搜索--Divider & Conquer + Hashmap(数字三角形)
    记忆化搜索是DP的一种实现方式,等价于动态规划一个经典的例子:数字三角形给定一个三角形triangle,找出自顶向下的最小路径和。每一步只能移动到下一行中相邻的结点上。相......
  • 20220813 早间闲话
    今天早上八点,我们拿到了我亲爱的电话,我发现我的朋友都疯了,所以我疯了。Lh和Dkd在玩弗洛瑞,他们说lrc太强了,提升空间太小了。他对dkd经常押韵感到震惊。lh正在下载一个......