【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 herenew 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());}}
}
转载于:https://www.cnblogs.com/AWCXV/p/10392256.html
【Codeforces 467C】George and Job相关推荐
- 【CodeForces - 467C】George and Job(dp,思维)
题干: The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, ...
- 【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)
题干: A string t is called an anagram of the string s, if it is possible to rearrange letters in t so ...
- 【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)
题干: Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Ri ...
- 【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)
题干: 题目大意: 题意是在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围.现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭.否 ...
- 「一题多解」【CodeForces 85D】Sum of Medians(线段树 / 分块)
题目链接 [CodeForces 85D]Sum of Medians 题目大意 实现一个setsetset,支持插入,删除,求∑a5k+3∑a5k+3\sum a_{5k+3}.注意,setsets ...
- 【CodeForces 997C】Sky Full of Stars(组合计数)
题目链接:[CodeForces 997C]Sky Full of Stars 官方题解:Codeforces Round #493 - Editorial 题目大意:有一个n×nn×nn\times ...
- 【codeforces 812C】Sagheer and Nubian Market
[题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...
- 【codeforces 508B】Anton and currency you all know
[题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...
- 【codeforces 711B】Chris and Magic Square
[题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...
- 【codeforces 807C】Success Rate
[题目链接]:http://codeforces.com/contest/807/problem/C [题意] 给你4个数字 x y p q 要求让你求最小的非负整数b; 使得 (x+a)/(y+b) ...
最新文章
- igstk 学习笔记
- easyui datagrid 每行数据添加 按钮
- 网站优化排名提升不发愁,这几点你掌握了吗?
- 第二届「机器智能前沿论坛」强势来袭,众多机器学习大咖邀你共话AI未来!
- Mongodb 集群加keyFile认证,Mongodb用户管理(转:http://blog.csdn.net/wlzjsj/article/details/61421230)
- Azure系列2.1.13 —— CloudBlockBlob
- python连接redis哨兵_python连接redis sentinel集群
- python中的readalllines用法_什么是最好的编程语言?(怎样才能爱上编程呢?)
- Ubuntu 16.04 orb-slam2配置
- python-numpy.vectorize()
- EtherCAT总线伺服速度控制功能块(H5U PLC)
- Guitar Pro新手入门教程
- LX4056耐高压线性锂电池充电IC(耐压30V,带OVP,带NTC)
- (黑)群晖系统 ds photo 相机自动备份 无法识别DICM下的Camera解决方案
- matlab 汉字是方块,linux下Matlab 2020中文字体方框问题解决方法
- 基于JAVA-超市会员积分管理系统-计算机毕业设计源码+系统+lw文档+部署
- 安装DCOS,关于docker异常引发的调查
- jquery 处理页面弹出层查询数据等待的操作(gif图片加载等待)
- 罗克韦尔AB PLC RSLogix5000中的位指令使用方法介绍
- 小计合计总计表格_总计总计重复的Excel小计