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

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

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

A

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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 <= 30
		sort(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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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;
	else
		cout << (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();
}
相关推荐
SoraLuna17 分钟前
「Mac玩转仓颉内测版7」入门篇7 - Cangjie控制结构(下)
算法·macos·动态规划·cangjie
我狠狠地刷刷刷刷刷21 分钟前
中文分词模拟器
开发语言·python·算法
鸽鸽程序猿21 分钟前
【算法】【优选算法】前缀和(上)
java·算法·前缀和
九圣残炎27 分钟前
【从零开始的LeetCode-算法】2559. 统计范围内的元音字符串数
java·算法·leetcode
安步当歌39 分钟前
【WebRTC】视频发送链路中类的简单分析(下)
网络·音视频·webrtc·视频编解码·video-codec
YSRM39 分钟前
Experimental Analysis of Dedicated GPU in Virtual Framework using vGPU 论文分析
算法·gpu算力·vgpu·pci直通
韭菜盖饭1 小时前
LeetCode每日一题3261---统计满足 K 约束的子字符串数量 II
数据结构·算法·leetcode
geng小球1 小时前
LeetCode 78-子集Ⅱ
java·算法·leetcode
AnFany1 小时前
LeetCode【0028】找出字符串中第一个匹配项的下标
python·算法·leetcode·字符串·kmp·字符串匹配
远望清一色1 小时前
基于MATLAB的图片中文字的提取及识别
算法·matlab