Codeforces Round #267 (Div. 2) C. George and Job (dp)
wa哭了,,t哭了,,还是看了题解。。。
| 8170436 | 2014-10-11 06:41:51 | njczy2010 | C - George and Job | GNU C++ | Accepted | 109 ms | 196172 KB |
| 8170430 | 2014-10-11 06:39:47 | njczy2010 | C - George and Job | GNU C++ | Wrong answer on test 1 | 61 ms | 196200 KB |
| 8170420 | 2014-10-11 06:37:14 | njczy2010 | C - George and Job | GNU C++ | Runtime error on test 23 | 140 ms | 196200 KB |
| 8170348 | 2014-10-11 06:16:59 | njczy2010 | C - George and Job | GNU C++ | Time limit exceeded on test 11 | 1000 ms | 196200 KB |
| 8170304 | 2014-10-11 06:05:15 | njczy2010 | C - George and Job | GNU C++ | Time limit exceeded on test 12 | 1000 ms | 196200 KB |
| 8170271 | 2014-10-11 05:53:29 | njczy2010 | C - George and Job | GNU C++ | Wrong answer on test 3 | 77 ms | 196200 KB |
| 8170266 | 2014-10-11 05:52:37 | njczy2010 | C - George and Job | GNU C++ | Wrong answer on test 3 | 62 ms | 196200 KB |
| 8170223 | 2014-10-11 05:39:00 | njczy2010 | C - George and Job | GNU C++ | Time limit exceeded on test 12 | 1000 ms | 196100 KB |
| 8170135 | 2014-10-11 05:07:06 | njczy2010 | C - George and Job | GNU C++ | Wrong answer on test 9 | 93 ms | 196100 KB |
| 8170120 | 2014-10-11 05:01:28 | njczy2010 | C - George and Job | GNU C++ | Wrong answer on test 5 | 78 ms | 196100 KB |
1 second
256 megabytes
standard input
standard output
The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.
Given a sequence of n integers p1, p2, ..., pn. You are to choose k pairs of integers:
[l1, r1], [l2, r2], ..., [lk, rk] (1 ≤ l1 ≤ r1 < l2 ≤ r2 < ... < lk ≤ rk ≤ n; ri - li + 1 = m),
in such a way that the value of sum
is maximal possible. Help George to cope with the task.
The first line contains three integers n, m and k (1 ≤ (m × k) ≤ n ≤ 5000). The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ 109).
Print an integer in a single line — the maximum possible value of sum.
5 2 1 1 2 3 4 5
9
7 1 3 2 10 7 18 5 33 0
61
转移方程为
dp[i][j]=max(dp[i+1][j],dp[i+m][j-1]+sum[i]); (转自:http://www.tuicool.com/articles/m6v6z23) C:显然是dp,设f[i][j]表示第i段的结尾为j时的最优值,显然f[i][j]=max{f[i-1][k]+sum[j]-sum[j-m]}(0<=k<=j-m) (转自:http://www.bkjia.com/ASPjc/881176.html)
不过这样是O(k*n^2),可能超时。
我们发现每一阶段的转移能用到的最优状态都是上一阶段的前缀最优值,于是dp时直接记录下来用来下面的转移,这样就不用枚举了,变为O(k*n)水过。
貌似卡内存,用了滚动数组。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<string>
//#include<pair> #define N 5005
#define M 1000005
#define mod 1000000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,m,k;
ll dp[N][N];
ll p[N];
ll ans;
ll sum[N]; void ini()
{
memset(dp,,sizeof(dp));
memset(sum,,sizeof(sum));
int i;
ll te=;
for(i=;i<=n;i++){
scanf("%I64d",&p[i]);
} for(i=n-m+;i<=n;i++){
te+=p[i];
}
sum[n-m+]=te;
dp[n-m+][]=te;
// ma=te;
for(i=n-m;i>=;i--){
sum[i]=sum[i+]+p[i]-p[i+m];
}
//for(i=1;i<=n;i++) printf(" i=%d sum=%I64d\n",i,sum[i]);
} void solve()
{
int i,j;
dp[n][]=sum[n];
for(i=n-;i>=;i--){
for(j=k;j>=;j--){
if(i+m<=n+)
dp[i][j]=max(dp[i+][j],dp[i+m][j-]+sum[i]);
else
dp[i][j]=dp[i+][j];
}
} } void out()
{
//for(int i=1;i<=n;i++){
// for(int j=0;j<=k;j++){
// printf(" i=%d j=%d dp=%I64d\n",i,j,DP(i,j));
// }
// }
printf("%I64d\n",dp[][k]);
} int main()
{
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
// scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
//if(n==0 && k==0 ) break;
//printf("Case %d: ",ccnt);
ini();
solve();
out();
} return ;
}
Codeforces Round #267 (Div. 2) C. George and Job (dp)的更多相关文章
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- Codeforces Round #390 (Div. 2) C. Vladik and chat(dp)
http://codeforces.com/contest/754/problem/C C. Vladik and chat time limit per test 2 seconds memory ...
- Codeforces Round #605 (Div. 3) D. Remove One Element(DP)
链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...
- Codeforces Round #651 (Div. 2) E. Binary Subsequence Rotation(dp)
题目链接:https://codeforces.com/contest/1370/problem/E 题意 给出两个长为 $n$ 的 $01$ 串 $s$ 和 $t$,每次可以选择 $s$ 的一些下标 ...
- Codeforces Round #321 (Div. 2) D Kefa and Dishes(dp)
用spfa,和dp是一样的.转移只和最后一个吃的dish和吃了哪些有关. 把松弛改成变长.因为是DAG,所以一定没环.操作最多有84934656,514ms跑过,实际远远没这么多. 脑补过一下费用流, ...
- 01背包 Codeforces Round #267 (Div. 2) C. George and Job
题目传送门 /* 题意:选择k个m长的区间,使得总和最大 01背包:dp[i][j] 表示在i的位置选或不选[i-m+1, i]这个区间,当它是第j个区间. 01背包思想,状态转移方程:dp[i][j ...
- Codeforces Round #184 (Div. 2) E. Playing with String(博弈)
题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...
- Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)
Problem Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)
E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- saltstack-day1
一.远程执行命令 1.指定一个ipv4地址或者一个子网 salt -S 172.16.7.19 test.ping salt -S test.ping 2. 正则表达式 salt -E "j ...
- 爬虫5_python2_使用 Beautiful Soup 解析数据
使用 Beautiful Soup 解析数据(感谢东哥) 有的小伙伴们对写正则表达式的写法用得不熟练,没关系,我们还有一个更强大的工具,叫Beautiful Soup,有了它我们可以很方便地提取出HT ...
- Base64编码密钥时关于换行的几个问题。
在windows下一个javaweb应用,需要用http传递公钥pk.一般是String pk = BASE64ENCODER.encode(pkBytes);base64编码时,每76个字母就要换行 ...
- CPP-基础:wchar_t
目 录 1简介 2例如 3将char转换成wchar_t 1.简介 wchar_t是C/C++的字符数据类型,是一种扩展的字符存储方式,wchar_t类型主要用在国际化程序的实现中,但它不等同于uni ...
- C# IsNullOrEmpty与IsNullOrWhiteSpace
IsNullOrEmpty:非空非NULL判断 IsNullOrWhiteSpace:非空非NULL非空格判断 后者优于前者 if (!string.IsNullOrWhiteSpace(valueE ...
- 【Java_多线程并发编程】基础篇——synchronized关键字
1. synchronized同步锁的原理 当我们调用某对象的synchronized方法或代码块时,就获取了该对象的同步锁.例如,synchronized(obj)就获取了“obj这个对象”的同步锁 ...
- GIMP选择区域Selection Editor
如图我要选择该图的衣服部分和这个球的部分, 选择Select下的Selection Editor工具,然后点击魔法棒工具(Fuzzy Select Tool),选择衣服: 需要注意以下白色部分是选择的 ...
- 解决img标签上下出现间隙的方法
图片与父元素下边缘有 2px 的间隙,并不是因为空格.多个 inline-block 元素之间的间隙才是因为空格. 任何不是块级元素的可见元素都是内联元素,其表现的特性是“行布局”形式.----< ...
- 本机机器ssh docker容器
https://blog.csdn.net/u010324465/article/details/77184506 1.在docker中安装openssh-server 2.sudo /etc/ini ...
- 自定义iOS上双击Home键图切换
如果双击Home,会来到iOS App的switcher页面,在这儿列出了当前系统挂起的App, 上面有每个App的切屏,相信大家都熟悉这个东东了.它其实是每个App在挂起前,对App后个载屏. 那么 ...