2020 牛客国庆集训派对 Day6
Contents
- Contest Info
- Solutions
Contest Info
Solved | A | B | C | D | E | F | G | H | I | J | K |
---|---|---|---|---|---|---|---|---|---|---|---|
6/11 | O | O | - | - | O | O | - | - | O | Ø | - |
- O 在比赛中通过
- Ø 赛后通过
- ! 尝试了但是失败了
- - 没有尝试
Solutions
Problem A. Fractions
Solved By Dup4 & lts. 01:38(+)
题意
给出
, 并且有 。 。 。
思路
- 大力猜测两个分数就够了。
- 然后枚举出两个分母,做
exgcd
即可。
Code
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
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, int Mod = mod) {
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 nextInt() {
int x;
cin >> x;
return x;
}
void rd() {}
template <class T, class... Ts>
void rd(T &arg, Ts &...args) {
cin >> arg;
rd(args...);
}
#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...);
}
void ptt() {
cout << endl;
}
template <class T, class... Ts>
void ptt(const T &arg, const Ts &...args) {
cout << ' ' << arg;
ptt(args...);
}
template <class T, class... Ts>
void pt(const T &arg, const Ts &...args) {
cout << arg;
ptt(args...);
}
void pt() {}
template <template <typename...> class T, typename t, typename... A>
void pt(const T<t> &arg, const A &...args) {
for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1];
pt(args...);
}
inline ll qpow(ll base, ll n) {
assert(n >= 0);
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;
ll ex_gcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = ex_gcd(b, a % b, x, y);
ll temp = x;
x = y;
y = temp - a / b * y;
return d;
}
bool liEu(ll a, ll b, ll c, ll &x, ll &y) {
ll d = ex_gcd(a, b, x, y);
if (c % d != 0)
return 0;
ll k = c / d;
x *= k;
y *= k;
return 1;
}
void run() {
rd(n);
vector<int> fac;
for (int i = 1; 1ll * i * i <= n; ++i) {
if (n % i == 0) {
if (i != 1 && i != n)
fac.push_back(i);
if (n / i != 1 && n / i != n)
fac.push_back(n / i);
}
}
sort(all(fac));
fac.erase(unique(all(fac)), fac.end());
int m = SZ(fac);
for (int i = 0; i < m; ++i) {
if ((n - 1) % (n / fac[i]) == 0) {
cout << "YES\n";
pt(1);
pt((n - 1) / (n / fac[i]), fac[i]);
return;
}
for (int j = i + 1; j < m; ++j) {
ll a = n / fac[i];
ll b = n / fac[j];
ll c = n - 1;
ll x, y;
ll g = __gcd(a, b);
ll t;
if (liEu(a, b, c, x, y)) {
// if (x < 1) {
t = b / g;
x = (x % t + t) % t;
// }
// if (y < 1) {
t = a / g;
y = (y % t + t) % t;
// }
cout << "YES\n";
pt(2);
pt(x, fac[i]);
pt(y, fac[j]);
return;
}
}
}
cout << "NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(20);
int _T = 1;
// nextInt();
while (_T--) run();
// for (int kase = 1; kase <= _T; ++kase) {
// cout << "Case #" << kase << ": ";
// run();
// }
// while (cin >> n) run();
// run();
return 0;
}
Problem B. Guest Student
Solved By Dup4. 00:31(+1)
题意
给出一周七天,哪些天有课,然后给出
思路
枚举第一周和最后一周的开始位置和结束位置。
Code
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
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, int Mod = mod) {
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 nextInt() {
int x;
cin >> x;
return x;
}
void rd() {}
template <class T, class... Ts>
void rd(T &arg, Ts &...args) {
cin >> arg;
rd(args...);
}
#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...);
}
void ptt() {
cout << endl;
}
template <class T, class... Ts>
void ptt(const T &arg, const Ts &...args) {
cout << ' ' << arg;
ptt(args...);
}
template <class T, class... Ts>
void pt(const T &arg, const Ts &...args) {
cout << arg;
ptt(args...);
}
void pt() {}
template <template <typename...> class T, typename t, typename... A>
void pt(const T<t> &arg, const A &...args) {
for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1];
pt(args...);
}
inline ll qpow(ll base, ll n) {
assert(n >= 0);
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, a[10], f[10];
int ceil(int x, int y) {
return (x + y - 1) / y;
}
void run() {
rd(n);
f[0] = 0;
for (int i = 1; i <= 7; ++i) rd(a[i]), f[i] = f[i - 1] + a[i];
int sum = f[7];
int res = 7 * n;
if (n <= sum) {
for (int i = 1; i <= 7; ++i) {
for (int j = i; j <= 7; ++j) {
if (f[j] - f[i - 1] >= n) {
chmin(res, j - i + 1);
}
}
}
}
for (int i = 1; i <= 7; ++i) {
for (int j = 1; j <= 7; ++j) {
int need = n - f[j] - (sum - f[i - 1]);
int now = 7 - i + 1 + j;
now += ceil(need, sum) * 7;
// dbg(i, j, need, now);
chmin(res, now);
}
}
pt(res);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(20);
int _T = nextInt();
while (_T--) run();
// for (int kase = 1; kase <= _T; ++kase) {
// cout << "Case #" << kase << ": ";
// run();
// }
// while (cin >> n) run();
// run();
return 0;
}
Problem C. Harder Satisfiability
Upsolved By -.
题意
思路
Problem D. Interval-Free Permutations
Upsolved By -.
题意
思路
Problem E. King Kog's Reception
Solved By Dup4. 03:20(+)
题意
给出
+ t d
, 表示在第时刻有个骑士需要参见国王,参见所需事件为 。 - i
,表示取消第个事件。 ? t
,表示询问公主如果在第时刻来见国王,需要等多久。如果同一时刻,也有骑士在等,那么骑士优先。
思路
我们用
然后发现这个东西可以用线段树维护。
Code
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
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, int Mod = mod) {
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 nextInt() {
int x;
cin >> x;
return x;
}
void rd() {}
template <class T, class... Ts>
void rd(T &arg, Ts &...args) {
cin >> arg;
rd(args...);
}
#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...);
}
void ptt() {
cout << endl;
}
template <class T, class... Ts>
void ptt(const T &arg, const Ts &...args) {
cout << ' ' << arg;
ptt(args...);
}
template <class T, class... Ts>
void pt(const T &arg, const Ts &...args) {
cout << arg;
ptt(args...);
}
void pt() {}
template <template <typename...> class T, typename t, typename... A>
void pt(const T<t> &arg, const A &...args) {
for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1];
pt(args...);
}
inline ll qpow(ll base, ll n) {
assert(n >= 0);
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;
int q, t[N], d[N];
struct SEG {
struct node {
ll sum, Max;
node() {
sum = Max = 0;
}
node(ll sum, ll Max) : sum(sum), Max(Max) {}
node operator+(const node &other) const {
node res = node();
res.sum = sum + other.sum;
res.Max = max(other.Max, Max + other.sum);
return res;
}
} t[N << 2], res;
void build(int id, int l, int r) {
if (l == r) {
t[id] = node(0, l);
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
t[id] = t[id << 1] + t[id << 1 | 1];
}
void update(int id, int l, int r, int pos, ll v) {
if (l == r) {
t[id].sum += v;
t[id].Max += v;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
update(id << 1, l, mid, pos, v);
else
update(id << 1 | 1, mid + 1, r, pos, v);
t[id] = t[id << 1] + t[id << 1 | 1];
}
void query(int id, int l, int r, int ql, int qr) {
if (l >= ql && r <= qr) {
res = res + t[id];
return;
}
int mid = (l + r) >> 1;
if (ql <= mid)
query(id << 1, l, mid, ql, qr);
if (qr > mid)
query(id << 1 | 1, mid + 1, r, ql, qr);
}
} seg;
void run() {
rd(q);
char op[5];
int m = 1e6;
seg.build(1, 1, m);
for (int i = 1, _t, x; i <= q; ++i) {
cin >> op;
if (*op == '+') {
rd(t[i], d[i]);
seg.update(1, 1, m, t[i], d[i]);
} else if (*op == '-') {
rd(x);
seg.update(1, 1, m, t[x], -d[x]);
} else {
rd(_t);
seg.res = SEG::node();
seg.query(1, 1, m, 1, _t);
pt(seg.res.Max - _t);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(20);
int _T = 1;
// nextInt();
while (_T--) run();
// for (int kase = 1; kase <= _T; ++kase) {
// cout << "Case #" << kase << ": ";
// run();
// }
// while (cin >> n) run();
// run();
return 0;
}
Problem F. Lazyland
Solved By Dup4. 00:38(+)
题意
给出
思路
将每个任务不用劝说的人中,除了代价最高的那个人,其他人全都丢进最小堆中,然后贪心取出来去做其他任务。
Code
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
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, int Mod = mod) {
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 nextInt() {
int x;
cin >> x;
return x;
}
void rd() {}
template <class T, class... Ts>
void rd(T &arg, Ts &...args) {
cin >> arg;
rd(args...);
}
#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...);
}
void ptt() {
cout << endl;
}
template <class T, class... Ts>
void ptt(const T &arg, const Ts &...args) {
cout << ' ' << arg;
ptt(args...);
}
template <class T, class... Ts>
void pt(const T &arg, const Ts &...args) {
cout << arg;
ptt(args...);
}
void pt() {}
template <template <typename...> class T, typename t, typename... A>
void pt(const T<t> &arg, const A &...args) {
for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1];
pt(args...);
}
inline ll qpow(ll base, ll n) {
assert(n >= 0);
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, k, a[N], b[N], c[N];
void run() {
rd(n, k);
for (auto &arr : {a, b}) {
for (int i = 1; i <= n; ++i) {
rd(arr[i]);
}
}
memset(c, 0, sizeof c);
vector<pII> vec;
for (int i = 1; i <= n; ++i) {
++c[a[i]];
vec.push_back(pII(b[i], a[i]));
}
ll sum = 0;
int need = 0;
for (int i = 1; i <= k; ++i) need += c[i] == 0;
sort(all(vec), [&](pII a, pII b) {
return a.fi > b.fi;
});
while (need) {
pII back = vec.back();
vec.pop_back();
if (c[back.se] == 1)
continue;
else {
--need;
--c[back.se];
sum += back.fi;
}
}
pt(sum);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(20);
int _T = 1;
// nextInt();
while (_T--) run();
// for (int kase = 1; kase <= _T; ++kase) {
// cout << "Case #" << kase << ": ";
// run();
// }
// while (cin >> n) run();
// run();
return 0;
}
Problem G. Minegraphed
Upsolved By -.
题意
思路
Problem H. Archery Tournament
Upsolved By -.
题意
思路
Problem I. Box
Solved By groggy_. 01:09(+1)
题意
对于给出宽为
思路
长方体的构成方式可以看作两种,用不同边长尝试出各种宽与高的可能性,判断即可
Code
#include <algorithm>
#include <iostream>
#include <vector>
#define maxn 100005
using namespace std;
int w, h;
bool solve(int W, int H) {
return W <= w && H <= h;
}
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
scanf("%d%d", &w, &h);
int flag = 0;
flag = flag | solve((a + b) * 2, c + 2 * min(a, b));
flag = flag | solve((a + c) * 2, b + 2 * min(a, c));
flag = flag | solve((b + c) * 2, a + 2 * min(b, c));
flag = flag | solve(3 * b + a + c, c + a);
flag = flag | solve(3 * a + b + c, b + c);
flag = flag | solve(3 * c + a + b, a + b);
flag = flag | solve(c + 2 * min(a, b), (a + b) * 2);
flag = flag | solve(b + 2 * min(a, c), (a + c) * 2);
flag = flag | solve(a + 2 * min(b, c), (b + c) * 2);
flag = flag | solve(c + a, 3 * b + a + c);
flag = flag | solve(b + c, 3 * a + b + c);
flag = flag | solve(a + b, 3 * c + a + b);
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
Problem J. Connections
Upsolved By Dup4.
题意
给出
思路
随便找个点作为根,跑出一个 dfs
树,然后在反图上,还是以这个点为根,跑出一个 dfs
树即可。
显然,这两棵树所用的边不会超过
Code
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
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, int Mod = mod) {
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 nextInt() {
int x;
cin >> x;
return x;
}
void rd() {}
template <class T, class... Ts>
void rd(T &arg, Ts &...args) {
cin >> arg;
rd(args...);
}
#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...);
}
void ptt() {
cout << endl;
}
template <class T, class... Ts>
void ptt(const T &arg, const Ts &...args) {
cout << ' ' << arg;
ptt(args...);
}
template <class T, class... Ts>
void pt(const T &arg, const Ts &...args) {
cout << arg;
ptt(args...);
}
void pt() {}
template <template <typename...> class T, typename t, typename... A>
void pt(const T<t> &arg, const A &...args) {
for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1];
pt(args...);
}
inline ll qpow(ll base, ll n) {
assert(n >= 0);
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, vis[N], f[N];
pII e[N];
vector<vector<pII>> G, T;
void dfs(int u) {
for (auto &it : G[u]) {
int v = it.fi, id = it.se;
if (!vis[v]) {
f[id] = 1;
vis[v] = 1;
dfs(v);
}
}
}
void run() {
rd(n, m);
G.clear();
G.resize(n + 1);
T.clear();
T.resize(n + 1);
for (int i = 1, u, v; i <= m; ++i) {
rd(u, v);
e[i] = pII(u, v);
G[u].push_back(pII(v, i));
T[v].push_back(pII(u, i));
f[i] = 0;
}
memset(vis, 0, sizeof(vis[0]) * (n + 5));
dfs(1);
memset(vis, 0, sizeof(vis[0]) * (n + 5));
G = T;
dfs(1);
int tot = m - n * 2;
for (int i = 1; i <= m; ++i) {
if (f[i] == 0 && tot) {
--tot;
pt(e[i].fi, e[i].se);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(20);
int _T = nextInt();
while (_T--) run();
// for (int kase = 1; kase <= _T; ++kase) {
// cout << "Case #" << kase << ": ";
// run();
// }
// while (cin >> n) run();
// run();
return 0;
}
Problem K. The Final Level
Upsolved By -.
题意
思路
Last update: March 23, 2022
Created: March 23, 2022
Created: March 23, 2022