zoukankan      html  css  js  c++  java
  • 2017 JUST Programming Contest 3.0 B. Linear Algebra Test

    B. Linear Algebra Test
    time limit per test
    3.0 s
    memory limit per test
    256 MB
    input
    standard input
    output
    standard output

    Dr. Wail is preparing for today's test in linear algebra course. The test's subject is Matrices Multiplication.

    Dr. Wail has n matrices, such that the size of the ith matrix is (ai × bi), where ai is the number of rows in the ith matrix, and bi is the number of columns in the ith matrix.

    Dr. Wail wants to count how many pairs of indices i and j exist, such that he can multiply the ith matrix with the jth matrix.

    Dr. Wail can multiply the ith matrix with the jth matrix, if the number of columns in the ith matrix is equal to the number of rows in the jthmatrix.

    Input

    The first line contains an integer T (1 ≤ T ≤ 100), where T is the number of test cases.

    The first line of each test case contains an integer n (1 ≤ n ≤ 105), where n is the number of matrices Dr. Wail has.

    Then n lines follow, each line contains two integers ai and bi (1 ≤ ai, bi ≤ 109) (ai ≠ bi), where ai is the number of rows in the ith matrix, and bi is the number of columns in the ith matrix.

    Output

    For each test case, print a single line containing how many pairs of indices i and j exist, such that Dr. Wail can multiply the ith matrix with the jth matrix.

    Example
    input
    1
    5
    2 3
    2 3
    4 2
    3 5
    9 4
    
    output
    5
    
    Note

    As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

    In the first test case, Dr. Wail can multiply the 1st matrix (2 × 3) with the 4th matrix (3 × 5), the 2nd matrix (2 × 3) with the 4th matrix (3 × 5), the 3rd matrix (4 × 2) with the 1st and second matrices (2 × 3), and the 5th matrix (9 × 4) with the 3rd matrix (4 × 2). So, the answer is 5.


    题意:给你多个矩阵ai表示行数,bi表示列数

    要你求行数与列数相等的对数有多少种?

    思路:利用map可以把复杂度降到O(n)

    需要注意的是

    2 3

    3 2

    这一类的情况结果是2对,而不是一对;另外由于数据是1e9,所以以后见到1e9都用上long long

    #include <iostream>
    #include<string.h>
    #include<stdio.h>
    #include<algorithm>
    #include<math.h>
    #include<map>
    using namespace std;
    const int maxn=1e5+10;
    typedef long long ll;
    struct node
    {
        ll col,row;
        int flag;
    };
    struct mnode
    {
        int flag;
    };
    
    
    int main()
    {
        ll t;
        scanf("%lld",&t);
        while(t--)
        {
            map<ll,node> m;
           // map<mnode,mnode>mab;
           map<int,int>mab;
            ll n;
            scanf("%lld",&n);
            ll a,b;
            ll ans=0,flag=0;
            for(ll i=1;i<=n;i++)
                {
                    scanf("%lld%lld",&a,&b);
                    mab[a]=b;
                        m[a].col+=1;
                        m[b].row+=1;
    
                }
            map<ll,node>::iterator it;
            for(it=m.begin();it!=m.end();it++){
                    ans+=it->second.col*it->second.row;
    
            }
            printf("%lld
    ",ans-flag);
        }
        return 0;
    }
    



  • 相关阅读:
    【版本控制工具】 Git进阶1
    【版本控制工具】 Git基础
    问题:com.alibaba.dubbo.rpc.RpcException: Failed to invoke ......
    互联网安全架构之常见的Web攻击手段及解决办法
    【Spring Boot】七、整合actuator实现监控管理
    问题:tomcat启动后,可以访问主页面,但是无法访问dubbo-admin
    【Spring Boot】六、整合dubbo(注解的方式)
    这篇文章,彻底搞懂八大开源框架源码
    Spring Cloud Greenwich.SR4 发布了,跟不上了……
    手把手教你画架构图,看一次就会了!
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387150.html
Copyright © 2011-2022 走看看