Jige series story-perfect formation II (marathon deformation), Marathon Formation
Jige series story-perfect formation II
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission (s): 4014 Accepted Submission (s): 1599
Problem Description gigo has come up with a new perfect formation game!
Suppose there are n people standing in front of him in order. Their height is h [1], h [2]... h [n], gigo hopes to pick out some people from them to form a new formation. If the new formation meets the following three requirements, it will be a new perfect formation:
1. The picked persons remain unchanged in the relative sequence of the original formation, and must be in a continuous order in the original formation;
2. symmetric between the left and right. If m people form a new formation, the height of 1st people is the same as that of m people, and that of 2nd people is the same as that of m people, of course, if m is an odd number, the person in the middle can be arbitrary;
3. The height of the person from left to middle must be kept unchanged. If H is used to represent the height of the new formation, then H [1] <= H [2] <= H [3]... <= H [mid].
Now, gigo wants to know: How many people can be selected to form a new perfect formation?
The first line of Input data contains an integer T, indicating that there are a total of T groups of test data (T <= 20 );
Each group of data is first an integer n (1 <= n <= 100000), indicating the number of people in the original formation. Then, enter n integers in the next row, the height of the person standing from left to right in the original formation (50 <= h <= 250, especially short and tall ).
Output please Output the maximum number of people that can form a perfect formation, and each Output group occupies one row.
Sample Input
2 3 51 52 51 4 51 52 51
Sample Output
3 4
Idea: Change the naked marathon to a height comparison condition.
Code:
#include
#include
#include#include
#include
#include
#include
#include
#include
#include
#define mem(a,b) memset(a,b,sizeof(a))#define mod 1000000007using namespace std;typedef long long ll;const int maxn = 1e6+5;const double esp = 1e-7;const int ff = 0x3f3f3f3f;int s[maxn],str[maxn];int l[maxn],n;void pretreatment(int *str,int len){int cnt = 0;s[cnt] = 5;for(int i = 0;i< len;i++){s[++cnt] = 999;s[++cnt] = str[i];}s[++cnt] = 999;return ;}void solve(int *s,int len){int mx = 0,id;for(int i = 1;i< len;i++){if(i< mx)l[i] = min(l[2*id-i],mx-i);elsel[i] = 1;while(s[i+l[i]] == s[i-l[i]]&&(s[i+l[i]] == 999||s[i+l[i]]<= s[i+l[i]-2]))l[i]++;if(i+l[i]> mx)mx = i+l[i],id = i;}}int main(){int t;cin>>t;while(t--){mem(l,0);mem(s,0);scanf("%d",&n);for(int i = 0;i< n;i++)scanf("%d",&str[i]);int len = n;pretreatment(str,len);solve(s,2*len+2);int ans = 0;for(int i = 1;i< 2*len+2;i++){int tmp = l[i]-1;ans = max(ans,tmp);}cout<