Training-1——B. Linear Algebra Test
题目来源:Codeforces
题解
用map容器
自动排序,然后使用迭代器iterator
对map容器进行遍历和搜索。
或许会被long long
卡测试点WA
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef map<ll, ll> Map;
typedef pair<ll, ll> Pair;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
// 初始化
Map r;
Map c;
r.clear(), c.clear();
ll n;
scanf("%lld", &n);
ll a, b;
// 控制输入
while (n--)
{
scanf("%lld%lld", &a, &b);
// 如果 r 中已经有了 key = a ,则 a 对应的 val + 1
// 否则插入 < a,1 >
if (r.find(a) != r.end())
{
r.find(a)->second++;
}
else
{
r.insert(pair<ll, ll>{a, 1});
}
// 如果 c 中已经有了 key = b ,则 b 对应的 val + 1
// 否则插入 < b,1 >
if (c.find(b) != c.end())
{
c.find(b)->second++;
}
else
{
c.insert(pair<ll, ll>{b, 1});
}
}
// 迭代器遍历 r 容器
Map ::iterator it;
ll ans = 0;
for (it = r.begin(); it != r.end(); ++it)
{
// 如果 c 容器中存在 key 等于 it->first(即 r 容器中的 key )
if (c.find(it->first) != c.end())
{
// 出现次数相乘的结果累加
ans += it->second * c.lower_bound(it->first)->second;
}
}
printf("%lld\n", ans);
}
return 0;
}