A. Split the Multiset
思路:
最优的策略是每次操作分出 k−1𝑘−1 个 1,然后考虑最后是否会剩下一个单独的 1。
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
typedef long long ll;
typedef unsigned long long ull;
ll n, m, t, h, k;
ll a, b, c;
ll ans, num, sum1=0, sum, sum2=0, cnt, maxx, minn = 1e9;
ll dp[N], f1[N], f2[N];
ll mp[105][105];
bool flag, vis[N];
string s;
void solve() {ll n, k, sum = 0;cin >> n >> k;while (n > k) {n -= (k - 1);sum++;}if (n != 1) sum++;cout << sum << endl;
}
int main()
{cin >> t;while (t--) {solve();}return 0;
}
B. Make Majority
思路:
首先对全 0 连续段进行操作使它们均变为一个 0,先尽可能减少 0 的个数,然后判断是否有 c1>c0𝑐1>𝑐0 即可.
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k;
ll a, b, c;
ll ans, num, sum1=0, sum, sum2=0, cnt, maxx, minn = 1e9;
ll dp[N], f1[N], f2[N];
ll mp[105][105];
bool flag, vis[N];
string s;
string t;
void solve() {ll n, k, sum = 0;cin >> n >> k;while (n > k) {n -= (k - 1);sum++;}if (n != 1) sum++;cout << sum << endl;
}
int main()
{cin >> k;while (k--) {s.clear();t.clear();cin >> n >> s;for (int i = 0; i < s.size(); ) {if (s[i] != '0') {t += s[i];i++;}else {int j = i + 1;t += s[i];while (j < s.size() && s[j] == '0') {j++;}i = j;}}ll ans1 = 0;ll ans2 = 0;for (auto it : t) {if (it == '0') {ans1++;}else {ans2++;}}if (ans2 > ans1) {cout << "Yes" << endl;}else {cout << "No" << endl;}}return 0;
}
C. Increasing Sequence with Fixed OR
思路:
要构造的数组严格递增,那么最后一个位置一定可以放n,为了保证严格单调递增,直接低位枚举n的二进制位即可,假设当前枚举到第i位时n的二进制位为1,那么把n-(1<<i)加入答案即可
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k;
ll a, b, c;
ll ans, num, sum1=0, sum, sum2=0, cnt, maxx, minn = 1e9;
ll dp[N], f1[N], f2[N];
ll mp[105][105];
bool flag, vis[N];
string s;
string t;
void solve() {cin >> n;ans = 0;if (n - (n & -n) == 0){cout << 1 << endl << n << endl;return;}for (int i = 60; i >= 0; i--) {if ((n >> i) & 1) {++ans;f1[ans] = n - pow(2, i);}}cout << ans + 1 << endl;sort(f1 + 1, f1 + 1 + ans);for (int i = 1; i <= ans; i++)cout<< f1[i] << " ";cout << n << endl;
}
int main()
{cin >> k;while (k--) {solve();}return 0;
}