一、题目
二、解析
因为set的查找的时间复杂度为O(log n),所以使用set比使用vector更快。
三、代码
#include <iostream>
#include <set>
#include <utility>
using namespace std;
//垃圾点
set<pair<int, int> > trash;
//垃圾点的个数
int n;
//统计评分为0、1、2、3、4的回收站的个数
int cnt[5] = { 0,0,0,0,0 };
int main()
{
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
//插入垃圾点
trash.insert(pair<int, int>(x, y));
}
//cout<<"Trash:"<<endl;
//for(set<pair<int, int> >::iterator it=trash.begin(); it!=trash.end(); it++)
// cout<<(*it).first<<" "<<(*it).second<<endl;
for (set<pair<int, int> >::iterator it = trash.begin(); it != trash.end(); it++) {
int x = (*it).first;
int y = (*it).second;
//判断这个点的上下左右有没有垃圾点
set<pair<int, int> >::iterator u = trash.find(pair<int, int>(x, y + 1));
set<pair<int, int> >::iterator d = trash.find(pair<int, int>(x, y - 1));
set<pair<int, int> >::iterator r = trash.find(pair<int, int>(x + 1, y));
set<pair<int, int> >::iterator l = trash.find(pair<int, int>(x - 1, y));
//设个点的上下左右都有垃圾点,可以作为回收站,并开始评分
if (u != trash.end() && d != trash.end() && r != trash.end() && l != trash.end()) {
int score = 0;
//这个点的右上、右下、左上、左下是否有垃圾点
set<pair<int, int> >::iterator ru = trash.find(pair<int, int>(x + 1, y + 1));
set<pair<int, int> >::iterator rd = trash.find(pair<int, int>(x + 1, y - 1));
set<pair<int, int> >::iterator lu = trash.find(pair<int, int>(x - 1, y + 1));
set<pair<int, int> >::iterator ld = trash.find(pair<int, int>(x - 1, y - 1));
if (ru != trash.end())
score++;
if (rd != trash.end())
score++;
if (lu != trash.end())
score++;
if (ld != trash.end())
score++;
cnt[score]++;
//cout<<"("<<x<<","<<y<<") "<<score<<endl;
}
}
for (int i = 0; i < 5; i++)
cout << cnt[i] << endl;
return 0;
}
输入:
11
9 10
10 10
11 10
12 10
13 10
11 9
11 8
12 9
10 9
10 11
12 11
输出:
0
2
1
0
0