跳到正文

个人记录

4.22周二下午训练 -- 东塘407

Meovow约 10 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月29日
篇幅
约 10 分钟

版本

  1. v1当前

4.22周二下午训练

P3817 小A的毒药

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int a[N],diff[N];
int n,x,ans;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>x;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		int diff=a[i]+a[i-1]-x;
		if(diff>0)
		{
			ans+=diff;
			a[i]=max(a[i-1]-diff,0ll);
		}
	}
	cout<<ans;
	return 0;
}

eee,改了前面那个 aia_i 没改后面那个

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int a[N],diff[N];
int n,x,ans;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>x;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		int diff=a[i]+a[i-1]-x;
		if(diff>0)
		{
			ans+=diff;
			a[i]=max(a[i-1]-diff,0ll);
		}
	}
	cout<<ans;
	return 0;
}

P1055 恐怖号码

没判识别码为 1010 的情况

C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
	char s[14], c;
	cin >> s;
	int h = 0, k = 0;
	for(int i = 0; i < 11; i++)
	{
		if(s[i] != '-')
		{
			k++;
			h += k * (s[i] - '0');
		}
	}
	h %= 11;
	if(h == 10) c = 'X';
	else c = h + '0';
	if(c == s[12]) cout << "Right" << endl;
	else
	{
		s[12] = c;
		cout << s;
	}
	return 0;
}

P4826 Superman S

数组大小应该开到 n2n^2 ,因为双重循环存应该是 n∗nn*n ,明显的,我数组开小了

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4e6 + 5;
int n, fa[N],z[N];
struct node{
	int x,y,z;
}a[N];
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;
	return ;
}
bool cmp(node xx, node yy)
{
	return xx.z > yy.z;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		fa[i] = i;
		cin >> z[i];
	}
	int m = 0;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			m++;
			a[m].x = i;
			a[m].y = j;
			a[m].z = z[i] ^ z[j];
		}
	}
	sort(a + 1, a + m + 1, cmp);
	int cnt = 0, ans = 0;
	for (int i = 1; i <= m; i++)
	{
		int f1 = get(a[i].x), f2 = get(a[i].y);
		if (f1 != f2)
		{
			unite(f1, f2);
			cnt++;
			ans += a[i].z;
		}
		if (cnt == n - 1)
		{
			cout << ans;
			return 0;
		}
	}
	cout << ans;
	return 0;
}

我真是个人机 /px

P2058 海港

使用队列存储每一个船上的每一个人,当一条新的船到达时,把之前超时的人弹出队列,然后输出答案。

Q:如何判超时? A:看这次的时间和那一次的时间差是否 ≥≥ 8640086400

Q:队列怎么定义? A:queue<int> q;

Q:头文件怎么打 A:…

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int T, a[N], ans;
map<int, int> cnt;
struct node{
	int s, g;
};
queue<node> q;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	while(T--)
	{
		int t, n;
		cin >> t >> n;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			cnt[a[i]]++;
			if (cnt[a[i]]==1)
				ans++;
			q.push({t, a[i]});
		}
		while (t - q.front().s >= 86400)
		{
			node now = q.front();
			q.pop();
			cnt[now.g]--;
			if (cnt[now.g] == 0)
				ans--;
		}
		cout << ans << "\n";
	}
	return 0;
}

P1582 喝水

我的思路

eee,要两两合并的话,如果是 22 的正整数次方就可以合并成一杯水,数据范围也只有 22 ×× 10910^9 ,所以可以把 22 ×× 10910^9 以内的 22 的正整数次方的表给打出来: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147482628 太好了我们复活了

现在就看这个数 nn 能不能被分为 kk 份及以内就ok了 /hhd

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[35] = {0, 1, 2, 4, 8, 16, 32, 64, 128, 256,
512, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
131072, 262144, 524288, 1048576, 2097152, 4194304,
8388608, 16777216, 33554432, 67108864, 134217728,
268435456, 536870912, 1073741824, 2147482628};
int f, n, k, ans;
void check(int x, int c ,int id)
{
	if (f != 0)
		return ;
	if (x == 0)
	{
		f = 1;
		return ;
	}
	if (c == 0 || id == 0)
	{
		f = 2;
		return ;
	}
	for (int i = id; i >= 1; i--)
	{
		if (x >= a[i])
			check(x - a[i], c - 1, i);
	}
	return ;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> k;
	check(n, k, 31);
	if (f == 1)
	{
		cout << ans;
		return 0;
	}
	while (n++)
	{
		f = 0;
		ans++;
		check(n, k, 31);
		if (f == 1)
		{
			cout << ans;
			return 0;
		}
	}
	return 0;
}

然后 9090 pts

仔细观察下载数据后发现,当样例为1999999999 1时,一个一个加太慢了 然后时间不够我也没研究了

老师方法

原瓶数二进制下合并后的瓶数
111
2101
3112
41001
51012
61102
71113
810001

这个东西可以看成二进制的形式,其实就是瓶数在二进制下 11 的个数

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k;
int get_1(int x)
{
	int cnt = 0;
	for (int i = 32; i >= 0; i--)
		if ((x >> i) & 1)
			cnt++;
	return cnt;
}
int get_id1(int x)
{
	for (int i = 0; i <= 32; i++)
		if ((x >> i) & 1)
			return i;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);
	cin >> n >> k;
	int ans = 0;
	while (get_1(n) > k)
	{
		int tmp = get_idi1(n);
		ans += (1ll << tmp);
		n += (1ll << tmp);
	}
	cout << ans;
	return 0;
}
原文