zoukankan      html  css  js  c++  java
  • hihocoder-1497-Queen Attack

    hihocoder-1497-Queen Attack

    #1497 : Queen Attack

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    There are N queens in an infinite chessboard. We say two queens may attack each other if they are in the same vertical line, horizontal line or diagonal line even if there are other queens sitting between them.

    Now given the positions of the queens, find out how many pairs may attack each other?

    输入

    The first line contains an integer N.

    Then N lines follow. Each line contains 2 integers Ri and Ci indicating there is a queen in the Ri-th row and Ci-th column.  

    No two queens share the same position.  

    For 80% of the data, 1 <= N <= 1000

    For 100% of the data, 1 <= N <= 100000, 0 <= Ri, Ci <= 1000000000

    输出

    One integer, the number of pairs may attack each other.

    样例输入
    5  
    1 1  
    2 2  
    3 3   
    1 3
    3 1
    样例输出
    10

    题解:

      使用unordered_map, 记录之前添加过的position,因为不可能同时两个position重叠,两两attack的position必定只存在着一种相交方式。

    #include <cstdio> 
    #include <cstdlib> 
    
    #include <iostream>
    #include <unordered_map> 
    using namespace std;  
    
    int main()
    {
        int n, x, y;
        long long ans = 0; 
        scanf("%d", &n); 
        unordered_map<int, int> hor; 
        unordered_map<int, int> vet; 
        unordered_map<int, int> dx; 
        unordered_map<int, int> vdx; 
        for(int i=0; i<n; ++i)
        {
            scanf("%d %d", &x, &y); 
            if(hor.find(x) != hor.end())
            {
                ans += hor[x]; 
                hor[x] += 1; 
            }else{
                hor[x] = 1; 
            }
    
            if(vet.find(y) != vet.end())
            {
                ans += vet[y]; 
                vet[y] += 1; 
            }else{
                vet[y] = 1; 
            }
    
            if(dx.find(x-y) != dx.end())
            {
                ans += dx[x-y]; 
                dx[x-y] += 1; 
            }else{
                dx[x-y] = 1; 
            }
    
            if(vdx.find(x+y) != vdx.end())
            {
                ans += vdx[x+y]; 
                vdx[x+y] += 1; 
            }else{
                vdx[x+y] = 1; 
            }
    
        }
        printf("%lld
    ", ans );
    }
    

      

  • 相关阅读:
    树形dp--P2014 [CTSC1997]选课
    背包变形--P1759 通天之潜水
    区间dp--P1880 [NOI1995]石子合并
    动态规划--P2758 编辑距离
    筛法--CF449C Jzzhu and Apples
    BZOJ3998: [TJOI2015]弦论(后缀自动机,Parent树)
    BZOJ3530: [Sdoi2014]数数(Trie图,数位Dp)
    BZOJ1444: [Jsoi2009]有趣的游戏(Trie图,矩乘)
    BZOJ1195: [HNOI2006]最短母串(Trie图,搜索)
    BZOJ3238: [Ahoi2013]差异(后缀数组)
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/11022811.html
Copyright © 2011-2022 走看看