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 ...
随机推荐
- 《gis空间分析及应用案例解析》培训总结
<gis空间分析及应用案例解析>培训总结 来源:常德水情 作者:唐校准 发布日期:2014-01-02 2013年12月2630日由中国科学院计算技术研究所教育中心组织的< ...
- BloomFilter学习
看大数据面试题,看到BloomFilter,找了篇文章学习一下: http://www.cnblogs.com/heaad/archive/2011/01/02/1924195.html Bloom ...
- 【python】Python的字典get方法:从字典中获取一个值
转自: http://blog.sina.com.cn/s/blog_6be89284010183xm.html
- Cleave js 使用
1111111111111111 xxxxxx Cleave.js 键入时格式化< input />内容 信用卡号码格式 明确 美国运通:从34/37开始 34 签证:从4开始 ...
- PHP出现Warning: A non-numeric value encountered问题的原因及解决方法
本文介绍php出现Warning: A non-numeric value encountered问题,用实例分析出现这种错误的原因,并提供避免及解决问题的方法. <?php error_rep ...
- VC2010 利用 def 文件生成 dll 文件的方法
近期有个需求,要生成一个dll 文件.文件里的函数都是採用 stdcall 函数调用约定,可是不希望函数名被修饰(add 被修饰成 add@8). 这时就要用def 文件了. 比方我有以下两个函数: ...
- C 编程中fseek、ftell的用法总结
fseek 函数功能是将文件指针移动到指定的地方,因此可以通过fseek重置文件指针的位置.函数原型: int fseek(FILE *stream, long offset, int origi ...
- [NOIP2012] day2 T3疫情控制
题目描述 H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,也是树中的根节点. H 国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散到 ...
- HDU1054 Strategic Game —— 最小点覆盖 or 树形DP
题目链接:https://vjudge.net/problem/HDU-1054 Strategic Game Time Limit: 20000/10000 MS (Java/Others) ...
- HDU1281 棋盘游戏 —— 二分图最大匹配 + 枚举
题目链接:https://vjudge.net/problem/HDU-1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...