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;
}
目录
相关文章
|
安全 Java API
使用 Java 构建强大的 REST API 的四个基本技巧
本文结合探险领域案例,分享Java构建REST API的四大核心策略:统一资源命名、版本控制与自动化文档、安全防护及标准化异常处理,助力开发者打造易用、可维护、安全可靠的稳健API服务。
736 116
|
存储 缓存 运维
Nginx缓存大小限制详解(手把手教你控制缓存空间,避免磁盘爆满)
本文教你如何设置Nginx缓存大小限制,避免缓存无节制增长导致磁盘占满。通过配置proxy_cache_path的max_size等参数,合理控制缓存空间,提升网站性能的同时保障服务器稳定运行,适合小白用户快速上手。
Nginx缓存大小限制详解(手把手教你控制缓存空间,避免磁盘爆满)
|
算法 C++ 容器
C++初阶之一篇文章教会你queue和priority_queue(理解使用和模拟实现)(下)
优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。 此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元素)。 优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器
|
存储 Shell Android开发
基于Android P,自定义Android开机动画的方法
本文详细介绍了基于Android P系统自定义开机动画的步骤,包括动画文件结构、脚本编写、ZIP打包方法以及如何将自定义动画集成到AOSP源码中。
1535 2
基于Android P,自定义Android开机动画的方法
|
人工智能 生物认证 数据安全/隐私保护
AI检测器:我们如何识别机器生成的内容?
AI检测器:我们如何识别机器生成的内容?
946 3
|
人工智能 缓存 自然语言处理
TokenSwift:90分钟生成10万Token!文本生成提速3倍,无损加速黑科技
TokenSwift 是北京通用人工智能研究院团队推出的超长文本生成加速框架,能在90分钟内生成10万Token的文本,速度提升3倍,生成质量无损,支持多种模型架构。
758 16
TokenSwift:90分钟生成10万Token!文本生成提速3倍,无损加速黑科技
|
监控 安全 BI
10 种最常见的 Active Directory (AD) 攻击
Active Directory(AD)是组织信息管理的核心,但在身份验证与访问控制中也面临诸多攻击风险。本文总结了十大常见AD攻击类型,包括基于密码的攻击(暴力破解、密码喷洒)、NTLM认证攻击(哈希传递、中继攻击)、Kerberos认证攻击(Kerberoasting、银票、金票)、复制机制攻击(DCSync、DCShadow)以及勒索软件攻击等,并提供检测与防护建议。为强化AD安全,ManageEngine ADAudit Plus可有效分析威胁、生成报表,助力构建坚固的安全防线。
959 11
|
机器学习/深度学习 编解码 人工智能
SeedVR:高效视频修复模型,支持任意长度和分辨率,生成真实感细节
SeedVR 是南洋理工大学和字节跳动联合推出的扩散变换器模型,能够高效修复低质量视频,支持任意长度和分辨率,生成真实感细节。
1406 16
SeedVR:高效视频修复模型,支持任意长度和分辨率,生成真实感细节
|
Kubernetes 监控 测试技术
k8s学习--基于Ingress-nginx实现灰度发布系统
k8s学习--基于Ingress-nginx实现灰度发布系统
1099 2
k8s学习--基于Ingress-nginx实现灰度发布系统
|
搜索推荐 数据挖掘 双11
淘宝运营进阶秘籍:从业余到专业
淘宝运营涉及市场分析、产品定位、店铺装修、营销推广、客户服务、数据分析等多环节。要脱颖而出,需掌握进阶秘籍。本文从精准定位、店铺装修、定价策略、流量获取、客户服务、数据分析及跨平台合作七大方面深入探讨,助商家实现从平凡到卓越的蜕变。通过目标受众分析、优化店铺形象、合理定价、多种促销手段、提升客户体验、利用数据反馈调整策略以及拓展销售渠道,商家可逐步提升运营能力,在竞争激烈的电商环境中取得成功。
2229 4