跳到正文

个人记录

3.1-小六集训队-407

David_Lu约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年3月1日
收录
2026年3月2日
更新
2026年11月18日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

T737740 落医生

题意

有一个长度为nn的二进制字符串aa,现在又创建nn个新的字符串,第ii个字符串等于aa先复制过来再对第ii个字符进行反转(11变成00,00变成11),输出新的nn个字符串中有几个11

思路

把aa里面所有的11的数量记录下来,再循环模拟反转过程,如果位置ii是11,就带表新的字符串会少一个11,否则就是多一个11

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
void solve(){
	int n;
	cin >> n;
	string s;
	cin >> s;
	int s1 = 0;
	for(int i = 0;i < s.size();i++){
		if(s[i] == '1'){
			s1++;
		}
	}
	int ans = 0;
	for(int i = 0;i < s.size();i++){
		if(s[i] == '1'){
			ans += (s1 - 1);
		}else{
			ans += (s1 + 1);
		}
	}
	cout << ans<< '\n';
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

T737754 彩带涂色

题意

有一个序列要进行涂色,这个序列长度为nn,数字00到n−1n - 1在这个序列里都正好出现一次,涂色的规则是:给第ii个元素涂色时,11到ii缺失的最小的元素,就是这个元素的颜色,请问该如何排列这个序列,才能让涂上颜色xx的元素最多,请输出字典序最小的

思路

其实只要把xx移到最后面就行了

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
void solve(){
	int n, x;
	cin >> n>> x;
	if(n == 1){
		cout << 0<< '\n';
		return ;
	}
	if(n == x){
		for(int i = 0;i < n;i++){
			cout << i<< ' ';
		}
		cout << '\n';
		return ;
	}
	for(int i = 0;i < n;i++){
		if(i != x){
			cout << i<< ' ';
		}
	}
	cout << x<< '\n';
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

T737778 樱桃炸弹

题意

有两个数组aa和bb,bb丢失了一些元素,请问bb有几种可能每个位置与aa对应的元素相加结果都一样(bb的所有元素都小于等于kk),形成互补

思路

如果都不是−1-1,答案就是k−(最大值−最小值)+1k - (最大值 - 最小值) + 1,如果bb差值不相等就带表无法构成互补,或者bib_i的值求出后大于kk或小于00,也代表无法构成,都可以的话就输出11,否则输出00

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
const int N = 2e5 + 5;
int a[N], b[N];
void solve(){
	int n, k;
	cin >> n>> k;
	int minn = INT_MAX, maxn = INT_MIN;
	for(int i = 1;i <= n;i++){
		cin >> a[i];
		minn = min(minn, a[i]);
		maxn = max(maxn, a[i]);
	}
	for(int i = 1;i <= n;i++){
		cin >> b[i];
	}
	int x = 0;
	bool f = true;
	for(int i = 1;i <= n;i++){
		if(b[i] != -1){
			f = false;
			x = a[i] + b[i];
		}
	}
	if(f){
		cout << k + 1 - (maxn - minn)<< '\n';
	}else{
		for(int i = 1;i <= n;i++){
			if(b[i] != -1){
				if(a[i] + b[i] != x){
					cout << 0<< '\n';
					return ;
				}
			}else{
				if(x - a[i] > k || x - a[i] < 0){
					cout << 0<< '\n';
					return ;
				}
			}
		}
		if(maxn >= x){
			cout << 1<< '\n';
		}else{
			cout << 0<< '\n';
		}
	}
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> t;
	while(t--){
		solve();
	}
	return 0;
}
/*
7
3 10
1 3 2
-1 -1 1
5 1
0 1 0 0 1
-1 0 1 0 -1
5 1
0 1 0 0 1
-1 1 -1 1 -1
5 10
1 3 2 5 4
-1 -1 -1 -1 -1
5 4
1 3 2 1 3
1 -1 -1 1 -1
5 4
1 3 2 1 3
2 -1 -1 2 0
5 5
5 0 5 4 3
5 -1 -1 -1 -1
*/

T737789 花之少年

题意

有nn朵花,每朵花有一个好看值,要收集mm朵花,这些花只能挨个收集,每朵花都要大于等于bib_i的好看值,不过可以将一躲好看值为kk的花插进任何一个地方,请问kk至少是多是,如果无法完成收集输出−1-1

思路

用两个数组记录每个位置前最多能选取多少个数和每个位置前最多能选取多少个数,分别用prepre和lastlast,就可以知道前缀和后缀,知道了每个数字之间的空隙,只要满足pre[i]+last[i+1]=m−1pre[i]+last[i+1] = m−1就说明这里面之间差了一个数,kk就是b[pre[i+1]]b[pre[i+1]],只要继续打擂台寻找最小值就行了

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
const int N = 2e5 + 5;
int a[N], b[N];
int pre[N], last[N];
void solve(){
	int n, m;
	cin >> n>> m;
	for(int i = 1;i <= n;i++){
		cin >> a[i];
	}
	for(int i = 1;i <= m;i++){
		cin >> b[i];
	}
	pre[0] = 0;
	last[n + 1] = 0;
	int j = 1;
	for(int i = 1;i <= n;i++){
		if(j <= m and a[i] >= b[j]){
			pre[i] = pre[i - 1] + 1;
			j++;
		}else{
			pre[i] = pre[i - 1];
		}
	}
	j = m;
	for(int i = n;i >= 1;i--){
		if(j >= 1 and a[i] >= b[j]){
			last[i] = last[i + 1] + 1;
			j--;
		}else{
			last[i] = last[i + 1];
		}
	}
	int ans = LLONG_MAX;
	for(int i = 0;i <= n;i++){
		if(pre[i] + last[i + 1] == m){
			cout << 0<< '\n';
			return ;
		}
		if(pre[i] + last[i + 1] == m - 1){
			ans = min(ans, b[pre[i] + 1]);
		}
	}
	if(ans == LLONG_MAX){
		cout << -1<< '\n';
	}else{
		cout << ans<< '\n';
	}
}
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

T737792 狼

题意

有一个数组长度为nn,数组里的元素各不相同,要查找[L,R][L,R]之间kk的位置,但数组可能没有排序,可以挑dd个下标,在他们原有的位置上排序,让结果变对,请问要最少挑几个下标才能让结果正确,否则输出−1-1

思路

进行二分时,目标在右边,需要amid<ka_{mid} < k才能向右⾛,要用⼀个可⽤的⽐kk⼩的数,否则用一个⽐kk⼩的数替换,左边同理。最后先判断数字是否够用,不够输出−1-1,随后计算最⼩的修改次数,先配对needsmallneedsmall和needbigneedbig,每对贡献22。要更多⽐kk⼤的数,检查bigavbigav是否⾜够更小的数字也一样,否则输出ans+(要用的比k大的数字和小的数字的差)∗2ans+(要用的比k大的数字和小的数字的差)*2

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, q;
const int N = 2e5 + 5;
int t;
int a[N];
int id[N];
void solve(){
	cin >> n>> q;
	for(int i = 1;i <= n;i++){
		cin >> a[i];
		id[a[i]] = i;
	}
	while(q--){
		int l, r, k;
		cin >> l>> r>> k;
		if(id[k] < l or id[k] > r){
			cout << -1<< ' ';
			continue;
		}
		int small = k - 1;
		int big = n - k;
		int smallav = k - 1;
		int bigav = n - k;
		int needsmall = 0;
		int needbig = 0;
		int L = l, R = r;
		while(L <= R){
			int mid = (L + R) / 2;
			if(a[mid] == k){
				break;
			}
			if(mid < id[k]){
				if(k < a[mid]){
					needsmall++;
				}else{
					smallav--;
				}
				small--;
				L = mid + 1;
			}else{
				if(k > a[mid]){
					needbig++;
				}else{
					bigav--;
				}
				big--;
				R = mid - 1;
			}
		}
		if(big < 0 || small < 0){
			cout << -1<< ' ';
			continue;
		}
		int ans = 2 * min(needsmall, needbig);
		int diff = abs(needsmall - needbig);
		if(needbig > needsmall){
			if(bigav < diff){
				cout << -1<< ' ';
			}else{
				cout << ans + 2 * diff<< " ";
			}
		}else{
			if(smallav < diff){
				cout << -1<< ' ';
			}else{
				cout << ans + 2 * diff<< " ";
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while(t--){
		solve();
	}
	return 0;
}
原文