Daily Practice 4th: Codeforces Round #766 (Div. 2)

VP*4;

rating:800--1300--1400

A. Not Shading

给出n*m的矩阵,每一格是黑色或者白色,问若想使给定方格成为黑色,最少经过几次操作。操作是指:选择一个黑色方格,将它所在的这一行或者这一列全部涂黑。

思路: 可以分三种情况讨论:1、所有方格中没有黑色的方格,不可能经过操作使其成为黑色;2、该方格原来就是黑色,不需要操作就可以满足条件;3、若要涂成黑色的方格所在的这一行或者这一列有黑色的方格,则经过1次操作可以满足;4、若矩阵中存在黑色方格,但都与目标方格不在同一行或者同一列,则最多需要两次操作可以满足条件。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		print(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 55;
int t, n, m, r, c;
std::string s[N];

int main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	std::cin >> t;
	while (t--) {
		std::cin >> n >> m >> r >> c;
		for (int i = 0; i < n; i++) {
			std::cin >> s[i];
		}
		if (s[r - 1][c - 1] == 'B') {
			std::cout << 0 << '\n';
			continue;
		}
		bool flag = false;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (s[i][j] == 'B')
					flag = true;
			}
		}
		if (!flag) {
			std::cout << -1 << '\n';
			continue;
		}
		flag = false;
		for (int i = 0; i < m; i++) {
			if (s[r - 1][i] == 'B')
				flag = true;
		}
		for (int i = 0; i < n; i++) {
			if (s[i][c - 1] == 'B')
				flag = true;
		}
		if (flag)
			std::cout << 1 << '\n';
		else
			std::cout << 2 << '\n';
	}
	return 0;
}

B. Not Sitting

 给出n*m的矩阵,随机选择0~n*m-1个方格上色,a想靠近b,b想远离a,a只能在未上色的方格,a和b之间的距离是|ax-bx|+|ay-by|,问两人之间的距离随着k变化,最近距离是多少。

思路:首先a想靠近b,那他会选择矩阵比较靠近中间的位置;b想远离a,则她一定会选择距离a最远的一个角落。最后每一个格子都要被上色,所以我们不必考虑上色的顺序,因为a和b都是选择最利于自己的方式操作,所以我们需要考虑每一个格子到较远的四角的距离,把所有距离放在一起,排序即可得到涂色0~n*m-1个格子时的最短距离。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		print(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 55;
int t, n, m;

int main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	std::cin >> t;
	while (t--) {
		std::cin >> n >> m;
		std::vector<int>vec;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				vec.push_back(std::max(i - 1, n - i) + std::max(j - 1, m - j));
			}
		}
		std::sort(vec.begin(), vec.end());
		for (int i = 0; i <= n * m - 1; i++) {
			std::cout << vec[i] << " \n"[i == n * m - 1];
		}
	}
	return 0;
}

C. Not Assigning

 给出一棵树,问怎样给每一条边赋权值,使得这是一棵prime tree。prime tree是指:每条边的边权是质数,长度为2的边权和也是质数,若无法满足条件输出-1。

思路:我们可以知道若两个质数的和为质数,那么这两个数中必有一个为2。那么我们给边权赋值时,大可以2,3交替赋值;再考虑不满足条件的情况,对于一个节点,若该点的度大于2,则无法满足条件,因为我们赋值时要么2个2,一个3,要么两个3,一个2,这样都不满足条件,度数更多时更不满足条件。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		print(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
int t, n, cnt, to[N], nex[N], head[N];
bool vis[N];
std::map<std::pair<int, int>, int>mp;//存边权

void add_edge(int u, int v) {
	to[cnt] = v;
	nex[cnt] = head[u];
	head[u] = cnt++;
}

void dfs(int u, int t) {//dfs给边2,3交替赋值
	vis[u] = true;
	for (int i = head[u]; i != -1; i = nex[i]) {
		int j = to[i];
		if (!vis[j]) {
			vis[j] = true;
			mp[ {u, j}] = mp[ {j, u}] = t;
			dfs(j, t ^ 1);//2和3的转化,因为2^1=3,3^1=2
		}
	}
}

int main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	std::cin >> t;
	while (t--) {
		std::cin >> n;
		memset(vis, 0, sizeof(vis));
		memset(head, -1, sizeof(head));
		cnt = 0;
		std::vector<int>vec(n + 1, 0);
		std::vector<std::pair<int, int>>pp;
		for (int i = 0; i < n - 1; i++) {
			int a, b;
			std::cin >> a >> b;
			add_edge(a, b);
			add_edge(b, a);
			pp.push_back({a, b});//存入输入顺序
			vec[a]++, vec[b]++;//记录度数
		}
		bool flag = true;
		for (int i = 1; i <= n; i++) {
			if (vec[i] > 2) {
				flag = false;
				break;
			}
		}
		if (!flag) {
			std::cout << -1 << '\n';
			continue;
		}
		for (int i = 1; i <= n; i++) {
			if (vec[i] == 1) {
				dfs(i, 2);
				break;
			}
		}
		for (int i = 0; i < n - 1; i++) {//按输入顺序输出
			int p = pp[i].first, q = pp[i].second;
			std::cout << mp[ {p, q}] << ' ';
		}
		std::cout << '\n';
	}
	return 0;
}

os:图论,链式前向星存树。

D. Not Adding

给出n个数的数列,输出可操作的最多次数。操作是指选择两个数,满足这两个数的gcd不在序列中,把两数的gcd放到序列中。

思路:直接枚举,在a的范围内枚举i,看i的倍数是否存在与数列中,若存在则计数,最后要减去数组长度,因为数组内每个数的1倍都被计算了。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		print(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int n;
int cnt[N], cc[N];

int main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	std::cin >> n;
	for (int i = 1; i <= n; i++) {
		int x;
		std::cin >> x;
		cnt[x]++;
	}
	int ans = 0;
	for (int i = 1; i < N; i++) {
		for (int j = i; j < N; j += i) {
			if (cnt[j])
				cc[i] = std::__gcd(cc[i], j);
		}
		ans += (cc[i] == i);
	}
	std::cout << ans - n << '\n';
	return 0;
}

os:数论相关就要好好想想了,不然有时候想不太过来。。。

若有错误请指教orzorz

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值