题目链接

A Prepend and Append

使用双指针一前一后向中间走,将合法的变为特定字符,直到遇见不合法的情况就停止,最后统计特定字符的数量,用n减去就是最后字符串的长度。

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

string s;
int n;

int main() {
    int T;
    cin>>T;
    while (T--) {
        cin >> n >> s;
        for (int i = 0, j = s.size() - 1; i < j; i++, j--) {
            if (s[i] == '1' && s[j] == '0' || s[i] == '0' && s[j] == '1')s[i] = s[j] = '#';
            else break;
        }
        //cout<<s<<endl;
        int cnt = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '#')cnt++;
        }
        cout << n - cnt << endl;
    }
    return 0;//1011011010
}

B Distinct Split

使用数组记录两侧字母出现的数量。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<set>
 
using namespace std;
 
const int N = 200010;
 
string s;
int n;
int a[30], b[30];
 
int main() {
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> s;
        for(int i=0;i<30;i++)a[i]=b[i]=0;
        for (int i = 0; i < s.size(); i++)a[s[i] - 'a']++;
        //for(int i=0;i<10;i++)cout<<a[i]<<' ';
        int cnt1 = 0, cnt2 = 0;
        int mx = 0;
        for (int i = 0; i < 30; i++)if (a[i] > 0)cnt2++;
        for (int i = 0; i < s.size(); i++) {
            a[s[i] - 'a']--;
            if (a[s[i] - 'a'] == 0)cnt2--;
            b[s[i] - 'a']++;
            if (b[s[i] - 'a'] == 1)cnt1++;
            //cout<<cnt1<<' '<<cnt2<<endl;
            mx = max(mx, cnt1 + cnt2);
        }
        cout << mx << endl;
    }
    return 0;
}

C Negatives and Positives

由题目可得,只要有一对负数就可以相互抵消,所以最后变成讨论负数出现的次数,是奇数还是偶数,如果是奇数,就把所有数中绝对值最小的变为负数,如果是偶数,则全部都是正数,最后求和即可。

//
// Created by jkxy on 2023/3/21.
//
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#define ll long long

using namespace std;

const int N = 200010;

int a[N], b[N];
ll cnt, sum;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        sum=cnt=0;
        for (int i = 0; i < n; i++) {
            int x;
            cin >> x;
            if (x > 0)a[i] = x;
            else cnt++, a[i] = -x;
            sum += a[i];
        }
        sort(a, a + n);
        if (cnt & 1)cout << sum - 2 * a[0] << endl;
        else cout << sum << endl;
    }
    return 0;
}

D Range Update Point Query

因为9*9=81,最多三次就可以就可以将数修改到10以下,所以我们可以先记录修改的次数,等查询的时候再修改,这样就是动态的修改一段区间的数所要进行修改的次数,可以使用树状数组来做,降低时间复杂度。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#define IOS ios::sync_with_stdio(false)
#define CC cin.tie(nullptr),cout.tie(nullptr)
#define ll long long

using namespace std;

const int N = 2e6 + 10;
const int M = 1e9 + 7;
int a[N], tr[N];
int n,m;


int lowbit(int x) {
    return x & (-x);
}

void add(int x,int y) {
    for (int i = x; i <= n; i += lowbit(i))tr[i] += y;
}

int query(int x) {
    int res = 0;
    for (int i = x; i; i -= lowbit(i))res += tr[i];
    return res;
}

int get(int x) {
    int res = 0;
    while (x) {
        res += x % 10;
        x /= 10;
    }
    return res;
}

int main() {
    IOS, CC;
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> m;
        for (int i = 1; i <= n; i++)cin >> a[i],tr[i]=0;
        while (m--) {
            int k;
            cin >> k;
            if (k == 1) {
                int l, r;
                cin >> l >> r;
                add(l, 1);
                add(r + 1, -1);
            } else {
                int x;
                cin >> x;
                int ans = a[x];
                if (ans >= 10) {
                    for (int i = 1; i <= query(x); i++) {
                        ans = get(ans);
                        if (ans < 10)break;
                    }
                }
                cout << ans << endl;
            }
        }
    }
    return 0;
}

E Dima and Guards

题目的意思就是找通过两个警卫所需要买礼物的花费最少,最后输出每个礼物所花费的数额,要特别注意说的是刚好花n元,所以确定第一个礼物后,第二个礼物直接用n减去即可

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int n;

int main() {
    cin >> n;
    for (int i = 1; i <= 4; i++) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        int x, y;
        x = min(a, b);
        y = min(c, d);
        //int z = min({x + y, x + x, y + y});
        if (x + y <= n) {
            cout << i << ' ' << x << ' ' << n - x << endl;
            return 0;
        }
    }
    puts("-1");
    return 0;
}

F Dima and To-do List

可以发现只要遍历前k个数就可以求出所有的情况,在求的过程中不断更新最小值就可以了。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>

using namespace std;

const int N = 100010;

int n, k;
int a[N];

int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++)scanf("%d", &a[i]);
    int pos;
    int mi = INT_MAX;
    for (int i = 1; i <= k; i++) {
        int sum = 0;
        for (int j = i; j <= n; j += k)sum += a[j];
        if (sum < mi) mi = sum, pos = i;
    }
    cout << pos << endl;
    return 0;
}

G Dima and Salad

由图中的信息可以将公式转换为c[i] = a[i] - k * b[i],从而转化为一个01背包的问题,因为c[i]的值可能为正为负,所以我们可以做两遍01 背包正负数分别做一次,最后再求最大值即可,这道题最重要的就是要注意把公式转换一下,就成了最简单的01背包的问题。

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N=100100, M = 10000;

int f1[N], f2[N], a[N], b[N], c[N];
int n, k;

int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++)cin >> a[i];
    for (int i = 1; i <= n; i++)cin >> b[i];
    for (int i = 1; i <= n; i++)c[i] = a[i] - k * b[i];
    for (int i = 1; i <= M; i++)f1[i] = f2[i] = -1e9;
    for (int i = 1; i <= n; i++) {
        if (c[i] >= 0) {
            for (int j = M; j >= c[i]; j--) {
                f1[j] = max(f1[j], f1[j - c[i]] + a[i]);
            }
        } else {
            for (int j = M; j >= -c[i]; j--)
                f2[j] = max(f2[j], f2[j + c[i]] + a[i]);
        }
    }
    int res = 0;
    for (int i = 0; i <= M; i++) {
        res = max(res, f1[i] + f2[i]);
    }
    if (res == 0)cout << "-1" << endl;
    else cout << res << endl;
    return 0;
}