【HDU】5291 Candy Distribution 【DP+打标记】

来源:互联网 发布:dw制作淘宝尺码表格 编辑:程序博客网 时间:2024/06/06 04:13

传送门:【HDU】5291 Candy Distribution

my  code:

/*************************************************************************    > File Name: D.cpp    > Author: poursoul    > Created Time: 2015年07月22日 星期三 13时04分24秒 ************************************************************************/#include <bits/stdc++.h>using namespace std ;typedef long long LL ;typedef long long Int ;typedef pair < int , int > pi ;#define clr(a,x) memset ( a , x , sizeof a )const int MAXN = 100005 ;const int N = 50000 ;const int mod = 1e9 + 7 ;int dp[2][MAXN] ;int c1[MAXN] , c2[MAXN] ;int n ;void update ( int& x , int y ) {    x += y ;    while ( x >= mod ) x -= mod ;    while ( x < 0 ) x += mod ;}void solve () {    int x , cur = 0 , sum = 0 ;    scanf ( "%d" , &n ) ;    clr ( dp[cur] , 0 ) ;    dp[cur][N] = 1 ;    for ( int i = 1 ; i <= n ; ++ i ) {        cur ^= 1 ;        scanf ( "%d" , &x ) ;        int L1 = N - sum - x , R1 = N + sum + x ;        int L2 = N - sum , R2 = N + sum ;        sum += x ;        for ( int j = L1 - 2 ; j <= R1 + 10 ; ++ j ) {            c1[j] = c2[j] = dp[cur][j] = 0 ;        }        for ( int j = L2 ; j <= R2 ; ++ j ) if ( dp[cur ^ 1][j] ) {            int s = dp[cur ^ 1][j] ;            if ( x % 2 == 0 ) {                update ( c1[j - x] , s ) ;                update ( c1[j + 2] , -s ) ;                update ( c2[j + 1] , -s ) ;                update ( c2[j + x + 3] , s ) ;            } else {                update ( c1[j - x] , s ) ;                update ( c1[j + 1] , -s ) ;                update ( c2[j + 2] , -s ) ;                update ( c2[j + x + 3] , s ) ;            }        }        for ( int j = L1 ; j <= R1 ; ++ j ) {            update ( c1[j] , c1[j - 2] ) ;            update ( c2[j] , c2[j - 2] ) ;        }        for ( int j = L1 ; j <= R1 ; ++ j ) {            update ( c1[j] , c2[j] ) ;        }        for ( int j = L1 ; j <= R1 ; ++ j ) {            dp[cur][j] = ( dp[cur][j - 1] + c1[j] ) % mod ;        }    }    printf ( "%d\n" , dp[cur][N] ) ;}int main () {    int T ;    scanf ( "%d" , &T ) ;    for ( int i = 1 ; i <= T ; ++ i ) {        solve () ;    }    return 0 ;}
0 0
原创粉丝点击