A. 禁止翻倍(double)
题意
给定 个整数的可重集合 ,请从中删去一些元素,使得该集合内不存在某数对 满足 。问至少需要删去几个元素才能满足条件。
思路
考虑 dp,设计状态 表示数字 选或不选。
枚举 的所有数,考虑转移:
当当前数 为偶数时:
当当前数 为奇数时:
表示数字 在可重集合 中的出现次数。
代码
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
const int N = 1e5 + 10, INF = 2e5, M = 1e6;
int n, a[N], ans;
int dp[1000010][3];
gp_hash_table<int, int> f;
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];
f[a[i]]++;
}
for(int i = 1; i <= M; i++){
if(i & 1) dp[i][0] = 0, dp[i][1] = f[i];
else{
dp[i][1] = min({dp[i / 2][0], dp[i / 2][1]}) + f[i];
dp[i][0] = dp[i / 2][1];
}
if(i > 500000) ans += min(dp[i][0], dp[i][1]);
}
cout << ans;
return 0;
}
B. 飞石水上漂(rubble)
题意
有一块石头,首次飞行会飞行 米后落地,落地后会有两种情况发生:
- 再次飞行 后落地;
- 不在飞行,停留在原地。
思路
对于每个 ,可以枚举条约次数,然后二分初始距离,然后 处理,最终距离,如果当前距离正好等于 则立马输出答案,不在继续枚举寻找。
代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int t;
ll d;
ll f(ll mid, int p){
ll sum = 0;
while(p--){
sum += mid;
mid /= 2;
}
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;
for(int i = 60; i >= 1; i--){
ll l = 1, r = d, ans = 0;
while(l <= r){
ll mid = l + r >> 1;
if(f(mid, i) <= d){
l = mid + 1;
ans = mid;
}
else{
r = mid - 1;
}
}
if(f(ans, i) == d){
cout << ans << "\n";
break;
}
}
}
return 0;
}