Codeforces Round 799 (Div. 4)

简介: Codeforces Round 799 (Div. 4)

A. Marathon

输入三个数字,判断有几个比第一个数字大
#include <bits/stdc++.h>
using namespace std;
void solve(){
    int a, b, cnt = 0;
    cin >> a;
    for(int i = 0; i < 3; i ++ )
    {
        cin >> b;
        if(b > a) cnt ++ ;
    }
    cout << cnt << 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;
}

B. All Distinct

用set存不同个数,如果要删除的个是偶数,就输出set.size();

否则,输出set.size() - 1;

#include<bits/stdc++.h>
using namespace std;
void solve()
{
    int n;
    cin >> n;
    set<int> se;
    for(int i = 0; i < n; i ++ )
    {
        int x;
        cin >> x;
        se.insert(x);
    }
    int res = se.size();
    cout << ((n - res) & 1 ? res - 1 : res) << endl;
}
int main()
{
    freopen("test.in", "r", stdin);
    int _t;
    cin >> _t;
    while(_t--)
    {
        solve();
    }
    return 0;
}

C. Where's the Bishop?

找主教的位置,题目说出主教的位置不在棋盘边缘

故统计每一行#的个数,如果当前行是1,而上下两行是2,就在当前行

遍历一遍寻找y坐标即可

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

char g[8][8];

void solve()
{
    int cnt[8] = {0};
    for(int i = 0; i < 8; i ++ )
    {
        cin >> g[i];
        for(int j = 0; j < 8; j ++ )
        {
            if(g[i][j] == '#')
                cnt[i] ++ ;
        }
    }
    for(int i = 0; i < 8; i ++ )
    {
        if(i == 0 || i == 7) continue;
        if(cnt[i] == 1 && cnt[i - 1] == 2 && cnt[i + 1] == 2)
        {
            for(int j = 0; j < 8; j ++ )
            {
                if(g[i][j] == '#')
                    cout << i + 1 << " " << j + 1 << endl;
            }
        }
    }
}

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

D. The Clock

直接模拟这个过程
#include<bits/stdc++.h>
using namespace std;

void solve()
{
    int h, m, t;
    scanf("%d:%d %d", &h, &m, &t);
    int h1 = h, m1 = m;

    int x = t / 60, y = t % 60;
    int cnt = 0;

    do
    {
        if(h % 10 == m / 10 && m % 10 == h / 10)
            cnt ++ ;
        
        if(m + y >= 60)
        {
            h = (h + x + 1) % 24;
            m = (m + y) % 60;
        }
        else
        {
            h = (h + x) % 24;
            m = (m + y) % 60;
        }
        // cout << h << " " << m << endl;

    } while (h != h1 || m != m1);
    cout << cnt << endl;
}

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

E. Binary Deque

删去最少的数,使得数组和为k

先找到如果左区间定为0,和为k的话,右区间在哪,如果大于数组长度了,说明无解

找到左右区间,进行双指针更新答案

#include <bits/stdc++.h>
using namespace std;
void solve()
{
    int n, k;
    cin >> n >> k;
    int a[n];
    int sum = 0;
    int res = 0;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int l = 0, r = -1;
    for (int i = 0; i < n; i++)
    {
        sum += a[i];
        if (sum == k)
        {
            r = i;
        }
        if (sum > k)
            break;
    }
    if (r == -1)
    {
        cout << -1 << endl;
        return;
    }
    res = max(res, r - l + 1);
    while (r >= l && r < n)
    {
        r++;
        sum += a[r];
        while (sum > k)
        {
            sum -= a[l];
            l++;
        }
        res = max(res, r - l + 1);
    }
    cout << n - res << endl;
}
int main()
{
    freopen("test.in", "r", stdin);
    int _t;
    cin >> _t;
    while (_t--)
    {
        solve();
    }
    return 0;
}

F. 3SUM

因为题目只在乎模十后的数,我们就只用模十后的数,模十后相同的数我们最多存3个

这样数组中最多也就30个数,直接n^3暴力求解

#include <bits/stdc++.h>
using namespace std;
void solve()
{
    int n;
    cin >> n;
    vector<int> a;
    vector<int> cnt(10, 0);
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        if (cnt[x % 10] < 3)
        {
            a.push_back(x % 10);
            cnt[x % 10] ++ ;
        }
    }
    n = a.size();
    for(int i = 0; i < n; i ++ )
        for(int j = i + 1; j < n; j ++ )
            for(int k = j + 1; k < n; k ++ )
                if((a[i] + a[j] + a[k]) % 10 == 3)
                {
                    cout << "YES" << endl;
                    return;
                }
    cout << "NO" << 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;
}

G. 2^Sort

找到一个长度为k+1的子数组,满足每个元素小于下一个元素的两倍
#include <bits/stdc++.h>
using namespace std;
void solve(){
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for(int i = 0; i < n; i ++ ) cin >> a[i];

    int cur = 1, cnt = 0;
    for(int i = 0; i < n - 1; i ++ )
    {
        if(a[i] < a[i + 1] * 2)
        {
            cur ++ ;
            cnt += (cur >= k + 1);
        }
        else cur = 1;
    }
    cout << cnt << 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;
}

H. Gambling

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

void solve(){
    int n;
    cin >> n;
    map<int, vector<int>> mp;
    for(int i = 1; i <= n; i ++ )
    {
        int x;
        cin >> x;
        mp[x].push_back(i);
    }

    int l, r, ans, maxv = 0;
    for(auto [x, a] : mp)
    {
        int t = -2e9, pos;
        for(int i = 0; i < a.size(); i ++ )
        {
            if(a[i] - 2 * i > t)
            {
                t = a[i] - 2 * i;
                pos = a[i];
            }
            int now = 2 * i - a[i] + t + 1;
            if(now > maxv)
            {
                maxv = now;
                ans = x;
                l = pos;
                r = a[i];
            }
        }
    }
    cout << ans << " " << l << " " << r << 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;
}
目录
相关文章
|
自然语言处理 安全 C++
【C++ 格式化输出 】C++20 现代C++格式化:拥抱std--format简化你的代码
【C++ 格式化输出 】C++20 现代C++格式化:拥抱std--format简化你的代码
10113 4
|
UED
鸿蒙next版开发:相机开发-适配不同折叠状态的摄像头变更(ArkTS)
在HarmonyOS 5.0中,ArkTS提供了强大的相机开发能力,特别是针对折叠屏设备的摄像头适配。本文详细介绍了如何在ArkTS中检测和适配不同折叠状态下的摄像头变更,确保相机应用在不同设备状态下的稳定性和用户体验。通过代码示例展示了具体的实现步骤。
463 8
|
JavaScript
Vue 双向数据绑定原理
Vue的双向数据绑定通过其核心的响应式系统实现,主要由Observer、Compiler和Watcher三个部分组成。Observer负责观察数据对象的所有属性,将其转换为getter和setter;Compiler解析模板指令,初始化视图并订阅数据变化;Watcher作为连接Observer和Compiler的桥梁,当数据变化时触发相应的更新操作。这种机制确保了数据模型与视图之间的自动同步。
|
自然语言处理 Java 数据处理
【速收藏】python字符串操作,你会几个?
【速收藏】python字符串操作,你会几个?
404 7
|
安全 网络安全 网络虚拟化
Cisco-三层交换机实现VLAN间路由
Cisco-三层交换机实现VLAN间路由
470 0
|
设计模式 算法 数据库连接
后端开发中的设计模式应用与实践
在软件开发的广袤天地中,设计模式如同夜空中最亮的星辰,引领着开发者们穿越复杂系统的迷雾。本文旨在通过深入浅出的方式,不仅探讨设计模式的理论精髓,揭示它们在后端架构中的重要性,还将以生动的实践案例,展示如何在实际项目中巧妙运用这些模式。我们邀请您一同踏上这场编程之旅,探索如何借助设计模式的力量,让后端系统更加健壮、灵活且易于维护,共同揭开后端技术神秘面纱的一角。
|
开发者 算法 虚拟化
惊爆!Uno Platform 调试与性能分析终极攻略,从工具运用到代码优化,带你攻克开发难题成就完美应用
【8月更文挑战第31天】在 Uno Platform 中,调试可通过 Visual Studio 设置断点和逐步执行代码实现,同时浏览器开发者工具有助于 Web 版本调试。性能分析则利用 Visual Studio 的性能分析器检查 CPU 和内存使用情况,还可通过记录时间戳进行简单分析。优化性能涉及代码逻辑优化、资源管理和用户界面简化,综合利用平台提供的工具和技术,确保应用高效稳定运行。
389 0
|
定位技术 数据处理
适用于UE的wgs84坐标系快捷拾取方法
UE开发中,为了精确的地理定位,常用到WGS84坐标系。而常规地图软件的拾取坐标不适用于UE,因此掌握WGS84坐标转换至关重要。与大家分享一个两步快速拾取WGS84坐标的方法~
|
搜索推荐 算法
排序算法:归并排序(递归和非递归)
排序算法:归并排序(递归和非递归)
641 0
|
安全 Java 数据安全/隐私保护
快速掌握 WinRAR:详细安装与使用指南
**WinRAR 摘要** WinRAR 是全能压缩工具,支持多格式,如 RAR, ZIP 等。要下载,访问 &lt;https://www.win-rar.com&gt; 选择适合的操作系统和语言。安装时,定制路径和选项,如桌面快捷方式。启动后,通过“选项”-&gt;“设置”配置首选项。使用上,能新建压缩文件,设定格式和选项,也可解压文件到指定目录。遇到问题,如文件损坏,可利用 WinRAR 的修复功能。本文提供下载、安装和使用指导,确保用户顺利操作。