Ja the Ghost is playing with rubber ducks. He has n piles of rubber ducks arranged in a row, where the i-th pile contains ai ducks. Quack the Duck gives Ja a strictly increasing sequence b1,b2,…,bn and commands him to make the piles a1,a2,…,an become exactly this sequence.
Ja performs the process in the following two stages:
Ja may add any number of ducks to each pile.
Formally, for each pile i, he chooses a non-negative integer xi and replaces ai with ai+xi.
Ja may repeatedly swap two adjacent piles.
Formally, he may perform the following operation any number of times, possibly zero: choose an index i such that 1≤i≤n−1, and swap the values of ai and ai+1.
A process is called valid if, after both stages end, the sequence of pile sizes is exactly b1,b2,…,bn.
Find the minimum possible number of operations performed in the second stage among all valid processes. If there is no valid process, output −1.
Ja the Ghost is playing with rubber ducks again! There are n piles of rubber ducks arranged in a row from left to right. Initially, the i-th pile contains ai rubber ducks.
While the sequence a is not sorted in nondecreasing order, Ja must perform the following operation:
Choose two adjacent piles such that the left pile contains more ducks than the right pile. Ja swaps these two piles, and then adds the number of ducks in the new left pile to the new right pile.
Formally, choose an index i such that 1≤i<n and ai>ai+1. Then replace the adjacent pair (ai,ai+1) with (ai+1,ai+ai+1).
For example, if two adjacent piles contain 7 and 3 rubber ducks, then after the operation they contain 3 and 10 rubber ducks.
Ja may choose any index satisfying the condition above at each step. It can be shown that, regardless of his choices, the process eventually ends with the sequence sorted in nondecreasing order.
Ja wants the largest pile at the end of the process to contain as few rubber ducks as possible. Determine the minimum possible value of the largest pile.
又是一道序列题
虽然有minimum possible value of the largest pile,但是这并不是一道二分答案问题,而是一道简单的贪心题。
A binary string is a string consisting only of characters 0 and 1. The two characters 0 and 1 are called opposite values.
Consider a binary string t. Let ∣t∣ be the length of t. When ∣t∣≥2, for every 1≤i<∣t∣, the characters ti and ti+1 are adjacent.
A binary string t is called beautiful if it can be reduced to a string of length exactly 1 by applying the following operation any number of times, possibly zero:
Choose two equal adjacent characters, remove both of them, and insert one character with the opposite value in their place.
For example, the string 10001 can become 1101 by replacing the first adjacent pair 00 with 1. Then it can become 001, then 11, and finally 0. Therefore, 10001 is beautiful.
On the other hand, 111 is not beautiful. After one operation, it becomes 01, and then no operation can be applied.
You are given a binary string s. Compute the number of non-empty beautiful substrings∗ of s.
∗A string a is a substring of a string b if a can be obtained from b by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.