【Codeforces 467C】George and Job
【链接】 我是链接,点我呀:)
【题意】
让你从1..n这n个数字中
选出来k个不相交的长度为m的区间
然后这个k个区间的和最大
求出这k个区间的和的最大值
【题解】
设dp[i][j]表示前i个数字已经选出了j个区间的最大值
看看是以当前位置为结尾选择一个区间,还是这个位置不包括在任何一个区间里.
分这两种情况转移就好
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = 5000;
static class Task{
int n,m,k;
int a[];
long sum[];
long dp[][];
long get_sum(int l,int r) {
return sum[r]-sum[l-1];
}
public void solve(InputReader in,PrintWriter out) {
n = in.nextInt();m = in.nextInt();k = in.nextInt();
a = new int[N+10];
sum = new long[N+10];
dp = new long[N+10][N+10];
for (int i = 1;i <= n;i++) {
a[i] = in.nextInt();
}
for (int i = 1;i <= n;i++) sum[i]=sum[i-1]+a[i];
long ans = 0;
dp[0][0] = 0;
for (int i = 1;i <= n;i++)
for (int j = 0;j <= Math.min(k, i);j++) {
if (j==0) {
dp[i][j] = Math.max(dp[i][j], dp[i-1][j]);
}else {
if (i-m>=0) {
dp[i][j] = Math.max(dp[i][j], dp[i-m][j-1]+get_sum(i-m+1,i));
dp[i][j] = Math.max(dp[i][j], dp[i-1][j]);
}
}
ans = Math.max(ans, dp[i][j]);
}
out.println(ans);
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 467C】George and Job的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- 分析智能卡的ATR格式【转】
本文转载自:http://blog.csdn.net/michaelcao1980/article/details/8215135 一些例子 NXP 080=========3b f8 T0 Y1 = ...
- .NET平台下Redis使用(二)【StackExchange.Redis学习】
Program.cs内容: using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Data; usi ...
- Scikit-learn库中的数据预处理(一)
数据标准化:当单个特征的样本取值相差甚大或明显不遵从高斯正态分布时,标准化表现的效果较差.实际操作中,经常忽略特征数据的分布形状,移除每个特征均值,划分离散特征的标准差,从而等级化,进而实现数据中心化 ...
- atoi函数——将字符串转换为整数
atoi在一个叫<cstdlib>的库里,可以把字符串直接转换为整数,贼强势. 还有一个atof,就是换成浮点数,实质上是一样的. 例子: #include<cstdlib> ...
- PCB SQL SERVER 枚举分割函数(枚举值分解函数)
在SQL SERVER字段采用枚举值作为字段后,如果直接查看字段的值是很难判断这个字段的带表什么意思, 在这里介绍如用函数的方法实现枚举值分割,只有分割后才很方便知道枚举值的意思. 一.问题说明 1. ...
- 9.2NOIP模拟题
9.2 NOIP模拟 题目名称 “与” 小象涂色 行动!行动! 输入文件 and.in elephant.in move.in 输出文件 and.out elephant.in move.in 时间限 ...
- 关于二分查找 使用 lower_bound
在寻找单调递增最长自序列 , 的时候能不能确认出来哪个是单调递增最长自序列 ? 我的想法是 if(location>=num) dp[location]=b; 这样的 , 基于http:// ...
- sql数据库中常用连接
很简单的知识点,今天有点搞不清楚左外连接,右外连接:详见以下: --表stu id name 1, Jack 2, Tom 3, Kity 4, nono --表exam id grade 1, 56 ...
- 解决Oracle 本地可以连接,远程不能连接问题
一.问题描述: 1.telnet 2.输入命令 lsnrctl status 远程访问不到的原因为:监听指向的是localhost,而不是你本机的远程访问ip地址,因此需要将监听的指向修改为你本机的远 ...
- Mybatis的Dao向mapper传多个参数(三种解决方案)
第一种方案 : DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id=" ...