跳到正文

个人记录

4.18 周五晚训练课-410总结

tanruiqing约 15 分钟
原文
文章信息与版本

文章信息

发布
2025年4月19日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 15 分钟

版本

  1. v1当前

24-10-总结

题目得分
T1T11010
T2T27070
T3T300
T4T44040
T5T54040
T6T600
总分160160

T1T1

考点:贪心

思路:

我们可以遍历一遍数组,看一下每个数字加上它旁边的数字是不是超过限制了,超过了就将它们吃掉一些,直到只剩下 xx 个。最后输出一共吃了多少个就行了。

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,m;
int a[100005];
int ans; 

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for(int i = 1 ; i <= n ; i++){
		cin >> a[i];
	}
	if(a[1] > m){//第一个要特殊判断。
		ans = a[1] - m;
		a[1] -= (a[1] - m);
	}
	for(int i = 1 ; i < n ; i++){
		if(i <= n){
			if(a[i] + a[i + 1] > m){//如果加起来超过m。
				ans += (a[i] + a[i + 1]) - m;//因为a[i]已经在上一次循环就减过了,所以这次减的就是i + 1。
				a[i + 1] -= (a[i] + a[i + 1]) - m;//就减去这么多。
			}
		}
	}
	cout << ans;//输出。
	return 0;
}

T2T2

考点:模拟

思路:

首先遍历一遍字符串,然后用前 99 个数字计算出最后一位因该是什么,然后判断这个数和最后一位是否相同,相同就输出 Right,否则输出正确的字符串。

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

string s;
int ans;

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> s;
	int cnt = 0;
	int l = s.length();
	for(int i = 0 ; i < 12 ; i++){
		if(s[i] >= '0' && s[i] <= '9'){
			cnt++;
			ans += ((int)(s[i] - '0') * cnt);
		}
	}
	ans %= 11;
	if(s[l - 1] == ans + '0' || (s[l - 1] == 'X' && ans == 10)){
		cout << "Right\n";
		return 0;
	}else{
		cout << s.substr(0,12) << ((ans == 10) ? 'X' : ans) << '\n';
	}
	return 0;
}

70 pts70\ pts

原因:输出 ‘X’ 的时候输出了一个ASCII表的值(88)。

正确代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

string s;
int ans;

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> s;
	int cnt = 0;
	int l = s.length();
	for(int i = 0 ; i < 12 ; i++){
		if(s[i] >= '0' && s[i] <= '9'){
			cnt++;
			ans += ((int)(s[i] - '0') * cnt);
		}
	}
	ans %= 11;
	if(s[l - 1] == ans + '0' || (s[l - 1] == 'X' && ans == 10)){
		cout << "Right\n";
		return 0;
	}else{
		cout << s.substr(0,12);
		if(ans >= 10)cout << 'X';
		else cout << ans;
	}
	return 0;
}

T3T3

考点:最大生成树

思路:

考虑情况:

  1. 只有一个队伍能没有淘汰的时候。
  2. 其他的队伍总会被一直队伍淘汰。
  3. 每支队伍能打败 >0,<n>0,<n 支队伍。
  4. 一共有 nn 场比赛。

其实可以发现,这 44 个点就对应着:树的根节点、父节点、每个节点的儿子节点、边的数量。

由此可得,我们可以将每两个队伍的总得分全部列举出来,然后在这些边中取一些权值最大且有用的边,然后输出挑选的边的权值的总和。

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

int n;
int fa[100005];

struct node{
	int x,y,k;
}a[4000005];

int b[200005];

int get(int x){
	if(fa[x] == x)return x;
	return fa[x] = get(fa[x]);
}

void unite(int x,int y){
	x = get(x);
	y = get(y);
	if(x != y)fa[x] = y;
}

bool cmp(node x,node y){
	return x.k > y.k;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 1 ; i <= n ; i++)fa[i] = i;
	for(int i = 1 ; i <= n ; i++){
		cin >> b[i];
	}
	int m = 0;
	for(int i = 1 ; i <= n ; i++){
		for(int j = i + 1 ; j <= n ; j++){
			if(i != j){
				m++;
				a[m].x = i;
				a[m].y = j;
				a[m].k = b[i] ^ b[j];
			}
		}
	}
	int ans = 0;
	sort(a + 1 , a + 1 + m , cmp);
	for(int i = 1 ; i <= m ; i++){
		if(get(a[i].x) != get(a[i].y)){
			unite(a[i].x,a[i].y);
			ans += a[i].k;
		}
	}
	cout << ans;
	return 0;
}

T4T4

考点:单调队列

思路:

我们可以维护一个队列,然后一次储存每个时刻的入港船,接着,我们判断如果目前第一艘船到达的时间(队头)和现在这艘船到达的时间相差大于等于一天的时间时,就要将对头删掉,然后重复以上步骤,直到目前第一艘船到达的时间(队头)和现在这艘船到达的时间相差小于一天为止。最后统计人的总数,最后输出就行了。

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int F = 86400;

int n;
int ans;
int mp[500005];

struct r{
	int t;
	vector<int> x;
}sum;

queue<r> q;
int t,k,x;

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 1 ; i <= n ; i++){
		cin >> t >> k;
		sum.t = t;
		sum.x.clear();
		for(int j = 1 ; j <= k ; j++){
			cin >> x;
			if(mp[x] == 0){
				ans++;
			}
			mp[x]++;
			sum.x.push_back(x);
		}
		q.push(sum);
		while(!q.empty() && t - F >= q.front().t){
			for(int j = 0 ; j < q.front().x.size() ; j++){
				if(mp[q.front().x[j]] == 1){
					ans--;
				}
				mp[q.front().x[j]]--;
			}
			q.pop();
		}
		cout << ans << '\n';
	}
	return 0;
}

T5T5

考点:进制

思路:

我们可以发现瓶子水量的增长,等于 22 的倒的次数次方。

比如:

C++
1 + 1 = 2L
2 + 2 = 4L
4 + 4 = 8L
8 + 8 = 16L
16 + 16 = 32L
32 + 32 = 64L
64 + 64 = 128L
...

所以,所有瓶子装的水的容积转换成二进制下所表示的数,对于某一位数,如果这个是 11,就代表这个位置需要一杯装的水的容积为这个位置所代表的权值这么多的水来填充。比如:

C++
17=(10001)2 -> 1杯1L的水,1杯16L的水,如果只能保留1个杯子,就要将1L的水一直加到16L的水去。

所以,我们只要每次找到权值最小的一位 11,然后用一杯同等质量的水来填充,只剩下 kk 以内个 11。

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

int nn,m,x,ans;
int qp[1005];

int check(int x){
	int cnt = 0;
	while(x){
		if(x & 1ll)cnt++;
		x >>= 1ll;
	}
	return cnt;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> nn >> x;
	while(check(nn) > x){
		for(int i = 0 ; i <= 63 ; i++){
			if(nn >> i & 1ll){
				nn += 1ll << i;
				ans += 1ll << i;
				break;
			}
		}
	}
	cout << ans;
	return 0;
}

T6T6

考点:模拟

思路:

首先的srot排序是必不可少的(这样可以更方便的找到一对兔子)。

我们可以用一个数组来记录所有兔子走的方向,然后我们可以分三种情况来处理:

  1. 两只相向而行的兔子。
  2. 向左行的兔子(多个跟一个)。
  3. 向右行的兔子(多个跟一个)。

对于第一种,我们可以看它左边/右边的兔子是不是向着它跑的且现在这只兔子是不是向它跑的,是的话就说明这是一对兔子,直接将这一对兔子到达的点设为这两只兔子相距的距离除以 22 就行了。

对于第二种,我们可以从右往左来遍历(因为,这样就可以找到最前的要往左走并且没被操作过的兔子,在这只兔子的前面必然是操作过的兔子(不然就会一直往左跑),所以可以直接将它的要到的点给找到),遇到一只向左跑的兔子并且没有操作过就将它将要到达的距离设为它前一个位置的兔子所达到的距离(因为这只兔子前面肯定是一对兔子(不然为什么这只兔子要往左跑),而现在这只兔子的目标也是前面那一对兔子中向右跑的兔子(不然在前面第一种情况就被标记过了),而那一对兔子要到达的点已经计算出来了,所以只需要将他们要到的点的坐标复制到这只兔子上就行了)。

对于第三种,我们可以从左往右来遍历(因为,这样就可以找到最前的要往右走的兔子,在这只兔子的后面必然是操作过的兔子,所以可以直接将它的要到的点给找到),遇到一只向右跑的兔子并且没有操作过就将它将要到达的距离设为它后一个位置的兔子所达到的距离(因为这只兔子前面肯定是一对兔子(不然为什么这只兔子要往右跑),而现在这只兔子的目标也是前面那一对兔子中向左跑的兔子(不然在前面第一种情况就被标记过了),而那一对兔子要到达的点已经计算出来了,所以只需要将他们要到的点的坐标复制到这只兔子上就行了)。

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

int n;
int b[100005];
int ans[100005];
bool vis[100005];
bool opt[100005];

struct node{
	int x,id;
}a[100005];

bool cmp(node a,node b){
	return a.x < b.x;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 1 ; i <= n ; i++){
		cin >> a[i].x;
		a[i].id = i;
	}
	sort(a + 1 , a + 1 + n , cmp);
	opt[1] = 1, opt[n] = 0;
	for(int i = 2 ; i < n ; i++){
		if(abs(a[i].x - a[i - 1].x) <= abs(a[i].x - a[i + 1].x)){
			opt[i] = 0;
		}else{
			opt[i] = 1;
		}
	}
	for(int i = 1 ; i < n ; i++){
		if(opt[i] == 1 && opt[i + 1] == 0){
			b[i] = (a[i].x + a[i + 1].x) >> 1;
			b[i + 1] = (a[i].x + a[i + 1].x) >> 1;
			vis[i + 1] = vis[i] = 1;
		}
	} 
	for(int i = 1 ; i <= n ; i++){
		if(opt[i] == 0 && vis[i] == 0){
			b[i] = b[i - 1];
			vis[i] = 1;
		}
	}
	for(int i = n ; i >= 1 ; i--){
		if(opt[i] == 1 && vis[i] == 0){
			b[i] = b[i + 1];
			vis[i] = 1;
		}
	}
	for(int i = 1 ; i <= n ; i++){
		ans[a[i].id] = b[i];
	}
	for(int i = 1 ; i <= n ; i++){
		cout << ans[i] << ' ';
	}
	return 0;
}

EndEnd

原文