2019 fjnu acm校赛 D:Xiao Ming facevalue

Xiao Ming facevalue

TimeLimit:4000MS  MemoryLimit:256MB
64-bit integer IO format:%lld
 
Problem Description

One day, Xiao Ming accidentally picked up a magic lamp. The lamp god said to Xiao Ming that he could satisfy his wish. Xiao Ming made a wish to the lamp god to make himself more handsome, so the lamp god promised him.

But the lamp god did not simply realize this wish for him. He gave Xiao Ming a sequence A containing n integers, and let him pick m numbers in A. The sum of the m numbers is handsome value which Xiao Ming will finally get.

Of course, Xiao Ming definitely thinks that the higher the handsome value, the better, but things are not so simple, the exact rules are defined as follows:

1. Xiao Ming selected a sequence b, b1, b2, b3 … bm (bi denotes the subscript in A, 1<=b1<b2<b3<… <bm<=n)

2. Any b[i] must satisfy b[i-1]+k>=b[i].(1<i<=m)

3. Xiao Ming's handsome value S=A[b1]+A[b2]+A[b3]+...+A[bm]

For the stupid Xiao Ming, he can only pick m number randomly, but he wants to get the most handsome value, so he asked you to help. Can you help him?

Input

The first line contain an integer t indicates the number of questions asked (1 <= t <= 20).

Next, the first line of each query contains two spaces separated by an integer n, m, k.

The next n integers are the n integers of sequence A.

(1<=k,m<=n<=3000. 0<=A[i]<=1e5)

(guarantee all inquiries ∑n<=3000) 

 

Output

For each query, output a line of integers indicating the value of the maximum handsome value. 

 

SampleInput
2
5 2 2
20 1 18 2 19
13 4 3
10 0 0 0 8 0 0 0 9 0 0 0 7
SampleOutput
38
19
 
题目地址:http://www.fjutacm.com/Contest.jsp?cid=659#P3
 
 
 
题意:该题的意思是给出一个t,代表t组,然后有n个数组,你可以选m个,每个选择的相邻的数字之间的下标差不能超过k
即i = 1, k = 3
那么[2, 4]的数都能选择1.
那么我们可以得到一个状态转移方程、
设状态为dp[i][j] 第一维i为当前选择了几个数字,第二维j为当前选择的是哪一个数字的下标
故可得状态转移方程为dp[i][j] = max(dp[i - 1][j], j 属于 [j - k, j - 1]) + dp[1][j];
那么我们可以得到一个O(n ^ 3)的dp
但是参考题目数据是无法通过该题的,可以采用ST表优化,优化查询[dp[i - 1][j - k], dp[i - 1][j - 1]]的最大值
复杂度可以降为O(n ^ 2 log n)
那么在比赛的时候可以通过该题
其实正解是可以优化到O(n ^ 2)的,使用单调队列优化该DP
 
先附上ST表优化的代码:
 
复制代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 3005;
int t;
ll dp[MAXN][MAXN];
ll f[MAXN][20];
void ST_prework(int n, int step){
    for(int i = 1; i <= n; i ++) f[i][0] = dp[step - 1][i];
    int t = log(n) / log(2) + 1;
    for(int j = 1; j < t; j ++){
        for(int i = 1; i <= n - (1 << j) + 1; i ++){
            f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
        }
    }
}
ll ST_query(int l, int r){
    int k = log(r - l + 1) / log(2);
    return max(f[l][k], f[r - (1 << k) + 1][k]);
}
int main(){
    scanf("%d", &t);
    while(t --){
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 1; i <= n; i ++){
            scanf("%lld", &dp[1][i]);
        }
        for(int i = 2; i <= m; i ++){
            ST_prework(n, i);
            for(int j = i; j <= n; j ++){
                int l = j - k;
                if(l <= 0){
                    l = 1;
                }
                dp[i][j] = ST_query(l, j - 1) + dp[1][j];
            }
        }
        ll re = 0;
        for(int i = 1; i <= n; i ++){
            re = max(re, dp[m][i]);
        }
        printf("%lld\n", re);
    }
    return 0;
}
复制代码

以下为单调队列优化:

复制代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXN = 3005;
int t;
ll dp[MAXN][MAXN];
deque<ll>que;
int main(){
    scanf("%d", &t);
    while(t --){
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 1; i <= n; i ++){
            scanf("%lld", &dp[1][i]);
        }
        for(int i = 2; i <= m; i ++){
            while(!que.empty()){
                que.pop_back();
            }
            int l = i - k;
            if(l <= 0){
                l = 1;
            }
            for(int j = l; j < i; j ++){
                if(que.empty()){
                    que.push_back(j);
                }
                else{
                    if(dp[i - 1][j] >= dp[i - 1][que.front()]){
                        while(!que.empty()){
                            que.pop_back();
                        }
                        que.push_back(j);
                    }
                    else{
                        while(dp[i - 1][j] >= dp[i - 1][que.back()]){
                            que.pop_back();
                        }
                        que.push_back(j);
                    }
                }
            }
            for(int j = i; j <= n; j ++){
                if(que.front() < j - k){
                    que.pop_front();
                }
                dp[i][j] = dp[i - 1][que.front()] + dp[1][j];
                if(dp[i - 1][j] >= dp[i - 1][que.front()]){
                    while(!que.empty()){
                        que.pop_back();
                    }
                    que.push_back(j);
                }
                else{
                    while(dp[i - 1][j] >= dp[i - 1][que.back()]){
                        que.pop_back();
                    }
                    que.push_back(j);
                }
            }
        }
        ll re = 0;
        for(int i = 1; i <= n; i ++){
            re = max(re, dp[m][i]);
        }
        printf("%lld\n", re);
    }
    return 0;
}
复制代码

 

 
 
posted @   moxin0509  阅读(516)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示