码蹄杯刷题

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;
}
相关推荐
刘琦沛在进步9 分钟前
【C / C++】引用和函数重载的介绍
c语言·开发语言·c++
我在人间贩卖青春1 小时前
C++之this指针
c++·this
爱敲代码的TOM1 小时前
数据结构总结
数据结构
云姜.1 小时前
java多态
java·开发语言·c++
CoderCodingNo1 小时前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法
陳10301 小时前
C++:红黑树
开发语言·c++
大闲在人1 小时前
7. 供应链与制造过程术语:“周期时间”
算法·供应链管理·智能制造·工业工程
一切尽在,你来1 小时前
C++ 零基础教程 - 第 6 讲 常用运算符教程
开发语言·c++
小熳芋1 小时前
443. 压缩字符串-python-双指针
算法
Charlie_lll1 小时前
力扣解题-移动零
后端·算法·leetcode