HDU 2079 dp解法
选课时间(题目已修改,注意读题)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5115 Accepted Submission(s): 3944
每组数据的第一行是两个整数n(1 <= n <= 40),k(1 <= k <= 8)。
接着有k行,每行有两个整数a(1 <= a <= 8),b(1 <= b <= 10),表示学分为a的课有b门。
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#define maxn 100005
#define maxm 50000
using namespace std;
typedef long long ll;
int dp[];
struct course
{
int s;
int n;
}f[];
int main(int argc, char const *argv[])
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
memset(dp,,sizeof(dp));
for(int i=;i<=m;i++)
{
cin>>f[i].s>>f[i].n;
}
dp[]=;
for(int i=;i<=m;i++) //枚举课程
{
for(int j=n;j>=f[i].s;j--) //枚举价值,要从大到小推!
{
for(int k=;k<=f[i].n;k++) ////枚举这个物品的个数,如果当前枚举到的价值能放入此物品的话,就加上之前的价值
{
if(j>=k*f[i].s)
dp[j]+=dp[j-k*f[i].s]; //更新数组
else
break;
}
}
}
cout<<dp[n]<<endl;
}
return ;
}
另一种 从小到大推,别人写的,摘自http://www.lai18.com/content/2463208.html
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#define maxn 100005
#define maxm 50000
using namespace std;
typedef long long ll;
int main()
{
int T, n, num, a[], b[], c1[], c2[];
//由于1<=num<=8,所以a,b开到10;由于1<=n<=40,所以c1,c2开到41
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &num);
for (int i = ;i <= num;i++)
scanf("%d%d", &a[i], &b[i]);
memset(c1, , sizeof(c1));
memset(c2, , sizeof(c2));
c1[] = ;//由于本题对每个a[i]有个数限制,所以不能使数组c1初始化1【那样相当于默认学分为1的课程无限个】
//但是可以在相乘多项式前多乘一个1,所以赋值c1[0] = 1,同时下面是从i=1开始的
for (int i = ;i <= num;i++)
{//i表示乘到了第几项
for (int j = ;j <= n;j++)
{
for (int k = ;k + j <= n&&k <= b[i]*a[i];k += a[i])//a[i]是第i个多项式的单个课程学分
c2[k + j] += c1[j];
}
memcpy(c1, c2, sizeof(c1));//c2赋给c1
memset(c2, , sizeof(c2));//c2清零
}
printf("%d\n", c1[n]);
}
return ;
}
还可以用母函数写 可是我不会母函数 有时间去研究研究~
HDU 2079 dp解法的更多相关文章
- hdu 2079 选课时间
hdu 2079 选课时间 题意:选的学分总和为n,并且学分为a的课有b种,总共有K(1<=k<=8)种学分不同的课,并且要选的学分最多为40:问选课方案有多少种?(学分相同的课即认为相同 ...
- HDU 3480 DP+斜率优化
题意:给你n个数字,然后叫你从这些数字中选出m堆,使得每一堆的总和最小,一堆的总和就是这一堆中最大值减去最小值的平方,最后要使得所有堆加起来的总和最小. 思路:对这些数字排序之后,很容易想到DP解法, ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 5928 DP 凸包graham
给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...
- hdu 2079 选课时间(题目已修改,注意读题)
http://acm.hdu.edu.cn/showproblem.php?pid=2079 背包 #include <cstdio> #include <cstring> # ...
- HDU 数位dp
模板http://www.cnblogs.com/jffifa/archive/2012/08/17/2644847.html 完全理解以后,我发现这种写法实在是太厉害了,简洁,优美,可以回避很多细节 ...
- 小明的密码-初级DP解法
#include #include #include using namespace std; int visited[5][20][9009];// 访问情况 int dp[5][20][9009] ...
- HDU 1069 dp最长递增子序列
B - Monkey and Banana Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDU 1160 DP最长子序列
G - FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- linux系统下,安装centos7.0系统,配置网卡出现的问题(与centos5.x、centos6.x版本,有差异)
1.新建虚拟机时,自己下载的是centos64系统,选择系统时,默认选择centos,而未选择centos64位,导致犯了一个低级错误,导致后面网卡安装一直有问题 2.查看ip命令与centos5.x ...
- Spring事务管理总结
本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 本文对应慕课网上课程Spring事务管理,详情可查看:点我 1: 概 ...
- Handler的解析和使用
1.handler为android中多线程间通信的一种机制, @1android中只允许在UI线程(主线程)操作或改变UI,其他线程不能操作UI. @2其他线程有刷新UI的需要,所以得告诉UI线程,这 ...
- 653. Two Sum IV - Input is a BST
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- 2018第一发:记一次【Advanced Installer】打包之旅
一.前言 2017年最后几天,你们都高高兴兴的跨年,博主还在加班制作.net安装包.因为年前要出来第一版的安装包,所以博主是加班加点啊.本来想用VS自带的制作工具,不过用过的人都知道,真是非常好(to ...
- 【深度学习系列】一起来参加百度 PaddlePaddle AI 大赛吧!
写这个系列写了两个月了,对paddlepaddle的使用和越来越熟悉,不过一直没找到合适的应用场景.最近百度搞了个AI大赛,据说有四个赛题,现在是第一个----综艺节目精彩片段预测 ,大家可以去检测一 ...
- jquery通过ajax查询数据动态添加到select
function addSelectData() { //select的id为selectId //清空select中的数据 $("#selectId").empty(); $.a ...
- C语言中static关键字的用法
C记得还是大一时学的,现在觉得好久没用了,又捧起来看看.今天刚看到有关static关键字,仔细地看了一遍<C和指针>这本书中的解释,现在觉得清楚多了. 首先,我们将static关键字,修饰 ...
- 【原创】java NIO FileChannel 学习笔记 FileChannel实现分析 即FileChannelImpl分析
上文已经说了FileChannel是一个抽象类,FileChannelImpl是其实现,接下来介绍FileChannelImpl,参考代码来自OpenJDK7 首先 public class File ...
- JQ trigger函数无法触发a标签的两种解决方法
起因:点击icon图标后要触发a标签的链接转跳动作,但是用 JQ 的 $('#a').trigger('click') 居然不起作用,遂百度之,总结两种方法如下: (原因:JQ 的 trigger() ...