CCF---201912-2---回收站選址---C++---Map優化

試題編號: 201912-2
試題名稱: 回收站選址
時間限制: 1.0s
內存限制: 512.0MB

問題描述:

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

吐槽

在這裏插入圖片描述
老實說這一題數據真的垃圾,很多人都是用數組直接跑過了,數據強點平均分還可以降個30-40左右。

實現代碼

#include<iostream>
#include<map>
#include<cstring>
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;
}
發佈了117 篇原創文章 · 獲贊 19 · 訪問量 6809
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章