Codeforces Round 865 (Div. 2)(A~C)

A. Ian Visits Mary

给出一个点(a, b),用最多两步从(0, 0)到达(a, b),且每一步的起点和终点的连线都不经过任何其他点,给出两步的目标点。

思路:每次在x或者y上移动1,另一个方向移动若干长度即可。

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
const int N = 2e5 + 5;
int t, a, b;

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	std::cin >> t;
	while(t --) {
		std::cin >> a >> b;
		if(a == 1 || b == 1) {
			std::cout << 1 << '\n';
			std::cout << a << ' ' << b << '\n';
			continue;
		}
		std::cout << 2 << '\n';
		std::cout << 1 << ' ' << b - 1 << '\n';
		std::cout << a << ' ' << b << '\n';
	}
	return 0;
}

B. Grid Reconstruction

有一个2*n的矩阵,在其中填入2n个数,,每次从(1, 1)出发到达(2, n),只采用向下走或者向右走两种方式,最大化所有路径中价值最小的路径,给出构造方案。

思路: 最大化最小值,可以考虑所有路径的价值都趋于相同。pzr佬 的证明。

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5;
int t, n;
int a[3][N];

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	std::cin >> t;
	while(t --) {
		std::cin >> n;
		a[1][1] = n * 2;
		for(int i = 1; i <= n; i ++) {
			if(i % 2)
				a[2][i] = i, a[2][i + 1] = i + n;
			else
				a[1][i] = i, a[1][i + 1] = i + n;
		}
		for(int i = 1; i <= 2; i ++) {
			for(int j = 1; j <= n; j ++) {
				std::cout << a[i][j] << " \n"[j == n];
			}
		}
	}
	return 0;
}

C. Ian and Array Sorting

每次可以对于相邻的两个数进行相同的+1或-1操作,问最后能否将数组变为非递减数列。

思路:实际上有两种思路,一是以第一个数为基准,令后面的数全部进行操作使得第一个数到第n-1个数全部相同,然后判断第n个数和前一个数的大小关系;二是以最后一个数为基准,令第二个数到第n个数全部相同,然后判断前两个数的大小关系。对于n是奇数时,可以使得仅最后一个数或第一个数不满足条件,然后两两分别变大或者变小,然后即可满足条件,所以奇数一定可以满足条件。

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
#define int long long
const int N = 3e5 + 5;
int t, n;
int a[N];

signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	std::cin >> t;
	while(t --) {
		std::cin >> n;
		for(int i = 1; i <= n; i ++) {
			std::cin >> a[i];
		}
		if(n & 1) {
			std::cout << "YES" << '\n';
			continue;
		}
		a[n] += 1e9, a[n - 1] += 1e9;
		for(int i = n - 1; i >= 2; i --) {
			int res = a[i] - a[i + 1];
			a[i] -= res, a[i - 1] -= res;
		}
		std::cout << (a[1] <= a[2] ? "YES" : "NO") << '\n';
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值