Codeforces Round # 632 (Div. 2) part of the solution to a problem

Codeforces Round #632 (Div. 2)

A. Little Artem

Meaning of the questions : slightly.

Analysis : This image construction:

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

The meaning of problems : Array \ (A_N \) a \ (\ - \ {1,0,1 \}) constituting the elements, and you can use any element of the array \ (a_j \) is replaced with \ (a_j + a_i (J> I) \) , the operation can be performed any number of times, asking array \ (A_N \) can transform into an array by \ (B_n \)

Analysis : If \ (a_j <b_j \) , then there must be \ (= a_i. 1 (I <J) \) ; if \ (a_j> b_j \) , then there must be \ (a_i = -1 (i < j) \) . Then special judge \ (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

Meaning of the questions : Statistics array \ (a_n \) there are several consecutive sequence satisfy: the sequence does not exist and is \ (0 \) is a continuous sequence.

Analysis : Statistical and turn to \ (0 \) in sequence, then subtract.

#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

The meaning of problems : Given a length \ (n-\) by the character and (L, R \) \ character strings, each run, you can select an arbitrary pair of adjacent \ (RL \) , which becomes \ (the LR \) , asked whether just by \ (K \) round the string \ (L \) moves to the left of all, \ (R & lt \) all move to the right.

Analysis : Each round operation we are all greedy to be able to exchange swap position, the statistics need at least a few rounds to complete the exchange (referred to as \ (cnt \) ), and count the total number of exchanges (referred to as \ (sum \ ) ), if \ (k \ in [cnt, sum] \) then you can give the exchange program, because we can in a round of operation to open up to be able to split into \ (sum \) round.

#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) {
		printf("-1\n");
		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

The meaning of problems : the set \ (S = \ {1,2, \ cdots, n \} \) , for each integer n \ (n-\ GEQ K>. 1 \) , to find a size \ (K \ ) subset, so that the sub-maximum concentration twenty-two greatest common factor of the minimum, this minimum demand.

Analysis : We consider how to build the minimum set of maximum twenty-two greatest common factor, of course, is the set of all prime numbers thrown in first, and then put the greatest common factor has been the number in the set \ (= 2 \ ) the number lost in, then \ (= 3 \) number ...... then noticed that if we add a composite number, then all he must have been a factor in the collection, so adding this number can be generated greatest common factor is his biggest factor, so greedy with Egypt sieve maintain this process, the output can be sorted again.

#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] << ' ';
}

Guess you like

Origin www.cnblogs.com/st1vdy/p/12664248.html