codeforces : Linear Algebra Test

codeforces : Linear Algebra Test

论long long 对于竞赛的重要性

限制:

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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 jth matrix.

Input
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 ≤ 10^5), 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 ≤ 10^9) (ai ≠ bi), where ai is the number of rows in the ith matrix, and bi is the number of columns in the ith matrix.

Output
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.

题目大意

A有一堆矩阵,要求你找出那些能够相乘的矩阵。

矩阵相乘的概念略,了解即可。

关键是对题目所给的数据进行快查,经过思考选择map储存查询。

ac代码

#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>

typedef unsigned long long ULL;
typedef long long LL;
typedef long L;

const int maxn=1e9+10;

int t;
long n;

struct node
{
    long b;
    int id;
};

map<long,long> Mapa,Mapb;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
       scanf("%ld",&n);
       long ta,tb;
       Mapa.clear(),Mapb.clear();
       for(int i=0;i<n;++i)
       {
           scanf("%ld%ld",&ta,&tb);
           Mapa[ta]++;
           Mapb[tb]++;
       }
       long long num=0;
       long long na,nb;
       map<long,long>::iterator it;
       for(it=Mapb.begin();it!=Mapb.end();it++)
       {
           nb=it->second;
           na=Mapa[it->first];
           num+=(nb*na);
       }
       printf("%lld\n",num);
    }
    return 0;
}

写的很丑,但是ac了,当时比赛没有ac,就因为num输出的"%lld"错了,整的挺好。

然后还有一个魔改过程中还不错的版本,代码:

#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstdio>

typedef unsigned long long ULL;
typedef long long LL;
typedef long L;

const int maxn=1e5+10;

int t;
long n;
int a[maxn][2];

int main()
{
    scanf("%d",&t);
    map<int,int> Map;
    while(t--)
    {
       scanf("%ld",&n);
       Map.clear();
       long long num=0;

       for(int i=0;i<n;++i)
       {
           scanf("%ld%ld",&a[i][0],&a[i][1]);
       }
       for(int i=0;i<n;++i)
       {
           Map[ a[i][0] ]++;
       }
       for(int i=0;i<n;++i)
       {
           num=num+Map[ a[i][1] ];
       }
       printf("%lld\n",num);
    }
    return 0;
}

还有一个版本,也是魔改出来的,一边添加一边查询,同样是用map搞出来的,主要思想就是第一种的由乘法转换成加法,代码不贴了,被覆盖了。

小注:

树状数组,前缀和,map,线段树,都是好东西

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值