Codeforces Round #806 (Div. 4)A~G

本文介绍了五道编程竞赛中的题目,包括字符串操作、字符计数、数字序列处理、字符串组合验证和矩阵旋转。每道题目分别给出了C++语言的解决方案,并详细解释了解题思路。通过这些实例,读者可以提升在算法和数据结构方面的技能。
摘要由CSDN通过智能技术生成

Codeforces Round #806 (Div. 4)

hhh晚上被抓去做核酸了,只能等白天补题

Problem - A - Codeforces

直接把所有的小写字母转成大写,再看看字符串是否等于“YES"。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>

#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 1e9 + 7;

void solve()
{
    string s;
    cin >> s;
    for (auto& i : s)
    {
        if (i >= 'a' && i <= 'z')i -= 32;
    }
    if (s == "YES")cout << s << endl;
    else cout << "NO" << endl;
}

signed main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Problem - B - Codeforces

记录一下每个字符的出现次数,如果这个字符是第一次出现,那么分数+2,其余时候分数+1.

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>

#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 1e9 + 7;

void solve()
{
    int n;
    string s;
    cin >> n >> s;
    map<char, int>mymap;
    int res = 0;
    for (auto i : s)
    {
        if (mymap.count(i) == 0)
        {
            mymap[i] = 1;
            res += 2;
        }
        else res++;
    }
    cout << res << endl;
}

signed main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Problem - C - Codeforces

遍历字符串,D代表数字++,U代表数字–,然后把值取余10得到0~9的数。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>

#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 1e9 + 7;

void solve()
{
    int n, len;
    string s;
    cin >> n;
    vector<int>a(n);
    for (int i = 0; i < n; i++)cin >> a[i];
    for (int i = 0; i < n; i++)
    {
        cin >> len >> s;
        int cnt = 0;
        for (auto i : s)
        {
            if (i == 'D')cnt++;
            else cnt--;
        }
        a[i] += cnt;
        if (a[i] < 0)
        {
            a[i] %= 10;
            a[i] += 10;
        }
        cout << a[i]%10 << " ";
    }
    cout << endl;
}

signed main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Problem - D - Codeforces

我们可以用哈希表记录每个字符串的出现次数。

对于一个长度为len的字符串,我们可以把它分成长度为{1,len-1},{2,len-2}……{i,len-i}的两个字符串,只要看哈希表是否出现这两个字符串,如果都出现,则可以被拼成。

比如字符串abcde,我们可以看(a和bcde)或(ab和cde)……是否出现就行。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>

#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 1e9 + 7;

void solve()
{
    int n;
    cin >> n;
    vector<string>v(n);
    unordered_map<string, int>mymap;
    for (int i = 0; i < n; i++)cin >> v[i], mymap[v[i]] = 1;
    for (int i = 0; i < n; i++)
    {
        bool flag = true;
        for (int j = 1; j < v[i].size(); j++)
        {
            if (mymap.count(v[i].substr(0, j)) && mymap.count(v[i].substr(j, v[i].size() - j)))
            {
                cout<<1;
                flag = false;
                break;
            }
        }
        if (flag)cout << 0;
    }
    cout << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Problem - E - Codeforces

0°时下标为(i,j),90°时对应的下标是(j,n-i-1),180°时对应的下标是(n-i-1,n-j-1),270°是对应的下标是(n-j-1,i)。

我们只要看这四个下标的点是否都是1或0,或者把少的那一方变成0即可。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>

#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 1e9 + 7;

void solve()
{
    int n;
    cin >> n;
    vector<string>v(n);
    for (int i = 0; i < n; i++)cin >> v[i];
    int res = 0;
    for (int i = 0; i <= n / 2; i++)
    {
        for (int j = 0; j <= n/2; j++)
        {
            int zero = 0, one = 0;
            int x = n - i - 1, y = n - j - 1;
            if (v[i][j] == '1')one++;
            else zero++;
            if (v[j][x] == '1')one++;
            else zero++;
            if (v[x][y] == '1')one++;
            else zero++;
            if (v[y][i] == '1')one++;
            else zero++;
            if (one <= zero)
            {
                res += one;
                v[i][j] = '0';
                v[j][x] = '0';
                v[x][y] = '0';
                v[y][i] = '0';
            }
            else
            {
                res += zero;
                v[i][j] = '1';
                v[j][x] = '1';
                v[x][y] = '1';
                v[y][i] = '1';
            }
        }
    }
    cout << res << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Problem - F - Codeforces

我们只要看哪个位置的值满足a[i]<i,然后再看之前有多少个这样的数即可。

比如:1 1 2 3 8 2 1 4,对于第五个位置来说,它满足a[i]<i,然后它前面有两个同样满足的数:第2个位置和第4个位置,所以第五个位置为答案提供了2个贡献。

至于快速算出前面有多少个满足的数,我们可以用区间和+单点修改线段树,初始全为0,第几个位置的数满足条件,我们就在线段树上把它变成1。这样计算第i个位置前有多少个满足的值,只用计算1到a[i]-1位置的区间和即可。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>

#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 1e9 + 7;

int f[4 * N], n;

void build_tree(int k, int l, int r)
{
    if (l == r)
    {
        f[k] = 0;
        return;
    }
    int m = (l + r) / 2;
    build_tree(k + k, l, m);
    build_tree(k + k + 1, m + 1, r);
    f[k] = 0;
}

void revise(int k, int l, int r, int x)
{
    if (l == r)
    {
        f[k]++;
        return;
    }
    int m = (l + r) / 2;
    if (x <= m)revise(k + k, l, m, x);
    else revise(k + k + 1, m + 1, r, x);
    f[k] = f[k + k] + f[k + k + 1];
}

int calc(int k, int l, int r, int x,int y)
{
    if (l == x && r == y)
    {
        return f[k];
    }
    int m = (l + r) / 2;
    if (y <= m)return calc(k + k, l, m, x, y);
    else
        if (x > m)return calc(k + k + 1, m + 1, r, x, y);
        else return calc(k + k, l, m, x, m) + calc(k + k + 1, m + 1, r, m + 1, y);
}

void solve()
{
    cin >> n;
    int cnt = 0;
    vector<int>a(n+1),b(n+1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    build_tree(1, 1, n);
    for (int i = 1; i <= n; i++)
    {
        if (a[i] < i)
        {
            if (a[i] != 0)
                cnt += calc(1, 0, n - 1, 0, a[i] - 1);
            revise(1, 0, n-1, i);
        }
    }
    cout << cnt << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Problem - G - Codeforces

设置状态转移二维数组f,f[i] [j]表示,对于到第i个位置时,一共使用了j坏把钥匙,获得的金币数是f[i] [j]。

但是我们每个位置都可以用一把钥匙,这样数组最多会开f[1e5] [1e5],明显会超,但我们可以想想,每个坏钥匙会让我们的金币数减半,金币数最多是1e9,那么只要用30把钥匙,金币就会变成0了,所以我们实际上只用开[1e5] [30]即可(我开了40,但其实三十就行)。

状态转移方程为:

  • 如果不拿假钥匙开门,则:f[i] [j]=f[i-1] [j]+a[i]/pow(2,j)-k;
  • 如果拿假钥匙开门,则:f[i] [j]=f[i-1] [j-1]+a[i]/pow(2,j);

维护过程中最大值即可。

注意,这个写法要在过程中就维护最大值,因为如果在第100个位置上,f[100] [31]=max(f[99] [31]-k,f[99] [30]) (由于已经用了3

0把坏钥匙所以金币已经为0了),但是正常情况下,我们f[100] [31]还可以取到f[99] [31],但由于状态转移数组的限制我们做不到,这就会使的后面的答案出问题,不过此时后面的答案已经不要紧了,反正30把坏钥匙后金币数都为0,既然得不到金币那么最后面的值也都是不变的,所以我们要在计算过程中维护最大值。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>

#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 1e5 + 50, MOD = 1e9 + 7;

int f[N][41], a[N], mypow[40];

void power()
{
    mypow[0] = 1;
    for (int i = 1; i < 40; i++)mypow[i] = mypow[i - 1] * 2;
}

void solve()
{
    int n, k;
    cin >> n >> k;
    f[0][0] = 0;
    int res = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        for (int j = 0; j < 40; j++)
        {
            f[i][j] = f[i - 1][j] + a[i] / mypow[j] - k;
            res = max(res, f[i][j]);
        }
        for (int j = 1; j < 40; j++)
        {
            f[i][j] = max(f[i][j], f[i - 1][j - 1] + a[i] / mypow[j]);
            res = max(res, f[i][j]);
        }
        
    }
    
    cout << res << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin >> t;
    power();
    while (t--)
        solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值