A. Split the Multiset
For example, {2,2,4} is a multiset.You have a multiset 𝑆. Initially, the multiset contains only one positive integer 𝑛. That is, 𝑆={𝑛}. Additionally, there is a given positive integer 𝑘.
In one operation, you can select any positive integer 𝑢 in 𝑆 and remove one copy of 𝑢 from 𝑆. Then, insert no more than 𝑘 positive integers into 𝑆 so that the sum of all inserted integers is equal to 𝑢.
Find the minimum number of operations to make 𝑆 contain 𝑛 ones(operations). => s.size = n
使得s.size == n, 每次插入k个,
#include<bits/stdc++.h>
using namespace std;
int f(int a,int b)
{
if(a%b) return a/b+1;
return a/b;
}
void solve(){
int n, k;
cin >> n >> k;
if(n==1) cout<<0<<endl;
else if(n<=k) cout<<1<<endl;
else cout<<f(n-k,k-1)+1<<endl;
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i ++)
solve();
return 0;
}
B. Make Majority
a sequence [𝑎1,…,𝑎𝑛] where each element ai𝑎𝑖 is either 0 or 1,You can apply several (possibly zero) operations to the sequence.
you select two integers 1≤𝑙≤𝑟≤|𝑎| (where |𝑎| is the current length of 𝑎) and replace [𝑎𝑙,…,𝑎𝑟] with a single element 𝑥, where 𝑥 is the majority of [𝑎𝑙,…,𝑎𝑟].
Here, the majority of a sequence consisting of 0 and 1 is defined as follows:
suppose there are c0 zeros and 𝑐1 ones in the sequence, respectively.
For example, suppose 𝑎=[1,0,0,0,1,1]. If we select 𝑙=1,𝑟=2, the resulting sequence will be [0,0,0,1,1]. If we select 𝑙=4,𝑟=6, the resulting sequence will be [1,0,0,1].
Determine if you can make 𝑎=[1] with a finite number of operations.
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
string s;
cin >> s;
int c0 = 0, c1 = 0;
for(int i = 0; i < n; i ++){
if(s[i] == '1')
c1 ++;
int j = i;
while(s[j] == '0')
j ++;
if(j != i){
i = j - 1;
c0 ++;
}
}
if(c1 > c0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i ++)
solve();
return 0;
}
C. Increasing Sequence with Fixed OR
You are given a positive integer 𝑛. Find the longest sequence of positive integers𝑎=[𝑎1,𝑎2,…,𝑎𝑘] that satisfies the following conditions, and print the sequence:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
void solve(){
ll n;
cin >> n;
ll t = n;
vector<ll> res;
res.push_back(n);
ll q = 1;
while(t){
if(t % 2){
if(n - q == 0)
break;
res.push_back(n - q);
}
q = q * 2;
t = t / 2;
}
int k = res.size();
cout << k << endl;
for(int i = k - 1; i >= 0; i --)
cout << res[i] << " \n"[i == 0];
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i ++)
solve();
return 0;
}