计蒜之道 2017第四场B 商汤科技的行人检测(简单)

https://nanti.jisuanke.com/t/15551
很简单的一道题。一开始就想用map储存结构体。一直报错也没找出问题来。后来知道因为map底层是红黑树,内部是会自动排序的。要对结构体重载一下<运算符。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
struct node{
    ll dx;
    ll dy;
    node(int x,int y){
        dx=x;
        dy=y;
    }
    bool operator <(const node &b)const{
        return dx==b.dx?dy<b.dy:dx<b.dx;
    }
};
map<node,int>p;
int n,a,b,c,d;
int main(){
    scanf("%d",&n);
    p.clear();
    while(n--){
        scanf("%d%d%d%d",&a,&b,&c,&d);
        p[node(c-a,d-b)]++;
    }
    map<node,int>::iterator it;
    node ans(0,0);
    int cnt=0;
    for(it=p.begin();it!=p.end();it++){
        if(cnt<(it->second)){
            ans=it->first;
            cnt=it->second;
        }
    }
    printf("%lld %lld\n",ans.dx,ans.dy);
    return 0;
}

差不多的思路。 不用map的做法。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
struct data{
    ll dx,dy;
}h[100005];
int cmp(struct data p,struct data q){
    return p.dx==q.dx?p.dy<q.dy:p.dx<q.dx;
}
int main(){
    int n,a,a1,b,b1;
    while(~scanf("%d",&n)){
        memset(h,0,sizeof(h));
        for(int i=0;i<n;i++){
            cin>>a>>b>>a1>>b1;
            h[i].dx=(a1-a);
            h[i].dy=(b1-b);
        }
        sort(h,h+n,cmp);
        struct data temp,ans;
        int maxn=-1,kl=-1;
        for(int i=0;i<n;i++){
            if(i==0||(temp.dx!=h[i].dx||temp.dy!=h[i].dy)){
                if(maxn<kl){
                    ans=temp;
                    maxn=kl;
                }
                temp=h[i];
                kl=1;
            }
            else{
                kl++;
            }
        }
        if(maxn<kl){
            ans=temp;
        }
        cout<<ans.dx<<" "<<ans.dy<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值