AtCoder Beginner Contest 132
AtCoder Beginner Contest 132
https://atcoder.jp/contests/abc132
持续被暴打的一天,因为晚上要打cf,所以明天再来写总结。悲
ct就是菜鸟newbie😫
A - Fifty-Fifty
#include <bits/stdc++.h>
using namespace std;
int main () {
string s;
cin >> s;
map<char, int> mp;
int cnt = 0;
for (auto i : s) mp[i]++;
for (auto i : mp) {
if (i.second != 2) {
puts ("No");
return 0;
}
cnt ++;
}
if (cnt != 2) puts ("No");
else puts ("Yes");
}
B - Ordinary Number
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int a[N], n, ans;
int main () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 2; i < n; i++) {
int maxn = max ({a[i], a[i-1], a[i+1]}), minn = min ({a[i], a[i-1], a[i+1]});
if (a[i] != maxn && a[i] != minn) ans ++;
}
cout << ans;
}
C - Divide the Problems
二分
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int a[N], n, ans, minn = 1e9, maxn;
signed main () {
cin >> n;
map<int, int> mp;
for (int i = 0; i < n; i++) {
cin >> a[i];
minn = min (minn, a[i]), maxn = max (maxn, a[i]);
}
sort (a, a + n);
for (int i = minn; i <= maxn; i++) {
int pos = lower_bound (a, a + n, i) - a;
//cout << i << ' ' << pos << endl;
if (pos * 2 == n) ans ++;
}
cout << ans;
}
D - Blue and Red Balls
组合计数
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2005, p = 1e9 + 7;
int fact[N], infact[N], n, k;
int qmi(int a, int k) {
int ans = 1;
while (k) {
if (k & 1) ans = ans * a % p;
k >>= 1;
a = a * a % p;
}
return ans;
}
void init() {
fact[0] = infact[0] = 1;
for (int i = 1; i <= N - 3; i++) {
fact[i] = fact[i - 1] * i % p;
infact[i] = infact[i - 1] * qmi (i, p - 2) % p;
}
}
int C(int a, int b) {
if (a < b) return 0;
return fact[a] * infact[a - b] % p * infact[b] % p;
}
signed main () {
init ();
cin >> n >> k;
for (int i = 1; i <= k; i++) {
cout << (C(k - 1, i - 1) * C (n - k + 1, i)) % p << endl;
}
}
E - Hopscotch Addict
建分层图。
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + 5, M = N * 3;
int dis[M], n, m, st, ed;
int h[M], e[M], ne[M], idx;
bool vis[M];
void add (int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int dijkstra () {
priority_queue <pii, vector<pii>, greater<pii>>q;
memset (dis, 0x3f, sizeof dis);
dis[st] = 0;
q.push ({0, st});
while (!q.empty()) {
auto t = q.top();
q.pop();
int ver = t.second, dist = t.first;
if (vis[ver]) continue;
vis[ver] = true;
for (int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i];
if (dis[j] > dist + 1) {
dis[j] = dist + 1;
q.push ({dis[j], j});
}
}
}
if (dis[ed] == 0x3f3f3f3f) return -1;
return dis[ed] / 3;
}
int main () {
memset (h, -1, sizeof h);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b; //建分层图
add (a, b + n), add (a + n, b + n * 2), add (a + n * 2, b);
}
cin >> st >> ed;
cout << dijkstra () << endl;
}
F - Small Products
整除分块优化dp
相似题目:https://www.luogu.com.cn/problem/P2261
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int M = 1e5 + 5, mod = 1e9 + 7;
ll n, k, m, f[M], tmp[M], a[M];
int main () {
//cout << sqrt (1e9);
cin >> n >> k;
ll l = 1, r;
while (l <= n) {
if (n / l == 0) r = n;
else r = n / (n / l);
a[++m] = r - l + 1;
l = r + 1;
}
for (int i = 1; i <= m; i++) f[i] = 1;
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= m; j++) (tmp[j] = f[m-j+1] * a[j] % mod) %= mod;
for (int j = 1; j <= m; j++) (tmp[j] += tmp[j-1]) %= mod, f[j] = tmp[j];
}
cout << f[m] << endl;
}
//整除分块优化dp -> 根号n
//f[i][j]: i上放j, 滚掉一维