forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10720.cpp
78 lines (76 loc) · 1.29 KB
/
10720.cpp
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
setvbuf(stdin, NULL, _IOFBF, 10000);
setvbuf(stdout, NULL, _IOFBF, 10000);
int n;
int arr[10240];// Degree sequence
int c[10240]; // Conjugate sequence (as per exercise 105, Chapter 7 of TAOCP)
while (scanf("%d", &n) == 1 && n)
{
int parity = 0;
bool possible = true;
for (int i = 0; i < n; ++i)
{
scanf("%d", &arr[i]);
parity ^= arr[i] & 1;
if (arr[i] >= n)
{
possible = false;
}
}
if (possible && !parity)
{
sort(arr, arr + n);
int curr = n;
int run_len = 1;
int idx = 0;
int x = 0;
for (int i = 1; i < n; ++i)
{
if (arr[i] != arr[i - 1])
{
for (int j = 0; j < arr[i - 1] - x; ++j)
{
c[idx++] = curr;
}
curr -= run_len;
run_len = 0;
x = arr[i - 1];
}
++run_len;
}
for (; idx < arr[n - 1]; ++idx)
{
c[idx] = curr;
}
reverse(arr, arr + n);
int d_sum = 0;// degree sum
int c_sum = 0;// conjugate sum
for (int i = 0; i < n; ++i)
{
if (arr[i] < i + 1)
{
break;
}
d_sum += arr[i];
c_sum += c[i];
if (d_sum > c_sum - (i + 1))
{
possible = false;
break;
}
}
}
if (!possible || parity)
{
puts("Not possible");
}
else
{
puts("Possible");
}
}
return 0;
}