poj1837 01背包(雾
Description
1. Set the number of maximum passenger coaches a mini locomotive can pull, and a mini locomotive will not pull over the number. The number is same for all three locomotives.
2. With three mini locomotives, let them transport the maximum number of passengers to destination. The office already knew the number of passengers in each passenger coach, and no passengers are allowed to move between coaches.
3. Each mini locomotive pulls consecutive passenger coaches. Right after the locomotive, passenger coaches have numbers starting from 1.
For example, assume there are 7 passenger coaches, and one mini locomotive can pull a maximum of 2 passenger coaches. The number of passengers in the passenger coaches, in order from 1 to 7, is 35, 40, 50, 10, 30, 45, and 60.
If three mini locomotives pull passenger coaches 1-2, 3-4, and 6-7, they can transport 240 passengers. In this example, three mini locomotives cannot transport more than 240 passengers.
Given the number of passenger coaches, the number of passengers in each passenger coach, and the maximum number of passenger coaches which can be pulled by a mini locomotive, write a program to find the maximum number of passengers which can be transported by the three mini locomotives.
Input
The first line of the input file contains the number of passenger coaches, which will not exceed 50,000. The second line contains a list of space separated integers giving the number of passengers in each coach, such that the ith number of in this line is the number of passengers in coach i. No coach holds more than 100 passengers. The third line contains the maximum number of passenger coaches which can be pulled by a single mini locomotive. This number will not exceed 1/3 of the number of passenger coaches.
Output
Sample Input
1
7
35 40 50 10 30 45 60
2
Sample Output240
在做poj01背包的时候搜到这一题,说实话,看不懂,想了很久,看题解一开始也没懂怎么dp的。。。
基本是把大佬的啊题解照搬过来了,因为讲的很详细QAQ,也容易懂
题目大意:
有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。
其中可以把天枰看做一个以x轴0点作为平衡点的横轴
dp思路:
每向天平中方一个重物,天平的状态就会改变,而这个状态可以由若干前一状态获得。
首先定义一个平衡度j的概念
当平衡度j=0时,说明天枰达到平衡,j>0,说明天枰倾向右边(x轴右半轴),j<0则相反
那么此时可以把平衡度j看做为衡量当前天枰状态的一个值
因此可以定义一个 状态数组dp[i][j],意为在挂满前i个钩码时,平衡度为j的挂法的数量。
由于距离c[i]的范围是-15~15,钩码重量的范围是1~25,钩码数量最大是20
因此最极端的平衡度是所有物体都挂在最远端,因此平衡度最大值为j=15*20*25=7500。原则上就应该有dp[ 1~20 ][-7500 ~ 7500 ]。
因此为了不让下标出现负数,做一个处理,使使得数组开为 dp[1~20][0~15000],则当j=7500时天枰为平衡状态(ps 关键)
那么每次挂上一个钩码后,对平衡状态的影响因素就是每个钩码的 力臂
力臂=重量 *臂长 = w[i]*c[k]
那么若在挂上第i个砝码之前,天枰的平衡度为j
(换言之把前i-1个钩码全部挂上天枰后,天枰的平衡度为j)
则挂上第i个钩码后,即把前i个钩码全部挂上天枰后,天枰的平衡度 j=j+ w[i]*c[k]
其中c[k]为天枰上钩子的位置,代表第i个钩码挂在不同位置会产生不同的平衡度
就可以推出动态转移方程 dp[i][ j+ w[i]*c[k] ]= ∑(dp[i-1][j])
//1496K 0MS #include<iostream>
using namespace std; int dp[21][15001]; //状态数组dp[i][j]
//放入(挂上)前i个物品(钩码)后,达到j状态的方法数
int main(int i,int j,int k)
{
int n; //挂钩数
int g; //钩码数
int c[21]; //挂钩位置
int w[21]; //钩码重量 /*Input*/ cin>>n>>g; for(i=1;i<=n;i++)
cin>>c[i];
for(i=1;i<=g;i++)
cin>>w[i]; /*Initial*/ memset(dp,0,sizeof(dp)); //达到每个状态的方法数初始化为0
dp[0][7500]=1; //7500为天枰达到平衡状态时的平衡度
//放入前0个物品后,天枰达到平衡状态7500的方法有1个,就是不挂钩码 /*DP*/ for(i=1;i<=g;i++)
for(j=0;j<=15000;j++)
if(dp[i-1][j]) //优化,当放入i-1个物品时状态j已经出现且被统计过方法数,则直接使用统计结果
//否则忽略当前状态j
for(k=1;k<=n;k++)
dp[i][ j+w[i]*c[k] ] += dp[i-1][j]; //状态方程 /*Output*/ cout<<dp[g][7500]<<endl;
return 0;
}
poj1837 01背包(雾的更多相关文章
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- 51nod1085(01背包)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...
- *HDU3339 最短路+01背包
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
- POJ 3624 Charm Bracelet(01背包)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34532 Accepted: 15301 ...
- (01背包变形) Cow Exhibition (poj 2184)
http://poj.org/problem?id=2184 Description "Fat and docile, big and dumb, they look so stupid ...
- hdu3339 In Action(Dijkstra+01背包)
/* 题意:有 n 个站点(编号1...n),每一个站点都有一个能量值,为了不让这些能量值连接起来,要用 坦克占领这个站点!已知站点的 之间的距离,每个坦克从0点出发到某一个站点,1 unit dis ...
随机推荐
- 10.扩展:Zero Copy
- burp suite 之 proxy(代理)
proxy 代理 通过 Options(选项)的edit(编辑) 更改代理的端口号.我的是10086 (不许更改与本机使用端口冲突的端口号) 使用火狐浏览器将代理更改为10086. 抓取火狐浏览器的包 ...
- 实现select下拉框的无限加载(懒加载)
在实际开发中我们有时无法避免select下拉功能数据过大导致页面卡顿(如在我在一次迭代中有一个select项接口返回了5000多条数据).用户体验差!结合实际开发给出了3个解决方案: 方案1.sele ...
- C++实现职工管理系统(下)
C++实现职工管理系统(下) 大家好,今天是在博客园的第十五天,博主今天给大家带来的是职工管理系统(C++)(下) 这次的随笔记录是实现(中)结语处说的几个功能,另外新增一个修改功能 此次要实现的功能 ...
- spring aop 源码分析(三) @Scope注解创建代理对象
一.源码环境的搭建: @Component @Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON,proxyMode = ScopedP ...
- Hadoop框架:HDFS简介与Shell管理命令
本文源码:GitHub·点这里 || GitEE·点这里 一.HDFS基本概述 1.HDFS描述 大数据领域一直面对的两大核心模块:数据存储,数据计算,HDFS作为最重要的大数据存储技术,具有高度的容 ...
- 认证授权:IdentityServer4 - 数据持久化
前言: 前面的文章中IdentityServer4 配置内容都存储到内存中,本篇文章开始把配置信息存储到数据库中:本篇文章继续基于github的代码来实现配置数据持久化到MySQL中 一.基于EFCo ...
- Java知识系统回顾整理01基础03变量01变量定义
定义: 变量:用来命名一个数据的标识符 一.什么是变量? 用具体实例定义变量 变量的定义是: 用来命名一个数据的标识符 1949 这是一个数字,代表某年 如果要命名这个数字,在java里就会写成: i ...
- Java知识系统回顾整理01基础06数组05复制数组
数组的长度是不可变的,一旦分配好空间,是多长,就多长,不能增加也不能减少 一.复制数组 把一个数组的值,复制到另一个数组中 System.arraycopy(src, srcPos, dest, de ...
- JavaScript打印给定区间年份的闰年
要求: 用户输入需要判断的年份区间,开始年份和结束年份,输出该区间内所有的闰年. 代码实现: function isRunYear(year) { // 是闰年返回true,否则返回false var ...