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

2024-02-05 09:04

本文主要是介绍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();
}

这篇关于2024牛客寒假算法基础集训营1(视频讲解全部题目)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/680408

相关文章

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

C#基础之委托详解(Delegate)

《C#基础之委托详解(Delegate)》:本文主要介绍C#基础之委托(Delegate),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 委托定义2. 委托实例化3. 多播委托(Multicast Delegates)4. 委托的用途事件处理回调函数LINQ

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为