UVA - 12563 Jin Ge Jin Qu hao(劲歌金曲)(0-1背包+滚动数组)
2024-09-06 03:14:41
题意:在KTV唱歌剩下的t秒时间内,决定选最爱的n首歌中的一部分歌,在时间结束之前唱一首时长678秒的《劲歌金曲》,使得唱的总曲目尽量多(包括《劲歌金曲》),在此前提下尽量晚的离开KTV。(n<=50,t<=109)
分析:
1、输入保证所有n+1首曲子总长度严格大于t,虽然,t<=109,实际上t不会超过180n+678。
2、dp[i]剩余时间为i时唱的总曲目,time[i]剩余时间为i时唱歌时间总长度。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int dp[MAXT];
int time[MAXT];
int main(){
int T;
scanf("%d", &T);
int kase = 0;
while(T--){
int n, t;
scanf("%d%d", &n, &t);
memset(dp, 0, sizeof dp);
memset(time, 0, sizeof time);
int len = Min(t - 1, 180 * n);
for(int i = 0; i < n; ++i){
int x;
scanf("%d", &x);
for(int j = len; j >= 0; --j){
if(j >= x){
if(dp[j - x] + 1 > dp[j]){
dp[j] = dp[j - x] + 1;
time[j] = time[j - x] + x;
}
else if(dp[j - x] + 1 == dp[j]){
time[j] = Max(time[j], time[j - x] + x);
}
}
}
}
int ansnum = -1;
int anstime = 0;
for(int i = len; i >= 0; --i){
if(dp[i] > ansnum){
ansnum = dp[i];
anstime = time[i];
}
else if(dp[i] == ansnum){
anstime = Max(anstime, time[i]);
}
}
printf("Case %d: %d %d\n", ++kase, ansnum + 1, anstime + 678);
}
return 0;
}
最新文章
- .NET WEB程序员需要掌握的技能
- 数据结构->;冒泡排序
- NetworkComms V3 使用TCP通信传递IList<;T>;类型的数据
- PHP 与pdf文档 与条码
- [2016.01.01]万峰文本处理专家 v2.0
- window常用命令
- Jenkins搭建
- Oracle 客户端配置
- 重定向 vs output redirect
- String类的split方法以及StringTokenizer
- ios创建画笔的样例(双笔画效果)
- postman定义公共函数
- 几个OOD概念
- 安装OpenSSL缺失Microsoft Visual C++ 2008 Redistributables的解决方案
- PHP-Iterator迭代器(遍历)接口详讲
- Coxph model Pvalue Select2
- Markdown语法简单介绍
- Dbvisualizer设置SQL语句自动提示
- linux 中更改权限命令chown,chmod,chgrp
- SQL Server开窗函数之OVER子句、PARTITION BY 子句