首页 > 其他分享 >Increase Subarray Sums

Increase Subarray Sums

时间:2024-04-10 21:26:28浏览次数:22  
标签:pre int Sums len Increase 答案 区间 Subarray

原题链接

题解

观察数据范围,看到 \(n<=5000\) 便确定了 \(O(n^2)\) 左右的算法,这样一来我可以遍历所有的区间
虽然每个 \(f(k)\) 对应的答案区间都不同,但一定能遍历到,所以我可以再遍历一遍k,算出以该区间为答案区间时的 \(f(k)\)
但是这样一来时间复杂度就超了,于是能不能优化?
假如存在某个k,使得答案区间长度等于 \(len\) , 那么这个答案区间一定是所有长度为 \(len\) 的区间里原始区间和最大的那个

code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int pre[5005]={0},ans[5005]={0};
        int n,x;
        cin>>n>>x;
        int a;
        for(int i=1;i<=n;i++)
        {
            cin>>a;
            pre[i]=pre[i-1]+a;
        }


        for(int len=1;len<=n;len++)
        {
            int sum=pre[len];
            for(int r=len+1;r<=n;r++)
            {
                sum=max(sum,pre[r]-pre[r-len]);
            }

            for(int k=0;k<=n;k++)
            {
                ans[k]=max(ans[k],sum+min(k,len)*x);//构造样例,当只有某一段区间为正值,其他区间为负无穷大时,就算加上k我也不选
            }
        }

        for(int i=0;i<=n;i++) cout<<ans[i]<<" ";
        puts("");
    }
    return 0;
}

标签:pre,int,Sums,len,Increase,答案,区间,Subarray
From: https://www.cnblogs.com/pure4knowledge/p/18127459

相关文章

  • LeetCode 2461. Maximum Sum of Distinct Subarrays With Length K
    原题链接在这里:https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/description/题目:Youaregivenanintegerarray nums andaninteger k.Findthemaximumsubarraysumofallthesubarraysof nums thatmeetthefollowingconditi......
  • CF1270B - Interesting Subarray | 思维
    links给出一个长度为\(n\)的序列\(a_1,a_2,\cdots,a_n\),求一子段\(a_l,a_{l+1},a_{l+2},\cdots,a_{r-1},a_r\),满足\(\max\{a_l,\cdots,a_r\}-\min\{a_l,\cdots,a_r\}\geqr-l+1\)。若有多个,输出任意一个子段的左右端点即可。若不存在,输出NO。\(n\leq......
  • const [increaseBigCats, increaseSmallCats] = useCatStore( (state) => [state.incr
    const[increaseBigCats,increaseSmallCats]=useCatStore((state)=>[state.increaseBigCats,state.increaseSmallCats],shallow);这段代码是在使用zustand这个React状态管理库。zustand提供了一种简洁的方式来创建可复用的状态存储,并允许组件通过hoo......
  • P1466 [USACO2.2] 集合 Subset Sums
    题目传送门:P1466[USACO2.2]集合SubsetSums-洛谷|计算机科学教育新生态(luogu.com.cn)https://www.luogu.com.cn/problem/P1466//https://www.luogu.com.cn/problem/P1466//背包#include<bits/stdc++.h>usingnamespacestd;intval[40],f[40][1005];//f[i][......
  • CF1398C Good Subarrays(写给我们萌新团体)
    GoodSubarrays传送门:GoodSubarrays-洛谷|计算机科学教育新生态(luogu.com.cn)思路暴力!!!!!一如既往的暴力!!!复杂度O(n^2)数据n到1e5TLE必定TLE我们可以用一个桶来优化实质上其实还是高中所学的排列组合思想第一步:当然是前缀和了,这边讲给新手写一下,有点冗杂,是高手直接......
  • C1. Good Subarrays (Easy Version)
    找子数组的个数双指针#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;constintN=2e5+10;inta[N];voidsolve(){ intn; cin>>n; for(inti=1;i<=n;i++)cin>>a[i]; intl=1,r=1; intans=0; while(l<=r){ if(l>n||r>......
  • C. Grouping Increases
    原题链接经过若干组数据发现贪心可行性后试图证明请移步code#include<bits/stdc++.h>usingnamespacestd;intmain(){intt;cin>>t;while(t--){intn,ans=0;cin>>n;intx=2e9,y=3e9;for(inti=1;i<=n;i++)......
  • Educational Codeforces Round 145 (Rated for Div. 2)C. Sum on Subarrays(构造)
    很意思的一道构造题题意:给一个\(n、k\),让构造长度为n的数组满足,子数组为整数的个数为k个,负数的为\(k-(n+1)*n/2\),每个数的范围为\([-1000,1000]\)这种构造题可以考虑就是前一段可以一直用一样的、最小的。我们观察可以发现\(k+k-(n+1)*n/2=(n+1)*n/2\)也就是所有子数组......
  • Hello 2024C. Grouping Increases(贪心)
    我们只需要记录每个数结尾的数是多少(有点最长上升子序列的味道)这种子序列的题目很多都是这样的,因为不需要连续很多时候我们只记录最后一个元素是多少。\(记s为较大子序列结尾当前的数,t为较小子序列结尾的数,下面分类讨论\)\(当a[i]<=t<s时\)我们将a[i]既可以放进t所在的子序列,......
  • nvm安装Nodejs时报错,Could not retrieve https://npm.taobao.org/mirrors/node/latest
    1.首先要使用管理员运行命令2.在安装nvm的目录下找到settings.txt,没有就手动增加一个node_mirror:https://npm.taobao.org/mirrors/node/npm_mirror:https://npm.taobao.org/mirrors/npm/这个地方有点奇怪,安装18的时候把上面的Https://去掉以后就下载成功了3.安装19以及......