dp,dp[i][j]为前j个人中,i+1到j为一组时,说真话的最大人数。注意预判同一组人数个数是否超出组内人数上限。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
int n, ans;
int a[505][505], dp[505][505], s[505];
int main(){
//freopen("1.txt","r",stdin);
int i, j, x, y;
while(scanf("%d", &n)!=EOF){
ans=0;
memset(a, 0, sizeof(a));
memset(s, 0, sizeof(s));
for(i=0; i<n; i++){
scanf("%d%d", &x, &y);
if(x+y<n)
a[x][y]++;
if(a[x][y]>n-x-y)a[x][y]=n-x-y;
}
memset(dp, 0, sizeof(dp));
for(j=1; j<=n; j++){
for(i=0; i<j; i++){
dp[i][j]=s[i];
dp[i][j]+=a[i][n-j];
if(dp[i][j]>s[j])
s[j]=dp[i][j];
}
}
for(i=0; i<n; i++)
if(ans<dp[i][n])ans=dp[i][n];
printf("%d\n", ans);
}
return 0;
}