Codeforces Round #768 (Div. 2)(A-D)

本文解析了四道算法题:MinMaxSwap、FunwithEvenSubarrays、AndMatching 和 RangeandPartition,涉及双指针、模拟、构造及思维等多种解题策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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&(n1k)=0,一定成立。
n−1n-1n1kkk 配对,000n−kn - knk 配对,其他数字使用上述结论处理。
这时 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=((n1)&k)+(0&(nk))+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=((n1)&(n2))+(1&3)+(0&(n4))+0+...+0=n2+1+0+0+...+0=n1=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(ncnt)>=k,则一定存在一种划分方法满足要求。
双指针枚举 111nnn 找到合法的并且 y−xy-xyx 最小的一组 (x,y)(x, y)(x,y)
划分方法,先划分 k-1 满足的小区间(设区间长度为 len,cnt−(len−cnt)=1cnt - (len - cnt) = 1cnt(lencnt)=1),留下的元素构造最后一个区间即可。
证明:
对于划分好的 k-1 个区间,一定有 ∑1k−1cnti=∑1k−1leni+k−1\sum_{1}^{k-1}cnt_i = \sum_{1}^{k-1}len_i + k - 11k1cnti=1k1leni+k1
对于整数数组有 cnt>=n−cnt+kcnt >= n - cnt + kcnt>=ncnt+k
对于第 k 个区间:

  • [x,y][x,y][x,y]范围内的元素个数为:cnt’=cnt−∑1k−1cnticnt’ =cnt -\sum_{1}^{k-1}cnt_icnt=cnt1k1cnti
  • 区间的长度为:len′=n−∑1k−1lenilen' = n - \sum_{1}^{k-1}len_ilen=n1k1leni

cnt′>=len′−cnt′+1cnt' >= len' - cnt' + 1cnt>=lencnt+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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值