周报
周报2
写在前言
这周大概调整了下情绪,参加了三场牛客,打得有好有坏吧,有时总是心不在焉,不过也不想解释太多,归根结底是自己能力不够,赛后也进行了补题,大概每场补到了8题左右,以下是补题后写的题解
2024牛客寒假算法基础集训营4
A-柠檬可乐_2024牛客寒假算法基础集训营4 (nowcoder.com)
\(\mathcal{O}(1)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a,b,k;
cin >> a >> b >> k;
cout << (a >= k * b ? "good\n" : "bad\n");
return 0;
}
B-左右互博_2024牛客寒假算法基础集训营4 (nowcoder.com)
偶数堆是奇数时,讨厌鬼总可以获得胜利.
\(\mathcal{O}(n)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, ans = 0;
cin >> n;
vector<int> a(n);
for (auto &i : a) {
cin >> i;
if(i % 2 == 0) ans ++;
}
if(ans & 1){
cout << "gui\n";
}else{
cout << "sweet\n";
}
return 0;
}
C-冬眠_2024牛客寒假算法基础集训营4 (nowcoder.com)
范围小,直接暴力
\(\mathcal{O}(n^3)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, x, y;
cin >> n >> m >> x >> y;
vector<string> s(n);
for (auto &i : s) cin >> i;
int p, q;
cin >> p >> q;
vector<PII> ok(q);
for (auto &[x, y] : ok)
cin >> x >> y;
for (int i = 0; i < p; i ++) {
for (int j = 0; j < q; j ++) {
char c;
auto [a, b] = ok[j];
b--;
if (a == 1) {
c = s[b][m - 1];
for (int k = m - 2; k >= 0; k --)
s[b][k + 1] = s[b][k];
s[b][0] = c;
} else {
c = s[n - 1][b];
for (int k = n - 2; k >= 0; k --)
s[k + 1][b] = s[k][b];
s[0][b] = c;
}
}
}
cout << s[x - 1][y - 1] << '\n';
return 0;
}
D-守恒_2024牛客寒假算法基础集训营4 (nowcoder.com)
先求和,然后从1到平均数, 总和能够整除的数都可以作为约数,1需要特判
\(\mathcal{O}(n)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
i64 n, ans = 0;
cin >> n;
vector<int> a(n);
for (auto &i : a) {
cin >> i;
ans += i;
}
if(n == 1) {
cout << 1 << '\n';
return 0;
}
int num = 0;
for (int i = 1; i <= ans / n; i ++) {
if (ans % i == 0) {
num ++;
}
}
cout << num << '\n';
return 0;
}
E-漂亮数组_2024牛客寒假算法基础集训营4 (nowcoder.com)
从左往右选,如果有合法区间就选上,每次更新前缀和的位置
\(\mathcal{O}(nlogn)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
int sum = 0, pre = -1, ans = 0;
map<int, int> mp;
mp[0] = -1;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
sum = (sum + x) % k;
if (mp.count(sum) && mp[sum] >= pre) {
ans++;
pre = i;
}
mp[sum] = i;
}
cout << ans << '\n';
return 0;
}
G-数三角形(easy)_2024牛客寒假算法基础集训营4 (nowcoder.com)
模拟,先记录每个\(*\)往右的延续个数,然后从头往下每次去判断是否连续,满足要求则去判断左下角的点往右的\(*\)个数是否满足条件
\(\mathcal{O}(n^3)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> s(n);
for (auto &i : s) cin >> i;
vector ok(n, vector<int>(m));
for (int i = 0; i < n ; i ++) {
for (int j = m - 1; j >= 0 ; j --) {
int k = j;
if (s[i][j] == '*') {
if (j == m - 1) ok[i][j] = 1;
else ok[i][j] = ok[i][j + 1] + 1;
}
}
}
int num = 0;
for (int i = 0; i < n ; i ++) {
for (int j = 0; j < m ; j ++) {
if (s[i][j] == '*') {
int zuo = j - 1, you = j + 1, h = i + 1;
while (h < n && zuo >= 0 && you < m && s[h][zuo] == s[h][you] && s[h][zuo] == '*') {
int l = you - zuo + 1;
if (ok[h][zuo] >= l) num ++;
zuo --, you ++, h ++;
}
}
}
}
cout << num << '\n';
return 0;
}
2024牛客寒假算法基础集训营5
A-mutsumi的质数合数_2024牛客寒假算法基础集训营5 (nowcoder.com)
1既不是质数也不是合数
\(\mathcal{O}(n)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,f = 0;
cin >> n;
vector<int> a(n);
for(auto &i : a) {
cin >> i;
if(i == 1) f ++;
}
cout << n - f << '\n';
return 0;
}
C-anon的私货_2024牛客寒假算法基础集训营5 (nowcoder.com)
考虑在\(a_i\)和\(a_{i+1}\)之间插入若干个0
\(\mathcal{O}(n)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<i64> a(n + 2);
for (int i = 1; i <= n; i ++)
cin >> a[i];
a[0] = a[1], a[n + 1] = a[n];
i64 ans = 0;
for (int i = 0; i <= n; i ++) {
i64 x = min(a[i], a[i + 1]) - 1;
ans += x;
a[i + 1] -= x;
}
cout << ans << '\n';
return 0;
}
G-sakiko的排列构造(easy)_2024牛客寒假算法基础集训营5 (nowcoder.com)
H-sakiko的排列构造(hard)_2024牛客寒假算法基础集训营5 (nowcoder.com)
暴力打表出后找规律,有规律就是当前值到它的下一个最小质数之内倒序输出,且两者范围内都是有解,于是处理出\(2n\)内的质数,对于每一个数找到它的下一个最小质数,从前往后倒序输出
\(\mathcal{O}(2n)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
vector<bool> euler_range(int n) {
vector<int> phi(n + 1), prime;
vector<bool> is_prime(n + 1, true);
is_prime[1] = 0, phi[1] = 1;
for (int i = 2; i <= n; i++) {
if (is_prime[i]) prime.push_back(i), phi[i] = i - 1;
for (int j = 0; j < (int)prime.size() && i * prime[j] <= n; j++) {
is_prime[i * prime[j]] = 0;
if (i % prime[j]) phi[i * prime[j]] = phi[i] * (prime[j] - 1);
else {
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
}
}
return is_prime;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
auto isprime = euler_range(2 * n);
for (int i = n; i > 0; i--) {
int x = i + 1;
while (!isprime[x]) {
x++;
}
for (int j = x - i - 1; j < i; j++) {
a[j] = x - (j + 1);
}
i = x - i;
}
for (int i = 0; i < n; i++)
cout << a[i] << " \n"[i == n - 1];
return 0;
}
I-rikki的最短路_2024牛客寒假算法基础集训营5 (nowcoder.com)
\(A\)和\(T\)在同一方向,若\(A\)小于等于\(T\),则为\(t\),,否则就加两倍\(T\)到\(A\)的路程,不同方向就看最开始视野能不能找到\(A\),能则先找\(A\),不能则先去找\(T\)
\(\mathcal{O}(1)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
i64 t,a,k;
cin >> t >> a >> k;
if(a * t >= 0){
a = abs(a), t = abs(t);
if(a <= t)
cout << t << '\n';
else
cout << t + 2 * (a - t) << '\n';
}else {
a = abs(a), t = abs(t);
if(a <= k)
cout << 2 * a + t << '\n';
else
cout << t + 2 * (a + t) << '\n';
}
return 0;
}
L-anon的星星_2024牛客寒假算法基础集训营5 (nowcoder.com)
遍历一遍,如果答案唯一则输出,否则\(-1\)
\(\mathcal{O}(n)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, x, f = 0, ans1, ans2;
cin >> n >> x;
for (int i = 0; i <= n; i ++) {
int j = n - i;
if (i - j == x) {
ans1 = i, ans2 = j;
f ++;
}
}
if (f == 1) cout << ans1 << ' ' << ans2 << '\n';
else cout << "-1\n";
return 0;
}
M-mutsumi的排列连通_2024牛客寒假算法基础集训营5 (nowcoder.com)
特判1,2,然后看有没有斜着一样或者对等一样的就是删除一个即可,否则需要删除两个
\(\mathcal{O}(n)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
void solve() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
if (n == 1 || (n == 2 && a[0] == b[0])) {
cout << "-1\n";
return;
}
for (int i = 0; i + 1 < n; i++) {
if ((i && a[i] == b[i]) || a[i] == b[i + 1] || b[i] == a[i + 1]) {
cout << "1\n";
return;
}
}
cout << "2\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T --)
solve();
return 0;
}
2024牛客寒假算法基础集训营6
A-宇宙的终结_2024牛客寒假算法基础集训营6 (nowcoder.com)
筛出100内的素数,然后三个for循环暴力找一下即可
\(\mathcal{O}(1)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int l, r;
cin >> l >> r;
auto ss = [](int x) {
if (x == 2) return true;
if (x < 2 || x % 2 == 0) return false;
for (int i = 2; i <= sqrt(x); i ++)
if (x % i == 0) return false;
return true;
};
vector<int> pr;
for (int i = 2; i <= 100; i ++)
if (ss(i)) pr.push_back(i);
for (int i = 0; i < pr.size(); i ++) {
for (int j = i + 1; j < pr.size(); j ++) {
for (int k = j + 1; k < pr.size(); k ++) {
int x = pr[i] * pr[j] * pr[k];
if (x >= l && x <= r) {
cout << x << '\n';
return 0;
}
}
}
}
cout << "-1\n";
return 0;
}
B-爱恨的纠葛_2024牛客寒假算法基础集训营6 (nowcoder.com)
对\(a\)排序,然后遍历\(b\)去找到最小的绝对值,记录位置,交换
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<i64> a(n), b(n);
for (auto &i : a) cin >> i;
for (auto &i : b) cin >> i;
sort(a.begin(), a.end());
i64 ans = INT_MAX;
int pos = 0, ori = 0;
for (int j = 0; j < n; j ++) {
int i = b[j];
auto t = lower_bound(a.begin(), a.end(), i);
if (t == a.end()) t = prev(t);
int x = abs(i - *t);
if (x < ans) {
pos = t - a.begin();
ans = x;
ori = j;
}
if (t != a.begin()) {
x = abs(i - *prev(t));
if (x < ans) {
pos = t - a.begin() - 1;
ans = x;
ori = j;
}
}
}
swap(a[pos], a[ori]);
for (auto i : a)
cout << i << ' ';
cout << '\n';
return 0;
}
C-心绪的解剖_2024牛客寒假算法基础集训营6 (nowcoder.com)
1e9以内的素数只有45个,筛出来后三个for循环预处理,然后就判断n输出
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<i64> mp(46);
mp[0] = 0, mp[1] = 1;
set<i64> op;
op.insert(0), op.insert(1);
for (int i = 2; i <= 45; i++) {
mp[i] = mp[i - 1] + mp[i - 2];
op.insert(mp[i]);
}
map<i64, vector<i64>> ans;
for (int i = 0; i < 46; i ++) {
for (int j = 0; j < 46; j ++) {
for (int k = 0; k < 46; k ++) {
i64 n = mp[i] + mp[j] + mp[k];
if (ans.count(n)) continue;
ans[n].push_back(mp[i]);
ans[n].push_back(mp[j]);
ans[n].push_back(mp[k]);
}
}
}
int Q;
cin >> Q;
while (Q --) {
i64 n;
cin >> n;
bool ok = false;
if (ans.count(n)) {
for (auto i : ans[n])
cout << i << ' ';
cout << '\n';
continue;
} else cout << "-1\n";
}
return 0;
}
D-友谊的套路_2024牛客寒假算法基础集训营6 (nowcoder.com)
有两种结局,小红让二追三或者小紫让二追三
\(\mathcal{O}(1)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
double p, ans = 0.0;
cin >> p;
cout << (1 - p) * (1 - p) * p * p * p + p * p * (1 - p) * (1 - p) * (1 - p) << '\n';
return 0;
}
E-未来的预言_2024牛客寒假算法基础集训营6 (nowcoder.com)
遍历一遍判断即可
\(\mathcal{O}(n)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string bo, s;
cin >> bo >> s;
i64 num = 0;
for (int i = 2; i < bo.size(); i ++) {
num = num * 10 + (bo[i] - '0');
}
i64 p = 0, r = 0;
for (int i = 0; i < s.size(); i ++) {
if (s[i] == 'P') p ++;
else r ++;
if (p > num / 2) {
cout << "yukari!\n" << i + 1 << '\n';
return 0;
} else if (r > num / 2) {
cout << "kou!\n" << i + 1 << '\n';
return 0;
}
}
cout << "to be continued.\n" << s.size() << '\n';
return 0;
}
F-命运的抉择_2024牛客寒假算法基础集训营6 (nowcoder.com)
素数筛+并查集,对每个数和它的质因子建立集合,最后就是看有没有1个以上的集合
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
const i64 maxn = 1e6 + 5;
bool ch[maxn];
int num , prime[maxn];
void phi() {
for (int i = 2; i <= maxn; i ++) {
if (!ch[i]) prime[num ++] = i;
for (int j = 0; j < num; j ++) {
if (prime[j] * i > maxn) break;
ch[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
}
vector<int> fa(maxn);
vector<bool> ex(maxn);
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
phi();
int Q;
cin >> Q;
while (Q--) {
int n, ma = 0, one = 0;
cin >> n;
vector<bool>(maxn).swap(ex);
vector<int> a(n);
for (auto &i : a) {
cin >> i;
ma = max(ma, i);
ex[i] = 1;
one += (i == 1);
}
if (one == n) {
cout << "-1 -1\n";
continue;
}
if (one != n && one) {
cout << one << ' ' << n - one << '\n';
for (int i = 1; i <= one; i ++)
cout << 1 << " \n"[i == one];
for (int i = 0; i < n; i ++) {
if (a[i] != 1)
cout << a[i] << ' ';
}
cout << '\n';
continue;
}
for(int i = 0;i <= ma;i ++) fa[i] = i;
for (int i = 0; i < num; i ++) {
if (prime[i] > ma) break;
for (int j = 1; j * prime[i] <= ma; j ++) {
int x = j * prime[i];
if (ex[x]) {
int fx = find(x), fy = find(prime[i]);
if (fx != fy) fa[fx] = fy;
}
}
}
map<int, bool> ok;
vector<int> ans1, ans2;
int sum = 0, is = 0;
for (int i = 0; i < n; i ++) {
int x = find(a[i]);
if (!ok.count(x)) {
sum ++ ;
if (!is) is = x;
ok[x] = true;
}
if (x == is) ans1.push_back(a[i]);
else ans2.push_back(a[i]);
}
if (sum == 1) {
cout << "-1 -1\n";
} else {
cout << ans1.size() << ' ' << ans2.size() << '\n';
for (auto i : ans1)
cout << i << ' ';
cout << '\n';
for (auto i : ans2)
cout << i << ' ';
cout << '\n';
}
}
return 0;
}
I-时空的交织_2024牛客寒假算法基础集训营6 (nowcoder.com)
前缀和处理,记录\(a,b\)前缀和之间连续的最大正数,最小负数等,最后就是比较一下
\(\mathcal{O}(n)\)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<i64> a(n), b(m), prea(n), preb(m);
for (auto &i : a) cin >> i;
for (auto &i : b) cin >> i;
prea[0] = a[0];
for (int i = 1; i < n; i ++)
prea[i] = prea[i - 1] + a[i];
i64 maxa = a[0], mina = 0, mia = a[0], ma = 0, mi = a[0];
for (int i = 0; i < n; i ++) {
maxa = max(maxa, prea[i] - mina);
mina = min(prea[i], mina);
mia = min(mia, a[i]);
mi = min(mi, prea[i] - ma);
ma = max(prea[i], ma);
}
preb[0] = b[0];
for (int i = 1; i < m; i ++)
preb[i] = preb[i - 1] + b[i];
i64 maxb = b[0], minb = 0, mib = b[0], mb = 0, bi = a[0];
for (int i = 0; i < m; i ++) {
maxb = max(maxb, preb[i] - minb);
minb = min(preb[i], minb);
mib = min(mib, b[i]);
bi = min(bi, preb[i] - mb);
mb = max(preb[i], mb);
}
cout << max({maxa * maxb, mib * maxa, mia * maxb, mi * bi}) << '\n';
return 0;
}
J-绝妙的平衡_2024牛客寒假算法基础集训营6 (nowcoder.com)
一个红节点无子节点或所有子节点均为红色时无解,处理掉这些情况考虑贪心,先将所有节点都赋值为1,然后根据红色节点的子节点权值和来修改该红节点和它的一个节点的权值
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
string s;
cin >> n >> s;
vector<int> sum(n + 1, 1), ans(n + 1, 1);
vector e(n + 1, vector<int>());
map<int, int> mp;
for (int i = 0; i < n; i ++)
if (s[i] == 'R')
mp[i + 1] = 1;
for (int x, i = 2; i <= n; i ++) {
cin >> x;
e[x].push_back(i);
}
for (int i = 1; i <= n; i ++) {
bool ok = false;
for (auto v : e[i]) {
if (!mp[v]) {
ok = true;
break;
}
}
if (mp[i] && !ok) {
cout << "-1\n";
exit(0) ;
}
}
auto dfs = [&](auto self, int u) -> void{
int res = 1;
for (auto v : e[u]) {
self(self, v);
if (!mp[v]) res += sum[v];
}
if (mp[u]) {
if (res % 3 == 2) ans[u] ++;
else if (res % 3 == 1) {
ans[u] ++;
ans[e[u][0]] ++;
}
}
sum[u] = res;
};
dfs(dfs, 1);
for (int i = 1; i <= n; i ++)
cout << ans[i] ;
cout << '\n';
return 0;
}