【AtCoder】AtCoder Grand Contest 041

本文详细解析了AtCoder Grand Contest 041的六个问题,包括Table Tennis Training、Voting Judges、Domino Quality等。针对每个问题,提供了O(1)到O(N^3)不同复杂度的解决方案,涉及二分搜索、动态规划和图论等算法。
摘要由CSDN通过智能技术生成

比赛链接

点击打开链接

官方题解

点击打开链接

Problem A. Table Tennis Training

A , B A,B A,B 奇偶性相同,则答案为 B − A 2 \frac{B-A}{2} 2BA

否则,答案为 M i n { A + B − 1 , 2 N − A − B + 1 } 2 \frac{Min\{A+B-1,2N-A-B+1\}}{2} 2Min{ A+B1,2NAB+1}

时间复杂度 O ( 1 ) O(1) O(1)

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
int main() {
	ll n, a, b; read(n), read(a), read(b);
	if ((b - a) % 2 == 0) cout << (b - a) / 2 << endl;
	else cout << min(a + b - 1, 2 * n - a - b + 1) / 2 << endl;
	return 0;
}

Problem B. Voting Judges

二分答案,考虑如何判断某个题是否能够能够出线。

问题可以规约为如下新问题:
给定数组 a i a_i ai ,两个整数 x , y x,y x,y
可以进行 x x x 次操作,每次选择 y y y 个数 − 1 -1 1 ,问是否能够使得 a i ≤ 0 a_i\leq 0 ai0

考虑每次操作最大的 y y y a i a_i ai ,可以发现可以满足条件当且仅当 a i ≤ x , ∑ a i ≤ x × y a_i\leq x,\sum{a_i}\leq x\times y aix,aix×y

时间复杂度 O ( N L o g N ) O(NLogN) O(NLogN)

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
int n, m, v, p, a[MAXN];
bool check(int pos) {
	int kill = n - p; ll sum = 0;
	for (int i = n; i >= 1 && kill; i--)
		if (i != pos) {
			if (a[pos] - a[i] > m) return false;
			sum += max(a[pos] - a[i], 0), kill--;
		}
	return sum <= 1ll * m * v;
}
int main() {
	read(n), read(m), read(v), read(p), v = n - v;
	for (int i = 1; i <= n; i++)
		read(a[i]), a[i] = 1e9 - a[i];
	sort(a + 1, a + n + 1);
	int l = 1, r = n;
	while (l < r) {
		int mid = (l + r + 1) / 2;
		if (check(mid)) l = mid;
		else r = mid - 1;
	}
	cout << l << endl;
	return 0;
}

Problem C. Domino Quality

N = 3 N=3 N=3 时可以简单构造,且可以通过重复这个构造解决 N N N 3 3 3 的倍数的情况。

N N N ≥ 4 \geq 4 4 的偶数时,可以简单构造,使得各行各列的权值均为 3 3 3

构造 N = 5 , 7 N=5,7 N=5,7 时的解,对于其余是奇数的 N N N ,可以拼一个 N = 5 N=5 N=5 的构造使得 N N N 变为偶数。

时间复杂度 O ( N 2 ) O(N^2) O(N

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值