Codeforces Gym101502 B.Linear Algebra Test-STL(map)
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.
这个题就是用结构体直接比较行列会超时,看数据范围。。。要用map写,猪队友的mapヾ(◍°∇°◍)ノ゙
ma的first是行的值n,ma的second是行n出现的次数,然后mb是列的。嗯,就这样。
还有一个地方就是这个题数据有点大,ans那里要用long long,队友在这里被卡了一手(我不管,反正我队友最厉害(Dog脸))
代码:
1 //B-用map写,学一手-跑过了
2 #include<iostream>
3 #include<cstring>
4 #include<cstdio>
5 #include<algorithm>
6 #include<cmath>
7 #include<map>
8 using namespace std;
9 typedef long long ll;
10 int main(){
11 int t,n;
12 scanf("%d",&t);
13 while(t--){
14 scanf("%d",&n);
15 map<int ,int>ma;
16 map<int ,int>mb;
17 for(int i=0;i<n;i++){
18 int x,y;
19 scanf("%d%d",&x,&y);
20 ma[x]++;
21 mb[y]++;
22 }
23 map<int ,int>::iterator it;
24 ll ans=0;
25 for(it=ma.begin();it!=ma.end();it++){
26 int s1=it->first;
27 int s2=it->second;
28 ans+=(ll)mb[s1]*s2;
29 }
30 printf("%lld\n",ans);
31 }
32 return 0;
33 }
Codeforces Gym101502 B.Linear Algebra Test-STL(map)的更多相关文章
- Here’s just a fraction of what you can do with linear algebra
Here’s just a fraction of what you can do with linear algebra The next time someone wonders what the ...
- 2017 JUST Programming Contest 3.0 B. Linear Algebra Test
B. Linear Algebra Test time limit per test 3.0 s memory limit per test 256 MB input standard input o ...
- Linear Algebra lecture1 note
Professor: Gilbert Strang Text: Introduction to Linear Algebra http://web.mit.edu/18.06 Lecture 1 ...
- stl::map之const函数访问
如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...
- hdu4941 Magical Forest (stl map)
2014多校7最水的题 Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit ...
- [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map
13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...
- STL MAP及字典树在关键字统计中的性能分析
转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...
- 算法库:基础线性代数子程序库(Basic Linear Algebra Subprograms,BLAS)介绍
调试DeepFlow光流算法,由于作者给出的算法是基于Linux系统的,所以要在Windows上运行,不得不做大量的修改工作.移植到Windows平台,除了一些头文件找不到外,还有一些函数也找不到.这 ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
随机推荐
- verilog $fopen 函数的小缺陷
system task $fopen 的argument 为1.文件名字(可以包含具体的文件路径但是注意用)2.打开方式比如"r"."w"."a&qu ...
- POJ:3160-Father Christmas flymouse
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as c ...
- 安装liteIDE on mac
download and install: http://sourceforge.net/projects/liteide/files/ 解决不能编译,没有自动完成的问题: http://stacko ...
- alex 推荐的书
这两本书不错, 追风筝的人<白鹿原>~~~反天不错~~~可以看下.14:27:22AndyZhang 2018-1-29 14:27:22 改变人的东西 读书.看电影.旅行.经历各种事 ...
- [POJ 1001] Exponentiation C++解题报告 JAVA解题报告
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 126980 Accepted: 30 ...
- Eclipse下创建Spring MVC web程序--非maven版
首先, 安装eclipse和tomcat, 这里我下载的是tomcat9.0版本64位免安装的:地址https://tomcat.apache.org/download-90.cgi 免安装的如何启动 ...
- [python][oldboy]字符串 format
#coding=utf8 def format(self, *args, **kwargs): # known special case of str.format """ ...
- HDU——1257最少拦截系统(贪心)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- [LOJ#6002]「网络流 24 题」最小路径覆盖
[LOJ#6002]「网络流 24 题」最小路径覆盖 试题描述 给定有向图 G=(V,E).设 P 是 G 的一个简单路(顶点不相交)的集合.如果 V 中每个顶点恰好在 P 的一条路上,则称 P 是 ...
- BZOJ4916 神犇和蒟蒻 【欧拉函数 + 杜教筛】
题目 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; 输入格式 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; 输出格式 请你输出一个整数A=\sum ...