AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 商店
  • 应用
  • 文章
    • 题解
    • 分享
    • 问答
  • 吐槽
  • 登录/注册

AcWing 3283. 回收站选址    原题链接    简单

作者: 作者的头像   defineシツ0x7fffffff ,  2022-08-06 22:47:28 ,  所有人可见 ,  阅读 14


0


解法

运用到了 multimap 和 pair。

C++ 代码

#include<cstdio>
#include<algorithm>
#include<map>
#define m(x,y) make_pair(x,y)//减少打字量
using namespace std;
using pii = pair<int, int>;
multimap<pii, int>g;//存下所有点
int res[5];//0-4有5个数
int main(){
    int n, x, y; scanf("%d", &n);//输入坐标数
    for (int i = 1; i <= n; ++i)
        scanf("%d%d", &x, &y), g.insert(m(m(x, y), 1));//将坐标加入集合之中
    for (auto c = g.begin(); c != g.end(); c++) {//离线遍历集合
        auto coor = c->first; int x = coor.first, y = coor.second;//取出坐标
        auto f1 = g.find(m(x + 1, y)), f2 = g.find(m(x - 1, y)),
             f3 = g.find(m(x, 1 + y)), f4 = g.find(m(x, y - 1));//不容易错的写法
        if (f1 != g.end() and f2 != g.end() and f3 != g.end() and f4 != g.end())
        {//当前点的上下左右均在集合之中
            int tmp = 0;//为了算出桶数组的标号
            f1 = g.find(m(x + 1, y + 1)), f2 = g.find(m(x + 1, y - 1)),
            f3 = g.find(m(x - 1, y + 1)), f4 = g.find(m(x - 1, y - 1));
            if (f1 != g.end())tmp++;if (f2 != g.end())tmp++;
            if (f3 != g.end())tmp++;if (f4 != g.end())tmp++;
            res[tmp]++;}
    }
    for (int i = 0; i <= 4; ++i, puts(""))printf("%d", res[i]);//输出答案
    return 0;}

0 评论

你确定删除吗?

© 2018-2022 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息