【CodeForces】CodeForces Round #477 (Div. 1 + Div. 2) 题解

本文提供了CodeForces Round #477 分数赛的题解,包括Div. 1 和 Div. 2 的题目。涉及的算法包括贪心、二分查找、排序、网络流等,时间复杂度从( O(Ans*N) )到( O(Dinic(N,M)*LogV) )不等。" 52337171,5586421,ROS环境下的四旋翼无人机仿真:对比与实践,"['无人机开发', 'ROS仿真', 'Gazebo环境', '四旋翼控制']
摘要由CSDN通过智能技术生成

【比赛链接】

【题解链接】

【Div.2 A】Mind the Gap

【思路要点】

  • 从小到大枚举答案,检查合法性
  • 时间复杂度\(O(Ans*N)\)。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
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;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int n, s, a[MAXN];
bool valid(int x) {
	if (x + s < a[1]) return true;
	if (x - s > a[n]) return true;
	for (int i = 1; i < n; i++)
		if (x - s > a[i] && x + s < a[i + 1]) return true;
	return false;
}
int main() {
	read(n), read(s);
	for (int i = 1; i <= n; i++) {
		int x, y;
		read(x), read(y);
		a[i] = x * 60 + y;
	}
	int ans = 0;
	while (!valid(ans)) ans++;
	printf("%d %d\n", ans / 60, ans % 60);
	return 0;
}

【Div.2 B】Watering System

【思路要点】

  • 显然我们会优先堵住大小较大的洞,排序后贪心即可。
  • 时间复杂度\(O(NLogN)\)。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
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;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int main() {
	int n, A, B, C, T, ans = 0;
	read(n), read(A), read(B), read(C), read(T);
	for (int i = 1; i <= n; i++) {
		int x; read(x);
		ans += A;
		if (C >= B) ans += (T - x) * (C - B);
	}
	writeln(ans);
	return 0;
}

【Div.2 C/Div.1 A】Stairs and Elevators

【思路要点】

  • 不难发现我们只有可能使用起点左起以及右起的第一个楼梯与电梯,总共四种情况。
  • 用二分实现即可,时间复杂度\(O(QLogC_l+QLogC_e)\)。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
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;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int func(int dist, int v) {
	return dist / v + (dist % v != 0);
}
int a[MAXN], b[MAXN];
int main() {
	int r, n, m, v;
	read(r), read(r), read(n), read(m), read(v);
	for (int i = 1; i <= n; i++)
		read(a[i]);
	for (int i = 1; i <= m; i++)
		read(b[i]);
	int q; read(q);
	for (int i = 1; i <= q; i++) {
		int sx, sy, tx, ty;
		read(sx), read(sy);
		read(tx), read(ty);
		if (sx == tx) {
			writeln(abs(sy - ty));
			continue;
		}
		int tmp = lower_bound(a + 1, a + n + 1, sy) - a;
		int tnp = tmp - 1;
		int ans = 2e9;
		if (tmp <= n) chkmin(ans, abs(a[tmp] - sy) + abs(a[tmp] - ty) + abs(sx - tx));
		if (tnp >= 1) chkmin(ans, abs(a[tnp] - sy) + abs(a[tnp] - ty) + abs(sx - tx));
		tmp = lower_bound(b + 1, b + m + 1, sy) - b;
		tnp = tmp - 1;
		if (tmp <= m) chkmin(ans, abs(b[tmp] 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值