-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathequalizing-arrays.cpp
43 lines (32 loc) · 1.04 KB
/
equalizing-arrays.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
#include <bits/stdc++.h>
template <typename T>
using V = std::vector<T>;
int main()
{
int N; std::cin >> N;
V<int> a1(N), a2(N);
V<std::list<std::pair<int, int>>> movesVec(N);
for ( auto& elm : a1 ) std::cin >> elm;
for ( auto& elm : a2 ) std::cin >> elm;
int curDiff{ 0 }, totalMoves{ 0 };
for ( auto i = 0; i < N; ++i ) {
if ( curDiff ) {
++totalMoves;
if ( curDiff > 0 ) movesVec[i ].emplace_back(-1, curDiff);
else movesVec[i - 1].emplace_back( 1, -curDiff);
}
curDiff += a2[i] - a1[i];
}
std::cout << totalMoves << "\n";
for ( int i = 0; i < N; ++i ) {
while ( i >= 0 && !std::empty(movesVec[i]) ) {
auto [D, V] = movesVec[i].front();
if (a1[i] < V)
break;
a1[i ] -= V;
a1[i + D] += V;
movesVec[i].pop_front();
std::cout << i-- + 1 << " " << D << " " << V << "\n";
}
}
}