会员
周边
众包
新闻
博问
闪存
赞助商
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
SDJL的足迹
首页
博问
闪存
管理
USACO_3_1_Score Inflation
基本的动态规划题目,如果不会做的话参考下面两篇文章:
通过金矿模型介绍动态规划
USACO_2_3_Money Systems
我的java代码提交了好多次才通过,读取文件的速度太慢,哪个知道怎么才能快速的从文件中读取一堆整数?
Code
/**/
/*
ID: sdjllyh1
PROG: inflate
LANG: JAVA
complete date: 2008/12/29
complexity: O(n * m)
author: LiuYongHui From GuiZhou University Of China
more articles: www.cnblogs.com/sdjls
*/
import
java.io.
*
;
import
java.util.
*
;
public
class
inflate
{
private
static
int
m, n;
private
static
int
[] points;
//
每个题目的得分
private
static
int
[] minutes;
//
每个题目花费的时间
private
static
int
[] score;
//
得到的分数,下标为可以使用的时间
private
static
int
answer;
public
static
void
main(String[] args)
throws
IOException
{
init();
run();
output();
System.exit(
0
);
}
private
static
void
run()
{
for
(
int
i
=
0
; i
<
n; i
++
)
{
for
(
int
j
=
1
; j
<=
m; j
++
)
{
if
(j
>=
minutes[i])
{
score[j]
=
Math.max(score[j], score[j
-
minutes[i]]
+
points[i]);
}
}
}
answer
=
score[m];
}
private
static
void
init()
throws
IOException
{
BufferedReader f
=
new
BufferedReader(
new
FileReader(
"
inflate.in
"
));
char
[] buff
=
new
char
[
130000
];
f.read(buff);
StringTokenizer st
=
new
StringTokenizer(String.valueOf(buff));
m
=
Integer.parseInt(st.nextToken());
n
=
Integer.parseInt(st.nextToken());
points
=
new
int
[n];
minutes
=
new
int
[n];
for
(
int
i
=
0
; i
<
n; i
++
)
{
points[i]
=
Integer.parseInt(st.nextToken());
minutes[i]
=
Integer.parseInt(st.nextToken());
}
f.close();
score
=
new
int
[m
+
1
];
}
private
static
void
output()
throws
IOException
{
PrintWriter out
=
new
PrintWriter(
new
BufferedWriter(
new
FileWriter(
"
inflate.out
"
)));
out.println(answer);
out.close();
}
}
posted on
2008-12-29 17:14
刘永辉
阅读(
289
) 评论(
0
)
编辑
收藏
举报
刷新页面
返回顶部