acwing757果然刚刚开始就翻车了

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
int main(){
    int n,m;
    cin>>n>>m;
    //哈希表,坐标奇数偶数位置
    unordered_map<int,int>AS,BS;
    
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int x;
            cin>>x;
            if((i+j)%2) AS[x]++;
            else BS[x]++;
        }
    }
    //出现次数和数字,哈希表转成数组
    vector<PII> A,B;
    for(auto item:AS) A.push_back({item.second,item.first});
    for(auto item:BS) B.push_back({item.second,item.first});
    
    //排序
    sort(A.begin(),A.end()),reverse(A.begin(),A.end());
    sort(B.begin(),B.end()),reverse(B.begin(),B.end());
    
    int res=0;
    for(int i=0;i<2&&i<A.size();i++){
        for(int j=0;j<2&&j<B.size();j++){
        //数字相同选最大
        if(A[i].second==B[j].second) res=max(res,max(A[i].first,B[j].first));
        //不同时,次数相加,都不改
        else res=max(res,A[i].first+B[j].first);
        }
    }
    //只有偶数或者奇数时
    if(A.empty()) res=B[0].first;
    if(B.empty()) res=A[0].first;
    
    cout<<n*m-res<<endl;
}
全部评论

相关推荐

昨天 16:41
复旦大学 Python
ynq2126:我一直觉得现在考算法题没啥意义 真要选拔人才不如把公司实际项目中遇到的问题当成一系列场景题抛给求职者答 这才是能检测能力的东西
点赞 评论 收藏
分享
05-15 14:58
已编辑
南昌航空大学科技学院 C++
mcart:上海150怎么活,睡公司吗
点赞 评论 收藏
分享
好骗:没关系的,同26届,我是准备回老家那边做一个c#老系统了,月薪估计4000多,跟自己过得去,每天开心才是人生,java确实太卷不是你的问题
我的求职总结
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务