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).
InputEach 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.
OutputFor 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).
InputEach 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.
OutputFor 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).
InputEach 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.
OutputFor 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:
Find the minimum number of operations required to sort the array in non-decreasing order.
InputEach 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.
OutputFor 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; }