T737740 落医生
题意
有一个长度为的二进制字符串,现在又创建个新的字符串,第个字符串等于先复制过来再对第个字符进行反转(变成,变成),输出新的个字符串中有几个
思路
把里面所有的的数量记录下来,再循环模拟反转过程,如果位置是,就带表新的字符串会少一个,否则就是多一个
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
void solve(){
int n;
cin >> n;
string s;
cin >> s;
int s1 = 0;
for(int i = 0;i < s.size();i++){
if(s[i] == '1'){
s1++;
}
}
int ans = 0;
for(int i = 0;i < s.size();i++){
if(s[i] == '1'){
ans += (s1 - 1);
}else{
ans += (s1 + 1);
}
}
cout << ans<< '\n';
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}T737754 彩带涂色
题意
有一个序列要进行涂色,这个序列长度为,数字到在这个序列里都正好出现一次,涂色的规则是:给第个元素涂色时,到缺失的最小的元素,就是这个元素的颜色,请问该如何排列这个序列,才能让涂上颜色的元素最多,请输出字典序最小的
思路
其实只要把移到最后面就行了
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
void solve(){
int n, x;
cin >> n>> x;
if(n == 1){
cout << 0<< '\n';
return ;
}
if(n == x){
for(int i = 0;i < n;i++){
cout << i<< ' ';
}
cout << '\n';
return ;
}
for(int i = 0;i < n;i++){
if(i != x){
cout << i<< ' ';
}
}
cout << x<< '\n';
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}T737778 樱桃炸弹
题意
有两个数组和,丢失了一些元素,请问有几种可能每个位置与对应的元素相加结果都一样(的所有元素都小于等于),形成互补
思路
如果都不是,答案就是,如果差值不相等就带表无法构成互补,或者的值求出后大于或小于,也代表无法构成,都可以的话就输出,否则输出
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
const int N = 2e5 + 5;
int a[N], b[N];
void solve(){
int n, k;
cin >> n>> k;
int minn = INT_MAX, maxn = INT_MIN;
for(int i = 1;i <= n;i++){
cin >> a[i];
minn = min(minn, a[i]);
maxn = max(maxn, a[i]);
}
for(int i = 1;i <= n;i++){
cin >> b[i];
}
int x = 0;
bool f = true;
for(int i = 1;i <= n;i++){
if(b[i] != -1){
f = false;
x = a[i] + b[i];
}
}
if(f){
cout << k + 1 - (maxn - minn)<< '\n';
}else{
for(int i = 1;i <= n;i++){
if(b[i] != -1){
if(a[i] + b[i] != x){
cout << 0<< '\n';
return ;
}
}else{
if(x - a[i] > k || x - a[i] < 0){
cout << 0<< '\n';
return ;
}
}
}
if(maxn >= x){
cout << 1<< '\n';
}else{
cout << 0<< '\n';
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}
/*
7
3 10
1 3 2
-1 -1 1
5 1
0 1 0 0 1
-1 0 1 0 -1
5 1
0 1 0 0 1
-1 1 -1 1 -1
5 10
1 3 2 5 4
-1 -1 -1 -1 -1
5 4
1 3 2 1 3
1 -1 -1 1 -1
5 4
1 3 2 1 3
2 -1 -1 2 0
5 5
5 0 5 4 3
5 -1 -1 -1 -1
*/T737789 花之少年
题意
有朵花,每朵花有一个好看值,要收集朵花,这些花只能挨个收集,每朵花都要大于等于的好看值,不过可以将一躲好看值为的花插进任何一个地方,请问至少是多是,如果无法完成收集输出
思路
用两个数组记录每个位置前最多能选取多少个数和每个位置前最多能选取多少个数,分别用和,就可以知道前缀和后缀,知道了每个数字之间的空隙,只要满足就说明这里面之间差了一个数,就是,只要继续打擂台寻找最小值就行了
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
const int N = 2e5 + 5;
int a[N], b[N];
int pre[N], last[N];
void solve(){
int n, m;
cin >> n>> m;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
for(int i = 1;i <= m;i++){
cin >> b[i];
}
pre[0] = 0;
last[n + 1] = 0;
int j = 1;
for(int i = 1;i <= n;i++){
if(j <= m and a[i] >= b[j]){
pre[i] = pre[i - 1] + 1;
j++;
}else{
pre[i] = pre[i - 1];
}
}
j = m;
for(int i = n;i >= 1;i--){
if(j >= 1 and a[i] >= b[j]){
last[i] = last[i + 1] + 1;
j--;
}else{
last[i] = last[i + 1];
}
}
int ans = LLONG_MAX;
for(int i = 0;i <= n;i++){
if(pre[i] + last[i + 1] == m){
cout << 0<< '\n';
return ;
}
if(pre[i] + last[i + 1] == m - 1){
ans = min(ans, b[pre[i] + 1]);
}
}
if(ans == LLONG_MAX){
cout << -1<< '\n';
}else{
cout << ans<< '\n';
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}T737792 狼
题意
有一个数组长度为,数组里的元素各不相同,要查找之间的位置,但数组可能没有排序,可以挑个下标,在他们原有的位置上排序,让结果变对,请问要最少挑几个下标才能让结果正确,否则输出
思路
进行二分时,目标在右边,需要才能向右⾛,要用⼀个可⽤的⽐⼩的数,否则用一个⽐⼩的数替换,左边同理。最后先判断数字是否够用,不够输出,随后计算最⼩的修改次数,先配对和,每对贡献。要更多⽐⼤的数,检查是否⾜够更小的数字也一样,否则输出
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, q;
const int N = 2e5 + 5;
int t;
int a[N];
int id[N];
void solve(){
cin >> n>> q;
for(int i = 1;i <= n;i++){
cin >> a[i];
id[a[i]] = i;
}
while(q--){
int l, r, k;
cin >> l>> r>> k;
if(id[k] < l or id[k] > r){
cout << -1<< ' ';
continue;
}
int small = k - 1;
int big = n - k;
int smallav = k - 1;
int bigav = n - k;
int needsmall = 0;
int needbig = 0;
int L = l, R = r;
while(L <= R){
int mid = (L + R) / 2;
if(a[mid] == k){
break;
}
if(mid < id[k]){
if(k < a[mid]){
needsmall++;
}else{
smallav--;
}
small--;
L = mid + 1;
}else{
if(k > a[mid]){
needbig++;
}else{
bigav--;
}
big--;
R = mid - 1;
}
}
if(big < 0 || small < 0){
cout << -1<< ' ';
continue;
}
int ans = 2 * min(needsmall, needbig);
int diff = abs(needsmall - needbig);
if(needbig > needsmall){
if(bigav < diff){
cout << -1<< ' ';
}else{
cout << ans + 2 * diff<< " ";
}
}else{
if(smallav < diff){
cout << -1<< ' ';
}else{
cout << ans + 2 * diff<< " ";
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}