CCF 201912-2垃圾站选址

在这里插入图片描述



#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
typedef pair<int, int> P;
int score[6];
vector<P> v;
bool cmp(pair<int,int>a,pair<int,int>b){
    if(a.first!=b.first)return a.first<b.first;
    else return a.second<b.second;
}
//按照第一元素从小到大排
//第一元素相同按照第二元素从小到大排
int getScore(int pos) {
    int scoreR = 0, scoreL = 0;
    int x = v[pos].first, y = v[pos].second;
    //保证前后左右都有垃圾
    bool up = false, down = false, left = false, right = false;
    //分左右
    //左
    for (int i = pos; i >= 0; i--) {
        int tempx = v[i].first, tempy = v[i].second;
        if (tempx < x - 1)
            break;
        if (tempx == x - 1 && tempy == y)//左
            left = true;
        if (tempx == x - 1 && tempy == y + 1)//左上
            scoreL++;
        if (tempx == x - 1 && tempy == y - 1)//左下
            scoreL++;
        if (tempx == x && tempy == y - 1)//下
            up = true;
    }
    //右
    for (int i = pos; i < v.size(); i++) {
        int tempx = v[i].first, tempy = v[i].second;
        if (tempx > x + 1)
            break;
        if (tempx == x + 1 && tempy == y)//右
            right = true;
        if (tempx == x + 1 && tempy == y + 1)//右上
            scoreR++;
        if (tempx == x + 1 && tempy == y - 1)//右下
            scoreR++;
        if (tempx == x && tempy == y + 1)//上
            down = true;
    }



    if (up && down && left && right)
        return scoreL + scoreR;
    else return -1;


}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        P tempP;
        scanf("%d%d", &tempP.first, &tempP.second);
        v.push_back(tempP);
    }
    //从小到大排序
    sort(v.begin(), v.end(),cmp);
    for (int i = 0; i < v.size(); i++) {

        int type = getScore(i);
        if (type > -1)
            score[type]++;
    }
    for (int i = 0; i < 5; i++) {
        printf("%d\n", score[i]);
    }
    return 0;
}
/*
7
1 2
2 1
0 0
1 1
1 0
2 0
0 1


11
9 10
10 10
11 10
12 10
13 10
11 9
11 8
12 9
10 9
10 11
12 11

 */

测试结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有胡子的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值