lj

普通版

#include<stdio.h>
#include<algorithm>
const int N=5e3+5;
int n,px[N],py[N],cnt[5];
struct dat{
    int x,y;
    dat(int x=0,int y=0):x(x),y(y){}
    bool operator <(const dat &a)const{
        return x!=a.x?x<a.x:y<a.y;
    }
    bool operator ==(const dat &a)const{
        return x==a.x&&y==a.y;
    }
}po[N];
inline int search(int x,int y){
    int p=std::lower_bound(po+1,po+n+1,dat(x,y))-po;
    return po[p]==dat(x,y);
}
inline int judge(const int &x,const int &y){
    if(!search(x-1,y)) return 0;
    if(!search(x+1,y)) return 0;
    if(!search(x,y-1)) return 0;
    if(!search(x,y+1)) return 0;
    return 1;
}
inline int calc(const int &x,const int &y){
    return search(x-1,y-1)+search(x+1,y-1)+search(x-1,y+1)+search(x+1,y+1);
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d%d",px+i,py+i),po[i]=dat(px[i],py[i]);
    std::sort(po+1,po+n+1);
    for(int i=1,t;i<=n;i++){
        if(judge(px[i],py[i])){
            t=calc(px[i],py[i]);
            cnt[t]++;
        }
    }
    for(int i=0;i<5;i++) printf("%d\n",cnt[i]);
    return 0;
}

set版

#include<stdio.h>
#include<set>
#include<algorithm>
#define mp make_pair
using namespace std;
const int N=5e3+5;
int n,px[N],py[N],cnt[5];
set<pair<int,int>>s;
inline int search(int x,int y){
    return s.find(mp(x,y))!=s.end(); 
}
inline int judge(const int &x,const int &y){
    if(!search(x-1,y)) return 0;
    if(!search(x+1,y)) return 0;
    if(!search(x,y-1)) return 0;
    if(!search(x,y+1)) return 0;
    return 1;
}
inline int calc(const int &x,const int &y){
    return search(x-1,y-1)+search(x+1,y-1)+search(x-1,y+1)+search(x+1,y+1);
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) 
        scanf("%d%d",px+i,py+i),s.insert(mp(px[i],py[i]));
    for(int i=1,t;i<=n;i++){
        if(judge(px[i],py[i])){
            t=calc(px[i],py[i]);
            cnt[t]++;
        }
    }
    for(int i=0;i<5;i++) printf("%d\n",cnt[i]);
    return 0;
}

asd

猜你喜欢

转载自www.cnblogs.com/shenben/p/12755946.html
lj
JJ
bj
DJ
kj,
LA
LD
lb
cj
le
今日推荐