In general, k digits are deleted from a string, which is the longest continuous number and the output length in the remaining strings.
Paste two others' codes and add comments.
[Cpp]
/*
*/
# Include <iostream>
# Include <algorithm>
# Include <stdio. h>
# Include <string. h>
# Include <math. h>
# Include <queue>
# Define OOS 2000000000
# Define ll long
Using namespace std;
Struct node
{
Int next, w;
} Point [2, 200005];
Int s [200005], B [200005], last [200005], n, m, k, x, q;
Int main ()
{
Int t, ans, m, p;
Memset (s, 0, sizeof (s ));
Memset (point, 0, sizeof (point ));
Memset (B, 0, sizeof (B ));
Scanf ("% d", & n, & m, & k );
Ans = q = 0;
For (t = 1; t <= n; t ++)
{
Scanf ("% d", & x );
S [x] ++; // This number has one more
If (! B [x]) // not accessed
{
B [x] = t; // records the position of the first x.
Last [x] = t; // record this. This is the position of the first segment header and is used to create subsequent pointers.
Point [t]. w = s [x]; // It is very useful to record the number of x records. It is useful to delete fewer other numbers in the calculation process.
} Else if (x! = Q) // has an ancestor and is not equal to the previous one. This is the beginning of a new clip.
{
Point [last [x]. next = t; // The subsequent pointer of the previous segment header points to the new segment header.
Point [t]. w = s [x]; // specifies the number of records.
Last [x] = t; // update
} // If there is an ancestor and the previous one is, there is no processing because only the beginning of one segment is recorded.
Q = x; // update record
// B [x] indicates the position of a Part header, that is, from which x is consecutive.
M = t-B [x] + 1; // The number of all numbers from the segment header to here
While (m-(s [x]-point [B [x]. w + 1)> k) // the difference between the number of x from the fragment header to the number of x here is that the number of digits needs to be deleted.
{
B [x] = point [B [x]. next; // move backward through the subsequent pointer
M = t-B [x] + 1; // calculate the total number of new numbers
}
If (s [x]-point [B [x]. w + 1> ans) ans = s [x]-point [B [x]. w + 1; // if possible, update the answer
}
Printf ("% d \ n", ans );
Return 0;
}
/*
*/
# Include <iostream>
# Include <algorithm>
# Include <stdio. h>
# Include <string. h>
# Include <math. h>
# Include <queue>
# Define OOS 2000000000
# Define ll long
Using namespace std;
Struct node
{
Int next, w;
} Point [2, 200005];
Int s [200005], B [200005], last [200005], n, m, k, x, q;
Int main ()
{
Int t, ans, m, p;
Memset (s, 0, sizeof (s ));
Memset (point, 0, sizeof (point ));
Memset (B, 0, sizeof (B ));
Scanf ("% d", & n, & m, & k );
Ans = q = 0;
For (t = 1; t <= n; t ++)
{
Scanf ("% d", & x );
S [x] ++; // This number has one more
If (! B [x]) // not accessed
{
B [x] = t; // records the position of the first x.
Last [x] = t; // record this. This is the position of the first segment header and is used to create subsequent pointers.
Point [t]. w = s [x]; // It is very useful to record the number of x records. It is useful to delete fewer other numbers in the calculation process.
} Else if (x! = Q) // has an ancestor and is not equal to the previous one. This is the beginning of a new clip.
{
Point [last [x]. next = t; // The subsequent pointer of the previous segment header points to the new segment header.
Point [t]. w = s [x]; // specifies the number of records.
Last [x] = t; // update
} // If there is an ancestor and the previous one is, there is no processing because only the beginning of one segment is recorded.
Q = x; // update record
// B [x] indicates the position of a Part header, that is, from which x is consecutive.
M = t-B [x] + 1; // The number of all numbers from the segment header to here
While (m-(s [x]-point [B [x]. w + 1)> k) // the difference between the number of x from the fragment header to the number of x here is that the number of digits needs to be deleted.
{
B [x] = point [B [x]. next; // move backward through the subsequent pointer
M = t-B [x] + 1; // calculate the total number of new numbers
}
If (s [x]-point [B [x]. w + 1> ans) ans = s [x]-point [B [x]. w + 1; // if possible, update the answer
}
Printf ("% d \ n", ans );
Return 0;
}
[Cpp]
? /*
Filter out different colors and record the original location
At the same time, we need to record the number
Calculate the number of elements to be deleted in a certain interval: Number of all elements-the number of colors in this section
*/
# Include <cstdio>
# Include <cstring>
# Include <cmath>
# Include <iostream>
# Include <string>
# Include <algorithm>
# Include <vector>
# Include <queue>
# Include <map>
Using namespace std;
Const int MAXN = 200000 + 5, MAXM = 100000 + 5;
Const int maxh= 1000007;
Int n, m, k, c [MAXN];
Int s, r, ans, p [MAXN];
Vector <int> V [MAXM];
Void find (int x, int y)
{
Int mid = (x + y)> 1;
Int z = (V [r] [mid]-s)-(mid-p [s]); // always calculate the number of z to be deleted with s.
If (z <= k)
{
Ans = max (ans, mid-p [s] + 1 );
If (x! = Y)
Find (mid + 1, y );
}
Else
{
If (x! = Y)
Find (x, mid );
}
}
Int main ()
{
Scanf ("% d", & n, & m, & k );//
For (int I = 1; I <= n; I ++)
{
Scanf ("% d", & c [I]);
V [c [I]. push_back (I); // place position I in the queue of its color
P [I] = V [c [I]. size ()-1; // What is the current number?
}
Ans = 0;
For (s = 1; s <= n; s ++)
{
R = c [s];
Find (p [s], V [r]. size ()-1); // the last position of the current element in the queue
}
Printf ("% d \ n", ans );
Return 0;
}
Author: qq172108805