首页 > 试题广场 >

Maximize The Beautiful Value

[编程题]Maximize The Beautiful Value
  • 热度指数:10 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 128M,其他语言256M
  • 算法知识视频讲解
Today HH finds a non-decreasing sequence(a1,a2....an,ai≤ai+1), he thinks it's not beautiful so he wants to make it beautiful.
To make it, HH will choose exactly one number and move it forward at least k steps(i.e. you can move ai to aj if k≤i−j), and then he defines the beautiful value F(n) as 
HH asks you to calculate max(F(n))

输入描述:
The first line contains an positive integer T(1≤T≤10), represents there are T test cases. 
For each test case: 
The first line contains two positive integers n,k(1≤n≤105,1≤k<n),the length of the sequence ,the least steps you need to move. 
The second line contains n integers a1,a2…an(1≤ai≤108) - the sequence.


输出描述:
For each test case, you should output the max F(n).
示例1

输入

3
5 3
1 1 3 4 5
5 2
1 1 3 4 5
5 1
1 1 3 4 5

输出

46
50
53

说明

In the first example, you can move the fifth number 4 for 3 steps and make the sequence become [4,1,1,3,5], then the beautiful value is 4×1+1×2+1×3+3×4+5×5=46.
You can also move the fifth number to make it become [1,5,1,3,4], the beautiful value is also 46.
In the second example, you can move the fifth number 5 for 2 steps and make the sequence become [1,1,5,3,4]
In the second example, you can move the second number 1 for 1 steps and then the sequence is still [1,1,3,4,5]

备注:
scanf is commended。
头像 HerioOvO
发表于 2020-04-07 21:49:07
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+5; ll a[N],pre[N]; int main(){ int t; cin>>t; 展开全文
头像 子希
发表于 2020-04-07 22:16:28
A题:一开始想着直接想k + 1往前面移动,然后直接输出,因为这个答案是 使得它最大化,那么一种贪心的想法就是直接移动k + 1移动到第一个,这样可以保证k + 2的答案是最优的,但是样例2就给了我一巴掌,这样是不行的,可能移动后面产生价值更好,所以我们需要从[k + 1,n]枚举往前面移动,但是n 展开全文
头像 翔村渡渡鸟
发表于 2020-04-07 22:53:45
思路:1. 首先定义一个初始和sum=a[i]i+a[i+1](i+1)....(i=1~n)2. 答案可以由初始的sum减去一个值M得到3. 通过模拟发现这个值为M=a[i]k-(a[i-k]+a[i-k+1]+...+a[i-1]),共k个数*4. 举个例子 5 2 展开全文
头像 回归梦想
发表于 2020-04-11 01:05:38
传送 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format:%lld 题目描述 Today HH finds a non-decreasing sequence(a1,a2....an,ai≤ai+1), het 展开全文

问题信息

上传者:牛客301599号
难度:
0条回答 1828浏览

热门推荐

通过挑战的用户

Maximize The Beautiful Value