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

This blog presents a solution to the 'CandyDistribution' problem from HDOJ using dynamic programming. The author explores an algorithmic approach to distribute candies among participants based on certain rules and constraints, aiming for an efficient and optimal solution.

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:【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 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值