[codevs1554]最佳课题选择
题目描述
Matrix67要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择。由于课题数有限,Matrix67不得不重复选择一些课题。完成不同课题的论文所花的时间不同。具体地说,对于某个课题i,若Matrix67计划一共写x篇论文,则完成该课题的论文总共需要花费Ai*x^Bi个单位时间(系数Ai和指数Bi均为正整数)。给定与每一个课题相对应的Ai和Bi的值,请帮助Matrix67计算出如何选择论文的课题使得他可以花费最少的时间完成这n篇论文。
输入输出格式
输入格式:
第一行有两个用空格隔开的正整数n和m,分别代表需要完成的论文数和可供选择的课题数。
以下m行每行有两个用空格隔开的正整数。其中,第i行的两个数分别代表与第i个课题相对应的时间系数Ai和指数Bi。
输出格式:
输出完成n篇论文所需要耗费的最少时间。
输入输出样例
输入样例#1:
10 3
2 1
1 2
2 1
输出样例#1:
19
说明
【样例说明】
4篇论文选择课题一,5篇论文选择课题三,剩下一篇论文选择课题二,总耗时为2*4^1+1*1^2+2*5^1=8+1+10=19。可以证明,不存在更优的方案使耗时小于19。
【数据规模与约定】
对于30%的数据,n<=10,m<=5;
对于100%的数据,n<=200,m<=20,Ai<=100,Bi<=5。
解题报告
这个题目容易想到是一个背包,每一篇论文都有在当前课题下写或不写两个选择,预处理数组f[i,j]表示第i个课题写第j个论文时的花费,ff[i,j]:=min(ff[i-1,j],f[i,j-1])即可。
后来研究了几组数据发现想的还是太少了,例如说对于样例数据的f[2,3]的值,结果不是6,9或许其他的什么,而是写两篇一课题,一篇二课题,而这样的话我的方程显然就不对了。
于是我又机智的写了一个在30%数据内的搜索完成这项功能,代码如下,也确实是30分。
var f:array[-..,-..] of int64;
ff:array[-..,-..] of int64;
a:array[..] of int64;
b:array[..] of int64;
n,m,i,j,k:longint;
minn:int64=; function min(x,y:int64):int64;
begin
if x>y then exit(y) else exit(x);
end; function mi(a,b:int64):int64;
var t,y:int64;
begin
t:=;
y:=a;
while b<> do
begin
if (b and )= then t:=t*y;
y:=y*y;
b:=b shr ;
end;
exit(t);
end; procedure five;
var i,j,k,l,o:longint;
begin
for i:= to n do
for j:= to n do
for k:= to n do
for l:= to n do
for o:= to n do
if (i+j+k+l+o=n) then
minn:=min(minn,f[,i]+f[,j]+f[,k]+f[,l]+f[,o]);
end; procedure four;
var i,j,k,l,o:longint;
begin
for i:= to n do
for j:= to n do
for k:= to n do
for l:= to n do
if (i+j+k+l=n) then
minn:=min(minn,f[,i]+f[,j]+f[,k]+f[,l]);
end; procedure three;
var i,j,k,l,o:longint;
begin
for i:= to n do
for j:= to n do
for k:= to n do
if (i+j+k=n) then
minn:=min(minn,f[,i]+f[,j]+f[,k]);
end; procedure two;
var i,j,k,l,o:longint;
begin
for i:= to n do
for j:= to n do
if (i+j=n) then
minn:=min(minn,f[,i]+f[,j]);
end; procedure one;
var i:longint;
begin
minn:=f[,n];
end; begin
readln(n,m);
fillchar(f,sizeof(f),);
fillchar(ff,sizeof(ff),);
for i:= to m do
readln(a[i],b[i]);
for i:= to m do
for j:= to n do
f[i,j]:=a[i]*mi(j,b[i]);
if m= then one;
if m= then two;
if m= then three;
if m= then four;
if m= then five;
if minn= then
begin
for i:= to m do
for j:= to n do
ff[i,j]:=min(f[i,j-],f[i-,j]);
minn:=f[m,n]
end;
writeln(minn);
end.
其实实际上也就是一个背包,再多考虑一种循环,表示当现在一共写了j篇论文时,用k篇是在当前课题下写的,这样就可以考虑到所有的情况了,代码如下:
{AC}
var f:array[-..,-..] of int64;
//前i个课题写了j个论文的最小花费
a:array[..] of int64;
b:array[..] of int64;
n,m,i,j,k:longint;
function min(x,y:int64):int64;
begin
if x>y then exit(y) else exit(x);
end;
function mi(a,b:int64):int64;
var t,y:int64;
begin
t:=;
y:=a;
while b<> do
begin
if (b and )= then t:=t*y;
y:=y*y;
b:=b shr ;
end;
exit(t);
end;
//快速幂,其实对于这道题的数据加不加用处不大
begin
readln(n,m);
fillchar(f,sizeof(f),);
for i:= to m do
readln(a[i],b[i]);
for i:= to n do f[i,]:=;
for i:= to n do
f[,i]:=a[]*mi(i,b[]);
//预处理最底层的元素
for i:= to m do //一共选了多少课题
for j:= to n do //一共写了多少论文
begin
f[i,j]:=f[i-,j]; //一定要先赋值,否则可能得出不正确答案
for k:= to j do //在当前课题下写了多少论文
f[i,j]:=min(f[i,j],f[i-,j-k]+a[i]*mi(k,b[i]));
end;
writeln(f[m,n]);
end.
这次的惨痛教训表明我的DP思想还不完善,练习还太少,还需要更多的练习和总结。
加油,争取拿出联赛一等!
[codevs1554]最佳课题选择的更多相关文章
- 洛谷 P1336 最佳课题选择
P1336 最佳课题选择 题目提供者 yeszy 标签 动态规划 福建省历届夏令营 传送门 难度 尚无评定 题目描述 Matrix67要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择.由于课 ...
- P1336 最佳课题选择
P1336 最佳课题选择 题目描述 Matrix67要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择.由于课题数有限,Matrix67不得不重复选择一些课题.完成不同课题的论文所花的时间不同 ...
- 【线性DP】【lgP1336】最佳课题选择
传送门 Description Matrix67要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择.由于课题数有限,Matrix67不得不重复选择一些课题.完成不同课题的论文所花的时间不同.具 ...
- luogu P1336 最佳课题选择
题目描述 Matrix67要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择.由于课题数有限,Matrix67不得不重复选择一些课题.完成不同课题的论文所花的时间不同.具体地说,对于某个课题i ...
- RQNOJ 117 最佳课题选择:多重背包
题目链接:https://www.rqnoj.cn/problem/117 题意: NaCN_JDavidQ要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择. 由于课题数有限,NaCN_JD ...
- luogu P1336 最佳课题选择 |背包dp
题目描述 Matrix67要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择.由于课题数有限,Matrix67不得不重复选择一些课题.完成不同课题的论文所花的时间不同.具体地说,对于某个课题i ...
- luogu1336 最佳课题选择
背包问题加强版orz #include<iostream> #include<cstdio> #include<cmath> #include<cstring ...
- 洛谷 题解 P1336 【最佳课题选择】
详细解析解题过程 设计状态 dp[i][j]表示前i节课题写j篇论文花费的最少时间 初始数组 for(int i=0;i<=20;i++) for(int j=0;j<=200;j++)d ...
- 最新版 INSPINIA IN+ - WebApp Admin Theme v2.7.1,包含asp.net MVC5示例代码,做管理系统最佳的选择。
下载地址:http://download.csdn.net/download/wulang1988/10039402 最新版 INSPINIA IN+ - WebApp Admin Theme v2. ...
随机推荐
- JS 页面打印
var hkey_root, hkey_path, hkey_key hkey_root = "HKEY_CURRENT_USER" hkey_path = "\\Sof ...
- RepeatedDNASequences BestTime_to_Buy_and_SellStockIV
/** * @Author: weblee * @Email: likaiweb@163.com * @Blog: http://www.cnblogs.com/lkzf/ * @Time: 2015 ...
- Web API 返回json文件的2中不用方式
//方法一:直接返回序列化后的json文件 public static HttpResponseMessage ConvertToJson(this Object obj) { String str= ...
- ubuntu10.04搭建嵌入式开发环境
改源 配置vim set number set autoindent set smartindent set tabstop=4 set incsearch 安装g++ 配置samba 1.先安装程序 ...
- C++ map映射的使用方法
今天考试做了道题,用上了map,这是一道提高组联赛难度的题目,先发题目: ****************************** 1. A-B problem( dec.c/cpp/pas) . ...
- Eclipse启动Tomcat错误:Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are already(转载)
转载自:http://blog.csdn.net/aigochina/article/details/7891107 Eclipse启动Tomcat错误: Several ports (8080, 8 ...
- sharepoint 2013 sp1
http://www.microsoft.com/en-us/download/details.aspx?id=42544 http://soussi-imed.over-blog.com/artic ...
- javascript移动设备触屏事件
ontouchstartontouchmoveontouchendontouchcancel 目前移动端浏览器均支持这4个触摸事件: /** * onTouchEvent */ var div = d ...
- Java Portlet 规范概述
首先,解释几个基本的术语. 1)Portal Portal 是一种 web 应用,通常具有个性化.单点登录.来自不同源的内容聚合(aggregation)并提供信息系统表现层等特点.所谓聚合,是指将不 ...
- treeview 点击时选中节点
private void tv_WebList_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { Point clickPo ...