Codeforces Round 806 (Div. 4)

简介: Codeforces Round 806 (Div. 4)A~G

A. YES or YES?

给一个字符串,看它是不是YES

把小写字母转换成大写字母,在判断是不是YES

#include<bits/stdc++.h>
using namespace std;

void solve()
{
    string str;
    cin >> str;
    for(int i = 0; i < 3; i ++ )
    {
        if(str[i] < 'z' && str[i] > 'a')
            str[i] -= 32;
    }
    cout << (str == "YES" ? "YES" : "NO") << endl;
}

int main()
{
    freopen("test.in", "r", stdin);
    int tt;
    cin >> tt;
    while(tt -- )
    {
        solve();
    }
}

B. ICPC Balloons

给一个字符串,遍历,如果是第一个出现的字母,就+2,否则 + 1
#include<bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    string s;
    cin >> n >> s;
    int point = 0;
    unordered_map<char, int> mp;
    for(auto c : s)
    {
        if(mp[c] == 0)
            point += 2;
        else
            point += 1;
        mp[c] ++ ;
    }
    cout << point << endl;
}

int main()
{
    freopen("test.in", "r", stdin);
    int tt;
    cin >> tt;
    while(tt -- )
    {
        solve();
    }
    return 0;
}

C. Cypher

模拟倒着回去
#include<bits/stdc++.h>
using namespace std;
int a[110];
void solve()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++ ) cin >> a[i];
    for(int i = 0; i < n; i ++ )
    {
        int x;
        string str;
        cin >> x >> str;
        int res = a[i];
        for(int j = 0; j < x; j ++ )
        {
            if(str[j] == 'D') res = (res + 1) % 10;
            else res = (res - 1 + 10) % 10;
        }
        cout << res << " ";
    }
    cout << "\n";
}

int main()
{
    freopen("test.in", "r", stdin);
    int tt;
    cin >> tt;
    while(tt -- )
    {
        solve();
    }
    return 0;
}

D. Double Strings

先把所有字符串存到set中,在对每个字符串,枚举它断开的位置,因为字符串数量较多,但长度最多为8,查看字符串分开的前后字符串是否都存在,如果都存在,就输出1,否则输出0
#include<bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<string> s(n);
    for(int i = 0; i < n; i ++ )
        cin >> s[i];

    set<string> se;
    for(int i = 0; i < n; i ++ )
        se.insert(s[i]);
    
    string ans;
    for(int i = 0; i < n; i ++ )
    {
        bool f = false;
        int x = s[i].size();
        for(int j = 1; j < x; j ++ )
        {
            if(se.count(s[i].substr(0, j)) == 1 && se.count(s[i].substr(j)) == 1)
                f = true;
        }
        if(f) ans += "1";
        else ans += "0";
    }
    cout << ans << endl;
}

int main()
{
    freopen("test.in", "r", stdin);
    int tt;
    cin >> tt;
    while(tt -- )
    {
        solve();
    }
    return 0;
}

E. Mirror Grid

经过旋转,每个位置都有自己联系的位置,对于每个联系,统计这几个位置的0的个数跟1的个数,答案累加上最小的数,并将这几个位置置为true,不在统计
#include<bits/stdc++.h>
using namespace std;

char g[110][110];
bool st[110][110];

void solve()
{
    memset(st, 0, sizeof st);
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++ )
        for(int j = 0; j < n; j ++ )
            cin >> g[i][j];
    
    int res = 0;
    for(int i = 0; i < n; i ++ )
        for(int j = 0; j < n; j ++ )
        {
            if(st[i][j]) continue;
            int cnt0 = 0, cnt1 = 0;
            if(g[i][j] == '1') cnt1 ++ ;
            else cnt0 ++ ;
            if(g[j][n - i - 1] == '1') cnt1 ++ ;
            else cnt0 ++ ;
            if(g[n - i - 1][n - j - 1]== '1') cnt1 ++ ;
            else cnt0 ++ ;
            if(g[n - j - 1][i] == '1') cnt1 ++ ;
            else cnt0 ++ ;

            res += min(cnt0, cnt1);

            st[i][j] = true;
            st[j][n - i - 1] = true;
            st[n - i - 1][n - j - 1] = true;
            st[n - j - 1][i] = true;
        }
    cout << res << endl;
}

int main()
{
    freopen("test.in", "r", stdin);
    int tt;
    cin >> tt;
    while(tt -- )
    {
        solve();
    }
    return 0;
}

F. Yet Another Problem About Pairs Satisfying an Inequality

计算索引对的数量,使得ai < i < aj < j,先统计都哪个 ai < i,然后前缀和,对于每一个成立的,答案累加前面所有的数量 p[a[i] - 1],因为 i < a[j]
#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];

    vector<int> cnt(n + 1);
    for (int i = 1; i <= n; i++)
        cnt[i] = (i > a[i]);

    vector<int> p(n + 1);
    for (int i = 1; i <= n; i++)
        p[i] = p[i - 1] + cnt[i];

    long long ans = 0;
    for (int i = 1; i <= n; i++)
        if (1 < a[i] && cnt[i])
            ans += p[a[i] - 1];

    cout << ans << endl;
}

int main()
{
    freopen("test.in", "r", stdin);
    int tt;
    cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}

G. Good Key, Bad Key

如果用好钥匙的个数是固定的,那么把钥匙都放到最前面开是最优解

所以就枚举到底用几把好钥匙,然后更新答案

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[100010], s[100010];
void solve()
{
    int n, k;
    cin >> n >> k;
    for(int i = 1; i <= n; i ++ )
    {
        cin >> a[i];
        s[i] = s[i - 1] + a[i] - k;
    }
    ll res = 0;
    for(int i = 0; i <= n; i ++ )
    {
        ll now = s[i];
        for(int j = i + 1; j <= min(n, i + 31); j ++ )
            now += a[j] >> (j - i);
        
        res = max(res, now);
    }
    cout << res << endl;
}

int main()
{
    freopen("test.in", "r", stdin);
    int _t;
    cin >> _t;
    while(_t--)
    {
        solve();
    }
    return 0;
}
目录
相关文章
|
7月前
Codeforces Round #192 (Div. 2) (330A) A. Cakeminator
如果某一行没有草莓,就可以吃掉这一行,某一列没有也可以吃点这一列,求最多会被吃掉多少块蛋糕。
25 0
|
9月前
|
人工智能 算法 BI
Codeforces Round 891 (Div. 3)
Codeforces Round 891 (Div. 3)
92 0
Codeforces Round 891 (Div. 3)
|
9月前
|
机器学习/深度学习 人工智能
Codeforces Round 889 (Div. 2)
Codeforces Round 889 (Div. 2)
129 0
|
12月前
Codeforces Round 799 (Div. 4)
Codeforces Round 799 (Div. 4)
91 0
Codeforces Round 849 (Div. 4)
Codeforces Round 849 (Div. 4)A~G2题解
83 0
Codeforces Round 835 (Div. 4)
Codeforces Round 835 (Div. 4) A~F题解
87 0
Equidistant Vertices-树型dp-Codeforces Round #734 (Div. 3)
Description A tree is an undirected connected graph without cycles. You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words,
119 0