Codeforces Beta Round #95 (Div. 2)

来源:互联网 发布:编程对电脑要求 编辑:程序博客网 时间:2024/06/12 14:45

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.

<p< p=""><p< p="">
Input
<p< p="">

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.

<p< p=""><p< p="">
Output
<p< p="">

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

<p< p=""><p< p="">
Sample test(s)
<p< p=""><p< p="">
input
8 44 34 86 51 6
output
0 3 0 1 0 0 0 0 0 
input
10 31 11 21 3
output
0 2 1 0 0 0 0 0 0 


#include<iostream>using namespace std;#define N 300000struct Point { int r, c; } p[N];int high[N], low[N], Left[N], Right[N], dia1[N][2], dia2[N][2];int main(){    int n, m;    cin >> n >> m;    for ( int i = 1; i < N; i++ )    {        high[i] = -1; low[i] = N;        Left[i] = N; Right[i] = -1;        dia1[i][0] = N; dia1[i][1] = -1;        dia2[i][0] = N; dia2[i][1] = -1;    }    for ( int i = 1; i <= m; i++ )    {        cin >> p[i].r >> p[i].c;        high[p[i].c] = max ( high[p[i].c], p[i].r );        low[p[i].c] = min ( low[p[i].c], p[i].r );        Left[p[i].r] = min ( Left[p[i].r], p[i].c );        Right[p[i].r] = max ( Right[p[i].r], p[i].c );        dia1[p[i].r+p[i].c][0] = min ( dia1[p[i].r+p[i].c][0], p[i].c );        dia1[p[i].r+p[i].c][1] = max ( dia1[p[i].r+p[i].c][1], p[i].c );        dia2[p[i].r-p[i].c+n][0] = min ( dia2[p[i].r-p[i].c+n][0], p[i].c );        dia2[p[i].r-p[i].c+n][1] = max ( dia2[p[i].r-p[i].c+n][1], p[i].c );    }    int attak[10] = {0};    for ( int i = 1; i <= m; i++ )    {        int cnt = 0;        if ( p[i].r < high[p[i].c] ) cnt++;        if ( p[i].r > low[p[i].c] ) cnt++;        if ( p[i].c < Right[p[i].r] ) cnt++;        if ( p[i].c > Left[p[i].r] ) cnt++;        if ( p[i].c > dia1[p[i].r+p[i].c][0] ) cnt++;        if ( p[i].c < dia1[p[i].r+p[i].c][1] ) cnt++;        if ( p[i].c > dia2[p[i].r-p[i].c+n][0] ) cnt++;        if ( p[i].c < dia2[p[i].r-p[i].c+n][1] ) cnt++;        attak[cnt]++;    }    for ( int i = 0; i <= 7; i++ )        cout << attak[i] << ' ';    cout << attak[8] << endl;}


原创粉丝点击