CCF---201912-2---回收站选址---C++---Map优化

试题编号: 201912-2
试题名称: 回收站选址
时间限制: 1.0s
内存限制: 512.0MB

问题描述:

CCF---201912-2---回收站选址---C++---Map优化_第1张图片
CCF---201912-2---回收站选址---C++---Map优化_第2张图片
CCF---201912-2---回收站选址---C++---Map优化_第3张图片
在这里插入图片描述

吐槽

CCF---201912-2---回收站选址---C++---Map优化_第4张图片
老实说这一题数据真的垃圾,很多人都是用数组直接跑过了,数据强点平均分还可以降个30-40左右。

实现代码

#include
#include
#include
using namespace std;

const int maxn = 1e3 + 5;

struct Node{
	int x, y;
	Node(){}
	Node(int a, int b){
		x = a, y = b;
	}
	bool operator < (const Node& other) const{
		if(x != other.x) return x < other.x;
		else return y < other.y;
	}
};

int cnt[5];
int yway[] = {-1,1,0,0};
int xway[] = {0,0,-1,1};
map<Node, int> mp;

bool fun(Node node){
	for(int i = 0; i < 4; i++){
		int x = node.x + xway[i], y = node.y + yway[i];
		if(mp.find(Node(x, y)) == mp.end()) return false;
	}
	if(mp.count(Node(node.x-1,node.y+1))) mp[node]++;
	if(mp.count(Node(node.x-1,node.y-1))) mp[node]++;
	if(mp.count(Node(node.x+1,node.y-1))) mp[node]++;
	if(mp.count(Node(node.x+1,node.y+1))) mp[node]++;
	return true;
}

int main(){
	memset(cnt, 0, sizeof(cnt));
	int n, x, y;
	cin >> n;
	while(n--){
		cin >> x >> y;
		mp[Node(x, y)] = 0;		
	}
	for(map<Node,int>::iterator it = mp.begin(); it != mp.end(); ++it){
		if(fun(it->first)) cnt[it->second]++;
	}
	for(int i = 0; i < 5; i++){
		cout << cnt[i] << endl;
	}
	return 0;
}

你可能感兴趣的:(CCF,C++,map)