垃圾站回收(ccf)

题目大意:只有上下左右全部有坐标存在的时候,对应的该坐标才可以被选为垃圾站,同时该坐标的四个对角代表分数(0,1,2,3,4),求分数为0,1,2,3,4的垃圾站个数

收获点:

  1. 安全性较高的文件输入输出流
  2. map和array的组合使用(出发点是尽可能为了整合信息)
#include <iostream>
#include <stdio.h>
#include <map>
#include <array>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;

//输入所有垃圾站的个数以及坐标,输出得分为0,1,2,3,4垃圾站的个数。
//为了尽可能的缩小时间复杂度,可以使用哈希表(map),争取循环一遍过。一边扫描一边处理。
int main()
{
	FILE *stream1;
	FILE *stream2;
	freopen_s(&stream1,"input.txt", "r" ,stdin);
	freopen_s(&stream2, "output.txt", "w", stdout);
	map< array<ll, 2>, array<ll, 2> > hash;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		array<ll, 2> temp;
		cin >> temp[0];
		cin >> temp[1];
		int cornum = 0;
		int neinum = 0;

		//遍历已有的表项,修正已有的对角的个数和上下左右相邻的个数
		for (map< array<ll, 2>, array<ll, 2> >::iterator it = hash.begin(); it != hash.end(); it++)
		{
			array<ll, 2> temp1 = (*it).first;
			if (abs(temp[0] - temp1[0]) == 1 && abs(temp[1] - temp1[1]) == 1)//对角位置
			{
				cornum++;
				hash[temp1][0]++;
			}
			else if (abs(temp[0] - temp1[0]) == 1&& abs(temp[1] - temp1[1]) ==0 || abs(temp[1] - temp1[1]) == 1&& abs(temp[0] - temp1[0]) == 0)
			{
				neinum++;
				hash[temp1][1]++;
			}
		}

		hash.insert({ temp,{cornum,neinum} });

	}


	int ans[10] = { 0 };

	for (map< array<ll, 2>, array<ll, 2> >::iterator it = hash.begin(); it != hash.end(); it++)
	{
		array<ll, 2> temp2 = (*it).second;
		if (temp2[1] == 4)
		{
			ans[temp2[0]]++;
		}
	}

	for (int i = 0; i < 5; i++)
	{
		cout << ans[i] << endl;
	}
	fclose(stdin);
	fclose(stdout);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,问题描述为201912-2回收站选址问题。该问题要求统计每种得分的选址个数。具体来说,给定了待清理的垃圾位置,需要判断每个位置是否适合建立回收站,条件包括:该位置必须是整数坐标且存在垃圾;上下左右四个邻居位置必须全部存在垃圾;四个对角位置中存在垃圾的个数作为该位置的得分。最后需要统计每种得分的选址个数。 根据问题描述,我们需要读取输入数据并进行处理。输入的第一行是一个整数n,表示待清理的垃圾位置的个数。接下来的n行,每行包含两个整数xi和yi,表示每个垃圾位置的坐标。 然后,我们可以使用嵌套循环来遍历每个垃圾位置,判断其是否满足建立回收站的条件。对于每个位置,我们可以检查该位置及其上下左右四个邻居位置是否存在垃圾,并统计存在垃圾的个数。 最后,我们可以使用一个数组来记录每种得分的选址个数,根据每个位置的得分进行计数。 请注意,这只是问题的大致解决方法,具体的实现细节可能还需要进行进一步的分析和编码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [2019-12-2:回收站选址【map】【set】](https://blog.csdn.net/han_hhh/article/details/108327976)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [CCF_Java_201912-2_回收站选址](https://blog.csdn.net/weixin_43662429/article/details/104051247)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值