日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Maximize The Beautiful Value

發布時間:2023/12/3 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Maximize The Beautiful Value 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

傳送

時間限制: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), 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。

題解:
比如:A B C D E
對應:1 2 3 4 5
此時的值為ans1=1A+2B+3C+4D+5E
現在D移動到B前面,移動了兩步
A D B C E
此時的值:ans2 = 1A+2D+3B+4C+5E
=1A+2B+3C+4D+5E-3D+(B+C+D)
觀察ans1與ans2相比有什么變化
發生改變就是減去這個數字的k倍,再加上被移動數字(共k個)的和
因為D向前移動k,說明Di–>D(i-k)
BC因為D向前而被拱到后面,從Bi—>b(i+1)
被移動數字之和我們可以用前綴和pre實現
看代碼:

#include<bits/stdc++.h> #define for(w) for(int i=w;i<=n;i++) using namespace std; const int maxn=1e5+3; long long int sum[maxn],pre[maxn]; long long int a[maxn]; long long tot=0,ans=0; int main() {long long t,n,k;//cin>>t;scanf("%lld",&t);while(t--){tot=0;ans=0;memset(pre,0,sizeof(pre));scanf("%lld%lld",&n,&k);for(1){// cin>>a[i];scanf("%lld",&a[i]);ans=ans+a[i]*i;}for(1){pre[i]=pre[i-1]+a[i];}for((k+1)){tot=max(tot,ans-a[i]*k+(pre[i-1]-pre[i-k-1]));}cout<<tot<<endl;} }

總結

以上是生活随笔為你收集整理的Maximize The Beautiful Value的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。