-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path696C.cpp
More file actions
103 lines (101 loc) · 3.15 KB
/
696C.cpp
File metadata and controls
103 lines (101 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
n *= 2;
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
sort(arr, arr + n);
vector<pair<int, pair<int, int>>> sum;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
sum.push_back({arr[i] + arr[j], {i, j}});
}
}
sum.push_back({100000000, {-1, -1}});
sort(sum.begin(), sum.end());
vector<int>sum1;
vector<pair<int,int>>sum2;
for (int i = 0; i < sum.size(); i++)
{
sum1.push_back(sum[i].first);
// sum2.push_back({sum[i].second.first,sum[i].second.second});
}
bool vis[n];
bool no = false;
for (int i = sum.size() - 2; i >= 0; i--)
{
int l = sum[i].first;
int k = i;
bool no = false;
vector<pair<int, int>> ans;
int cnt = 1;
memset(vis,false,sizeof(vis));
ans.push_back({sum[i].second.first, sum[i].second.second});
vis[arr[sum[i].second.first]] = true;
vis[arr[sum[i].second.second]] = true;
while (1)
{
int j = lower_bound(sum1.begin(), sum1.begin()+k+1, arr[sum[k].second.second]) - sum1.begin();
if(vis[sum[j].second.first] || vis[sum[j].second.second]){
int p = sum1[j];
while (sum1[j] == p)
{
if(!vis[sum[j].second.first] && !vis[sum[j].second.second]){
break;
}
else
{
j++;
}
}
if(vis[sum[j].second.first] || vis[sum[j].second.second])continue;
}
// printf("%d %d",sum1[j],sum[k].second.second);
if (arr[sum[k].second.second] == sum[j].first)
{
// printf("%d %d ",sum[j].second.first,sum[j].second.second);
cnt++;
vis[sum[j].second.first] = true;
vis[sum[j].second.second] = true;
ans.push_back({arr[sum[j].second.first], arr[sum[j].second.second]});
k = j;
}
else
{
break;
}
if (cnt == n / 2)
{
printf("YES\n");
printf("%d\n", l);
for (int i = 0; i < ans.size(); i++)
{
printf("%d %d\n", ans[i].first, ans[i].second);
}
no = true;
break;
}
printf("\n");
}
if (no)
{
break;
}
}
if(!no){
printf("NO\n");
}
}
}