# 2020牛客暑期多校训练营（第三场）

## Contest Info

Solved A B C D E F G H I J K L
8 / 13 O O Ø Ø O - Ø - - - - O
• O 在比赛中通过
• Ø 赛后通过
• ! 尝试了但是失败了
• - 没有尝试

## Solutions

### A. Clam and Fish

Code
// Author : heyuhhh
// Created Time : 2020/07/18 12:23:24
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
void run() {
int n;
cin >> n;
string s;
cin >> s;
int ans = 0;
int p = 0;
int d = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
if (p > d) {
--p;
++ans;
} else if (d > 0) {
--d;
++ans;
}
} else if (s[i] == '1') {
if (p > d) {
//make it clam and use it later
++d;
} else {
++p;
}
} else {
++ans;
}
}
ans += d;
cout << ans << '\n';
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


### B. Classical String Problem

Code
// Author : heyuhhh
// Created Time : 2020/07/18 12:11:54
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
void run() {
string s; cin >> s;
int n = s.length();
int q; cin >> q;
int p = 0;
while (q--) {
string op;
int x;
cin >> op >> x;
if (op == "M") {
if (x > 0) {
p = (p + x) % n;
} else {
p = (p + x + n) % n;
}
} else {
int pos = (p + x - 1) % n;
cout << s[pos] << '\n';
}
}
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}


### C. Operation Love

Code
// Author : heyuhhh
// Created Time : 2020/07/18 21:11:26
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
void run() {
int n = 20;
vector<double> x(n + 1), y(n + 1);
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
}
x[n] = x[0], y[n] = y[0];
auto sq = [&] (double t) {
return t * t;
};
vector<double> d(n);
double tot = 0;
for (int i = 0; i < n; i++) {
d[i] = sqrt(sq(x[i + 1] - x[i]) + sq(y[i + 1] - y[i]) + 0.5);
tot += x[i] * y[i + 1] - x[i + 1] * y[i];
}
if (tot < 0) {
reverse(all(d));
}
int a, b;
for (int i = 0; i < n; i++) {
if ((int)(d[i] + 0.5) == 6) a = i;
if ((int)(d[i] + 0.5) == 8) b = i;
}
if ((a + 2) % n == b) {
cout << "right" << '\n';
} else {
cout << "left" << '\n';
}
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


### D. Points Construction Problem

$$n,m\leq 200$$

Code
// Author : heyuhhh
// Created Time : 2020/07/19 09:35:17
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
void err(int x) {cerr << x;}
void err(long long x) {cerr << x;}
void err(double x) {cerr << x;}
void err(char x) {cerr << '"' << x << '"';}
void err(const string &x) {cerr << '"' << x << '"';}
void _print() {cerr << "]\n";}
template<typename T, typename V>
void err(const pair<T, V> &x) {cerr << '{'; err(x.first); cerr << ','; err(x.second); cerr << '}';}
template<typename T>
void err(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), err(i); cerr << "}";}
template <typename T, typename... V>
void _print(T t, V... v) {err(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef Local
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
const int N = 1e5 + 5;
void run() {
int n, m;
cin >> n >> m;

int a, b;
int Min = INF;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
if (i * j >= n && (i - 1) * j < n && i * (j - 1) < n) {
if (2 * (i + j) < Min) {
Min = 2 * (i + j);
a = i, b = j;
}
if (2 * (i + j) == m) {
a = i, b = j;
goto nxt;
}
}
}
} nxt:;
if (m & 1 || m > 4 * n || m < 2 * (a + b)) {
cout << "No" << '\n';
return;
}
dbg(a, b);
vector<pii> ans;
int tot = 2 * (a + b);
int r = n;
for (int i = 1; i <= a && r; i++) {
for (int j = 1; j <= b && r; j++, --r) {
ans.push_back(MP(i, j));
}
}
vector<pii> res;
int x = 1000000, y = 100000000;
while (sz(ans) > 1 && m - tot >= 4) {
pii now = ans.back();
ans.pop_back();
if (now.se == 1 || now.fi == 1) {
tot += 2;
} else {
tot += 4;
}
res.push_back(MP(x, y));
x += 2;
}
assert(m - tot <= 2);
if (m - tot >= 2) {
pii now = ans.back();
ans.pop_back();
if (now.fi == 1 || now.se == 1) {
res.push_back(MP(x, y));
} else {
res.push_back(MP(now.fi - 1, b + 1));
}
}
cout << "Yes" << '\n';
for (auto it : res) {
cout << it.fi << ' ' << it.se << '\n';
}
for (auto it : ans) {
cout << it.fi << ' ' << it.se << '\n';
}
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


### E. Two Matchings

Code
// Author : heyuhhh
// Created Time : 2020/07/18 13:37:37
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
void run() {
int n;
cin >> n;
vector<ll> a(n);
ll ans = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(all(a));
for (int i = 1; i < n; i += 2) {
ans += a[i] - a[i - 1];
}
vector<ll> dp(n);
dp[0] = -a[0];
dp[2] = -a[0] - a[1] + a[2];
for (int i = 4; i < n - 1; i += 2) {
dp[i] = dp[i - 2] + a[i] - a[i - 1];
if (i < n - 2) {
dp[i] = min(dp[i], dp[i - 4] - a[i - 3] + a[i - 2] + a[i - 1] - a[i]);
}
}
ans += dp[n - 2] + a[n - 1];
cout << ans << '\n';
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


### F. Fraction Construction Problem

• $$\frac{c}{d}-\frac{e}{f}=\frac{a}{b}$$;
• $$d<b,f<b$$;
• $$1\leq c,e\leq 4\cdot 10^{12}$$.

$$g=gcd(a,b)$$，首先注意到如果$$g>1$$那么就win了，方案很容易构造。

Code
// Author : heyuhhh
// Created Time : 2020/07/18 15:52:18
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e6 + 5;
const ll MAX = 4e18;

int v[N], prime[N];
int num;
void Euler() {
v[1] = 1;
for(int i = 2; i < N; i++) {
if(v[i] == 0) {
v[i] = i;
prime[++num] = i;
}
for(int j = 1; j <= num && prime[j] * i < N; j++) {
v[prime[j] * i] = prime[j] ;
}
}
}

void exgcd(ll a, ll b, ll &x, ll &y) {
if(b == 0) {
x = 1, y = 0;
return ;
}
exgcd(b,a%b,x,y);
ll z = x ;
x = y;
y = z - y * (a / b);
}
//ax + by = c
ll calc(ll a, ll b, ll c) {
ll x, y;
ll g = __gcd(a, b);
assert(g == 1);
// g = 1
a /= g, b /= g, c /= g;
exgcd(a, b, x, y);
assert(x <= MAX / c);
// x *= c;
x = (x % b + b) % b;
if (x == 0) x += b;
x *= c;
return x;
}

void run() {
int a, b;
cin >> a >> b;
int g = __gcd(a, b);
if (g == 1) {
// c / d - e / f = a / b
// cf - ed = a
if (v[b] == b) {
cout << -1 << ' ' << -1 << ' ' << -1 << ' ' << -1 << '\n';
} else {
int f = 1;
int x = b;
while (x % v[b] == 0) {
x /= v[b];
f *= v[b];
}

if (x == 1) {
cout << -1 << ' ' << -1 << ' ' << -1 << ' ' << -1 << '\n';
return;
}

int d = b / f;
ll c = calc(f, d, a);
ll e = (c * f - a) / d;

if (e < 0) {
e = -e;
}

assert(e > 0 && c > 0 && c <= 1000000000000 && e <= 1000000000000);
cout << c << ' ' << d << ' ' << e << ' ' << f << '\n';
}
} else {
a /= g, b /= g;
int t = a / b;
int c = t + 1, d = 1;
ll e = b - a % b, f = b;
cout << c << ' ' << d << ' ' << e << ' ' << f << '\n';
}
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
Euler();
int T; cin >> T; while(T--)
run();
return 0;
}


### G. Operating on a Graph

Code
// Author : heyuhhh
// Created Time : 2020/07/20 09:41:10
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 8e5 + 5;

int n, m;
int f[N];
list<int> lists[N];
vector<int> G[N];

void init() {
for (int i = 0; i < n; i++) {
f[i] = i;
G[i].clear();
lists[i].clear();
lists[i].push_back(i);
}
}

int find(int x) {
return f[x] == x ? f[x] : f[x] = find(f[x]);
}

void Union(int x, int y) {
int fx = find(x), fy = find(y);
if (fx != fy) {
f[fy] = fx;
lists[fx].splice(lists[fx].end(), lists[fy]);
}
}

void run() {
cin >> n >> m;
init();
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
int q;
cin >> q;
while (q--) {
int o;
cin >> o;
if (find(o) != o) continue;
int size = sz(lists[o]);
for (int i = 0; i < size; i++) {
int u = lists[o].front();
for (auto v : G[u]) {
Union(o, v);
}
lists[o].pop_front();
}
}
for (int i = 0; i < n; i++) {
cout << find(i) << ' ';
}
cout << '\n';
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


### L. Problem L is the Only Lovely Problem

Code
// Author : heyuhhh
// Created Time : 2020/07/18 12:01:57
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
void run() {
string s;
cin >> s;
if (s.length() < 6) {
cout << "ugly" << '\n';
return;
}
for (int i = 0; i < 6; i++) {
if (s[i] >= 'A' && s[i] <= 'Z') {
s[i] = s[i] - 'A' + 'a';
}
}
string t = s.substr(0, 6);
if (t == "lovely") {
cout << "lovely" << '\n';
} else {
cout << "ugly" << '\n';
}
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}

posted @ 2020-07-20 10:55  heyuhhh  阅读(219)  评论(0编辑  收藏  举报