码蹄杯刷题

MC0102房间打扫

贪心解法:统计每列0的个数,超过一半就打扫

有3个测试点会WA

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int res = 0;
    vector<vector<char>> s(n, vector<char>(n));
    vector<int> cnt(n);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> s[i][j];
            if (s[i][j] == '0')
            {
                cnt[j]++;
            }
        }
    }
    for (int j = 0; j < n; j++)
    {
        if (cnt[j] * 2 > n)
        {
            for (int i = 0; i < n; i++)
            {
                if (s[i][j] == '1')
                {
                    s[i][j] = '0';
                }
                else
                {
                    s[i][j] = '1';
                }
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        bool flag = true;
        for (int j = 0; j < n; j++)
        {
            if (s[i][j] == '0')
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            res++;
        }
    }
    cout << res;
    return 0;
}

正确解法是哈希表

统计相同的字符串

cpp 复制代码
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
int main()
{
    unordered_map<string, int> map;
    int n;
    cin >> n;
    int res = 0;
    string s;
    for (int i = 0; i < n; i++)
    {
        cin >> s;
        map[s]++;
        res = max(res, map[s]);
    }
    cout << res;
    return 0;
}

MC0104项链

cpp 复制代码
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    int n;
    cin >> n;
    vector<int> s(n);
    for (int i = 0; i < n; i++)
    {
        cin >> s[i];
    }
    sort(s.begin(), s.end());
    int high = 0;
    int low = 0;
    for (int i = 0; i < n / 2; i++)
    {
        low += s[i];
    }
    for (int i = n - n / 2; i < n; i++)
    {
        high += s[i];
    }
    cout << 2 * (high - low);
    return 0;
}
相关推荐
人工智能培训2 小时前
如何持续、安全地向大模型注入新知识?
人工智能·python·算法·大模型·大模型学习·大模型应用工程师·大模型工程师证书
Remember_9932 小时前
【数据结构】Java对象比较全解析:从equals到Comparable与Comparator,再到PriorityQueue应用
java·开发语言·数据结构·算法·leetcode·哈希算法
郝学胜-神的一滴2 小时前
深入浅出网络协议:从OSI七层到TCP/IP五层模型全解析
开发语言·网络·c++·网络协议·tcp/ip·程序人生
夏乌_Wx2 小时前
练题100天——DAY39:单链表练习题×5
c语言·数据结构·算法·链表
txinyu的博客2 小时前
布隆过滤器
数据结构·算法·哈希算法
jojo_zjx2 小时前
GESP 25年12月1级 手机电量显示
c++
52Hz1182 小时前
力扣240.搜索二维矩阵II、160.相交链表、206.反转链表
python·算法·leetcode
程序员zgh2 小时前
C语言 弱定义机制 解读
c语言·开发语言·c++
We་ct2 小时前
LeetCode 380. O(1) 时间插入、删除和获取随机元素 题解
前端·算法·leetcode·typescript