算法模板(持续更新)
板子
1234567891011121314151617181920212223242526272829303132333435363738394041#include <bits/stdc++.h>#define x first#define y second#define endl '\n'using namespace std;using ll = long long;using i128 = __int128; using PII = pair<int, int>;using PLI = pair<ll, int>;using PIL = pair<int, ll>;using PLL = pair<ll, ll>; constexpr int mod = 1e9 + 7;// constexpr int mod = 998244353;i128 read(){i128 x = 0, f = 1;char ch = getchar();while (ch < '0 ...
牛客周赛 Round 100 题解
A. 小红的双排列
签到,\(1-n\)
每个数输出两次即可。
code:
1234567891011121314151617181920212223242526272829303132333435363738#include <bits/stdc++.h>#define x first#define y second#define endl '\n'using namespace std;using ll = long long;using i128 = __int128; using PII = pair<int, int>;i128 read(){i128 x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if (ch == '-') f = -1;ch = getchar();}while(ch >= '0' && ch ...
牛客周赛 Round 99 题解
非常简单的一场周赛,可惜 \(D\)
读错题浪费一个多小时导致赛后 \(3\)
分钟才过 \(F\)。
A. Round 99
签到
code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849#include <bits/stdc++.h>#define x first#define y second#define endl '\n'using namespace std;using ll = long long;using i128 = __int128; using PII = pair<int, int>;i128 read(){i128 x = 0, f = 1;char ch = getchar();while (ch < '0' || ch > '9'){if (ch == '-') f = -1;ch = g ...
AtCoder Beginner Contest 413 题解
A. Content Too Large
签到,计算出来物品体积的总和,与袋子的大小做对比即可。
code:
1234567891011121314151617181920212223242526272829303132333435363738394041424344#include <bits/stdc++.h>#define x first#define y second#define endl '\n'using namespace std;using ll = long long;using PII = pair<int, int>;int read(){int x = 0, f = 1;char ch = getchar();while (ch < '0' || ch > '9'){if (ch == '-') f = -1;ch = getchar();}while (ch >= '0' &&am ...
牛客小白月赛119题解
A. 来!选!队!长
签到题。
由于队长的战力要乘以二,因此‘我’的队伍最大战力为 \(r1=a_1 \times
2+a_2+a_3+a_4+a_5\),好友队伍的最小战力为 \(r2=b_1+b_2+b_3+b_4+b_5 \times 2\)。
题目问是否有可能使得‘我’的队伍战力大于好友队伍战力,因此只需判断
\(r1 > r2\)
这个条件是否成立即可。
code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849#include <bits/stdc++.h>#define x first#define y second#define endl '\n'using namespace std;using ll = long long;using PII = pair<int, int>;int read(){int x = 0, f = 1;char ch = getchar();whi ...
牛客周赛 Round 75 题解
A. 小红的正整数计数
签到
code:
12345678910111213141516171819#include <bits/stdc++.h>using namespace std;int l, r;int main(){ cin >> l >> r; int res = 0; for (int i = l; i <= r; i++) if (i % 2 == 0) res++; cout << res << endl; return 0;}
B. 小红的双生串
分别统计前半段和后半段出现最多的字符,再把整个字符串修改成只有这两个字符。
code:
12345678910111213141516171819202122232425#include <bits/stdc++.h>using namespace std;string s;int main(){ cin >> ...
Codeforces Round 996 (Div. 2) 题解
A. Two Frogs
当两个青蛙之间的距离为 \(1\)
时,此时先跳的青蛙最终会被逼到角落。
不难发现,两个青蛙跳跃的次数相同时,它们之间距离的奇偶性是不变的。因此,若两青蛙初始距离为奇数,则它们距离为
\(1\) 时 \(Alice\) 先跳。若两青蛙初始距离为偶数,此时
\(Alice\),先跳一次,情况转变成 \(Bob\)
先手,并且它们之间的距离为奇数,所以当距离为 \(1\),时,\(Bob\) 先跳。
code:
123456789101112131415161718192021222324252627282930313233#include <bits/stdc++.h>#define int long long#define x first#define y second#define endl '\n'using namespace std;typedef pair<int, int> PII;const int N = 200010;int T;int n, a, b;signed main(){ ...
牛客周赛 Round 76 题解
A. 小红出题
签到
code:
12345678910111213141516171819#include <bits/stdc++.h>using namespace std;int n;int main(){ cin >> n; int res = 0; for (int i = 1; i <= n; i++) if (i % 7 != 6 && i % 7) res += 3; cout << res << endl; return 0;}
B. 串串香
一个字符串中出现次数最多的子串长度一定是 \(1\),因此只需要统计出现次数最多的字符即可。
code:
123456789101112131415161718192021222324#include <bits/stdc++.h>using namespace std;int n;string s;int main(){ ...
AtCoder Beginner Contest 388 题解
A. ?UPC
签到
code:
1234567891011121314#include <bits/stdc++.h>using namespace std;string s;int main(){ cin >> s; cout << s[0] << "UPC" << endl; return 0;}
B. Heavy Snake
暴力枚举
code:
1234567891011121314151617181920212223#include <bits/stdc++.h>using namespace std;const int N = 110;int n, d;int t[N], l[N];int main(){ cin >> n >> d; for (int i = 0; i < n; i++) cin >> t[i] >> l[i]; for (int i = 1; ...
Educational Codeforces Round 171 (Rated for Div. 2) 题解
A. Perpendicular Segments
不难发现, \(A,B,C,D\)
可以当作正方形的四个顶点,\(len(AB)=len(CD)=\)
正方形对角线长度,因此只要在给定区间找到最大正方形即可。
code:
1234567891011121314151617181920212223242526272829303132333435#include <bits/stdc++.h>#define x first#define y second#define int long long#define endl '\n'using namespace std;typedef pair<int, int> PII;const int N = 200010;int T;int x, y, k;void solve(){ cin >> x >> y >> k; int d = min(x, y); cout << "0 0 " << ...