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 ...
随机推荐
- linux 中断机制浅析
一.中断相关结构体 1.irq_desc中断描述符 struct irq_desc { #ifdef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED struct irq_ ...
- [转]Wireshark抓包工具--TCP数据包seq ack等解读
原文: http://blog.csdn.net/wang7dao/article/details/16805337/ ---------------------------------------- ...
- VS2015 android 设计器不能可视化问题解决。
近期安装了VS2015,体验了一下android 的开发,按模板创建执行了个,试下效果非常不错.也能够可视化设计.但昨天再次打开或创建一个android程序后,设计界面直接不能显示,显示错误:(可能是 ...
- Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)
Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Angular2.x-显示heroes列表
在此页面中,您将展开Tour of Heroes应用程序以显示heroes列表,并允许用户选择heroes并显示heroes的详细信息. 6.X 你需要一些heroes来展示. 最终你会从远程数据服务 ...
- SQL 主机
SQL 主机 SQL 主机 如果您想要您的网站存储数据在数据库并从数据库显示数据,您的 Web 服务器必须能使用 SQL 语言访问数据库系统. 如果您的 Web 服务器托管在互联网服务提供商(ISP, ...
- Android中个人推崇的数据库使用方式
手机应用开发中常常会使用到数据库存储一些资料或者进行数据缓存,android中为我们提供了一个轻量的数据库.在上层进行了一层封装,同一时候还为我们提供了ContentProvider的框架.方便我们进 ...
- 2016/05/25 get和post的区别
get是从服务器上获取数据,post是向服务器传送数据. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过HTTP ...
- Linux中的mysql.redis
1,Linux上的mysql MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源 ...
- Tomcat最多支持并发多少用户?
当一个进程有 500 个线程在跑的话,那性能已经是很低很低了.Tomcat 默认配置的最大请求数是 150,也就是说同时支持 150 个并发,当然了,也可以将其改大.当某个应用拥有 250 个以上并发 ...