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

17 篇文章 0 订阅
E. Yet Another Task with Queens
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop.

There are m queens on a square n × n chessboard. You know each queen's positions, the i-th queen is positioned in the square (ri, ci), where ri is the board row number (numbered from the top to the bottom from 1 to n), and ci is the board's column number (numbered from the left to the right from 1 to n). No two queens share the same position.

For each queen one can count w — the number of other queens that the given queen threatens (attacks). For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack. Obviously, for any queen w is between 0 and 8, inclusive.

Print the sequence t0, t1, ..., t8, where ti is the number of queens that threaten exactly i other queens, i.e. the number of queens that their w equals i.

Input

The first line of the input contains a pair of integers n, m (1 ≤ n, m ≤ 105), where n is the size of the board and m is the number of queens on the board. Then m following lines contain positions of the queens, one per line. Each line contains a pair of integers ri, ci (1 ≤ ri, ci ≤ n) — the queen's position. No two queens stand on the same square.

Output

Print the required sequence t0, t1, ..., t8, separating the numbers with spaces.

Examples
Input
8 4
4 3
4 8
6 5
1 6
Output
0 3 0 1 0 0 0 0 0 
Input
10 3
1 1
1 2
1 3
Output
0 2 1 0 0 0 0 0 0 
大体题意:
给你一个n×n 的棋盘,上面有m 个皇后,并且告诉你m 个皇后的具体位置!
每个皇后能攻击八个方向,问每个方向受到威胁的皇后有多少个(大体上这样,具体在仔细读读吧!)
思路:
根据八皇后来思考,每个皇后占据了一个位置,假设她在u 行v列,
那么第u 行  第v 列, 斜线 u+v  斜线 u-v+n均受到威胁!
但是还要考虑每行上的方向问题,
仔细想想可以发现:处在第u 行的皇后  他们的列数是不同的,可以根据这一点进行排序。
最后统计这个皇后时 如果发现他在第一个或者最后一个的话,那么方向只能+1
否则+2
关于处理位置可以用set,第一次用了vector 结果四次sort直接超时,用set 直接取begin 和rbegin 就好了!
代码比赛时写的 写的很乱~~
#include<bits/stdc++.h>
using namespace std;
const int maxn = 500000 + 10;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
typedef long long ll;
struct Node {
    int u,v;
    Node (int u,int v):u(u),v(v){}
    Node ():u(0),v(0){}
    bool operator < (const Node & rhs) const {
        return v < rhs.v;
    }
};
set<Node>hang[maxn],shu[maxn],xie1[maxn],xie2[maxn];
int a1[maxn],a2[maxn];
int ans[maxn];
int ans2[maxn];
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    for (int i = 0; i < m; ++i){
        int u,v;
        scanf("%d %d",&u,&v);
        a1[i] = u;
        a2[i] = v;
        hang[u].insert(Node(u,v));
        shu[v].insert(Node(v,u));
        xie1[u+v].insert(Node(u+v,u-v));
        xie2[u-v+n].insert(Node(u-v+n,u+v));
    }
    long long sum = 0;
    for (int i = 0; i < m; ++i){
        int u = a1[i],v = a2[i];

        if (hang[u].begin()->v != hang[u].rbegin()->v){
            if (hang[u].begin()->v != v && hang[u].rbegin()->v != v)ans2[i] += 2;
            else ans2[i]++;
        }
        if (shu[v].begin()->v != shu[v].rbegin()->v){
            if (shu[v].begin()->v != u && shu[v].rbegin()->v != u)ans2[i] += 2;
            else ans2[i]++;
        }
        if (xie1[u+v].begin()->v != xie1[u+v].rbegin()->v){
            if (xie1[u+v].begin()->v != u-v && xie1[u+v].rbegin()->v != u-v)ans2[i] += 2;
            else ans2[i]++;
        }
        if (xie2[u-v+n].begin()->v != xie2[u-v+n].rbegin()->v){
            if (xie2[u-v+n].begin()->v != u+v && xie2[u-v+n].rbegin()->v != u+v)ans2[i] += 2;
            else ans2[i]++;
        }
    }
    for (int i = 0; i < m; ++i){
        ans[ans2[i] ]++;
    }
    for (int i = 0; i <= 8; ++i){
        if (i)printf(" ");
        printf("%d",ans[i]);
    }
    puts("");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值