西南交通大学第十三届ACM决赛 F.Maximize The Beautiful Value【思维】

题目描述

Today HH finds a non-decreasing sequence(a 1,a 2....a n,a i≤a i+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 a i  to a j  if k≤i−j), and then he defines the beautiful value F(n) as 
西南交通大学第十三届ACM决赛 F.Maximize The Beautiful Value【思维】_第1张图片
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≤10 5,1≤k
The second line contains n integers a 1,a 2…a n(1≤a i≤10 8) - 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]
题目大意:


给出长度为N的一个序列,我们需要找一个数并且将其向前置换到一个位子,要求这个位子距离原位子的长度至少为K,序列的价值是Σai*i问我们怎样置换能够使得最终解最大。


思路:


观察到这题的数据是从小到大递增的话,问题就是水题了。

我们肯定希望一个数向前置的位子尽可能的近就行。


Ac代码:

#include
#include
#include
using namespace std;
#define ll long long int
ll a[150000];
ll sum[150000];
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        int n,k;
        scanf("%d%d",&n,&k);
        ll tot=0;
        for(int i=1;i<=n;i++)scanf("%lld",&a[i]),tot+=i*a[i];
        for(int i=1;i<=n;i++)
        {
            if(i==1)sum[i]=a[i];
            else sum[i]=sum[i-1]+a[i];
        }
        ll output=-10000000000000;
        for(int i=k+1;i<=n;i++)
        {
            ll now=tot+(sum[i-1]-sum[i-1-k])-a[i]*k;
            output=max(output,now);
        }
        printf("%lld\n",output);
    }
}












你可能感兴趣的:(水题)