【CCF历年题题解】201912-2 回收站选址

算法思路:

观察到n只有1000,用map<PII,bool> st来判断当前点是否出现。(这种嵌套的ST我也是第一次使用,用unordered_map不行)

#include <iostream>
#include <unordered_map>
#include <map>

using namespace std;

const int N = 1010;

typedef pair<int,int> PII;
PII p[N];
int cnt[5];
map<PII,bool> st; // 当前点是否出现

int main()
{
    
    
    int n ;
    cin >> n;
    for(int i=0;i<n;i++)
    {
    
    
        int x,y;
        cin >> x >> y;
        p[i] = {
    
    x,y};
        st[p[i]] = true;
    }
    
    for(int i=0;i<n;i++)
    {
    
    
        int x = p[i].first, y = p[i].second;
        
        // 如果四边都存在垃圾点
        if(st[{
    
    x+1,y}] && st[{
    
    x-1,y}] && st[{
    
    x,y+1}] && st[{
    
    x,y-1}])
            cnt[st[{
    
    x+1,y+1}] + st[{
    
    x+1,y-1}] + st[{
    
    x-1,y+1}] + st[{
    
    x-1,y-1}]] ++; // 评分++
    }
    
    for(int i=0;i<5;i++) cout<<cnt[i]<<endl;
    
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43154149/article/details/107960937