Codeforces Beta Round #95 (Div. 2) E. Yet Another Task with Queens(模拟)

44 篇文章 0 订阅
9 篇文章 0 订阅

题目地址:http://codeforces.com/problemset/problem/131/E

思路:行i,列j,对角线一 i+j,对角线二 i-j 。使用set(有序),若当前皇后对应项(行列对角线)在set一端且元素个数不为1,则该皇后在该项被1皇后攻击。若在set元素中间,则该皇后在该项被2皇后攻击。

#include<set>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+50;
struct Node
{
    int x,y;
    Node(int a=0,int b=0):x(a),y(b) {}
    bool operator < (const Node & rhs) const
    {
        return rhs.y<y;
    }
    bool operator !=(const Node & rhs) const
    {
        return rhs.y!=y;
    }
    bool operator ==(const Node & rhs) const
    {
        return rhs.y==y;
    }
};
int n,m;
int ans[10];
Node P[maxn];
set<Node> A[maxn],B[maxn];
set<Node> C[2*maxn],D[3*maxn];
int solve(const set<Node> & tmp,Node x)
{
    set<Node>::iterator it1;
    set<Node>::iterator it2;
    it1=tmp.begin(),it2=tmp.end(),it2--;
    if((x==(*it1)||x==(*it2))&&(*it1!=*it2)) return 1;
    if(x!=(*it1)&&x!=(*it2)) return 2;
    return 0;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<m; i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        P[i].x=x,P[i].y=y;
        A[x].insert(Node(x,y));
        B[y].insert(Node(y,x));
        C[x+y].insert(Node(x+y,x-y+2*n));
        D[x-y+2*n].insert(Node(x-y+2*n,x+y));
    }
    for(int i=0; i<m; i++)
    {
        int r=P[i].x,c=P[i].y,sum=0;
        sum+=solve(C[r+c],Node(r+c,r-c+2*n));
        sum+=solve(D[r-c+2*n],Node(r-c+2*n,r+c));
        sum+=solve(A[r],Node(r,c))+solve(B[c],Node(c,r));
        ans[sum]++;
    }
    for(int i=0; i<8; i++)
        printf("%d ",ans[i]);
    printf("%d\n",ans[8]);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值