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 ...
随机推荐
- 【Java并发编程】synchronized相关面试题总结
目录 说说自己对于synchronized关键字的了解 synchronized关键字的三种使用 synchronized关键字的底层原理 JDK1.6之后对synchronized关键字进行的优化 ...
- 腾讯云ClickHouse如何实现自动化的数据均衡?
一.引言 ClickHouse 是一个用于联机分析( OLAP )的列式数据库管理系统( DBMS ).它于 2016 年以 Apache 2.0 协议开源,以优秀的查询性能,深受广大大数据工程师欢 ...
- 二分类问题续 - 【老鱼学tensorflow2】
前面我们针对电影评论编写了二分类问题的解决方案. 这里对前面的这个方案进行一些改进. 分批训练 model.fit(x_train, y_train, epochs=20, batch_size=51 ...
- linux与linux间,互相拷贝文件
直接使用scp命令 和远程Linux主机 进行文件的拷贝 1.可以将远程Linux系统上的文件拷贝到本地计算机 2.也可以将本地计算机上的文件拷贝到远程Linux系统上. 比如:我们要拷贝 ...
- 简单渗透测试流程演示(445端口、IPC$、灰鸽子)
目录 一.实验流程 二.实验过程 2.1 信息收集 2.2 利用过程 2.3 暴力破解系统密码之445 2.4 通过木马留后门 一.实验流程 0.授权(对方同意被渗透测试才是合法的.)1.信息收集 ...
- 国产化之路-安装WEB服务器
专题目录 国产化之路-统信UOS操作系统安装 国产化之路-国产操作系统安装.net core 3.1 sdk 国产化之路-安装WEB服务器 国产化之路-安装达梦DM8数据库 国产化之路-统信UOS + ...
- 这里有一份Java程序员的珍藏书单,请您注意查收
前言 不要因为迷茫,而停止了脚下前进的路.给大家推荐一份Java程序员必看的书单,豆瓣评分都挺不错的,每一本都值得去读,都值得去收藏,加油呀 本文已经收录到github https://github. ...
- Java知识系统回顾整理01基础05控制流程03 while
while和do-while循环语句 一.while:条件为true时 重复执行 只要while中的表达式成立,就会不断地循环执行 public class HelloWorld { public s ...
- 【奇淫巧技】sqlmap绕过过滤的tamper脚本分类汇总
sqlmap绕过过滤的tamper脚本分类汇总
- (入门)matlab中创建和调用m文件
大学学过的一款软件,说实话没好好学,老师直接讲到高深的做仿真之类的 综合网上的教程讲述基础的matlab创建遇到的问题: 参考: 1. https://blog.csdn.net/weixin_423 ...