标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29065 Accepted Submission(s): 9921
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 7 using namespace std; 8 9 int n,t,x,maxtime; 10 long long ans; 11 const int N=1E5+7; 12 int a[N][15],dp[N][15]; 13 14 int MAX(int a,int b,int c) 15 { 16 int res = -1; 17 if ( a>res ) 18 res = a; 19 if ( b>res ) 20 res = b; 21 if ( c>res ) 22 res = c; 23 return res; 24 } 25 26 int main() 27 { 28 while ( scanf("%d",&n)!=EOF&&n ) 29 { 30 ans = -1 ; 31 maxtime = -1; 32 memset(a,0,sizeof(a)); 33 memset(dp,0,sizeof(dp)); 34 for ( int i = 1 ; i <= n ; i++ ) 35 { 36 scanf("%d %d",&x,&t); 37 a[t][x]++; 38 if ( t>maxtime ) 39 maxtime = t; 40 } 41 dp[1][4] = a[1][4]; 42 dp[1][5] = a[1][5]; 43 dp[1][6] = a[1][6]; 44 for ( int i = 2 ; i <= maxtime ; i++ ) 45 for ( int j = 0 ; j <= 10 ; j++ ) 46 { 47 if ( j==0 ) 48 dp[i][j] = max(dp[i-1][j],dp[i-1][j+1]) + a[i][j]; 49 else if ( j==10 ) 50 dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]) + a[i][j]; 51 else dp[i][j] = MAX(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])+a[i][j]; 52 // if ( dp[i][j]>ans ) 53 // ans = dp[i][j]; 54 55 } 56 for ( int i = 0 ; i <= 10 ; i++) 57 if ( dp[maxtime][i]>ans ) 58 ans = dp[maxtime][i]; 59 printf("%I64d\n",ans); 60 61 } 62 return 0; 63 }
WA代码:
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int n,t,x,maxtime; long long ans; const int N=1E5+7; int a[N][15],dp[N][15]; int MAX(int a,int b,int c) { int res = -1; if ( a>res ) res = a; if ( b>res ) res = b; if ( c>res ) res = c; return res; } int main() { while ( scanf("%d",&n)!=EOF&&n ) { ans = -1 ; maxtime = -1; memset(a,0,sizeof(a)); memset(dp,0,sizeof(dp)); for ( int i = 1 ; i <= n ; i++ ) { scanf("%d %d",&x,&t); a[t][x]++; if ( t>maxtime ) maxtime = t; } dp[1][4] = a[1][4]; dp[1][5] = a[1][5]; dp[1][6] = a[1][6]; for ( int i = 2 ; i <= maxtime ; i++ ) for ( int j = 0 ; j <= 10 ; j++ ) { if ( j==0 ) dp[i][j] = max(dp[i-1][j],dp[i-1][j+1]) + a[i][j]; else if ( j==10 ) dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]) + a[i][j]; else dp[i][j] = MAX(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])+a[i][j]; if ( dp[i][j]>ans ) ans = dp[i][j]; } // for ( int i = 0 ; i <= 10 ; i++) // if ( dp[maxtime][i]>ans ) // ans = dp[maxtime][i]; printf("%I64d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/111qqz/p/4395299.html