蓝桥杯算法双周赛

数树数

#include

using namespace std;
using ll = long long;
const int N = 2e5+5;
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}

inline void solve(){
	ll n, q; cin >> n >> q;
	while (q--) {
		ll cnt = 1;
		string s; cin >> s;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] == 'L') {
				cnt = cnt * 2 - 1;
			}
			else
				cnt *= 2;
		}
		cout << cnt << '\n';
	}
}
signed main(){

	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);

	int t = 1;
	//cin >> t;
	while (t--)
		solve();

	return 0;
}

健身

dp,但是非常简单的dp,位运算的处理比较关键

#include
#define int long long
using namespace std;
using ll = long long;
const int N = 2e5+5;

int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}

inline void solve(){
	int n, m, q; cin >> n >> m >> q;
	vectort(q + 1);
	vectordp(m+1);
	vectora;

	for (int i = 1; i <= q; i++) {
		cin >> t[i];
		if (i == 1)
			a.push_back(t[i] - 1);
		else
			a.push_back(t[i] - t[i - 1] - 1);
	}
	if (t[q] != n) {
		a.push_back(n - t[q]);
	}
	int t1, t2;
	for (int i = 1; i <= m; i++) {
		cin >> t1 >> t2;
		dp[t1] = max(dp[t1],t2);
	}
	for (int i = 1; i <= m; i++) {
		dp[i] = max(dp[i], 2 * dp[i - 1]);
	}
	int ans = 0;
	for (int i = 0; i < a.size(); i++) {
		while (a[i]) {
			int t = a[i] & (-a[i]);
			a[i] -= t;
			int b = 0;
			while (t > 1) {
				t = t >> 1;
				b++;
			}
			ans += dp[b];
		}
	}
	cout << ans << '\n';
}
signed main(){

	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);

	int t = 1;
	//cin >> t;
	while (t--)
		solve();

	return 0;
}

分组

这种最大找最小的一般都是二分答案

#include

using namespace std;
using ll = long long;
const int N = 2e5+5;
int h[100005];
int n, k;
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}

int check(int mid) {
	int cnt = 0;
	int t = -1e9-5;
	for (int i = 1; i <= n; i++) {
		if (h[i] - t > mid) {
			t = h[i];
			cnt++;
		}
	}
	return cnt;
}

inline void solve(){
	cin >> n >> k;

	for (int i = 1; i <= n; i++) {
		cin >> h[i];
	}

	sort(h + 1, h + n + 1);
	int l = 0, r = h[n] - h[1], ans = r;
	int mid;
	while (l <= r) {
		mid = l + r >> 1;
		if (check(mid)<=k) {
			r = mid - 1;
			ans = mid;
		}
		else
			l = mid + 1;
	}
	cout << ans << '\n';
}
signed main(){

	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);

	int t = 1;
	//cin >> t;
	while (t--)
		solve();

	return 0;
}

你可能感兴趣的:(蓝桥杯,算法)