zoukankan      html  css  js  c++  java
  • Codeforces Gym101502 B.Linear Algebra Test-STL(map)

    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.

     这个题就是用结构体直接比较行列会超时,看数据范围。。。要用map写,猪队友的mapヾ(◍°∇°◍)ノ゙

    ma的first是行的值n,ma的second是行n出现的次数,然后mb是列的。嗯,就这样。

    还有一个地方就是这个题数据有点大,ans那里要用long long,队友在这里被卡了一手(我不管,反正我队友最厉害(Dog脸))

    代码:

     1 //B-用map写,学一手-跑过了
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<map>
     8 using namespace std;
     9 typedef long long ll;
    10 int main(){
    11     int t,n;
    12     scanf("%d",&t);
    13     while(t--){
    14         scanf("%d",&n);
    15         map<int ,int>ma;
    16         map<int ,int>mb;
    17         for(int i=0;i<n;i++){
    18             int x,y;
    19             scanf("%d%d",&x,&y);
    20             ma[x]++;
    21             mb[y]++;
    22         }
    23         map<int ,int>::iterator it;
    24         ll ans=0;
    25         for(it=ma.begin();it!=ma.end();it++){
    26             int s1=it->first;
    27             int s2=it->second;
    28             ans+=(ll)mb[s1]*s2;
    29         }
    30         printf("%lld
    ",ans);
    31     }
    32     return 0;
    33 }
  • 相关阅读:
    HBase with MapReduce (MultiTable Read)
    HBase with MapReduce (SummaryToFile)
    HBase with MapReduce (Summary)
    HBase with MapReduce (Read and Write)
    HBase with MapReduce (Only Read)
    Hbase中的BloomFilter(布隆过滤器)
    HBase的快照技术
    How To Use Hbase Bulk Loading
    Cloudera-Manager修改集群的IP
    Java中的HashSet和TreeSet
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9703283.html
Copyright © 2011-2022 走看看