UVA-12563 01背包
来源:互联网 发布:focusky官方中文版mac 编辑:程序博客网 时间:2024/05/19 20:40
题意:去KTV唱歌,给定最大时间,求不超过这个时间能唱的最多歌曲,在有至少1秒的情况下还可以唱一首678秒的劲歌金曲。最大时间范围不超过1e9。歌曲数量不超过50首,每首歌长度不超过3分钟。
思路:关于范围,不要给题目中的t的范围是1e9吓到了,这个范围看似很大,但是题目限制了每首歌的时间范围和歌曲的数目,总体算一下其实不到1e4。所以可以直接用时间作为背包,dp[v]表示时间为v的情况下能唱的最多歌曲。但是还需要知道此时的时间。所以可以将其初始化为时间为v的时候恰好唱完,也就是恰好占用了v的时间。
#include <stdio.h>#include <algorithm>#include <math.h>#include <string>#include <cstring>#include <map>#include <iostream>#include <string.h>using namespace std;typedef long long ll;const int maxn = 1e4+5;const int inf=0x3f3f3f3f;int dp[maxn];int v[maxn];int n,m,V,cnt;void ZeroOnepack(int cost,int weight){ for(int v=V;v>=cost;v--) if(dp[v]<dp[v-cost]+weight) dp[v]=dp[v-cost]+weight;}int main(){ int t; scanf("%d",&t); int kase=1; while(t--){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%d",&v[i]); V=m-1; memset(dp,-1,sizeof(dp)); dp[0]=0; cnt=0; for(int i=0;i<n;i++){ ZeroOnepack(v[i],1); } int ans=0; for(int i=0;i<m;i++){ if(dp[i]>=cnt){ cnt=dp[i]; ans= i; } } printf("Case %d: %d %d\n",kase++,cnt+1,ans+678); } return 0;}
阅读全文
0 0
- UVA 12563 01背包
- UVA-12563 01背包
- uva 12563(01背包)
- UVa 12563 dp 背包
- uva 562( 01背包)
- uva 624(01背包)
- uva 10130(01背包)
- UVA 11400 01背包
- Uva-10130 SuperSale 01背包
- SuperSale - UVa 10130 01背包
- 【UVA】10130-SuperSale(01背包)
- Dividing coins+uva+01背包
- Homer Simpson+uva+01背包
- UVA 10130 SuperSale(01背包)
- UVA 624 CD (01 背包)
- uva 01背包记录路径
- UVA - 12563 Jin Ge Jin Qu hao (01背包)
- UVA 12563 Jin Ge Jin Qu hao 01背包
- Pycharm下的Anaconda配置
- Guava Cache应用
- Android自定义View——从零开始实现书籍翻页效果(三)
- python笔记007--代码风格
- CSS盒子模型要点补充
- UVA-12563 01背包
- canvas元素
- windows10 里 SQL 乱码问题解决方案
- iOS和macOS体验合一?苹果还得解决这些
- phpstorm 配置Xdebug进行调试PHP教程
- D3D 基础彩色矩形的绘制
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
- Navicat 连不上mysql数据库 问题及解决法
- 关于g++(gcc)编译c/c++的学习笔记