2023年ICPC沈阳站题解

因为思维和码力跟不上,补题补得太痛苦了,继续板刷去了

C. Swiss Stage(签到)

#include <bits/stdc++.h>
 
using namespace std;
 
#define int long long
using i64 = long long;
 
typedef pair<int, int> PII;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;
typedef pair<int, pair<int, bool>> PIIB;
 
const int N = 1e6 + 10;
const int maxn = 1e6 + 10;
const int mod = 998244353;
const int mod1 = 954169327;
const int mod2 = 906097321;
const int INF = 0x3f3f3f3f3f3f3f3f;
 
void solve()
{
	int x, y;
	cin >> x >> y;
	int ans = 0;
	for (; x < 3; x ++ )
	{
		if (x >= 2 || y >= 2) ans += 2;
		else ans ++ ; 
	}
	cout << ans << '\n';
}
 
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
 
	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}
}

E. Sheep Eat Wolves(dp)

设计 dp 状态表示为 : dp[i][j][k] 人在 i 边,左边有 j 只羊和 k 只狼的最小步数

然后用 bfs 来转移,同时需要注意,当更新答案后立刻输出并结束程序,否则会爆栈

#include <bits/stdc++.h>

using namespace std;

#define int long long
using i64 = long long;

typedef pair<int, int> PII;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;
typedef pair<int, pair<int, bool>> PIIB;

const int N = 1e6 + 10;
const int maxn = 1e6 + 10;
const int mod = 998244353;
const int mod1 = 954169327;
const int mod2 = 906097321;
const int INF = 0x3f3f3f3f3f3f3f3f;

struct node {
	int sheep, wolves, pos;
};

void solve()
{
	int a, b, pp, qq;
	cin >> a >> b >> pp >> qq;
	queue<node> q;
	vector<vector<vector<int>>> dp(2, vector<vector<int>>(a + 1, vector<int>(b + 1, INF)));
	dp[0][a][b] = 0;
	q.push({a, b, 0});
	while (q.size())
	{
		auto [s, w, pos] = q.front();
		q.pop();

		if (s == 0 && pos == 1)
		{
			cout << dp[pos][s][w] << '\n';
			return;
		}

		for (int i = 0; i <= (pos == 0 ? s : a - s); i ++ )
		{
			for (int j = 0; j <= (pos == 0 ? w : b - w); j ++ )
			{
				if (i + j > pp) break;
				if (pos == 0 && s - i + qq < w - j && s - i != 0) continue;
				if (pos == 1 && a - s - i + qq < b - w - j && a - s - i != 0) continue;

				int na, nb;
				if (pos == 0) na = s - i, nb = w - j;
				else na = s + i, nb = w + j;

				if (dp[pos ^ 1][na][nb] > dp[pos][s][w] + 1)
				{
					dp[pos ^ 1][na][nb] = dp[pos][s][w] + 1;
					q.push({na, nb, pos ^ 1});
				}
			}
		}
	}

	cout << -1 << '\n';
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}
}

J. Graft and Transplant(思维)

可以发现,每次只能对非叶子结点操作,并且每操作一次就会让一个非叶子结点变成叶子结点,所以判断非叶子结点的个数,奇数后手赢,偶数先手赢,再特判非叶子结点数为0的情况

#include <bits/stdc++.h>

using namespace std;

#define int long long
using i64 = long long;

typedef pair<int, int> PII;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;
typedef pair<int, pair<int, bool>> PIIB;

const int N = 1e6 + 10;
const int maxn = 1e6 + 10;
const int mod = 998244353;
const int mod1 = 954169327;
const int mod2 = 906097321;
const int INF = 0x3f3f3f3f3f3f3f3f;

void solve()
{
	int n;
	cin >> n;
	vector<int> ind(n + 1);
	for (int i = 0; i < n - 1; i ++ )
	{
		int u, v;
		cin >> u >> v;
		ind[u] ++ , ind[v] ++ ;
	}
	int cnt = 0;
	for (int i = 1; i <= n; i ++ )
	{
		if (ind[i] != 1) cnt ++ ;
	}
	if (cnt == 0)
	{
		cout << "Bob\n";
		return;
	}
	if (cnt & 1) cout << "Bob\n";
	else cout << "Alice\n";
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}
}

K. Maximum Rating(线段树+二分)

首先能够更新的最大次数一定是正数的个数,因为可以把所有正数放在最前面,那每次都会更新最大值

那最小次数是多少呢?很明显是把所有负数放在前面,然后用正数和它们抵消,直到抵消成为正数,更新的次数就是还没有用来和负数抵消的正数的个数+1

所以更新的次数的个数就是最大值减去最小值加1

因为需要修改原数组的值,所以用线段树维护,然后发现a的范围是-1e9到1e9,所以需要动态开点(码力太差了离散化线段树怎么也没调出来)

#include <bits/stdc++.h>

using namespace std;

#define int long long
using i64 = long long;

typedef pair<int, int> PII;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;
typedef pair<int, pair<int, bool>> PIIB;

const int N = 1e6 + 10;
const int maxn = 1e6 + 10;
const int mod = 998244353;
const int mod1 = 954169327;
const int mod2 = 906097321;
const int INF = 0x3f3f3f3f3f3f3f3f;

struct node {
	int l, r, ls, rs, sum, cnt;
} tr[N * 10];

int idx;

void pushup(int u)
{
	tr[u].l = tr[tr[u].ls].l, tr[u].r = tr[tr[u].rs].r;
	tr[u].sum = tr[tr[u].ls].sum + tr[tr[u].rs].sum;
	tr[u].cnt = tr[tr[u].ls].cnt + tr[tr[u].rs].cnt;
}

void modify(int& u, int l, int r, int pos, int x)
{
	if (!u)
	{
		u = ++ idx;
		tr[u].l = l, tr[u].r = r;
	}
	if (l == r)
	{
		tr[u].sum += pos * x;
		tr[u].cnt += x;
		return;
	}
	int mid = l + r >> 1;
	if (pos <= mid) modify(tr[u].ls, l, mid, pos, x);
	else modify(tr[u].rs, mid + 1, r, pos, x);
	pushup(u);
}

int query(int u, int l, int r, int sum)
{
	if (l == r) return (sum <= 0 ? 0 : (sum + l - 1) / l);
	int mid = l + r >> 1;
	if (tr[tr[u].rs].sum >= sum) return query(tr[u].rs, mid + 1, r, sum);
	else return query(tr[u].ls, l, mid, sum - tr[tr[u].rs].sum) + tr[tr[u].rs].cnt;
}

void solve()
{
	int n, q;
	cin >> n >> q;
	vector<int> a(n + 1);
	int sum = 0, rt = 0;
	for (int i = 1; i <= n; i ++ )
	{
		cin >> a[i];
		if (a[i] > 0) modify(rt, 1, INF, a[i], 1);
		sum += a[i];
	}
	while (q -- )
	{
		int pos, val;
		cin >> pos >> val;

		if (a[pos] > 0) modify(rt, 1, INF, a[pos], -1);
		if (val > 0) modify(rt, 1, INF, val, 1);

		sum += val - a[pos];
		a[pos] = val;
		cout << tr[1].cnt - query(rt, 1, INF, sum) + 1 << '\n';
	}
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}
}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Texcavator

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值