Codeforces Round #593 (Div. 2)

Contest Info


[Practice Link](https://codeforces.com/contest/1236)
Solved A B C D E F
5/6 O O O O Ø -
  • O 在比赛中通过
  • Ø 赛后通过
  • ! 尝试了但是失败了
  • - 没有尝试

Solutions


A. Stones

签到。

代码:

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 a, b, c; 
void run() {
	cin >> a >> b >> c;
	int res = 0;
	int x = min(b, c / 2);
	res += 3 * x;
	b -= x;
	res += min(a, b / 2) * 3;
	pt(res);
}

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;
}

B. Alice and the List of Presents

题意:
\(n\)种数,有\(m\)个集合,需要给\(m\)个集合放置一些数,使得每一种数至少要在一个集合中出现过。

思路:
考虑针对每个数考虑,一个数在\(m\)个集合中出现或不出现的方案数\(2^m\),然后减去全都不出现的情况即可。

代码:

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 ll 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;
ll n, m; 
void run() {
	pt(qpow((qpow(2, m) - 1 + mod) % mod, n));
}

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

C. Labs

题意:
\(n^2\)个实验室,标号大的能向标号小的流水,现在要将他们分组,组内的实验室可以互相流通,组与组之间的流量定义为\(f(X, Y)\),表示有多少对\((i, j)\)使得\(i \in X, j \in Y\)并且\(i > j\)
现在要将\(n^2\)个实验室分成\(n\)组,使得最小的\(f(X, Y)\)最大。

思路:
类似于这样:

1 6 7
2 5 8
3 4 9

代码:

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 = 5e2 + 10;
int n, a[N][N]; 
void run() {
	for (int j = 1; j <= n; ++j) {
		int id = (j - 1) * n;
		if (j & 1) {
			for (int i = 1; i <= n; ++i)
				a[i][j] = ++id;
		} else {
			for (int i = n; i >= 1; --i) {
				a[i][j] = ++id;
			}
		}
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			cout << a[i][j] << " \n"[j == n];		
		}
	}
}

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

D. Alice and the Doll

题意:
在一个\(n \cdot m\)的矩形中,有\(k\)个障碍物,现在从\((1, 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, m, k;
set <int> row[N], col[N]; 
bool del(int up, int down, int left, int right) {
	for (int i = up; i <= down; ++i) {
		for (int j = left; j <= right; ++j) {
			if (row[i].find(j) == row[i].end() || col[j].find(i) == col[j].end()) {
				return false;
			} else {
				row[i].erase(j);
				col[j].erase(i);
				--k; 
			}
		}
	}
	return true;
}
//dir
//0 right
//1 down
//2 left
//3 up
void run() {
	for (int i = 1; i <= n; ++i) row[i].clear();
	for (int i = 1; i <= m; ++i) col[i].clear();
	for (int i = 1, x, y; i <= k; ++i) {
		cin >> x >> y;
		row[x].insert(y);
		col[y].insert(x);
	}
	if (!k) return pt("Yes");
	int up = 1, down = n, left = 1, right = m; 
	int x = 1, y = 1, dir = 0;
	bool F = 0;
	while (k) {
	//	dbg(up, down, left, right, dir, x, y); 
		if (dir == 0) { // right
			auto it = row[x].begin(); 
			if (it == row[x].end()) { 
				y = right; 
			} else {
				int pos = *it; 
				if (!del(up, down, pos, right)) {
					return pt("No");
				}
				right = pos - 1; 
				y = pos - 1; 
			}
			left += F;
		} else if (dir == 1) { // down
			auto it = col[y].begin();
			if (it == col[y].end()) { 
				x = down;
			} else {
				int pos = *it;
				if (!del(pos, down, left, right)) {
					return pt("No");
				}
				down = pos - 1;
				x = pos - 1;
			}
			up += F;
		} else if (dir == 2) { // left
			auto it = row[x].end();
			if (it != row[x].begin()) {
				--it;
				int pos = *it;
				if (!del(up, down, left, pos)) {
					return pt("No");
				}
				left = pos + 1;
				y = pos + 1;
			} else {
				y = left;
			}
			right -= F;
		} else { // up
			auto it = col[y].end();
			if (it != col[y].begin()) {
				--it;
				int pos = *it;
				if (!del(up, pos, left, right)) {
					return pt("No");
				}
				up = pos + 1;
				x = pos + 1;
			} else {
				x = up;
			}
			down -= F;
		}
		dir = (dir + 1) % 4;
		F = 1;
	}
	pt("Yes");
}

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

E. Alice and the Unfair Game

题意:
在一个一维数轴上,长度为\(n\),在第\(i\)秒第\(a_i\)个位置会有怪物出现,定义一个二元组\((x, y)\)表示刚开始从\(x\)出发,最后能停留在\(y\)是否可行。
问有多少个这样的二元组可行。

思路:
猜测对于一个起点\(x\)来说,它可行的\(y\)的是一段范围,那么我们只需要找到最远的\(y\)即可。
我们将它放在二维坐标系上,一维是时间,一维是位置。
刚开始从\((0, x)\)出发,要到\((m, y)\),并且只能向右上、右、右下走,并且不能走到障碍物。
并且我们只需要关心最远能走到的地方,那么以右上为例,能右上走就右上,否则往右。
那么发现跟线段的斜率有关,贪心\(dp\)即可。

代码:

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 = 1e6 + 10, OFFSET = 2e5;
int n, m, a[N], l[N], r[N], f[N], last[N];
//y - x
void gaol() {
	memset(last, -1, sizeof last);
	for (int i = m; i >= 1; --i) {
		int p = a[i] + 1 + i;
		if (last[p] == -1) f[i] = max(1, a[i] - (m - i)); 
		else f[i] = f[last[p]];
		last[a[i] + i] = i;
	}
	for (int i = 1; i <= n; ++i) {
		int p = i;
//		dbg(i, p, last[p]);
		if (last[p] == -1) l[i] = max(1, i - (m + 1));
		else l[i] = f[last[p]];
	}
}
void gaor() {
	memset(last, -1, sizeof last);
	for (int i = m; i >= 1; --i) {
		int p = a[i] - 1 - i + OFFSET; 
		if (last[p] == -1) f[i] = min(n, a[i] + m - i); 
		else f[i] = f[last[p]];
		last[a[i] - i + OFFSET] = i;
	}
	for (int i = 1; i <= n; ++i) {
		int p = i + OFFSET;
		if (last[p] == -1) r[i] = min(n, i + m + 1);
		else r[i] = f[last[p]];
	}
}
void run() {
	for (int i = 1; i <= m; ++i) a[i] = rd();
	if (n == 1) return pt(0);
	gaol();
   	gaor();
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
	//	dbg(i, l[i], r[i]);
		ans += r[i] - l[i] + 1;
	}
	pt(ans);	
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	while (cin >> n >> m) run();
	return 0;
}
posted @ 2019-10-22 08:58  Dup4  阅读(539)  评论(1编辑  收藏  举报