描述
考虑一场有 n 支球队参加的足球锦标赛。每支球队都有两套球衣:主场比赛和客场比赛。第 i 队主场比赛球衣的颜色编号为Xi和客场比赛球衣的颜色编号Yi。
比赛分主、客场制,即和每一个球队都要打一场主场和一场客场(所有球队共需要赛×(−1)n×(n−1)场)。参加主场比赛的球队传统上穿着主场球衣。参加客场比赛的球队穿着客场球衣。但是,如果本场比赛两个球队颜色一样,则无法区分它们。在这种情况下,客队将穿着主场球衣进行比赛。
请计算每支球队在所有比赛中使用其主场球衣和客场球衣的数量。
输入
第一行包含一个整数 n (2≤≤1052≤n≤105)— 团队数量。接下来的 n 行为每个球队主、客场颜色编号(xi,yi:1<=<=105,≠1<=xi,yi<=105,xi=yi)
输出
按顺序输出每个队伍主队服穿了几次,客队服穿了几次。
Description
Consider a football tournament where n teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the i-th team has color xi and the kit for away games of this team has color yi (xi ≠ yi).
In the tournament, each team plays exactly one home game and exactly one away game with each other team (n(n - 1) games in total). The team, that plays the home game, traditionally plays in its home kit. The team that plays an away game plays in its away kit. However, if two teams has the kits of the same color, they cannot be distinguished. In this case the away team plays in its home kit.
Calculate how many games in the described tournament each team plays in its home kit and how many games it plays in its away kit.
The first line contains a single integer n (2 ≤ n ≤ 105) — the number of teams. Next n lines contain the description of the teams. The i-th line contains two space-separated numbers xi, yi (1 ≤ xi, yi ≤ 105; xi ≠ yi) — the color numbers for the home and away kits of the i-th team.
For each team, print on a single line two space-separated integers — the number of games this team is going to play in home and away kits, correspondingly. Print the answers for the teams in the order they appeared in the input.
Input
The first line contains a single integer n (2 ≤ n ≤ 105) — the number of teams. Next n lines contain the description of the teams. The i-th line contains two space-separated numbers xi, yi (1 ≤ xi, yi ≤ 105; xi ≠ yi) — the color numbers for the home and away kits of the i-th team.
Output
For each team, print on a single line two space-separated integers — the number of games this team is going to play in home and away kits, correspondingly. Print the answers for the teams in the order they appeared in the input.
Samples
输入数据 1
2
1 2
2 1
Copy
输出数据 1
2 0
2 0
Copy
输入数据 2
3
1 2
2 1
1 3
Copy
输出数据 2
3 1
4 0
2 2
解题思路
首先,每支球队都会进行n-1场主场比赛,所以,每支球队主场球衣数量为n-.,而客场球衣的数量为了后期方便计算,也初始化为n-1。接下来,一个一个判断与当前球队客场球衣颜色相同的主场球队,有几个当前球队的主场数量+1,客场球衣数量-1。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct balabala
{
ll x,y;
}a[111119];
int main()
{
ll n,ans=0,cnt=0;
cin >>n;
for(ll i=1;i<=n;i++)
{
cin >>a[i].x>>a[i].y;
}
for (ll i=1;i<=n;i++)
{
ans=n-1;
cnt=n-1;
for (ll j=1;j<=n;j++)
{
if(a[i].y==a[j].x)
{
ans++;
cnt--;
}
}
cout <<ans<<" "<<cnt<<endl;
}
return 0;
}
但是,马上你就会发现,这段代码——————————————
超时了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
所以,我们来改进一下,有哪种数据结构能存放两个数据呢?对,map。
所以我们可以统计出有多少个球队的主场球衣与当前球队的客场一样,最后在输出n-1加上一样的数量和n-1减去一样的数量,这样就不会超时了。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[111119];
int b[111119];
map <int,int> m;
int main()
{
int n;
cin >>n;
for (int i=1;i<=n;i++)
{
cin >>a[i]>>b[i];
m[a[i]]++;
}
for (int i=1;i<=n;i++)
{
cout <<n-1+m[b[i]]<<" ";
cout <<n-1-m[b[i]]<<endl;
}
return 0;
}
byebye!