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