跳到正文

个人记录

8月13日 2021基础算法普及测试 改错

rennan2003约 8 分钟
原文
文章信息与版本

文章信息

收录
2026年8月13日
更新
2026年8月13日
篇幅
约 8 分钟

版本

  1. v1当前

1.drive

赛道循环:直道(a米) → 弯道(b米) → 直道 → 弯道 → …,从直道开始。行驶c米后,经过多少米直道?
输入:2 1 10 输出:7

题解:

行驶过的直到可以通过 总路程 /(直道的长+弯道的长)*直道的长+ min(剩余距离,直道的长)求出
代码如下

C++
#include<bits/stdc++.h>
using namespace std;
long long a,b,c;
int main(){
	// freopen("drive.in","r",stdin);
	// freopen("drive.out","w",stdout);
	cin>>a>>b>>c;
	long long x=c % (a + b);
//	if(c%(a+b)<=a){
//		x=c%(a+b);//TODO
//	}
	
	cout<<c/(a+b)*a+ min(x,a);
	return 0;
}

2.shop

n件商品魔力值aᵢ,m个客户能力bⱼ。客户能买魔力值≤自身能力的商品,每件商品只能卖一次。最多满足多少订单?
输入:4 4 2 3 4 5 1 2 3 5 输出:3

题解:

先把商品和客户排序,按照从小到大来排序,如果最小的客户买不起最便宜的商品那么就跳过他,如果买得起,并且商品还没卖就匹配
代码:

C++
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main() {
	// freopen("shop.in","r",stdin);
	// freopen("shop.out","w",stdout);
	long long n, m;
	cin >> n >> m;
	long long a[200005] = {0};
	long long b[200005] = {0};
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i++) {
		cin >> b[i];
	}
	sort(a, a + n); 
	sort(b, b + m); 
	long long ans = 0;
	int ai = 0, bi = 0;
	while (ai < n && bi < m) {
		if (a[ai] <= b[bi]) {  
			ans++;
			ai++;  
			bi++;  
		} else {  
			bi++; 
		}
	}
	cout << ans;
	return 0;
}

3.unlock

6位数字PIN码(0-9,允许前导零)。字符串s长10,s[i]描述数字i:y必须出现、n不能出现、?随意。求合法PIN数量,不可能则输出0。
输入:y?nnnnnnnn 输出:63
输入:yyyyyyyyn? 输出:0

题解:

用暴力枚举,因为6位数的密码最大999999也就是1000000不用担心超时,只需要在枚举的时候判断是否合法,如果y的位数超过6位直接返回0
代码:

C++
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main() {
//	freopen("unlock.in", "r", stdin);
//	freopen("unlock.out", "w", stdout);
	string s;
	cin >> s;
	int ans = 0;
	for (int p = 0; p < 1000000; p++) {
		int d[6]={0};
		int tmp = p;
		for (int i = 5; i >= 0; i--) {
			d[i] = tmp % 10;
			tmp /= 10;
		}
		bool a[10] = {0};
		for (int i = 0; i <= 5; i++) {
			a[d[i]] = true;
		}	
		bool v = true;
		for (int i = 0; i < 10; i++) {
			if (s[i] == 'y' && !a[i]) {
				v = false;
				break;
			}
			if (s[i] == 'n' && a[i]) {
				v = false;
				break;
			}
		}
		if (v) ans++;
	}
	cout << ans ;
	return 0;
}

4.abyss

深渊A有n层(耗时aᵢ),深渊B有m层(耗时bᵢ),每个深渊只能从第1层逐层挑战,可自由切换。k分钟内最多挑战多少层?
输入:3 4 240 60 90 120 80 150 80 150 输出:3
输入:3 4 730 60 90 120 80 150 80 150 输出:7
输入:5 4 1 1e9 1e9 1e9 1e9 1e9 1e9 1e9 1e9 1e9 输出:0

题解:

利用双指针和前缀和,先存通过时间的前缀和,在看看哪个小就打哪给,如果不够直接返回0
代码:

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 2e5 + 5;
long long a[N], b[N];
long long A[N], B[N];
int main() {
//	freopen("abyss.in", "r", stdin);
//	freopen("abyss.out", "w", stdout);
	int n, m;
	long long k;
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= m; i++) cin >> b[i];
	A[0] = 0;
	for (int i = 1; i <= n; i++) {
		A[i] = A[i - 1] + a[i];
	}
	B[0] = 0;
	for (int i = 1; i <= m; i++) {
		B[i] = B[i - 1] + b[i];
	}
	int ans = 0;
	int j = m;
	for (int i = 0; i <= n; i++) {
		if (A[i] > k) break;
		while (j >= 1 && A[i] + B[j] > k) {
			j--;
		}
		if (j >= 0) {
			ans = max(ans, i + j);
		}
	}
	cout << ans ;
	return 0;
}

5.splice

n个数字积木(数字aᵢ),选k个排列拼接成数字串。所有选法和排列能组成多少种不同的数字串?
输入:6 4 12 13 23 1 2 3 输出:314

题解:

用set统计元素数量去除重复的元素,和利用dfs递归查找可能性
代码:

C++
#include<iostream>
#include<cstdio>
#include<string>
#include<set>
using namespace std;
int n, k, ans[105];
int  a[105], vis[105];
set <string> mp;
void f(int x) {
	if (x == k + 1) {
		string str;
		for (int i = 1; i <= k; i++) {
			str += to_string(ans[i]); //TODO
		}
		mp.insert(str);
		return ;//TODO
	}
	for (int i = 1; i <= n; i++) {
		if (vis[i] == 0) {
			vis[i] = 1;
			ans[x] = a[i]; //TODO
			f(x + 1);
			vis[i] = 0;
		}//TODO
	}
}
int main() {
//	freopen("splice.in","r",stdin);
//	freopen("splice.out","w",stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i]; //TODO
	}
	f(1);
//	for(int i=1;i<=n;i++){
//		string qqq;
//		for(int j=i+1;j<=k;j++){
//			qqq=str[j];
//			for(int l=j+1;l<k;l++){
//				qqq=qqq+str[l];//TODO
//			}
//			//TODO
//		}
////		if(mp==0){
////			ans++;
//			mp.insert(qqq);//TODO
////		}//TODO
//	}
	cout << mp.size();
	return 0;
}
原文