-
Notifications
You must be signed in to change notification settings - Fork 0
/
C1. Make Non Zero Sum.cpp
98 lines (94 loc) · 2.6 KB
/
C1. Make Non Zero Sum.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<bits/stdc++.h>
using namespace std;
struct meta
{
int x;
int y;
};
bool cmp(meta a,meta b)
{
return a.x <= b.x;
}
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int ar[n+10] ,sum = 0;
for(int i = 1; i <= n; ++i)
cin >> ar[i];
if(!(n%2))
{
ar[n+1] = -1e9;
vector < meta > xx,yy,z;
sum = 0;
for(int i = 1; i <= n; ++i)
{
int a, b;
a = i;
if(ar[i] == ar[i+1])
{
while(ar[i] == ar[i+1] && i < n)
i++;
b = i;
if((b-a+1)%2)
xx.push_back({a,a}),sum += ar[a],yy.push_back({a+1,b});
else
yy.push_back({a,b});
}
else
{
xx.push_back({a,a});
sum += ar[a];
}
}
// cout << "Sum = " << sum << endl;
// 1 -1 -1 1 -1 -1 1 -1 -1 1
if(sum)
{
int som = (sum > 0)? 1 : -1;
sum = abs(sum);
for(int i = 0; i < yy.size(); ++i)
{
int value = yy[i].y - yy[i].x + 1;
int value2 = ar[yy[i].y];
// cout << "value = " << value << endl;
// cout << "valu2 = " << value2 << endl;
// cout << "som = " << som << endl;
if(som != value2 && sum)
{
int j = 0;
while(sum && j < value)
{
xx.push_back({yy[i].x+j,yy[i].x+j});
sum--;
j++;
}
if(j < value)
xx.push_back({yy[i].x+j,yy[i].y});
}
else
xx.push_back({yy[i].x,yy[i].y});
}
// cout << "sum == " << som << endl;
}
else
{
for(int i = 0; i < yy.size(); ++i)
xx.push_back({yy[i].x,yy[i].y});
}
sort(xx.begin(),xx.end(),cmp);
cout << xx.size() << endl;
for(int i = 0; i < xx.size(); ++i)
cout << xx[i].x << " " << xx[i].y << endl;
}
else
{
cout << "-1" << endl;
}
}
return 0;
}