[Solution] Swap and Maximum Block Codeforces Solution
You are given an array of length . The elements of the array are numbered from to .
You have to process queries to this array. In the -th query, you will be given an integer (). To process the query, you should do the following:
- for every in ascending order, do the following: if the -th element was already swapped with some other element during this query, skip it; otherwise, swap and ;
- after that, print the maximum sum over all contiguous subsegments of the array (including the empty subsegment).
For example, if the array is ,
occupied
and , the query is processed as follows:
- the -st element wasn't swapped yet, so we swap it with the -rd element;
- the -nd element wasn't swapped yet, so we swap it with the -th element;
- the -rd element was swapped already;
- the -th element was swapped already;
- the -th element wasn't swapped yet, so we swap it with the -th element;
- the -th element wasn't swapped yet, so we swap it with the -th element.
So, the array becomes . The subsegment with the maximum sum is , and the answer to the query is .
Note that the queries actually change the array, i. e. after a query is performed, the array does not return to its original state, and the next query will be applied to the modified array.
The first line contains one integer ().
The second line contains integers ().
The third line contains one integer ().
Then lines follow, the -th of them contains one integer () describing the -th query.
For each query, print one integer — the maximum sum over all contiguous subsegments of the array (including the empty subsegment) after processing the query.
No comments:
Post a Comment