描述
At Bessie’s recent birthday party, she received N (2 <= N <= 100,000; N%2 == 0) pearls, each painted one of C different colors (1 <= C <= N).
Upon observing that the number of pearls N is always even, her creative juices flowed and she decided to pair the pearls so that each pair of pearls has two different colors.
Knowing that such a set of pairings is always possible for the supplied testcases, help Bessie perform such a pairing. If there are multiple ways of creating a pairing, any solution suffices.
输入
-
Line 1: Two space-separated integers: N and C
-
Lines 2…C + 1: Line i+1 tells the count of pearls with color i: C_i
输出 -
Lines 1…N/2: Line i contains two integers a_i and b_i indicating that Bessie can pair two pearls with respective colors a_i and b_i.
样例输入
8 3
2
2
4
样例输出
1 3
1 3
2 3
3 2
提示
INPUT DETAILS:
There are 8 pearls and 3 different colors. Two pearls have color I; two have color II; four have color III.
题意:就是他们这几个球,每个球都得有个队友但不能是同一类的队友必须不同类。
WA思路: 我就想这从数量大的开始分,就是先给它的分配队友。但是这样就会造成越到后边越没队友分配了。
AC思路:这个题我给想复杂啦,我设立了一个结构体,存数和下标,其实这样反而麻烦了
他不就是看数的匹配的问题么,你就把所有的下标排成一排,总共n个球,都是一个一个的,第一个球分谁去匹配呢
只有跟第n/2个匹配,每个球都去匹配第i+n/2项,就能完全匹配完了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
int cl[100100]; int b = 0;
int main()
{
int n, c; cin >> n >> c;
for (int i = 1; i <= c; i++)
{
int t; cin >> t;
for (int j = 0; j < t; j++)
{
cl[b] = i; b++;
}
}
for (int i = 0; i<n / 2; i++)
{
cout << cl[i] << " " << cl[i + (n / 2)] << endl;;
}
}