Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2468921
  • 博文数量: 392
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 4138
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-17 13:03
个人简介

范德萨发而为

文章分类

全部博文(392)

文章存档

2017年(5)

2016年(19)

2015年(34)

2014年(14)

2013年(47)

2012年(40)

2011年(51)

2010年(137)

2009年(45)

分类:

2010-01-25 20:05:49

Balance
Time Limit: 1000MS        Memory Limit: 30000K
Total Submissions: 2669        Accepted: 1469

Description
Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance.
It orders two arms of negligible weight and each arm's length is 15. Some hooks are attached to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct values in the range 1..25. Gigel may droop any weight of any hook but he is forced to use all the weights.
Finally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced.

Knowing the repartition of the hooks and the set of the weights write a program that calculates the number of possibilities to balance the device.
It is guaranteed that will exist at least one solution for each test case at the evaluation.

Input
The input has the following structure:
• the first line contains the number C (2 <= C <= 20) and the number G (2 <= G <= 20);
• the next line contains C integer numbers (these numbers are also distinct and sorted in ascending order) in the range -15..15 representing the repartition of the hooks; each number represents the position relative to the center of the balance on the X axis (when no weights are attached the device is balanced and lined up to the X axis; the absolute value of the distances represents the distance between the hook and the balance center and the sign of the numbers determines the arm of the balance to which the hook is attached: '-' for the left arm and '+' for the right arm);
• on the next line there are G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the weights' values.

Output
The output contains the number M representing the number of possibilities to poise the balance.

Sample Input

2 4    
-2 3
3 4 5 8

Sample Output

2

Source
Romania OI 2002

题意描述:
一个天平,坐标点0是中心轴,左右两边分别有挂钩,给出一组物品,求出使天平平衡的所有挂法,必须使用所有物品。

解题思路:
参看了这里的解题思路
状态入手,发现,已知一个状态,在此基础上多挂一个物体得到的新状态,只与原状态的平衡度有关,与上面挂的什么无关。因此得到了方程。

dp[i][j] 表示在挂满前i个物体的时,平衡度为j的挂法的数量。j为正表示右面重。最极端的情况是所有物体都挂在最远端,因此平衡度最大值为 15*20*25=7500。原则上就应该有dp[ 0..20 ][-7500 .. 7500 ]。因此做一个处理,使得数组开为 dp[0.. 20][0..15000]。

现在说这个方程。dp[i][j]=sigma( dp[i-1][ j-c[k]*w[i] ] ), k=1~C, j=0~15000, i=1~20。初始状态 dp[0][7500]=1 表示不用物体时,平衡度为0有一种挂法,当然那就是什么都不挂

复杂度O(C*G*15000)完全是可以接受的


#include <stdio.h>

#define SIZE 7500

int stat[22][15001], C, G, hook[21];

void dp(int row, int wgt)
{
    int i, j, dis, idx, bound;

    bound = SIZE * 2 + 1;
    for (i=0 ; i<C ; i++)
    {

        /*每次要遍历,判断是否可能转移到这个状态*/
        for (j=-7500 ; j<7500; j++)
        {
            dis = wgt * hook[i];
            idx = j - dis + SIZE;
            if (idx < 0 || idx >= bound || 0 == stat[row - 1][idx])
                continue;

            /*这是一种可能,累计之*/
            stat[row][j + SIZE] += stat[row - 1][idx];
        }
    }
}

int main(int argc, char *argv[])
{
    int i, wgt;

    scanf("%d %d", &C, &G);
    for (i=0 ; i<C ; i++)
    {
        scanf("%d", &hook[i]);
    }
    
    stat[0][SIZE] = 1;
    for (i=1 ; i<=G ; i++)
    {
        scanf("%d", &wgt);
        dp(i, wgt);
    }
    printf("%d\n", stat[G][SIZE]);
}


阅读(1816) | 评论(1) | 转发(0) |
0

上一篇:hadoop相关站点

下一篇:气质的培养

给主人留下些什么吧!~~

chinaunix网友2010-08-20 10:27:55

牛逼!!!!!!!