2024牛客寒假算法基础集训营1(视频讲解全部题目)

2024牛客寒假算法基础集训营1(题目全解)

  • A
  • B
  • C
  • D
  • E
  • F
  • G
  • H
  • I
  • J
  • K
  • L
  • M

2024牛客寒假算法基础集训营1(视频讲解全部题目)
在这里插入图片描述

A

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
using namespace std;void solve()
{int n; cin >> n;string s; cin >> s;bool f = false, F = false;for(int i = 0; i < n; i ++){if(s[i] == 'd'){for(int j = i + 1; j < n; j ++){if(s[j] == 'f'){for(int k = j + 1; k < n; k ++){if(s[k] =='s'){f = true; }}}}}}for(int i = 0; i < n; i ++){if(s[i] == 'D'){for(int j = i + 1; j < n; j ++){if(s[j] == 'F'){for(int k = j + 1; k < n; k ++){if(s[k] =='S'){F = true; }}}}}}cout << F << " " << f << endl;	
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;//t = 1;cin >> t;while(t--)solve();
}

B

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
#define x first
#define y second
#define int long long
using namespace std;
typedef pair<int,int> pii;
int dc[3] = {-1, 0, 1};
void solve()
{int n; cin >> n;int l = 2, r = 2;vector<pii>lp, rp;map<pii,bool>st;for(int i = 0; i < n; i ++){int r, c; cin >> r >> c;st[{c,r}] = true;if(c <= 0){lp.push_back({c, r});}else if(c > 0){rp.push_back({c, r});}}if(lp.size()){for(int i = 0; i < lp.size(); i ++){if(lp[i].first == 0 and lp[i].second == 2){if(!st[{-1, 1}]){l = min(1ll, l);}else{l = 0;}continue;}for(int j = 0; j < 3; j ++){int nc = lp[i].x + dc[j];int nr = 3 - lp[i].y;if(st[{nc, nr}]){l = 0;}else{l = min(l, 1ll);}}}}if((st[{-1, 1}] and l == 1)){st[{0, 2}] = true;}if(l == 2){st[{0, 2}] = true;}if(rp.size()){for(int i = 0; i < rp.size(); i ++){for(int j = 0; j < 3; j ++){int nc = rp[i].x + dc[j];int nr = 3 - rp[i].y;if(st[{nc, nr}]){r = 0;}else{r = min(r, 1ll);}}}}else{if(st[{0, 2}]){r = 1;}}int ans = l + r;cout << ans << endl;
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;//t = 1;cin >> t;while(t--)solve();
}

C

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;void solve()
{int n, q, tc; cin >> n >> q >> tc;vector<int>t(n + 1);vector<int>s(n + 1);vector<int>ss(n + 1);for(int i = 1; i <= n; i ++){cin >> t[i];}sort(t.begin() + 1, t.end());for(int i = 1; i <= n; i ++){s[i] = s[i - 1] + t[i];}for(int i = 1; i <= n; i ++){ss[i] = ss[i - 1] + s[i];}while(q--){int M; cin >> M;int l = 1, r = n;while(l < r){int mid = l + r >> 1;int cnt = n - mid + 1;if(cnt * tc <= M){r = mid;}else{l = mid + 1;}}if(l == n){if(tc > M){cout << s[n] + tc << endl;continue;} }cout << s[l - 1] + tc << endl;}
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;t = 1;//cin >> t;while(t--)solve();
}

D

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
#define int long long
using namespace std;void solve()
{int n, m; cin >> n >> m;vector<int>a(n);map<int,int>cnt;set<int>ans;ans.insert(0);for(int i = 0; i < n; i ++){cin >> a[i];cnt[a[i]] += 1;}if(n >= 30){//一定要减少绝对值不等于1的数字个数。for(auto [x, y]: cnt){if(n - cnt[x] - cnt[x - 2] > 30)continue;int mul = 1;bool flag = true;for(int i = 0; i < n; i ++){mul = mul * (a[i] - (x - 1));if(abs(mul) > 1e9){flag = false;break;}}if(flag)ans.insert(mul);mul = 1, flag = true;for(int i = 0; i < n; i ++){mul = mul * (a[i] - (x + 1));if(abs(mul) > 1e9){flag = false;break;}}if(flag)ans.insert(mul);}}else{//如果n <= 30sort(a.begin(), a.end());int tmp = a[0];for(int i = 0; i < n; i ++){a[i] -= tmp;}for(int i = -1e6; i <= 1e6; i ++){int mul = 1;bool flag = true;for(int j = 0; j < n; j ++){mul = mul * (a[j] + i);if(abs(mul) > 1e9){flag = false;break;}}if(flag)ans.insert(mul);}reverse(a.begin(), a.end());tmp = a[0];for(int i = 0; i < n; i ++){a[i] -= tmp;}for(int i = -1e6; i <= 1e6; i ++){int mul = 1;bool flag = true;for(int j = 0; j < n; j ++){mul = mul * (a[j] + i);if(abs(mul) > 1e9){flag = false;break;}}if(flag)ans.insert(mul);}}while(m--){int x; cin >> x;if(ans.count(x)){cout << "Yes" << endl;}else{cout << "No" << endl;}}}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;t = 1;//cin >> t;while(t--)solve();
}

E

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int,int> pii;void solve()
{int n, m; cin >> n >> m;vector<int>a(n);vector<pii>b(m);for(int i = 0; i < n; i ++){cin >> a[i];}for(int i = 0; i < m; i ++){int u, v; cin >> u >> v;u --, v --;b[i] = {u, v};}int ans = 11;function<void(int)> dfs = [&](int u)->void{if(u >= m){int sc = a[0];int top = 1;for(int i = 1; i < n; i ++){if(a[i] > sc){top ++;}}ans = min(ans, top);return;}int x = b[u].first, y = b[u].second;a[x] += 3;dfs(u + 1);//回溯a[x] -= 3;a[y] += 3;dfs(u + 1);a[y] -= 3;a[x] += 1, a[y] += 1;dfs(u + 1);a[x] -= 1, a[y] -= 1;};dfs(0);cout << ans << endl;
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;//t = 1;cin >> t;while(t--)solve();
}

F

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;int fact[N], infact[N];int qmi(int a, int b, int p){int res = 1;while(b){if(b & 1)res = res * a % p;a = a * a % p;b >>= 1;}return res;
}void init(){fact[0] = infact[0] = 1;for(int i = 1; i < N; i ++){fact[i] = fact[i - 1] * i % mod;infact[i] = infact[i - 1] * qmi(i, mod - 2, mod) % mod;}
}void solve()
{int n, m; cin >> n >> m;int res = 0;for(int i = 0; i <= m; i ++){if((m - i) % 2){res = ((res - (qmi(i, n, mod) * infact[i] % mod * infact[m - i] % mod)) % mod + mod) % mod;}else{res = (res + (qmi(i, n, mod) * infact[i] % mod * infact[m - i] % mod)) % mod;}}cout << res << endl;
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;t = 1;// cin >> t;init();while(t--)solve();
}

G

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define x first
#define y second
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int,int> pii;void solve()
{int n, m; cin >> n >> m;vector<pii>a(n + 1);for(int i = 1; i <= n; i ++){cin >> a[i].x >> a[i].y;}sort(a.begin(), a.end());vector<int>s(n + 1);for(int i = 1; i <= n; i ++){s[i] = s[i - 1] + a[i].y;}int ans = m;for(int i = n; i >= 1; i --){int dis = a[i].x - s[i];if(dis > m){continue;}ans = max(ans, m + s[i]);}cout << ans << endl;
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;//t = 1;cin >> t;while(t--)solve();
}

H

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
using namespace std;void solve()
{int n, m; cin >> n >> m;vector<int>v(n), w(n);for(int i = 0; i < n; i ++){cin >> v[i] >> w[i];}int ans = 0, pre = 0;for(int i = 31; i >= 0; i --){int x = pre;if((m >> i) & 1){x += (1 << i) - 1;pre += (1 << i);}int sum = 0;for(int j = 0; j < n; j ++){if((x | w[j]) == x){sum += v[j];}}ans = max(ans, sum);}int sum = 0;for(int j = 0; j < n; j ++){if((m | w[j]) == m){sum += v[j];}}ans = max(ans, sum);cout << ans << endl;
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;//t = 1;cin >> t;while(t--)solve();
}

I

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
using namespace std;
uniform_int_distribution<int> u1(-99,99);//生成圆心
uniform_int_distribution<int> u2(1,100);//生成半径
default_random_engine e;void test()
{int n = 1e5;double r1 = 0;for(int i = 1; i <= n; i ++){int x = u1(e), y = u1(e);while(1){int r = u2(e);if(x + r > 100 || x - r < -100 || y + r > 100 || y - r < -100){continue;}else{r1 += (r);break;}} }double r2 = 0;for(int i = 1; i <= n; i ++){while(1){int x = u1(e), y = u1(e);int r = u2(e);if(x + r > 100 || x - r < -100 || y + r > 100 || y - r < -100){continue;}else{r2 += (r);break;}} }r1 = r1 / n;r2 = r2	/ n;cout << r1 << " " << r2 << endl;
}
void solve(){int n; cin >> n;double sum = 0;for(int i = 0; i < n; i ++){int x, y, r; cin >> x >> y >> r;sum += r; }sum /= n;if((int)sum == 17){cout << "bit-noob" << endl;}else{cout << "buaa-noob" << endl;}
}
signed main()
{e.seed(time(NULL));ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;//t = 1;cin >> t;while(t--)// solve();test();
}

J

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
using namespace std;void solve()
{int n, x, y; cin >> n >> x >> y;vector<int>a(n);for(int i = 0; i < n; i ++){cin >> a[i];}auto check = [&](int dis)->bool{// cout << dis << endl;set<int>st;st.insert(x), st.insert(y);if(abs(x - y) > dis){return false;}for(int i = 0; i < a.size(); i ++){while(st.size() and abs(*st.begin() - a[i]) > dis){st.erase(st.begin());}while(st.size() and abs(*st.rbegin() - a[i]) > dis){st.erase(*st.rbegin());}if(!st.size()){return false;}st.insert(a[i]);}return true;};int l = 0, r = 1e9;while(l < r){int mid = l + r >> 1;if(check(mid)){r = mid;}else{l = mid + 1;}}cout << l << endl;
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;t = 1;// cin >> t;while(t--)solve();
}

K

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
#define int long long
using namespace std;
const int N = 1e5 + 10;
const int mod = 998244353;
int nex[N];
string choice[N];
int p[N], in[N];
bool st[N];
char t[N];
set<int> root;
int nums;int find(int x){if(x != p[x])p[x] = find(p[x]);return p[x];
}void init(int x){for(int i = 1; i <= x; i ++){p[i] = i;root.insert(i);}
}void merge(int x, int y){int px = find(x);int py = find(y);if(px != py){if(in[px]){p[px] = py;root.erase(px);}else{p[py] = px;root.erase(py);}}
}void dfs(int u, char answer){if(st[nex[u]]){if(answer == t[nex[u]]){nums ++;}return;}char nex_answer = choice[nex[u]][answer - 'A'];t[nex[u]] = answer;st[nex[u]] = true;dfs(nex[u], nex_answer);	st[nex[u]] = false;
}void solve()
{int n; cin >> n;init(n);for(int i = 1; i <= n; i ++){int x; string s;cin >> x >> s;choice[i] = s;nex[i] = x;in[x] ++;merge(x, i);}int ans = 1;for(auto x: root){for(int i = 0; i < 5; i ++){t[x] = ('A' + i);st[x] = true;dfs(x, choice[x][i]);}ans = nums * ans % mod;nums = 0;}cout << ans << endl;
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;t = 1;// cin >> t;while(t--)solve();
}

L

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
using namespace std;void solve()
{int c, d, h, w;cin >> c >> d >> h >> w;cout << 3 * w * c << endl;
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;//t = 1;cin >> t;while(t--)solve();
}

M

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
#define int long long
using namespace std;void solve()
{int n; cin >> n;if(n % 6)cout << (n / 6) * 2 << endl;elsecout << (n / 6) << endl;
}signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;//t = 1;cin >> t;while(t--)solve();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/453027.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

开源节点框架STNodeEditor使用

节点&#xff0c;一般都为树形Tree结构&#xff0c;如TreeNode&#xff0c;XmlNode。 树形结构有其关键属性Parent【父节点】&#xff0c;Children【子节点】 LinkedListNode为链表线性结构&#xff0c;有其关键属性Next【下一个】&#xff0c;Previous【上一个】&#xff0c…

item_get_video-获取视频详情(bili.item_get_video)

B站&#xff08;Bilibili&#xff09;的item_get_video API用于获取视频的详细信息。通过调用该API&#xff0c;您将能够获得视频的基本信息、元数据、播放链接等。这使得开发者可以轻松地将B站视频集成到自己的应用程序或网站中&#xff0c;为用户提供更丰富的内容和更好的体验…

时间序列预测 —— ConvLSTM 模型

时间序列预测 —— ConvLSTM 模型 时间序列预测是一项重要的任务&#xff0c;ConvLSTM&#xff08;卷积长短时记忆网络&#xff09;是深度学习领域中用于处理时序数据的强大工具之一。本文将介绍 ConvLSTM 的理论基础、优缺点&#xff0c;与其他常见时序模型&#xff08;如 LS…

java基本知识详解

八大基本数据类型 java的数据类型可以说很简洁&#xff0c;只有整型&#xff0c;浮点型&#xff0c;字符型&#xff0c;和布尔型四大种&#xff0c;八小种基本类型。 整型 byte&#xff1a;-2^7 ~ 2^7-1&#xff0c;即-128 ~ 127。1字节。 short&#xff1a;-2^15 ~ 2^15-…

Redisson看门狗机制

一、背景 网上redis分布式锁的工具方法&#xff0c;大都满足互斥、防止死锁的特性&#xff0c;有些工具方法会满足可重入特性。如果只满足上述3种特性会有哪些隐患呢&#xff1f;redis分布式锁无法自动续期&#xff0c;比如&#xff0c;一个锁设置了1分钟超时释放&#xff0c;…

leetcode 3.无重复字符的最长字串(滑动窗口) (C++)DAY2

文章目录 1.题目示例提示 2.解答思路3.实现代码结果 4.总结 1.题目 给定一个字符串 s &#xff0c;请你找出其中不含有重复字符的 最长子串 的长度。 示例 示例 1: 输入: s “abcabcbb” 输出: 3 解释: 因为无重复字符的最长子串是 “abc”&#xff0c;所以其长度为 3。 示…

专业145+总分420+电子科技大学858信号与系统考研经验电子信息与通信

今年考研各门都相对发挥比较好&#xff0c;总分420&#xff0c;专业858信号与系统145&#xff0c;数学135顺利上岸电子科技大学&#xff0c;应群里很多学弟学妹要求&#xff0c;我总结一下自己的复习经验&#xff0c;希望可以在考研路上&#xff0c;助大家一臂之力。专业课&…

eslint报错文档大量红色报错符号 不自动修正

确保eslint在工作 控制台大量报错信息 确保setting.json 开了保存的时候自动格式化代码 这个时候保存的时候代码可以自动被格式化 但是 文档中和控制台中仍然有大量的报错 信息 此时此刻说明 格式化文档的文件不是按照eslint 格式化的 可以网上找找现成可用的setting.json抄…

如何以管理员身份删除node_modules文件

今天拉项目&#xff0c;然后需要安装依赖&#xff0c;但是一直报错&#xff0c;如下&#xff1a; 去搜这个问题会让把node_modules文件先删掉 再去安装依赖。我在删除的过程中会说请以管理员身份来删除。 那么windows如何以管理员身份删除node_modules文件呢&#xff1f; wi…

【Script】使用pyOpenAnnotate搭建半自动标注工具(附python源码)

文章目录 0. Background1. Method2. Code3. Example: 雄鹿红外图像标注3.1 选择色彩空间3.2 执行阈值3.3 执行形态学操作3.4 轮廓分析以找到边界框3.5 过滤不需要的轮廓3.6 绘制边界框3.7 以需要的格式保存Reference本文将手把手教你用Python和OpenCV搭建一个半自动标注工具(包…

C# OMRON PLC FINS TCP协议简单测试

FINS(factory interface network service)通信协议是欧姆龙公司开发的用于工业自动化控制网络的指令&#xff0f;响应系统。运用 FINS指令可实现各种网络间的无缝通信&#xff0c;包括用于信息网络的 Etherne(以太网)&#xff0c;用于控制网络的Controller Link和SYSMAC LINK。…

SQL Server之DML触发器

一、如何创建一个触发器呢 触发器的定义语言如下&#xff1a; CREATE [ OR ALTER ] TRIGGER trigger_nameon {table_name | view_name}{for | After | Instead of }[ insert, update,delete ]assql_statement从这个定义语言我们可以知道如下信息&#xff1a; trigger_name&…