POJ 1837.Balance
来源:互联网 发布:管家软件哪个好 编辑:程序博客网 时间:2024/06/08 10:08
题目:http://poj.org/problem?id=1837
AC代码(C++):
#include <iostream>#include <algorithm>#include <stdio.h>#include <vector>#include <queue>#include <math.h>#include <string>#include <string.h>#include <bitset>#define INF 0xfffffff#define MAXN 100105using namespace std;int c,g;int p[25],w[25];int dp[25][15005];int main(){cin>>c>>g;for(int i = 0; i < c; i++)cin>>p[i];for(int i = 0; i < g; i++)cin>>w[i];memset(dp,0,sizeof(dp));dp[0][7500] = 1;for(int i = 1; i <= g; i++){for(int j = 0; j < 15005; j++){for(int k = 0; k < c; k++){if(j+p[k]*w[i-1]>=15005||j+p[k]*w[i-1]<0)continue;dp[i][j+p[k]*w[i-1]]+=dp[i-1][j];}}}cout<<dp[g][7500]<<endl;}总结: 01背包问题, dp[i][j]为放入第i个砝码后平衡位置在j的情况数. 因为平衡位置j作为下标不能取负数, 所以移15*20*25位后平衡位置在7500. 初始时天平在第0行的平衡位置7500, 即dp[0][7500] = 1. 之后一个一个砝码放, 每个砝码在不同的位置都要放, 直到放完所有砝码, 则dp[g][7500]就是答案.
阅读全文
0 0
- POJ 1837.Balance
- 【POJ】Balance
- POJ 1837 Balance
- poj 1837 Balance
- POJ 1837 Balance
- poj 1837 Balance
- POJ 1837 Balance DP
- poj 1837Balance
- POJ 1837 Balance
- Poj 1837 Balance
- POJ 1837 Balance
- POJ 1837 Balance
- poj - 2142 - The Balance
- POJ-1837-Balance
- POJ 1837 Balance
- POJ--1837--Balance--DP
- poj 1837 balance
- poj 1837 Balance
- 数据结构 基本知识1
- 类和对象
- javascript注入攻击及防止方法
- Openresty最佳案例 | 汇总
- 【Scikit-Learn 中文文档】集成方法
- POJ 1837.Balance
- 微信小程序wx.getlocation相邻两个页面连着的时候第二个页面一直显示定位中
- html总结
- 【Angular2】生成条形码并打印网页
- JEECG架构讲解及使用
- mysqldump导出包含存储过程(SP)的数据库文件
- tcpcopy
- scala使用par并行集合设置的线程池未关闭导致的内存占用问题
- Python读取txt、csv文件