A. Min Max Swap
签到题。
令 ai′=max(ai,bi)、bi′=min(ai,bi)a_i' = max(a_i, b_i)、b_i' = min(a_i,b_i)ai′=max(ai,bi)、bi′=min(ai,bi),取最大值相减即可。
#include <stdio.h>
#include <vector>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define ll long long
#define chushi(a, b) memset(a, b, sizeof(a))
#define endl "\n"
const double eps = 1e-8;
const ll INF=0x3f3f3f3f;
const ll mod=998244353;
const int maxn = 2e6 + 5;
const int N=1005;
using namespace std;
int a[105], b[105];
int main() {
int ncase;
cin >> ncase;
while(ncase--){
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) cin >> b[i];
ll res = 0, ma = 0, mb = 0;
for(int i = 1; i <= n; i++){
ll x = max(a[i], b[i]), y = min(a[i], b[i]);
ma = max(ma, x);
mb = max(mb, y);
res = ma * mb;
}
cout << res << endl;
}
return 0;
}
B. Fun with Even Subarrays
模拟题。
题意:每次操作可以使得 ai=ai+ka_i = a_{i+k}ai=ai+k (i∈[x+1,x+k],k>0)(i ∈[x+1, x+k],k>0)(i∈[x+1,x+k],k>0),问最小多少次操作可以使得数组内的元素全部相等。
思路:从最后依次操作即可,注意类似于 3 2 2 2 3 2 3 的情况。
#include <stdio.h>
#include <vector>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define ll long long
#define chushi(a, b) memset(a, b, sizeof(a))
#define endl "\n"
const double eps = 1e-8;
const ll INF=0x3f3f3f3f;
const ll mod=998244353;
const int maxn = 2e6 + 5;
const int N=1005;
using namespace std;
int a[maxn];
int main() {
int ncase;
cin >> ncase;
while(ncase--){
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
int res = 0, pos = n-1;
while(1){
while(pos && a[pos] == a[n]) pos--;
if(pos < 1) break;
int len = n - pos;
pos -= len;
res++;
}
cout << res << endl;
}
return 0;
}
C. And Matching
构造题。
题意:对于 0 到 n-1 (n = 2 ^ k, k > 1),两两配对得到 n / 2 对数字,满足每对数字的 &运算累加等于 k。
思路:
对于任意的 i&(n−1−k)=0i \& (n - 1 - k) = 0i&(n−1−k)=0,一定成立。
让 n−1n-1n−1 和 kkk 配对,000 和 n−kn - kn−k 配对,其他数字使用上述结论处理。
这时 sum=((n−1)&k)+(0&(n−k))+0+...+0=k+0+0+...+0=ksum = ((n-1) \& k) + (0 \& (n-k)) + 0 + ... + 0 = k + 0 + 0 +... + 0 = ksum=((n−1)&k)+(0&(n−k))+0+...+0=k+0+0+...+0=k,满足题意。
可以发现,当 k = n-1 时,上述操作不成立。
当 k = n-1 时,让 n-1 与 n-2 配对,1 和 3 配对,0 和 n-4 配对,其他数字使用上述结论处理。
这时 sum=((n−1)&(n−2))+(1&3)+(0&(n−4))+0+...+0=n−2+1+0+0+...+0=n−1=ksum = ((n-1) \& (n-2)) + (1 \& 3) + (0 \& (n-4)) + 0 + ... + 0 = n-2 + 1 + 0 + 0 + ... + 0 = n-1 = ksum=((n−1)&(n−2))+(1&3)+(0&(n−4))+0+...+0=n−2+1+0+0+...+0=n−1=k,满足题意。
可以发现,当 n = 4 时,上述操作不成立,输出 -1。
#include <stdio.h>
#include <vector>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define ll long long
#define chushi(a, b) memset(a, b, sizeof(a))
#define endl "\n"
const double eps = 1e-8;
const ll INF=0x3f3f3f3f;
const ll mod=998244353;
const int maxn = 2e6 + 5;
const int N=1005;
using namespace std;
int main() {
int ncase;
cin >> ncase;
while(ncase--){
int n, k;
cin >> n >> k;
if(n == 4 && k == 3) cout << -1 << endl;
else if(n == k+1){
cout << n-1 << ' ' << n-2 << endl;
cout << "1 3" << endl;
cout << "0 " << n-4 << endl;
for(int i = 0; i < n/2; i++){
if(i == 0 || i == 1 || i == 3) continue;
cout << i << ' ' << n-1-i << endl;
}
}
else if(k == 0){
for(int i = 0; i < n/2; i++){
cout << i << " " << n-1-i << endl;
}
}
else{
cout << n-1 << " " << k << endl;
cout << "0 " << n-1-k << endl;
for(int i = 1; i < n/2; i++){
if(i == k) continue;
if(n-1-i == k) continue;
cout << i << " " << n-1-i << endl;
}
}
}
return 0;
}
D. Range and Partition
思维题。
题意:找到一个区间 [x,y][x,y][x,y],使得数组 aaa 可以划分为 kkk 个区间,且每个区间满足“区间内元素在[x,y][x,y][x,y]的个数严格大于不在[x,y][x,y][x,y]的元素个数”,输出任意一组 x,y(要求y-x最小) 以及划分方法。
思路:
我们定义数组aaa中在区间[x,y][x,y][x,y]的元素个数为 cntcntcnt。
如果满足 cnt−(n−cnt)>=kcnt - (n-cnt) >= kcnt−(n−cnt)>=k,则一定存在一种划分方法满足要求。
双指针枚举 111 到 nnn 找到合法的并且 y−xy-xy−x 最小的一组 (x,y)(x, y)(x,y)。
划分方法,先划分 k-1 满足的小区间(设区间长度为 len,cnt−(len−cnt)=1cnt - (len - cnt) = 1cnt−(len−cnt)=1),留下的元素构造最后一个区间即可。
证明:
对于划分好的 k-1 个区间,一定有 ∑1k−1cnti=∑1k−1leni+k−1\sum_{1}^{k-1}cnt_i = \sum_{1}^{k-1}len_i + k - 1∑1k−1cnti=∑1k−1leni+k−1。
对于整数数组有 cnt>=n−cnt+kcnt >= n - cnt + kcnt>=n−cnt+k。
对于第 k 个区间:
- [x,y][x,y][x,y]范围内的元素个数为:cnt’=cnt−∑1k−1cnticnt’ =cnt -\sum_{1}^{k-1}cnt_icnt’=cnt−∑1k−1cnti
- 区间的长度为:len′=n−∑1k−1lenilen' = n - \sum_{1}^{k-1}len_ilen′=n−∑1k−1leni
cnt′>=len′−cnt′+1cnt' >= len' - cnt' + 1cnt′>=len′−cnt′+1 成立时,划分正确。
把上边几个标准出的等式带一带就出来了,手懒不写了。
#include <stdio.h>
#include <vector>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define ll long long
#define chushi(a, b) memset(a, b, sizeof(a))
#define endl "\n"
const double eps = 1e-8;
const ll INF=0x3f3f3f3f;
const int mod=1e9 + 7;
const int maxn = 1e6 + 5;
const int N=1005;
using namespace std;
int a[maxn], v[maxn];
int main() {
int ncase;
cin >> ncase;
while(ncase--){
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++) v[i] = 0;
for(int i = 1; i <= n; i++) cin >> a[i], v[a[i]]++;
int l = 1, r = 0, x = 1, y = n;
int cnt = 0;
while(r < n){
cnt += v[++r];
while(cnt - (n - cnt) >= k){
if(r-l < y-x) x = l, y = r;
if(l+1 <= r) cnt -= v[l++];
else break;
}
}
cout << x << " " << y << endl;
int pos = 1; cnt = 0;
for(int i = 1; i <= n; i++){
if(cnt+1 == k){
cout << i << ' ' << n << endl;
break;
}
int cnt1 = 0, cnt2 = 0;
for(int j = i; j <= n; j++){
if(a[j] >= x && a[j] <= y) cnt1++;
else cnt2++;
if(cnt1 - cnt2 > 0){
pos = j+1;
break;
}
}
cout << i << " " << pos-1 << endl;
i = pos-1;
cnt++;
}
}
return 0;
}