SAC e#1-t2 a simple question Sequence2
Clear version of the title description
Jack Bauer gets an array of 3XN, and to select a number (or not) in each column, the following conditions are met:
1. If the first line is selected, it must be greater than or equal to the previous number
2. If the second row is selected, then it must be less than or equal to the previous number
3. If the third row is selected, the number selected in the third row for successive periods must satisfy the same direction (both are less than or equal to the previous number or are greater than or equal to the previous number)
20 points: Violence enumeration Each location is not selected, choose 1, choose 2, choose 3
60 points: Use Dp[i][j] to indicate the longest length of the first column of row J.
Set DP I,j to indicate: the first I position selects the number of the J sequence, the first I position
The length of the oldest sequence that can be constituted. In particular, the DP i,2 represents the first digit
The number of the third sequence is selected, and is greater than or equal to the previous number; DP i,3 represents
The first position selects the number of the third sequence and is less than or equal to the previous number.
DP i,1 = MAX (DP j,k), (1≤j < i,1≤k≤4,a j,k≤a i,1) + 1
DP i,2 = MAX (DP j,k), (1≤j < i,1≤k≤4,a j,k≥a i,2) + 1
DP i,3 = MAX (DP j,k), (1≤j < i,1≤k≤4,k≠4,a j,k≤a i,3) + 1
DP i,4 = MAX (DP j,k), (1≤j < i,1≤k≤4,k≠3,a j,k≥a i,4) + 1
Boundary Condition: DP 0,j = 0.
Number of States O (n), transfer complexity O (n), total complexity O (n^2).
100 points: On the basis of 60 points, when the maximum value is selected, the tree array is used to optimize (need discretization).
Here to use 8 tree-like array, in order to save the amount of code, we only use a tree array to calculate the previous maximum (so we need to add some special processing) For example, when a j,k>=a i,2 is required we can follow m-a I, second query (a j,k>=a i,2 that is m-a j,k <= m-a i,2).
60 Points Bare DP
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define M 100009
#define LL Long Long
using namespace std;
int N,a[m][5],dp[m][5],ans;
int main ()
{
scanf ("%d", &n);
for (int j=1;j<=3;j++) for (int i=1;i<=n;i++)
scanf ("%d", &a[i][j]);
for (int i=1;i<=n;i++) a[i][4]=a[i][3];
for (int i=1;i<=n;i++) {for (Int.
j=1;j<i;j++)
{for
(int k=1;k<=4;k++)
{
if ( A[J][K]<=A[I][1]) Dp[i][1]=max (dp[i][1],dp[j][k]+1);
if (a[j][k]>=a[i][2]) Dp[i][2]=max (dp[i][2],dp[j][k]+1);
if (a[j][k]>=a[i][3]&&k!=4) Dp[i][3]=max (dp[i][3],dp[j][k]+1);
if (a[j][k]<=a[i][4]&&k!=3) Dp[i][4]=max (dp[i][4],dp[j][k]+1);}}
}
for (int i=1;i<=4;i++) Ans=max (Ans,dp[n][i]);
printf ("%d", ans);
return 0;
}
100 min
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #define M 100009 #
Define LL long long using namespace std;
int N,a[m][5],dp[m][5],ans;
int te[10][m*3],t[m*3],tot,m;
inline int read () {register int x=0,f=1; char Ch=getchar (); while (ch< ' 0 ' | |
Ch> ' 9 ') {if (ch== '-') f=-1; Ch=getchar ();}
while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f;
} int query (int id,int x) {int s=0;
for (int i=x;i;i-= (-i) &i) S=max (te[id][i],s);
return s;
} void Update (int id,int X,int p) {for (int i=x;i<=m;i+=i& (-i)) Te[id][i]=max (te[id][i],p);} int main () {
scanf ("%d", &n);
for (int. j=1;j<=3;j++) for (int i=1;i<=n;i++) A[i][j]=read (), t[++tot]=a[i][j];
Sort (t+1,t+tot+1);
M=unique (t+1,t+tot+1)-t-1; for (int j=1;j<=3;j++) for (int i=1;i<=n;i++) A[i][j]=lower_bound (T+1,t+m+1,a[i][j])-t;//discretization for (int
i=1;i<=n;i++) { Dp[i][1]=max (Dp[i][1],query (1,a[i][1]) +1);
Dp[i][1]=max (Dp[i][1],query (3,a[i][1]) +1);
Dp[i][1]=max (Dp[i][1],query (5,a[i][1]) +1);
Dp[i][1]=max (Dp[i][1],query (7,a[i][1]) +1);
Dp[i][2]=max (Dp[i][2],query (2,m-a[i][2]+1) +1);
Dp[i][2]=max (Dp[i][2],query (4,m-a[i][2]+1) +1);
Dp[i][2]=max (Dp[i][2],query (6,m-a[i][2]+1) +1);
Dp[i][2]=max (Dp[i][2],query (8,m-a[i][2]+1) +1);
Dp[i][3]=max (Dp[i][3],query (1,a[i][3]) +1);
Dp[i][3]=max (Dp[i][3],query (3,a[i][3]) +1);
Dp[i][3]=max (Dp[i][3],query (5,a[i][3]) +1);
Dp[i][4]=max (Dp[i][4],query (2,m-a[i][3]+1) +1);
Dp[i][4]=max (Dp[i][4],query (4,m-a[i][3]+1) +1);
Dp[i][4]=max (Dp[i][4],query (8,m-a[i][3]+1) +1);
Update (1,a[i][1],dp[i][1]);
Update (3,a[i][2],dp[i][2]);
Update (5,a[i][3],dp[i][3]);
Update (7,a[i][3],dp[i][4]);
Update (2,m-a[i][1]+1,dp[i][1]);
Update (4,m-a[i][2]+1,dp[i][2]); Update (6,M-A[I][3]+1,DP[I)[3]);
Update (8,m-a[i][3]+1,dp[i][4]);
} for (int i=1;i<=4;i++) Ans=max (Ans,dp[n][i]);
printf ("%d", ans);
return 0; }