K - Balance

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

Submit Status

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

题意:一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。其中可以把天枰看做一个以x轴0点作为平衡点的横轴

思路:钩码和挂钩式各不相同的,所以每一个钩码挂在不同的的挂钩上也唯一对应一个值,我们称之为平衡值,开始时没有挂钩是平衡的然后开始挂钩码,平衡值开始变化每次找到上一个钩码挂上后能够达到的平衡值进行平衡值变化操作。dp[i][j] 表示在挂满前i个物体的时,平衡度为j的挂法的数量。j为正表示右面重。最极端的情况是所有物体都挂在最远端,因此平衡度最大值为15*20*25=7500。原则上就应该有dp[ 0..20 ][-7500 .. 7500 ]。因此做一个处理,使得数组开为 dp[0.. 20][0..15000]。然后利用滚动数组进行优化;

AC代码:

未用滚动数组:

 #include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
int dp[][*+]={};
int sgin[]={};
int gou[]={}; int main()
{
// freopen("1.txt","r",stdin);
int c,g;
cin>>c>>g;
int i;
for(i=;i<=c;i++)
cin>>gou[i];
int a;
int s=;
cin>>a;
for(i=;i<=c;i++){
dp[s][gou[i]*a+]=;
}
s++;
while(s<=g){
cin>>a;
for(i=;i<=c;i++){
sgin[i]=gou[i]*a;
}
for(i=;i<=*+;i++){
if(dp[s-][i]){
for(int j=;j<=c;j++){
dp[s][sgin[j]+i]+=dp[s-][i];
}
}
}
s++;
}
cout<<dp[g][]<<endl;
return ;
}

使用滚动数组优化:

 #include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
int dp[][*+]={};
int sgin[]={};
int gou[]={}; int main()
{
// freopen("1.txt","r",stdin);
int c,g;
cin>>c>>g;
int i;
for(i=;i<=c;i++)
cin>>gou[i];
int a;
cin>>a;
for(i=;i<=c;i++){
dp[g%][gou[i]*a+]=;
}
g--;
while(g){
cin>>a;
for(i=;i<=c;i++){
sgin[i]=gou[i]*a;
}
memset(dp[g%],,sizeof(dp[g%]));
for(i=;i<=*+;i++){
if(dp[-g%][i]){
for(int j=;j<=c;j++){
dp[g%][sgin[j]+i]+=dp[-g%][i];
}
}
}
g--;
}
cout<<dp[][]<<endl;
return ;
}


K - Balance(动态规划专项)的更多相关文章

  1. A - Space Elevator(动态规划专项)

    A - Space Elevator Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  2. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)

    题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...

  3. 陕西师范第七届K题----动态规划

    ps: 自己的方法绝对是弱爆了 肯定存在更优的方法 O(n^3)复杂度 暴力求解的.. 链接:https://www.nowcoder.com/acm/contest/121/K来源:牛客网 柯怡最近 ...

  4. G - Zombie’s Treasure Chest(动态规划专项)

    G - Zombie’s Treasure Chest Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  5. 【动态规划】拦截导弹_dilworth定理_最长递增子序列

    问题 K: [动态规划]拦截导弹 时间限制: 1 Sec  内存限制: 256 MB提交: 39  解决: 10[提交][状态][讨论版] 题目描述 张琪曼:“老师,修罗场是什么?” 墨老师:“修罗是 ...

  6. 设计Account 对象如下:  private long id;       private double balance;       private String password; 要求完善设计,使得该Account 对象能够自动分配id。 给定一个List 如下: &#

    package homework005; public class Account { private long id; private double balance; private String ...

  7. 动态规划:POJ2576-Tug of War(二维费用的背包问题)

    Tug of War Time Limit: 3000MS Memory Limit: 65536K Description A tug of war is to be arranged at the ...

  8. Codeforces Round #287 D.The Maths Lecture

    The Maths Lecture 题意:求存在后缀Si mod k =0,的n位数的数目.(n <=1000,k<=100); 用f[i][j]代表 长为i位,模k等于j的数的个数. 可 ...

  9. poj1837挂砝码

    解法(背包DP问题) (下为转) 其实感觉 像此题这种类型的并不属于dp范畴 虽然程序看起来使用的是递推这一过程,但总不能说开个二重循环就是dp吧 如果只从求解上来讲(不考虑数据值的范围), 只有枚举 ...

随机推荐

  1. hdu1036

    #include<stdio.h>int main(){ int n; double d; int num; char h,m1,m2,s1,s2; scanf("%d" ...

  2. 浙大pat1050题解

    1050. String Subtraction (20) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard Given two string ...

  3. WinForm 进程和线程

    进程: //进程用到的类Process,需要进行解析 using System.Diagnostics Process.Start("calc");//Process是非静态方法, ...

  4. html5获取图片的宽高

    var fr = new FileReader; fr.readAsDataURL($("#inputFileId").files[0]); fr.onload = functio ...

  5. rzsz的安装

    rz,sz是Linux/Unix同Windows进行ZModem文件传输的命令行工具优点:比ftp命令方便,而且服务器不用打开FTP服务. sz:将选定的文件发送(send)到本地机器rz:运行该命令 ...

  6. 【实验室笔记】C#以本地时间创建txt文件

    前段时间做的一个小项目,要求上位机在打开时候,以打开软件的系统时间的建立一个txt文件来存储下位机发送来的数据. 在第一版上位机上,取名的办法太弱了,先是读取系统时间,然后截取字符串,太笨拙.昨天,查 ...

  7. Docker 基本管理

    镜像: Docker 运行容器前需要本地存在对应的镜像,如果镜像不存在本地,Docker 会从镜像仓库下载(默认是 Docker Hub 公共注册服务器中的仓库). 由于官方镜像pull很慢 我们这边 ...

  8. HDU 1525 Euclid's Game

    题目大意: 题目给出了两个正数a.b 每次操作,大的数减掉小的数的整数倍.一个数变为0 的时候结束. 谁先先把其中一个数减为0的获胜.问谁可以赢.Stan是先手. 题目思路: 无论a,b的值为多少,局 ...

  9. Url有值怎么使用get传值

    原来url有数据 test 我们可以$_GET可以获取出来 一般form表单头用get方式都可以满足大多需求 但有一种情况 url里已经有值的时候 用url就会覆盖原来的值 而数据就会丢失 : 数据又 ...

  10. bootstrap如何自定义5等分

    根据bootstrap源码改的1比5的栅格系统 /*5等分媒体查询样式begin*/ .col-xs-1-5,.col-sm-1-5,.col-md-1-5,.col-lg-1-5,.col-xs-4 ...