Codeforces Round #632 (Div. 2) 部分题解

目录

  • Codeforces Round #632 (Div. 2)

    • A. Little Artem
    • B. Kind Anton
    • C. Eugene and an array
    • D. Challenges in school №41
    • F. Kate and imperfection

Codeforces Round #632 (Div. 2)

A. Little Artem

题意:略。

分析:构造这样的图形:

BWW...W
BWW...W
BBB...B
#include <bits/stdc++.h>
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }

int main() {
	io(); int t;
	cin >> t;
	while (t--) {
		int n, m;
		cin >> n >> m;
		for (int i = 1; i < n; ++i) {
			cout << "B";
			for (int j = 1; j < m; ++j) cout << "W";
			cout << "\n";
		}
		for (int i = 1; i <= m; ++i) cout << "B";
		cout << "\n";
	}
}

B. Kind Anton

题意:数组 \(a_n\) 由 \(\{-1,0,1\}\) 中的元素构成,并且你可以将数组中的任意元素 \(a_j\) 替换为 \(a_j+a_i(j>i)\) ,该操作能进行任意次,询问数组 \(a_n\) 能否通过变换变成数组 \(b_n\)

分析:如果 \(a_j<b_j\) ,那么必须存在 \(a_i=1(i<j)\) ;如果 \(a_j>b_j\) ,那么必须存在 \(a_i=-1(i<j)\) 。再特判 \(j=1\) 。

#include <bits/stdc++.h>
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }

int main() {
	io(); int t;
	cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<int> a(n), b(n);
		vector<vector<int> > vis(n, vector<int>(2));
		for (auto& i : a) cin >> i;
		bool f1, f2; f1 = f2 = false;
		int cnt = 0;
		for (auto i : a) {
			if (i < 0) f1 = true;
			if (i > 0) f2 = true;
			vis[cnt][0] = f1;
			vis[cnt++][1] = f2;
		}
		for (auto& i : b) cin >> i;
		bool f = true;
		if (a[0] != b[0]) f = false;
		for (int i = 1; i < n; ++i) {
			if (b[i] > a[i] && !vis[i - 1][1]) {
				f = false;
				break;
			}
			if (b[i] < a[i] && !vis[i - 1][0]) {
				f = false;
				break;
			}
		}
		cout << (f ? "YES\n" : "NO\n");
	}
}

C. Eugene and an array

题意:统计数组 \(a_n\) 中有几个连续子序列满足:该序列中不存在和为 \(0\) 的连续子序列。

分析:反过来统计和为 \(0\) 的子序列,然后减去。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
map<ll, ll> MP;

int main() {
	io(); int n;
	cin >> n;
	MP[0] = 0;
	ll r = -1, cur = 0, ans = 0;
	for (int i = 1; i <= n; ++i) {
		ll x; cin >> x;
		cur += x;
		if (MP.count(cur)) r = max(r, MP[cur]);
		ans += r + 1;
		MP[cur] = i;
	}
	ans = (n + 1ll) * n / 2 - ans;
	cout << ans;
}

D. Challenges in school №41

题意:给定一个长度为 \(n\) 且由字符 \(L,R\) 构成的字符串,每轮操作中,你都能选择任意对相邻的 \(RL\) ,将其变为 \(LR\) ,询问能否正好用 \(k\) 轮将字符串中的 \(L\) 全部移动至左端, \(R\) 全部移动至右端。

分析:每一轮操作我们都贪心地将所有能够交换的位置互换,统计最少需要几轮才能完成交换(记为 \(cnt\) ),并统计交换的总次数(记为 \(sum\) ),如果 \(k\in[cnt,sum]\) 那么就可以给出交换方案,因为我们能将某一轮中的操作拆开来,最多能够拆成 \(sum\) 轮。

#include <bits/stdc++.h>
#define SIZE 3010
#define ll long long
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
vector<int> vec[SIZE];
char s[SIZE];

int main() {
	io(); int n, k;
	cin >> n >> k >> (s + 1);
	int cnt = 0, sum = 0;
	while (1) {
		bool flag = false;
		cnt++;
		for (int i = 1; i < n; ++i) {
			if (s[i] == ‘R‘ && s[i + 1] == ‘L‘) {
				flag = true;
				vec[cnt].emplace_back(i);
			}
		}
		for (auto it : vec[cnt]) swap(s[it], s[it + 1]);
		sum += vec[cnt].size();
		if (!flag) break;
	}

	cnt--;
	if (k < cnt || k > sum) {
		cout << "-1";
		return 0;
	}

	for (int i = 1; i <= cnt; ++i) {
		while (!vec[i].empty() && k > cnt - i + 1) {
			cout << "1 " << vec[i].back() << ‘\n‘;
			vec[i].pop_back();
			k--;
		}
		if (!vec[i].empty()) {
			cout << vec[i].size();
			for (auto it : vec[i]) cout << ‘ ‘ << it;
			cout << ‘\n‘;
			k--;
		}
	}
}

F. Kate and imperfection

题意:在集合 \(S=\{1,2,\cdots,n\}\) 中,对于每个正整数 \(n\geq k>1\) ,找出一个大小为 \(k\) 的子集,使得该子集中两两间最大公因数的最大值最小,求这个最小值。

分析:我们考虑如何构造两两间最大公因数的最大值最小的集合,首先肯定是把所有质数先丢进集合里,然后再把与已经在集合内的数的最大公因数 \(=2\) 的数丢进去,然后是 \(=3\) 的数……然后注意到,如果我们加入了一个合数,那么他的所有因子必定已经在集合内了,于是加入的这个数字能够产生的最大公因数就是他的最大因子,因此用埃筛维护这个贪心的过程,排序一遍输出即可。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
map<ll, ll> MP;

int main() {
	io(); int n;
	cin >> n;
	vector<int> ans(n + 1, 1);
	for (int i = 2; i <= n; ++i) {
        for (int j = i + i; j <= n; j += i) {
            ans[j] = i;
        }
	}
    sort(ans.begin(), ans.end());
    for (int i = 2; i <= n; ++i) cout << ans[i] << ‘ ‘;
}

原文地址:https://www.cnblogs.com/st1vdy/p/12664248.html

时间: 2024-08-02 02:38:49

Codeforces Round #632 (Div. 2) 部分题解的相关文章

# Codeforces Round #529(Div.3)个人题解

Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Repeating Cipher 传送门 题意:第一个字母写一次,第二个字母写两次,依次递推,求原字符串是什么 题解:1.2.3.4,非常明显的d=1的等差数列,所以预处理一个等差数列直接取等差数列的每一项即可 代码: #include<bits/stdc++.h> using namespace s

Codeforces Round #531 (Div. 3) ABCDE题解

Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividing 题意: 给一个数n,然后要求你把1,2.....n分为两个集合,使得两个集合里面元素的和的差的绝对值最小. 题解: 分析可以发现,当n%4==0 或者 n%3==0,答案为0:其余答案为1.之后输出一下就好了. 代码如下: #include <bits/stdc++.h> using name

Codeforces Round #540 (Div. 3) 部分题解

Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选一些题来写吧. B. Tanya and Candies 题意: 在n个数中任意删去一个,如果这个数被删去后,剩余数的奇数和以及偶数和相等,那么就定义这个数为"好数".现在问这n个数中有多少个“好数”. 题解: 预处理出奇数前缀和.偶数前缀和,删去一个数后所有的奇数位置和 就为前面的奇数和

Codeforces Round #Pi (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/567 听说Round #Pi的意思是Round #314... A. Lineland Mail time limit per test:3 seconds memory limit per test:256 megabytes All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with it

Codeforces Round #498 (Div. 3) 简要题解

[比赛链接] https://codeforces.com/contest/1006 [题解] Problem A. Adjacent Replacements        [算法] 将序列中的所有偶数替换为奇数即可 时间复杂度 : O(N) [代码] #include<bits/stdc++.h> using namespace std; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y);

Codeforces Round #200 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/344 A. Magnets time limit per test:1 second memory limit per test:256 megabytes Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets inst

Codeforces Round #250 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/433 A. Kitahara Haruki's Gift time limit per test:1 second memory limit per test:256 megabytes Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between th

Codeforces Round #105 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/148 比较简单的一场,最长的一题也才写了30行多一点 A. Insomnia cure time limit per test:2 seconds memory limit per test:256 megabytes ?One dragon. Two dragon. Three dragon?, - the princess was counting. She had trouble falling asleep, and

Codeforces Round #353 (Div. 2) ABCDE 题解 python

Problems # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring Painting standard input/output 1 s, 256 MB    x2519 C Money Transfers standard input/output 1 s, 256 MB    x724 D Tree Construction standard input/output 2