Question connection
The DP status of this question is DP [I] [J], which indicates that the first apron of the previous I time has the maximum value when J planes fly out. Because there are only two la s, the first one is only OK, and the second one is OK.
Of course, there is still a situation where the plane is not flying enough in this question. This can be handled in advance (0, 0 and all the planes in front are flying)
Think about the trouble, but it is not unsolvable.
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#define N 5010int n;struct note{ int a,b;}dat[N];int sum[2][N];int dp[N][N];int min(int a,int b){ if(a == -1) return b; if(a < b) return a;return b;}int main(){ int t; cin >> t; while(t--) { cin >> n; for(int i = 1;i <= n;i++) scanf("%d%d",&dat[i].a,&dat[i].b); for(int i = 0;i <= n;i++) for(int j = 0;j <= n;j++) dp[i][j] = -1; dp[0][0] = 0; sum[0][0] = sum[1][0] = 0; for(int i = 1;i <= n;i++) { sum[0][i] = sum[0][i-1] + dat[i].a; sum[1][i] = sum[1][i-1] + dat[i].b; } int kk = 0; for(int i = 1;i <= n;i++) { if(dat[i].a == 0 && dat[i].b == 0 && sum[0][i] + sum[1][i] <= kk) { continue; } else { kk++; dat[kk].a = dat[i].a; dat[kk].b = dat[i].b; } } n = kk; for(int i = 1;i <= n;i++) { sum[0][i] = sum[0][i-1] + dat[i].a; sum[1][i] = sum[1][i-1] + dat[i].b; } //cout << n << " "; for(int i = 0;i < n;i++) { for(int j = 0;j <= i;j++) { if(dp[i][j] == -1) continue; if(sum[0][i+1] >= j+1) dp[i+1][j+1] = min( dp[i+1][j+1], max(dp[i][j],max(sum[0][i+1] - j,sum[1][i+1] - (i - j)))); if(sum[1][i+1] >= i-j+1) dp[i+1][j] = min( dp[i+1][j], max(dp[i][j],max( sum[0][i+1] - j,sum[1][i+1] - (i - j) ))); } } int mi = 99999999; for(int i = 0;i <= n;i++) if(dp[n][i] != -1) mi = min(mi,dp[n][i]); if(mi < 1) mi = 1; printf("%d\n",mi-1); } return 0;}