Question Link: Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5087
Theme: Calculate the length of the second-largest Lis. Note that the two Lis with the same length are larger than the subscript and the larger Lis.
Solutions:
Struct records the maximum long fir of the current vertex, the second long sec.
The transfer of F [I]. FIR is actually a bare Lis.
But when f [J]. Fir + 1> = f [I]. fir, it also needs to be transferred. At this time, although the two LIS are of the same length, they are of different sizes.
For f [I]. Sec transfer, the first initial value is 0. Under the conditions of a [I]> A [J:
① When F [J]. Fir + 1> = f [I]. Fir:
We certainly have the maximum length of F [J]. Fir + 1, and the remaining values of F [I]. fir and f [J]. Sec + 1 are the same.
② When F [J]. fir + 1 <F [I]. at this time, the maximum length of the FIR is determined, but there is an additional optional value f [J]. fir + 1, note + 1
The HDU data is omitted, and no more than 1 can be used.
After the transfer of F [I] is completed, update the global result fir and SEC. I thought that the final result sec must be in all f [I]. Sec.
In fact, the SEC can also be f [I]. fir, which is a global growth.
If f [I]. Fir> = fir (still equal, although the length is the same)
At this time, there are three alternative answers: fir, F [I]. Sec, sec. Obviously, fir> = sec, so remove the SEC.
Update the longest fir.
#include "cstdio"#include "cstring"#include "iostream"using namespace std;int a[1005];struct status{ int fir,sec; status() {} status(int fir,int sec):fir(fir),sec(sec) {}}f[1005];int main(){ //freopen("in.txt","r",stdin); int T,n; scanf("%d",&T); while(T--) { int fir=0,sec=0; memset(f,0,sizeof(f)); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) { f[i]=status(1,0); for(int j=1;j<i;j++) { if(a[i]>a[j]) { if(f[j].fir+1>=f[i].fir) { f[i].sec=max(f[i].fir,f[j].sec+1); f[i].fir=f[j].fir+1; } else f[i].sec=max(f[i].sec,f[j].fir+1); } } if(f[i].fir>=fir) { sec=max(f[i].sec,fir); fir=f[i].fir; } } printf("%d\n",sec); }}
12046191 |
00:56:35 |
Accepted |
5087 |
125 Ms |
240 K |
1099 B |
C ++ |
Physcal |
HDU 5087 (linear DP + secondary large LIS)