Village debt problems
There is a village, the village has n families, they neatly in a row, from left to right are numbered 1 ~ n
Among them, some of the money owed to others, and some were others owe. Today, the village committee decided to unify deal with these debts.
However, this village has a magical properties, money can flow between adjacent people, that is to say for the i households, the money can only give his first i-1 or i + 1-user households
If you owe money 3 1, you must put the money to 2, and then from 2 to 3
Now the village would like to know how many operations they need at least to pay all the debt problem is solved.
Entry
The first line, an integer n
The second line n integers, the i-th V [i] where i-th user debt of others. He is representing owe, owe a negative number represents.
n<=1e5
v[i]<=1e9
It represents a minimum integer output flows
Sample
Entry
5
3 0 0 0 -3
Export
12
Explanation
1 to 23 dollars flows + 3 =
2 to 33 dollars flows + 3 =
3 to 43 dollars flows + 3 =
4 to 53 dollars flows + 3 =
Flow rate = 12
This question is in fact that after several operations, a state of a thing into another state. The final state is rather special, require everyone to owe neither, nor owe.
That is all v [i] are equal to 0
All ...... v [i] ......? A little trouble, it is better to think about a change in thinking, v [i] = 0 for any i∈ [1, n] are true, then for i = 1 certainly established
Why should I think so, because of the special position of 1, 2 and he could only relationship, regardless owe or are owed money, and 2 others only through exchange.
But for 2 ~ n-1, his neighbors, and he could be exchanged, the problem will be more complicated.
So we start with the most edge of the No. 1 start
Then it is not difficult to think of an algorithm
years = 0
for i=1 To n-1
ans + = abs (v [i]) // abs represents the absolute value
v [i + 1] + = v [i]
v [i] = 0
print (years)
Flip Game (Los Valley P1764)
Do not write the questions surface, directly attached to the portal (in fact, I'm too lazy to hit)
https://www.luogu.com.cn/problem/P1764
For ease of discussion, the definition of f (i, j) operative to:
Flip pieces i-th row j-th column
Flip (i, j) four vertical and horizontal pieces
Define the operation F (S) is:
For all (i, j) ∈S
f(i,j)
It is contemplated that a O (2 n-n-* ) Algorithm
Enumeration of all pieces of the subset S S f in the operation for all the pieces. The operation is complete check whether the same color
But this method of overtime , a century ran finish (do not rule out within a century appears to quickly finish this computer algorithm)
Do not be his [+ increase / provincial election -] difficulty scared, in fact, just the same ideas and questions
All the pieces have become one color, which will consider all become black, to discuss the whole algorithm is black, then white empathy
All black, it means that the first row of all the pieces are black
Why think so? Because want to affect the first line, only the operation in the first row is f (1,1), f (1,2), f (1,3) ... f (1, n) and a second line operation, i.e., f (2,1), f (2,2), f (2,3) ... f (2, n) only two lines
However, for the i-th row (i∈ [2, n-1]) that can influence the operation of the first row i-1, operation of the operation of the i-th row, the row i + 1, a total of three lines
It is not like most of the previous question?
So that we can optimize it that a century ran endless algorithm of
A subset of the original algorithm to enumerate all the pieces, we can enumerate the first line of the piece subset S 1
Do a F (S 1 )
And so on, so down the first line is likely to be a mess ah
Do not worry, just said, the first line may be operated affect the first and second rows of F, has been completely F. (S . 1 ), we can not directly operate the first line
So trying to become the first line of all black, it can only affect the operation of the second line of the first row
And since no operation of the first line, then reaches the first row of black want, you can only determine a subset S of the second row 2 such that F. (S 2 after), the first row is all black
F (S 2 after), the first all-black line, the second line is likely to be a mess, but this time can not operate the first and second rows, and can only influence by operating the third row of the second row
Then we can determine only the third row subset S . 3 such that F. (S . 3 ) after the second line is black
And so on, it may be made of S . 1 to determine S 2 ~ S n-
After performing S n later, 1 ~ n-1 must be all black, although the n-th row is likely to mess, but if the solution exists, then we can enumerate a S . 1 so that the operation is complete after the n-th row is all black
If not, then no solution
Pseudo code
years = + ∞;
S for . 1 ⊆ first line:
F(S1)
= TEMP | S . 1 | // | Sl | represents S . 1 the number of elements of
for i=2 to n
for j= 1 to n
if (i-1, j) is white:
f(i,j)
temp++
if 第n行全黑
ans=min(ans,temp)
if (ans<+∞) print(ans)
else print("Impossible")