Codeforces Round 817 (Div. 4)

简介: Codeforces Round 817 (Div. 4)A~G题解

A. Spell Check

把字符串都排序,查看是否和给定的字符串排序过后一样
#include <bits/stdc++.h>
using namespace std;
string str = "Timur";
void solve(){
    int n;
    string s;
    cin >> n >> s;
    sort(str.begin(), str.end());
    sort(s.begin(), s.end());
    if(str == s) puts("YES");
    else puts("NO");
}
int main()
{
    freopen("test.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _t = 1;
    cin >> _t;
    while(_t -- ){
        solve();
    }
    return 0;
}

B. Colourblindness

因为分不清G和B,所以遍历一遍,把G全部换成B,在比较两个字符串是否相同
#include <bits/stdc++.h>
using namespace std;
void solve()
{
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    for (int i = 0; i < a.size(); i++)
        if (a[i] == 'G')
            a[i] = 'B';
    for (int i = 0; i < b.size(); i++)
        if (b[i] == 'G')
            b[i] = 'B';
    
    if(a == b) puts("YES");
    else puts("NO");
}
int main()
{
    freopen("test.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _t = 1;
    cin >> _t;
    while (_t--)
    {
        solve();
    }
    return 0;
}

C. Word Game

用哈希表统计一下每个字符串出现的次数,然后分别查看每个人的字符串去计算每个人的分数
#include <bits/stdc++.h>
using namespace std;
void solve(){
    int n;
    cin >> n;
    unordered_map<string, int> cnt;
    vector<string> a[3];
    for(int i = 0; i < 3; i ++ )
        for(int j = 0; j < n; j ++ )
        {
            string s;
            cin >> s;
            cnt[s] ++ ;
            a[i].push_back(s);
        }
    
    for(int i = 0; i < 3; i ++ )
    {
        int point = 0;
        for(int j = 0; j < n; j ++ )
        {
            string s = a[i][j];
            if(cnt[s] == 1) point += 3;
            if(cnt[s] == 2) point += 1;
        }
        cout << point << " ";
    }
    cout << endl;
}
int main()
{
    freopen("test.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _t = 1;
    cin >> _t;
    while(_t -- ){
        solve();
    }
    return 0;
}

D. Line

a数组先记录一下当前每个位置能看几个人,ans记录一下当前看的人的总和

循环数组a判断一下,如果换方向会变大就存下了,存到v数组中去

将v数组从大到小排序,每次加最大的输出,加完后面就不在变化

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N];
void solve()
{
    int n;
    string s;
    cin >> n >> s;
    long long ans = 0;
    for (int i = 0; i < n; i++)
        if (s[i] == 'L')
        {
            a[i] = i;
            ans += i;
        }
        else
        {
            a[i] = n - 1 - i;
            ans += n - 1 - i;
        }
    
    vector<int> v;
    for(int i = 0; i < n; i ++ )
    {
        if(a[i] < n - 1 - a[i])
            v.push_back(n - 1 - a[i] - a[i]);
    }
    sort(v.begin(), v.end(), greater<int>());

    int len = v.size();
    for(int i = 0; i < n; i ++ )
    {
        if(i < len)
            ans += v[i];
        cout << ans << " ";
    }
    cout << endl;
}
int main()
{
    freopen("test.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _t = 1;
    cin >> _t;
    while (_t--)
    {
        solve();
    }
    return 0;
}

E. Counting Rectangles

给好多个矩形,然后给出个小矩形,在给出个大矩形,问比小矩形大而且比大矩形小的矩形面积总和是多少

把每个矩形的权值存到每个矩形的右上角,权值就是矩形的面积,然后求小矩形跟大矩形之间矩形的和

#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
long long a[N][N];
void solve(){
    int n, m;
    cin >> n >> m;
    memset(a, 0, sizeof a);
    while(n -- )
    {
        int x, y;
        cin >> x >> y;
        a[x][y] += (long long)x * y;
    }
    for(int i = 1; i <= 1000; i ++ )
        for(int j = 1; j <= 1000; j ++ )
            a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
    while(m -- )
    {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        x1 ++ , y1 ++ ;
        x2 -- , y2 -- ;
        cout << a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1] << endl;
    }
}
int main()
{
    freopen("test.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int _t = 1;
    cin >> _t;
    while(_t -- ){
        solve();
    }
    return 0;
}

F. L-shapes

暴力,如果见到*,就查看它周围与它连通的有几个,如果是三个且符合要求就继续循环

否则就直接输出NO,然后退出

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

char g[55][55];

bool get(int x, int y)
{
    int cnt = 0;
    for (int i = x - 1; i <= x + 1; i++)
        for (int j = y - 1; j <= y + 1; j++)
            if (g[i][j] == '*')
                cnt++;

    if (cnt == 3)
    {
        if (g[x][y - 1] == g[x - 1][y] && g[x][y - 1] == '*')
            return 1;
        if (g[x][y + 1] == g[x - 1][y] && g[x][y + 1] == '*')
            return 1;
        if (g[x][y - 1] == g[x - 1][y - 1] && g[x][y - 1] == '*')
            return 1;
        if (g[x - 1][y] == g[x - 1][y - 1] && g[x - 1][y] == '*')
            return 1;
        if (g[x][y + 1] == g[x - 1][y + 1] && g[x][y + 1] == '*')
            return 1;
        if (g[x - 1][y + 1] == g[x - 1][y] && g[x - 1][y] == '*')
            return 1;
        if (g[x][y - 1] == g[x + 1][y] && g[x + 1][y] == '*')
            return 1;
        if (g[x][y - 1] == g[x + 1][y - 1] && g[x][y - 1] == '*')
            return 1;
        if (g[x + 1][y - 1] == g[x + 1][y] && g[x + 1][y] == '*')
            return 1;
        if (g[x][y + 1] == g[x + 1][y] && g[x][y + 1] == '*')
            return 1;
        if (g[x + 1][y + 1] == g[x + 1][y] && g[x + 1][y] == '*')
            return 1;
        if (g[x][y + 1] == g[x + 1][y + 1] && g[x][y + 1] == '*')
            return 1;
        return 0;
    }
    else
        return 0;
}

void solve()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i <= n + 1; i++)
        for (int j = 0; j <= m + 1; j++)
            g[i][j] = '.';

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> g[i][j];

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (g[i][j] == '*' && !get(i, j))
            {
                cout << "NO" << endl;
                return;
            }
    cout << "YES" << endl;
    return;
}

int main()
{
    int tt;
    cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}

G. Even-Odd XOR

构造题,构造一个长度为n的数列,使得奇数索引上的元素的按位异或等于偶数索引上元素的按位异或

发现从0开始,每四个数可以按0 1 3 2 这样的序列排序,这样就需要讨论余数是几了

如果余数是零,就每四个按这个顺序输出

如果余数是一,从四开始每四个按这个顺序输出,最后输出一个0

如果余数是二,从16开始,往后每四个一输出,留下六个,最后输出4 1 2 12 3 8

如果余数是三,从4开始,往后每四个一输出,留下三个,最后输出2 1 3

#include<bits/stdc++.h>
using namespace std;
 
void solve()
{
    int n;
    cin >> n;
    if(n % 4 == 0)
    {
        for(int i = 0; i < n; i += 4)
            cout << i << " " << i + 1 << " " << i + 3 << " " << i + 2 << " ";
        puts("");
    }
    if(n % 4 == 1)
    {
        for(int i = 4; i < n; i += 4)
            cout << i << " " << i + 1 << " " << i + 3 << " " << i + 2 << " ";
        cout << "0" << endl;
    }
    if(n % 4 == 2)
    {
        for(int i = 16; i < n + 16 - 6; i += 4)
            cout << i << " " << i + 1 << " " << i + 3 << " " << i + 2 << " ";
        cout << "4 1 2 12 3 8\n";
    }
    if(n % 4 == 3)
    {
        for(int i = 4; i < n; i += 4)
            cout << i << " " << i + 1 << " " << i + 3 << " " << i + 2 << " ";
        cout << "2 1 3" << endl;
    }
}
 
int main()
{
    freopen("test.in", "r", stdin);
    int tt;
    cin >> tt;
    while(tt -- )
    {
        solve();
    }
    return 0;
}
目录
相关文章
|
10月前
Codeforces Round #178 (Div. 2)
在n条电线上有不同数量的鸟, Shaass开了m枪,每一枪打的是第xi条电线上的第yi只鸟,然后被打中的这只鸟左边的飞到第i-1条电线上,右边的飞到i+1条电线上,没有落脚点的鸟会飞走。
40 0
|
11月前
Codeforces Round #742 (Div. 2)
Codeforces Round #742 (Div. 2)
33 0
|
机器学习/深度学习 人工智能
Codeforces Round 889 (Div. 2)
Codeforces Round 889 (Div. 2)
143 0
|
人工智能 索引
Codeforces Round 806 (Div. 4)
Codeforces Round 806 (Div. 4)A~G
103 0
Codeforces Round #640 (Div. 4)
Codeforces Round #640 (Div. 4)
78 0