15国B组C++蓝桥杯真题

 P10907 [蓝桥杯 2024 国 B] 蚂蚁开会

#include
using namespace std;
typedef long long ll;
const int N = 520;
int ux[N], uy[N], vx[N], vy[N];
map, int> mp;
int gcd(int a, int b){
	if(b == 0)return a;
	return gcd(b, a % b);
}
void solve(int i){
	int dx = vx[i] - ux[i], dy = vy[i] - uy[i];
	int d = gcd(abs(dx), abs(dy));
	dx /= d, dy /= d;
	for(int j = 0; ; j++){
		int x = ux[i], y = uy[i];
		x += j * dx, y += j * dy;
		mp[{x, y}]++;
		if(x == vx[i] && y == vy[i])break;
	}
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> ux[i] >> uy[i] >> vx[i] >> vy[i];
		solve(i);
	}
	int ans = 0;
	for(auto t : mp){
		if(t.second >= 2)ans++;
	}
	cout << ans << endl;
	
	return 0;
}

P10909 [蓝桥杯 2024 国 B] 立定跳远

#include
using namespace std;
typedef long long ll;
int n, m; 
const int N = 100010;
int a[N];
bool check(int mid){
	int sum = 0;
	for(int i = 0; i < n; i++){
		if(a[i] == a[i + 1])continue;
		sum += (a[i + 1] - a[i] + mid - 1) / mid - 1;
	}
	return sum <= m + 1;
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= n; i++)cin >> a[i];
	int l = 0, r = a[n] + 1;
	while(l + 1 != r){
		int mid = l + r >> 1;
		if(check(mid))r = mid;
		else l = mid;
	} 
	cout << r << endl;
	
	return 0;
}

P10910 [蓝桥杯 2024 国 B] 最小字符串

#include
using namespace std;
typedef long long ll;
const int N = 100010;
int n, m;
char a[N], b[N];

int main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= m; i++) cin >> b[i];
    sort(b + 1, b + m + 1);
    
    int i = 1, j = 1;
    while(i <= n && j <= m){
        if(a[i] <= b[j]){
            cout << a[i++];
        } else {
            cout << b[j++];
        }
    }
    while(i <= n) cout << a[i++];
    while(j <= m) cout << b[j++];
    
    return 0;
}

P10911 [蓝桥杯 2024 国 B] 数位翻转

#include
using namespace std;
#define int long long
const int N = 1010;
int a[N], b[N];
int c[32], c1[32];
int dp[1010][1010][2];
int n, m;
int f(int x){
	if(x == 0)return 0;
	int rev = 0;
	while(x){
		rev = (rev << 1) | (x & 1);
		x >>= 1; 
	}
	return rev;
}
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++)cin >> a[i];
    int sum = 0; 
    for(int i = 1; i <= n; i++){
    	sum += a[i];
    	b[i] = f(a[i]);
    	dp[i][0][0] = sum;
	}
    for(int i = 1; i <= n; i++){
    	for(int j = 1; j <= m; j++){
    		dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]) + a[i];
    		dp[i][j][1] = max(dp[i - 1][j - 1][0], dp[i - 1][j][1]) + b[i];
		}
	}
	int ans = 0;
	for(int i = 1; i <= m; i++)ans = max(ans, max(dp[n][i][1], dp[n][i][0]));
	cout << ans << endl;
    
    return 0;
}

P10913 [蓝桥杯 2024 国 B] 套手镯

#include
using namespace std;
#define int long long
int n, w, h;
const int N = 1010;
struct node{
	int l, r, b, u;
}a[N * 2];
bool cmp(node a, node b){
	return a.r < b.r;
}
int ans1, ans2;
int solve(int wide, int len){
	int ans = 0;
	for(int i = 1; i <= n; i++){
		int bu = a[i].b, top = a[i].b + len;
		priority_queue, greater > q;
		for(int j = 1; j <= n; j++){
			if(a[j].b >= bu && a[j].u <= top)q.push(a[j].l);
			while(!q.empty() && q.top() < a[j].r - wide)q.pop();
			ans = max(ans, (int)q.size());
		}
	}
	return ans;
}
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> w >> h;
    for(int i = 1; i <= n; i++){
    	int x, y, r; cin >> x >> y >> r;
    	a[i] = {x - r, x + r, y - r, y + r};
	}
	sort(a + 1, a + n + 1, cmp);
	ans1 = solve(h, w);
	ans2 = solve(w, h);
	cout << max(ans1, ans2) << endl;
	/*
	for(int i = 1; i <= n; i++){
		cout << a[i].l << " " << a[i].r << " " << a[i].b << " " << a[i].u << endl;
	}*/
    
    return 0;
}

P10914 [蓝桥杯 2024 国 B] 跳石头

#include
using namespace std;
#define int long long
const int N = 40010;
int c[N];//dp[i]表示跳到i时有dp[i]个长度S 
int n, ans;
bitset f[N];
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++)cin >> c[i];
    for(int i = n; i >= 1; i--){
    	f[i][c[i]] = 1;
    	if(i + c[i] <= n)f[i] |= f[i + c[i]];
    	if(i * 2 <= n)f[i] |= f[i * 2];
    	ans = max(ans, (int)f[i].count());
	}
	cout << ans << endl;
    
    return 0;
}

P10915 [蓝桥杯 2024 国 B] 最长回文前后缀

#include
using namespace std;
typedef unsigned long long ull;
const int N = 500010;
ull ha[N], hb[N], pw[N]; 
ull gethash(ull t[], int l, int r){
	return t[r] - t[l - 1] * pw[r - l + 1];
}
int solve(string s){
	int n = s.size();
	s = " " + s;
	pw[0] = 1;
	int x = 131;
	for(int i = 1; i <= n; i++){
		ha[i] = ha[i - 1] * x + (ull)s[i];
		hb[i] = hb[i - 1] * x + (ull)s[n - i + 1];
		pw[i] = pw[i - 1] * x;
	}
	int maxx = 0;
	for(int i = 1; i <= n; i++){
		int l = 0, r = (n - i) / 2 + 1;
		while(l + 1 != r){
			int mid = l + r >> 1;
			if(gethash(ha, 1, mid) == gethash(hb, i + 1, i + mid))l = mid;
			else r = mid;
		}
		maxx = max(maxx, l);
	}
	return maxx;
}
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    string s; cin >> s;
    int l = 0, r = s.size() - 1;
    while(s[l] == s[r] && l <= r)l++, r--;
    if(l > r)cout << l << endl;
    else{
    	s = s.substr(l, r - l + 1);
		string t = s;
		reverse(t.begin(), t.end());
		cout << max(solve(s), solve(t)) + l << endl; 
	}
    
	
    
    return 0;
}

你可能感兴趣的:(蓝桥杯,职场和发展)