3.1-小六集训队-407

· · 个人记录

T737740 落医生

题意

有一个长度为n的二进制字符串a,现在又创建n个新的字符串,第i个字符串等于a先复制过来再对第i个字符进行反转(1变成0,0变成1),输出新的n个字符串中有几个1

思路

把a里面所有的1的数量记录下来,再循环模拟反转过程,如果位置i是1,就带表新的字符串会少一个1,否则就是多一个1

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 彩带涂色

题意

有一个序列要进行涂色,这个序列长度为n,数字0到n - 1在这个序列里都正好出现一次,涂色的规则是:给第i个元素涂色时,1到i缺失的最小的元素,就是这个元素的颜色,请问该如何排列这个序列,才能让涂上颜色x的元素最多,请输出字典序最小的

思路

其实只要把x移到最后面就行了

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 樱桃炸弹

题意

有两个数组a和b,b丢失了一些元素,请问b有几种可能每个位置与a对应的元素相加结果都一样(b的所有元素都小于等于k),形成互补

思路

如果都不是-1,答案就是k - (最大值 - 最小值) + 1,如果b差值不相等就带表无法构成互补,或者b_i的值求出后大于k或小于0,也代表无法构成,都可以的话就输出1,否则输出0

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 花之少年

题意

有n朵花,每朵花有一个好看值,要收集m朵花,这些花只能挨个收集,每朵花都要大于等于b_i的好看值,不过可以将一躲好看值为k的花插进任何一个地方,请问k至少是多是,如果无法完成收集输出-1

思路

用两个数组记录每个位置前最多能选取多少个数和每个位置前最多能选取多少个数,分别用pre和last,就可以知道前缀和后缀,知道了每个数字之间的空隙,只要满足pre[i]+last[i+1] = m−1就说明这里面之间差了一个数,k就是b[pre[i+1]],只要继续打擂台寻找最小值就行了

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 狼

题意

有一个数组长度为n,数组里的元素各不相同,要查找[L,R]之间k的位置,但数组可能没有排序,可以挑d个下标,在他们原有的位置上排序,让结果变对,请问要最少挑几个下标才能让结果正确,否则输出-1

思路

进行二分时,目标在右边,需要a_{mid} < k才能向右⾛,要用⼀个可⽤的⽐k⼩的数,否则用一个⽐k⼩的数替换,左边同理。最后先判断数字是否够用,不够输出-1,随后计算最⼩的修改次数,先配对needsmall和needbig,每对贡献2。要更多⽐k⼤的数,检查bigav是否⾜够更小的数字也一样,否则输出ans+(要用的比k大的数字和小的数字的差)*2

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;
}