Educational Codeforces Round 75


Contest Info


[Practice Link](https://codeforces.com/contest/1251)
Solved A B C D E1 E2 F
6/7 O O O O O O -
  • O 在比賽中通過
  • Ø 賽后通過
  • ! 嘗試了但是失敗了
  • - 沒有嘗試

Solutions


A. Broken Keyboard

題意:
有一個打字機,如果某個按鍵是好的,那么按下那個按鍵之后會在打字槽中追加一個該字符,如果是壞的則會追加兩個。
現在給出打印槽中最后的結果,問有哪些按鍵能確定一定是好的。

思路:
將連續的相同字符取出來,如果長度為奇數,那么一定能確定該字符對應的按鍵是好的。

代碼:

view code
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n" 
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long; 
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }  
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0) 
void err() { cout << "\033[39;0m" << endl; } 
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A> 
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
inline void pt() { cout << endl; } 
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg << ' '; pt(args...); }
template <template<typename...> class T, typename t, typename... A> 
void pt(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; pt(args...); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 1e5 + 10;
int n, cnt[30]; string s;
void run() {
	cin >> s;
	memset(cnt, -1, sizeof cnt);
	for (int i = 0, len = s.size(), num = 0; i <= len; ++i) {
		if (i == len) {
			if (num & 1) {
				cnt[s[i - 1] - 'a'] = 1;
			}
		} else if (i && s[i] != s[i - 1]) {
			if (num & 1) {
				cnt[s[i - 1] - 'a'] = 1; 
			}
			num = 0;
		}
		++num;
	}
	for (int i = 0; i < 26; ++i) if (cnt[i] > 0) 
		cout << char(i + 'a');
	cout << endl;
	
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = rd();
	while (_T--) run();
	return 0;
}

B. Binary Palindromes

題意:
給出\(n\)\(01\)串,可以任意交換任意兩個字符串的任意兩個位置的字符,問最終最多能有多少回文串。

思路:
任意交換,只需要考慮\(0\)有多少個,\(1\)有多少個,然后根據原串長度貪心構造。

代碼:

view code
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n" 
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long; 
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }  
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0) 
void err() { cout << "\033[39;0m" << endl; } 
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A> 
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
inline void pt() { cout << endl; } 
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg << ' '; pt(args...); }
template <template<typename...> class T, typename t, typename... A> 
void pt(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; pt(args...); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 1e5 + 10;
int n, cnt[2], len[110]; 
void run() {
	n = rd();
	cnt[0] = cnt[1] = 0;
	for (int i = 1; i <= n; ++i) {
		string s; cin >> s;
		len[i] = s.size();
		for (auto &c : s) ++cnt[c - '0'];
	}
	sort(len + 1, len + 1 + n);
	int res = 0;
	for (int i = 1; i <= n; ++i) {
		if (len[i] & 1) {
			if (cnt[0] & 1) {
				--cnt[0];
			} else if (cnt[1] & 1) {
				--cnt[1];
			} else if (cnt[0]) {
				--cnt[0];
			} else if (cnt[1]) {
				--cnt[1];
			} else {
				break;
			}
			--len[i];
		}
		if (len[i] > cnt[0]) {
			if (cnt[0] & 1) {
				len[i] = len[i] - cnt[0] + 1;
				cnt[0] = 1;
			} else {
				len[i] -= cnt[0];
				cnt[0] = 0;
			}
		} else {
			cnt[0] -= len[i];
			len[i] = 0;
		}
		if (len[i] > cnt[1]) {
			if (cnt[1] & 1) {
				len[i] = len[i] - cnt[1] + 1;
				cnt[1] = 1;
			} else {
				len[i] -= cnt[1];
				cnt[1] = 0;
			}
		} else {
			cnt[1] -= len[i];
			len[i] = 0;
		}
		if (len[i]) break;
		++res;
	}
	pt(res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = rd();
	while (_T--) run();
	return 0;
}

C. Minimize The Integer

題意:
給出一個\(n\)位的整數,可能有前導\(0\),現在可以任意交換兩個相鄰的並且奇偶性不同的數的位置,最終結果也可以存在前導\(0\),問最終結果的最小值是多少。

思路:
顯然,所有奇偶性相同的數的相對位置不變,那么將奇數取出來,偶數取出來,然后貪心取頭即可。

代碼:

view code
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n" 
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long; 
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }  
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0) 
void err() { cout << "\033[39;0m" << endl; } 
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A> 
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
inline void pt() { cout << endl; } 
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg << ' '; pt(args...); }
template <template<typename...> class T, typename t, typename... A> 
void pt(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; pt(args...); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 3e5 + 10;
int n; char s[N]; 
void out(vector <int> &vec) {
	cout << vec.back();
	vec.pop_back();
}
void run() {
	vector <int> vec[2];
	cin >> (s + 1);
	for (int i = 1; s[i]; ++i) {
		int num = s[i] - '0';
		vec[num & 1].push_back(num);
	}
	reverse(vec[0].begin(), vec[0].end());
	reverse(vec[1].begin(), vec[1].end());
	while (!vec[0].empty() || !vec[1].empty()) {
		if (vec[0].empty()) {
			out(vec[1]);
		} else if (vec[1].empty()) {
			out(vec[0]);
		} else {
			if (vec[0].back() < vec[1].back()) {
				out(vec[0]);
			} else {
				out(vec[1]);
			}
		}
	}
	cout << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T; cin >> _T;
	while (_T--) run();
	return 0;
}

D. Salary Changing

題意:
\(n\)個人,你是老板,你手里有\(s\)元錢,要給這\(n\)個人發工資,每個人工資的范圍是\([l_i, r_i]\),要如何發工資使得你的錢夠用並且\(n\)個人工資的中位數最高。

思路:
首先給每個人發\(l_i\)工資,那么得到一個答案的下界,那么發現這個下界到\(INF\)這個范圍,答案具有單調性,二分然后貪心\(check\)即可。

代碼:

view code
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n" 
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long; 
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }  
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0) 
void err() { cout << "\033[39;0m" << endl; } 
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A> 
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
inline void pt() { cout << endl; } 
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg << ' '; pt(args...); }
template <template<typename...> class T, typename t, typename... A> 
void pt(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; pt(args...); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 2e5 + 10;
int n; ll s;
pII a[N];
bool check(ll x) {
	int l = 0, r = 0;
	ll remind = s;
	for (int i = 1; i <= n; ++i) {
		if (a[i].se < x) {
			remind -= a[i].fi;
			++l;
		} else if (a[i].fi > x) {
			remind -= a[i].fi;
			++r;
		} 
	}
	if (l > n / 2 || r > n / 2) return false;
	for (int i = 1; i <= n; ++i) {
		if (a[i].fi <= x && a[i].se >= x) {
			if (l < n / 2) {
				++l;
				remind -= a[i].fi;
			} else {
				remind -= x;
			}
		}
	}
	return remind >= 0;
}
void run() {
	cin >> n >> s;
	for (int i = 1; i <= n; ++i) a[i].fi = rd(), a[i].se = rd();
	sort(a + 1, a + 1 + n);
	ll l = a[n / 2 + 1].fi, r = 1e9, res = l;
	while (r - l >= 0) {
		ll mid = (l + r) >> 1;
		if (check(mid)) {
			res = mid;
			l = mid + 1;
		} else {
			r = mid - 1;
		}
	}
	pt(res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = rd();
	while (_T--) run();
	return 0;
}

E2. Voting (Hard Version)

題意:
\(n\)個人,你可以花費\(p_i\)讓第\(i\)個人投票,或者拉夠\(m_i\)個人為你投票,這個人就會為你投票。
現在你想讓所有人都為你投票,需要花費的最小代價是多少?

思路:
假設我知道有\(x\)個人不需要花費代價能讓他們為我免費投票,那么我假設剛開始對每個人都付了錢讓他們投票,現在就是要去除\(x\)個人的花費,使得去除的花費最大。
那么我們從\(i \in [x, n]\)掃一遍,用一個大根堆維護\(m_i < i\)的所有人的最大\(p_i\),每次取出堆頂的\(p_i\)即可。
並且容易發現\(x\)具有單調性,直接二分即可。

代碼:

view code
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define endl "\n" 
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long; 
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }  
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0) 
void err() { cout << "\033[39;0m" << endl; } 
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A> 
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
inline void pt() { cout << endl; } 
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg << ' '; pt(args...); }
template <template<typename...> class T, typename t, typename... A> 
void pt(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; pt(args...); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 2e5 + 10;
int n; vector <vector<int>> vec;
ll gao(int x) {
	ll tot = 0;
	priority_queue <int, vector<int>, less<int>> pq;
	for (int i = 1; i <= n; ++i) {
		for (auto &it : vec[i]) pq.push(it);
		if (i >= x) {
			if (pq.empty()) return 0;
			tot += pq.top(); pq.pop();
		}
	}
	return tot;
}
void run() {
	n = rd();
	vec.clear(); vec.resize(n + 1);
	ll tot = 0;
	for (int i = 1, m, p; i <= n; ++i) {
		m = rd(); p = rd();
		vec[m + 1].push_back(p);
		tot += p;
	}
	int l = 1, r = n; ll Max = 0;
	while (r - l >= 0) {
		int mid = (l + r) >> 1;
		ll tmp = gao(mid);
		chmax(Max, tmp);
		//dbg(l, r, mid, Max);
		if (tmp > 0) {
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	pt(tot - Max);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = rd();
	while (_T--) run();
	return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM