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 ...
随机推荐
- WebLogic"域"的概念
WebLogic Server中的域是逻辑上相关的一组 WebLogic Server 资源,可以作为一个单元进行管理.一个域中包含一台或多台 WebLogic Server,也可以包含 WebLog ...
- 通过k8s(Kubernetes)搭建jmeter的压测环境master-slave架构,实现弹性伸缩
在k8s上部署jmeter的mater和slave,根据压测需求动态增减master和slave节点数量,即可以完成压测任务,又可以是资源利用最大化 https://blog.kubernauts.i ...
- struts2 动态工作流
话不多说,直接贴代码: public class TestAction { private String nextPage;//保存下一步内容的属性 public String destroy(){ ...
- Office EXCEL 复制粘贴 变成 #value,#REF!,#DIV怎么办
这些都是由于相对引用造成的,如下所示,我鼠标点进去之后变成了I10/L10,当数字和文字或空单元格进行加减乘除的运算就会出现这种问题 使用选择性粘贴,只粘贴数值即可.
- 解决echart在IE中使用时,在div中加入postion后图表不显示问题
<!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="main" style="height:400px;width:1 ...
- STL源代码剖析——STL算法之set集合算法
前言 本节介绍set集合的相关算法,各自是并集set_union,差集set_difference,交集set_intersection 和对称差集set_symmetric_difference.这 ...
- 五------Jsp九大内置对象。
Jsp九大内置对象,当中最基本的是前五个对象. 5-1 out对象 out对象是JSPWriter类的实例.是向client输出内容经常使用的对象. out对象经常使用的方法例如以下: 1.out.p ...
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>
MVC 默认 Request 方式为 Post. actionpublic JsonResult GetPersonInfo(){var person = new{Name = "张三&qu ...
- 使用变量作为js对象的属性名
<script> var test={aa:12,bb:34};//或者var test={}; var cc= "acqId" test[cc]=12; alert( ...
- C项目实践--俄罗斯方块(1)
俄罗斯方块游戏是由前苏联科学院计算机中心的工程师阿列克谢.帕基特诺夫发明的一款小游戏. 1.功能需求分析 1.1主要功能 实现三个功能:1.游戏欢迎界面:2.游戏执行功能,包括计算得分:3.游戏结束界 ...