2017 JUST Programming Contest 3.0 B. Linear Algebra Test
3.0 s
256 MB
standard input
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.
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.
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.
1
5
2 3
2 3
4 2
3 5
9 4
5
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的更多相关文章
- 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_{ ...
- gym101532 2017 JUST Programming Contest 4.0
台州学院ICPC赛前训练5 人生第一次ak,而且ak得还蛮快的,感谢队友带我飞 A 直接用claris的模板啊,他模板确实比较强大,其实就是因为更新的很快 #include<bits/stdc+ ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- gym101343 2017 JUST Programming Contest 2.0
A.On The Way to Lucky Plaza (数论)题意:m个店 每个店可以买一个小球的概率为p 求恰好在第m个店买到k个小球的概率 题解:求在前m-1个店买k-1个球再*p ...
- 2017 JUST Programming Contest 2.0
B. So You Think You Can Count? 设dp[i]表示以i为结尾的方案数,每个位置最多往前扫10位 #include<bits/stdc++.h> using na ...
随机推荐
- javascript 自定义错误处理
php 中是可以自定义程序的错误和异常处理函数的(handler).于是,我在想,javascript 中是否也存在和PHP中一样的异常和错误处理函数呢? try{}catch(){} 这种捕捉异常和 ...
- linux上安装启动elasticsearch-5.5.1完整步骤
linux上安装启动elasticsearch-5.5.1完整步骤 学习了:https://blog.csdn.net/hingcheung/article/details/77144574 http ...
- 深度学习笔记之使用Faster-Rcnn进行目标检测 (实践篇)
实验 我使用的代码是Python版本的Faster Rcnn,官方也有Matlab版本的,链接如下: py-faster-rcnn(python) faster-rcnn(matlab) 环境配置 按 ...
- Evaluate Reverse Polish Notation --leetcode
原题链接:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目大意:给出逆波兰式,然后求其结果. 解题方法:单个栈 ...
- vim怎么把一个写的代码文件另存到任意文件夹里?
比如你要保存到以下路径: D:\my_project\project001\ 那么有两个方法: 1. 直接保存 2. w D:\my_project\project001\xxx.xxx 3. 变更当 ...
- JS Debug
任何一个编程者都少不了要去调试代码,不管你是高手还是菜鸟,调试程序都是一项必不可少的工作.一般来说调试程序是在编写代码之后或测试期修改Bug 时进行的,往往在调试代码期间更加能够体现出编程者的水平高低 ...
- atom及其插件activate-power-mode下载安装
Atom是Github推出的一个文本编辑器,其中包含很多插件可以自行下载安装,其中一个最近比较火的就是插件activate-power-mode,可以实现打字屏振效果, 打字带特效哦,所以最近就尝试安 ...
- FOUNDATION OF ASYNCHRONOUS PROGRAMMING
The async and await keywords are just a compiler feature. The compiler creates code by using the Tas ...
- 安卓输入子系统之inotify与epoll机制【学习笔记】【原创】
平台信息:内核:linux3.1.0系统:android5.0平台:tiny4412 作者:庄泽彬(欢迎转载,请注明作者) 说明: 韦老师的安卓视频学习笔记 一.在安卓的输入子系统中如何监听文件的产生 ...
- Activity快速入门理解
在Java领域,JBPM和Activity是两个主流的工作流系统,而Activity的出现无疑将会取代JBPM(Activity的开发者就是从Jbpm开发者出来的 1. 1个插件 在Eclipse ...