2025 暑期集训A班 模拟赛#3

· · 个人记录

A. 禁止翻倍(double)

题意

给定 N 个整数的可重集合 {A_1,A_2,\dots,A_N},请从中删去一些元素,使得该集合内不存在某数对 (A_x, A_y) 满足 2 \times A_x = A_y。问至少需要删去几个元素才能满足条件。

思路

考虑 dp,设计状态 dp_{i,0/1} 表示数字 i 选或不选。

枚举 1\sim 10^6 的所有数,考虑转移:

当当前数 u 为偶数时:

\left\{\begin{split} &dp_{u,0} = dp_{\frac{u}{2}, 1} \\ &dp_{u, 1} = \min(dp_{\frac{u}{2},0}, dp_{\frac{u}{2},1}) + cnt_u \end{split} \right.

当当前数 u 为奇数时:

\left\{\begin{split} &dp_{u,0} = 0 \\ &dp_{u, 1} = cnt_u \end{split} \right. ## 代码 ```cpp #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) ## 题意 有一块石头,首次飞行会飞行 $x$ 米后落地,落地后会有两种情况发生: - 再次飞行 $\lfloor \frac{x}{2} \rfloor$ 后落地; - 不在飞行,停留在原地。 ## 思路 对于每个 $D$ ,可以枚举条约次数,然后二分初始距离,然后 $\O(\log)$ 处理,最终距离,如果当前距离正好等于 $D$ 则立马输出答案,不在继续枚举寻找。 ## 代码 ```cpp #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; } ```