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\n",ans-flag);
}
return 0;
}

2017 JUST Programming Contest 3.0 B. Linear Algebra Test的更多相关文章

  1. 2017 JUST Programming Contest 2.0 题解

    [题目链接] A - On The Way to Lucky Plaza 首先,$n>m$或$k>m$或$k>n$就无解. 设$p = \frac{A}{B}$,$ans = C_{ ...

  2. gym101532 2017 JUST Programming Contest 4.0

    台州学院ICPC赛前训练5 人生第一次ak,而且ak得还蛮快的,感谢队友带我飞 A 直接用claris的模板啊,他模板确实比较强大,其实就是因为更新的很快 #include<bits/stdc+ ...

  3. 2017 JUST Programming Contest 3.0 I. Move Between Numbers

    I. Move Between Numbers time limit per test 2.0 s memory limit per test 256 MB input standard input ...

  4. 2017 JUST Programming Contest 3.0 D. Dice Game

    D. Dice Game time limit per test 1.0 s memory limit per test 256 MB input standard input output stan ...

  5. 2017 JUST Programming Contest 3.0 H. Eyad and Math

    H. Eyad and Math time limit per test 2.0 s memory limit per test 256 MB input standard input output ...

  6. 2017 JUST Programming Contest 3.0 K. Malek and Summer Semester

    K. Malek and Summer Semester time limit per test 1.0 s memory limit per test 256 MB input standard i ...

  7. 2017 JUST Programming Contest 3.0 E. The Architect Omar

    E. The Architect Omar time limit per test 1.0 s memory limit per test 256 MB input standard input ou ...

  8. gym101343 2017 JUST Programming Contest 2.0

    A.On The Way to Lucky Plaza  (数论)题意:m个店 每个店可以买一个小球的概率为p       求恰好在第m个店买到k个小球的概率 题解:求在前m-1个店买k-1个球再*p ...

  9. 2017 JUST Programming Contest 2.0

    B. So You Think You Can Count? 设dp[i]表示以i为结尾的方案数,每个位置最多往前扫10位 #include<bits/stdc++.h> using na ...

随机推荐

  1. centos、mac的grafana安装和简单使用

    1.安装: 参考官方文档安装说明:https://grafana.com/grafana/download Redhat & Centos(64 Bit): wget https://s3-u ...

  2. 【Nginx】如何建立新连接

    处理新连接事件的回调函数是ngx_event_accept,原型如下: void ngx_event_accept(ngx_event_t *ev) 具体流程如下: 1)首先调用accept方法试图建 ...

  3. CentOS 5 全功能服务器搭建

    转自: http://www.php-oa.com/2007/12/27/centos-www.html 转:主要做为历史记录,以后用.另外很少见这么好的编译的文章,其实我不推荐用编译安装.但这个文章 ...

  4. Input系统—ANR原理分析(转)

    一. 概述 当input事件处理得慢就会触发ANR,那ANR内部原理是什么,哪些场景会产生ANR呢. “工欲善其事必先利其器”,为了理解input ANR原理,前面几篇文章疏通了整个input框架的处 ...

  5. Codeforces Round #253 (Div. 1) A Borya and Hanabi

    A. Borya and Hanabi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Linux学习笔记总结

    零.求人不如求已:        1. 在Linux中,文件,目录,驱动,命令,脚本都视为文件,也即一切皆file. 2.记住使用Linux 的关键就是六个字: 命令.选项.參数. 3.学会看帮助,不 ...

  7. Python爬虫开发【第1篇】【机器视觉及Tesseract】

    ORC库概述 在读取和处理图像.图像相关的机器学习以及创建图像等任务中,Python 一直都是非常出色的语言.虽然有很多库可以进行图像处理,但在这里我们只重点介绍:Tesseract 1.Tesser ...

  8. Latex 1: 解决latex中遇到一个常见错误:"Improper alphabetic constant."

    1.问题: 本人是在WIN7下用texlive 2016,编辑器用的是WinEdt 10.1 ,运行如下代码: \documentclass{ctexbook} \begin{document} \t ...

  9. MySql安装与使用图文教程

      2.下载完成后将其解压到你想要安装的路径下,例如我的解压到D:\MySql\mysql-5.7.12-winx64\路径下,刚解压完应该是下图这些文件夹:最好解压到根目录. 5.新建一个my.in ...

  10. jackson实体为NULL或者为空不显示

    1.实体上 @JsonInclude(JsonInclude.Include.NON_NULL) 将该注解放在属性上,如果该属性为null则不参与序列化: 如果放在类上边,那对这个类的全部属性起作用 ...