Consecutive Blocks Time Limit: 2 Seconds Memory Limit: 65536 KB
There areN(1 ≤N≤ 105) colored blocks (numbered 1NFrom left to right) which are lined up in a row. AndI-Th block's color isCi(1 ≤Ci≤ 109). Now you can remove at mostK(0 ≤K≤N) Blocks, then rearrange the blocks by their index from left to right. Please figure out the length of the largest consecutive blocks with the same color in the new blocks created by doing this.
For example, one sequence is {1 1 2 2 3 2} and K = 1. we can remove the 6-th block, then we will get sequence {1 1 1 2 2 2 2 }. the length of the largest consecutive blocks with the same color is 4.
Input
Input will consist of multiple test cases and each case will consist of two lines. For each test case the program has to read the integersNAndK, Separated by a blank, from the first line. The color of the blocks will be given in the second line of the test case, separated by a blank.I-Th integer meansCi.
Output
Please output the corresponding length of the largest consecutive blocks, one line for one case.
Sample Input
8 11 1 1 2 2 3 2 2
Sample Output
4
Author:
LIN, Xi
Source:ZOJ Monthly, Jun 2014
Question:
The length isN(1 ≤NOr less than or equal to 105. The range of values in the sequence isCi(1 ≤Ci≤ 109). You can extract Up to k from the sequence. However, the relative position cannot be changed. Now you can obtain the maximum length of the sequence with the same number through the extract operation.
Ideas:
The idea is very simple. The game soon took effect. But it was not done at the end. This is because a variable is written incorrectly. This article is only used to commemorate their own teasing ratio. Get rid of your careless mistakes. Now let's go. Since we require the longest length of a sequence with the same number. Therefore, each value in the sequence may be in the longest sequence. Therefore, we only need to consider including the sequence I value. And use the longest length of some of the preceding I values. Now the key is how to delete this k. The current value is set to v. If we know that the last v position and the last v use the maximum length of those v and the last v. In order to make the continuous v maximum, you must find a way to connect the current v with the previous v. If they are directly adjacent to each other, the maximum length must be the maximum length of the previous position plus 1. Otherwise, the sequence must be continuous by removing other values in the middle. Then you must remove the current position-the previous position + 1 element. So the idea is clear. Maintain a linked list for each value. Indicates the number of k that can be deleted using the previous v. Then add the n sequences to the corresponding linked list in sequence. If k is enough, add it directly. If not, delete the linked list from the back and restore k. Why is it deleted. Because it must be continuous from the current position. Where can I delete the data before the deletion. Because c is relatively large, hash is required. Each element is added to or removed from the linked list at most. Therefore, a maximum of 2 * n operations can be performed. In this way, the problem is solved with the time complexity of O (n.
For details, see the code:
# Include
# Include
# Include
Using namespace std; const int maxn = 100010; int H [maxn], arr [maxn], n, m, ptr; void init () // hash initialization {ptr = 0; sort (H, H + m); m = unique (H, H + m)-H;} struct node // head node of the linked list {int k, st, tail, len; // k and k elements can be deleted. St is the last pointer to add elements. Tail. It is convenient to delete from the back. Len linked list length} hd [maxn]; struct nd {int id, next; // linked list node. Id is the subscript in the sequence .} Me [maxn]; int Hash (int x) {return lower_bound (H, H + m, x)-H;} int main () {int I, k, v, tl, p, ans; while (~ Scanf ("% d", & n, & k) {ans = 1; for (I = 0; I