码迷,mamicode.com
首页 > 其他好文 > 详细

codeforce 1077 F1 and F2 - DP

时间:2018-11-24 14:08:39      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:lld   data-   ini   diff   code   pac   input   ssis   operator   

The only difference between easy and hard versions is the constraints.

Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of nn consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the ii-th picture has beauty aiai.

Vova wants to repost exactly xx pictures in such a way that:

  • each segment of the news feed of at least kk consecutive pictures has at least one picture reposted by Vova;
  • the sum of beauty values of reposted pictures is maximum possible.

For example, if k=1k=1 then Vova has to repost all the pictures in the news feed. If k=2k=2 then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them.

Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions.

Input

The first line of the input contains three integers n,kn,k and xx (1k,xn2001≤k,x≤n≤200) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109), where aiai is the beauty of the ii-th picture.

Output

Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement.

Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement.

Examples :
input
5 2 3
5 1 3 10 1
output
18
input
6 1 5
10 30 30 70 10 10
output
-1
input
4 3 1
1 100 1 1
output
 100
题目的大意:给定n个长度的序列,选出x个元素。要求:在任意长度为k的区间内至少有1个元素被选中

主要思路:利用dp的方法解决。
转移方程:dp[i][j]=max(dp[i][j],dp[s][j?1]+a[i]) s为区间[i?k,i)。
easy version 代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;                 //防止int上溢
const int MAXN = 200;
ll dp[MAXN + 5][MAXN + 5];
ll store[MAXN + 5];
int n = 0, k = 0, x = 0;
int main()
{
      while (cin >> n >> k >> x) {
		for (int i = 1; i <= n; ++i) {
			cin >> store[i];
		}
		if (k*(x+1) <= n) {           //查看是否有解
			printf("-1\n");
			continue;
		}
		int mi = 0, l = 0;
		for (int i = 1; i <= x; ++i) {
			l = min(n+1, i*k+1);       //选择i个最长能到l-1个元素
			for (int j = 1; j < l; ++j) {
				mi = max(j - k, 0);          //上一个状态转移的范围
				for (int s = mi; s < j; ++s) {
					dp[i][j] = max(dp[i][j], dp[i - 1][s] + store[j]);        //状态转移
				}
			}
		}
		mi = n - k + 1;
		ll m = 0;
		for (int i = mi; i <= n; ++i) {
			m = m > dp[x][i] ? m : dp[x][i];           //查找最大值
		}
		cout << m << endl;
	}
}


hard version 解题思路:将查找状态转移范围内的最大值用优先队列优化。
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
struct node {
    ll val;
    ll at;
    bool operator < (const node &a) const
    {
        return a.val > val;
    }
};
const int MAXN = 5000;
ll dp[MAXN + 2][MAXN + 2];
ll store[MAXN + 5];
int n = 0, k = 0, x = 0;
int main()
{
    scanf("%d %d %d", &n, &k, &x);
    for (int i = 1; i <= n; ++i) {
        scanf("%lld", &store[i]);
    }
    if (k*(x + 1) <= n) {
        printf("-1\n");
        return 0;
    }
    int l = 0, a = 0;
    node temp;
    for (int i = 1; i <= x; ++i) {
        l = (n + 1) > (i*k + 1) ? (i*k + 1) : n + 1;
        priority_queue<node> use;                  //使用优先队列查找转移范围内的最大值
        for (int j = 1; j < l; ++j) {
            a = max(j - k, 0);                     //可行范围
            temp.val = dp[i - 1][j - 1];
            temp.at = j - 1;
            use.push(temp);
            while (use.top().at < a) use.pop();    //将越界的元素弹出
            dp[i][j] = use.top().val + store[j];   //取出可行范围的最大值加上该点的数值
        }
    }
    a = n - k + 1;
    ll m = 0;
    for (int i = a; i <= n; ++i) {
        m = m > dp[x][i] ? m : dp[x][i];
    }
    printf("%lld\n", m);
}

感谢博主  : Ehanla提供的思路

文章链接:http://www.cnblogs.com/ehanla/p/9991806.html



codeforce 1077 F1 and F2 - DP

标签:lld   data-   ini   diff   code   pac   input   ssis   operator   

原文地址:https://www.cnblogs.com/molimao/p/10011401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!