Location>code7788 >text

AtCoder Beginner Contest 380 (A~E) Questions and Answers

Popularity:158 ℃/2024-11-17 23:25:58

A - 123233

Just iterate through the string counting the occurrences.

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int n, m, k;
int a[N];
signed main() {
	string s;
	cin >> s;
	map<char, int> mp;
	for (auto t : s) {
		mp[t]++;
	}
	if (mp['2'] == 2 && mp['1'] == 1 && mp['3'] == 3) {
		cout << "Yes\n";
	} else {
		cout << "No\n";
	}
}

B - Hurdle Parsing

Just count directly and output $ | $ when you encounter it.

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int n, m, k;
int a[N];
signed main() {
	string s;
	cin >> s;
	int cnt = 0, f = 0;
	for (int i = 0; i < (); i++) {
		if (s[i] == '|') {
			if (f == 1)
				cout << cnt << ' ';
			cnt = 0;
			f = 1;
		}
		if (s[i] != '|') {
			cnt++;
		}
	}
}

C - Move Segment

The problem is to merge the $ k $th consecutive character $ 1 $ segment with the $ k-1 $th segment.

Just use $ vector $ to store each successive segment and then order them according to the question.

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int n, m, k;
int a[N];
signed main() {
	string s;
	cin >> n >> k;
	cin >> s;
	string now1 = "", now2 = "";
	vector<string> a;
	for (int i = 0; i < n; i++) {
		if (s[i] == '1') {
			now1 += s[i];
			if (() != 0) {
//				cout << now2 << '\n';
				a.push_back(now2);
				now2 = "";
			}
		}
		if (s[i] == '0') {
			now2 += s[i];

			if (() != 0) {
				a.push_back(now1);
//				cout << now1 << '\n';
				now1 = "";
			}
		}
	}
	if (())
		a.push_back(now1);
	if (())
		a.push_back(now2);
//	for (auto t : a) {
//		cout << t << ' ';
//	}
	cout << '\n';
	int cnt = 0, d1 = 0, d2 = 0;
	for (int i = 0; i < (); i++) {
		if (a[i][0] == '1') {
			cnt++;
			if (cnt == k - 1) {
				d1 = i;
			}
			if (cnt == k) {
				d2 = i;
				for (int j = 0; j <= d1; j++) {
					cout << a[j];
				}
				cout << a[d2];
				for (int j = d1 + 1; j < (); j++) {
					if (j != d2) {
						cout << a[j];
					}
				}
				return 0;
			}
		}
	}
}

D - Strange Mirroring

Using the idea of symmetry, observe which character the first $ k $ character of each query is evolved from, and record the number of evolutions, according to the number of times to determine the case of the character. It can be observed that each time we can symmetrically make the length of the string reduced by $ n * 2 ^ x $ length, the length of the binary search, shortened many times we will be able to find the original character.

Time complexity: $ O(q*log k) $.

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int n, m, k;
int a[N];
int b[100];
signed main() {
	string s;
	cin >> s;
	n = ();
	s = ' ' + s;
	int q;
	cin >> q;
	vector<int> a;
	int x = n;
	for (int i = 1; i <= 64; i++) {
		if (x > 1e18) break;
		a.push_back(x);
		x *= 2;
	}
	for (int i = 0; i < (); i++) {
		b[i + 1] = a[i];
	}
	while (q--) {
		cin >> k;
		int d = 0;
		int cnt = 0;
		for (int i = 1; i <= 100; i++) {
			int x = lower_bound(b + 1, b + 1 + (int)(), k) - b;
			x -= 1;
			k -= b[x];
//			cout << x << '\n';
			if (b[x] != 0)
				cnt++;
			if (k <= n) break;
		}
//		cout << k << '\n';
//		cout << cnt << '\n';
		char x = s[k];
		if (cnt % 2 == 0) {
			cout << x << ' ';
		} else {
			if (x >= 'a' && x <= 'z')
				cout << (char)(x - 32) << ' ';
			else
				cout << (char)(x + 32) << ' ';
		}
//		cout << '\n';

	}
}

E - 1D Bucket Tool

Use two $ set $ maintenance $ l $ and $ r $ interval, with $ set<pair<int,int>> $ maintenance of the existence of the color of the interval, the use of $ map<pair<int,int>> $ maintenance of the color of the color interval. Each modification through the bisection to find out $ x $ classification discussion where the interval segment and then according to the left and right interval segment color $ c $, to determine whether the need to merge the interval, each time the modification is completed to update the value of the $ sum $ array, and constantly maintain the value of the $ stl $ container, pay attention to do not appear to cross the line of the problem, the idea is not chaotic to maintain a good can be.

That was a good $ set $ exercise.

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int n, m, k;
int sum[N];
set<pair<int, int>> col;
set<int> sel, ser;
map<pair<int, int>, int> mp;
void work(pair<int, int> l, pair<int, int> b, pair<int, int> r, int x) {
	(), (), ();
	(), (), ();
	int a = mp[ {, }], bt = x, c = mp[ {, }];
	int be = mp[ {, }];
	sum[be] -=  -  + 1;
	sum[x] +=  -  + 1;
	if (a == bt && bt == c) {
		pair<int, int> f = {, };
		(b), (l), (r);
		mp[f] = x;
		(l), (r), (b);
		(f);
		(), ();
	} else if (a == bt) {
		pair<int, int> f = {, };
		(b), (l);
		(b), (l);
		mp[f] = x;
		(f);
		(), ();
		(), ();
	} else if (bt == c) {
		pair<int, int> f = {, };
		(b), (r);
		(b), (r);
		mp[f] = x;
		(f);
		(), ();
		(), ();
	} else {
		pair<int, int> f = {, };
		mp[f] = x;
		(), ();
		(), ();
		(), ();
	}
}
signed main() {
	ios::sync_with_stdio(0), (0), (0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		(i), (i);
		({i, i});
		mp[ {i, i}] = i;
		sum[i] += 1;
	}
	while (m--) {
		int op, x, c;
		cin >> op;
		if (op == 1) {
			cin >> x >> c;
			auto it1 = sel.lower_bound(x);
			auto it2 = ser.lower_bound(x);
			if (it1 == ()) {
				it1 = --();
			}
			if (*it1 > x) it1--;
			auto it3 = col.lower_bound({*it1, *it2});
			pair<int, int> b = *it3, l, r;
			pair<int, int> ed = *--(), be = *();
			if (b != ed) {
				it3++;
				auto t = col.lower_bound(*it3);
				it3--;
				r = *t;
			}
			if (b != be) {
				it3--;
				auto t = col.lower_bound(*it3);
				it3++;
				l = *t;
			}
			work(l, b, r, c);
		} else {
			cin >> c;
			cout << sum[c] << '\n';
		}
	}
}