A. 禁止翻倍(double)
题意
给定 个整数的可重集合 ,请从中删去一些元素,使得该集合内不存在某数对 满足 。问至少需要删去几个元素才能满足条件。
思路
将 分组。当 ,将 分到 里。
可以 dp。设 表示数 选不选, 表示有多少 。
-
当 为奇数时,
-
当 为偶数时,
当 时,将 加到最终答案上。
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1e5 + 2, M = 1e6 + 1;
int n, a[N];
int cnt[M];
ll ans = 0;
int dp[M][2];
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
freopen("double.in", "r", stdin);
freopen("double.out", "w", stdout);
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
cnt[a[i]] ++;
}
for(int i = 1; i < M; i ++){
if(i % 2 == 1){
dp[i][1] = cnt[i];
dp[i][0] = 0;
}
else{
dp[i][1] = min(dp[i >> 1][0], dp[i >> 1][1]) + cnt[i];
dp[i][0] = dp[i >> 1][1];
}
if((i << 1) >= M){
ans += min(dp[i][0], dp[i][1]);
}
}
cout << ans;
return 0;
}
/***
5
1 2 4 3 6
***/
B. 飞石水上漂(rubble)
题意
飞石大师朝着水面掷出一块飞石。每当飞石在飞行 米后击中水面时,将发生以下两种事件之一:
- 石头没能弹起,沉入水中。
- 石头从水面弹起,飞行 米后再次击中水面;
飞石大师希望掷出一块总共飞行了恰好 米的飞石;
它可以任意控制掷出时的飞行距离,也可以控制每一次击中水面时是弹起还是下沉。请判断,为了达成这一目标,飞石掷出时的飞行距离至少需要是多少?
思路
枚举弹起的次数,对于每一种次数,掷出的距离增大,飞行距离增大,所以可以二分。
对于枚举的所有次数,当距离刚好等于 时统计答案。
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1e7 + 1;
int T;
ll d;
ll l, r, mid, p;
ll ans;
ll len(ll s, ll cnt){
ll sum = 0;
while(cnt --){
sum += s;
s >>= 1;
}
return sum;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
freopen("rubble.in", "r", stdin);
freopen("rubble.out", "w", stdout);
cin >> T;
while(T --){
cin >> d;
ans = 1e18;
for(int i = 60; i >= 1; i --){
l = 1, r = 1e18, p = 0;
while(l <= r){
mid = (l + r) >> 1;
if(len(mid, i) <= d){
p = mid;
l = mid + 1;
}
else{
r = mid - 1;
}
}
if(len(p, i) == d){
ans = p;
break;
}
}
cout << ans << '\n';
}
return 0;
}
/***
3
10
5
1123581321345589
***/
D. 010子串(olo)
题意
我们规定字符串 仅包含字符 0 和 1 , 是将字符串分割为数个互不相交的子串,且满足每个字串中仅包含一种字符的所有分割方案中分割数的最小值。
请统计所有包含 个 0 , 个 1 且长度为 的字符串 中,有多少满足 ,有多少不满足
思路
因为分割方案中分割数最小,所以分割的相邻字串的内容不能相等。
设 ,满足的方案数为
不满足用总数减去满足。
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1e6 + 2, mod = 1e9 + 7;
int T, n, m, a, b;
ll jc[N];
ll sum, sum2;
ll L, R;
ll po(ll a, ll b){
ll ans = 1;
while(b){
if(b & 1){
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
ll c(ll n, ll m) {
if(n < m){
return 0;
}
if(m <= 0){
return 0;
}
return jc[n - 1] * po(jc[m - 1], mod - 2) % mod * po(jc[n - m], mod - 2) % mod;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
freopen("olo.in", "r", stdin);
freopen("olo.out", "w", stdout);
jc[0] = 1;
for(int i = 1; i < N; i ++){
jc[i] = (jc[i - 1] * i) % mod;
}
cin >> T;
while(T --){
cin >> n >> m >> a >> b;
sum = jc[n] * po(jc[n - a], mod - 2) % mod * po(jc[a], mod - 2) % mod;
L = m >> 1;
R = m - L;
sum2 = (c(a, L) * c(b, R) % mod + c(b, L) * c(a, R) % mod) % mod;
cout << sum2 << ' ' << (sum - sum2 + mod) % mod << '\n';
}
return 0;
}
/***
1
4 2 2 2
***/