传送门

题目大意:

给一个序列,可以在这个序列中从左至右选若干个段,第i段的长度为i,对于任意的段i,段内元素和S[i]<S[i+1],求在该序列中最多可以选出几段。

思路:设dp[i][j]为从Ai个到第An个数中可以取j段满足条件时能达到的第一段sum的最大值

我们从后往前dp,一开始dp[N][1]=A[N],其他初始为0,当j=1时,显然有,dp[i][1]=max(A[i],dp[i+1][1])。

对于其他情况,设sum=A[i]+A[i+1]+...+A[i+j-1],如果dp[i+j][j-1] > 0 且 sum < dp[i+j][j-1](为了满足段的和必须递增,以及从A[i+j]开始取可以取j-1段),那么dp[i][j]=max(dp[i+1][j],sum),否则,dp[i][j]=dp[i+1][j]。

最后遍历所有dp[1][j],dp[1][j] > 0时即第一段和>0,说明可以取j段,最大的使dp[1][j] > 0的j即为答案。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int>PII;
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#pragma warning(disable:4996)
const int maxn = 101010; LL T, N, A[maxn], S[maxn];
LL dp[maxn][510];//从Ai个到第An个数中可以取j段满足条件时能达到的第一段sum的最大值 void solve()
{
for (int i = 1; i <= N; i++)
S[i] = S[i - 1] + A[i];
for (int i = 0; i <= N + 1000; i++)
{
for (int j = 0; j <= int(sqrt((N + 1) * 2)); j++)
dp[i][j] = -INF;
}
int ans = 1;
dp[N][1] = A[N];
for (int i = N - 1; i > 0; i--)
{
dp[i][1] = max(dp[i + 1][1], A[i]);
for (int j = 2; j <= int(sqrt((N + 1)*2)); j++)
{
dp[i][j] = dp[i + 1][j];
LL sum = S[i + j - 1] - S[i - 1];
if (dp[i + j][j - 1] > 0 && sum < dp[i + j][j - 1])
dp[i][j] = max(dp[i][j], sum);
// cout << i << "-" << j << ":::::" << dp[i][j] << endl;
}
}
for (int j = 2; j <= int(sqrt((N + 1) * 2)); j++)
{
if (dp[1][j] > 0)
ans = j;
}
cout << ans << endl;
} int main()
{
IOS;
cin >> T;
while (T--)
{
cin >> N;
for (int i = 1; i <= N; i++)
cin >> A[i];
solve();
} return 0;
}

Codeforces Round #750 (Div. 2) E. Pchelyonok and Segments的更多相关文章

  1. Codeforces Round #750 (Div. 2)

    Codeforces Round #750 (Div. 2) A. Luntik and Concerts 思路分析: 首先我们可以肯定的是a,b,c都大于等于1,所以我们先让它们自己抵消自己,最后a ...

  2. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  3. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  4. Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)

    https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...

  5. Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)

    https://codeforces.com/contest/1061/problem/F 题意 假设存在一颗完全k叉树(n<=1e5),允许你进行最多(n*60)次询问,然后输出这棵树的根,每 ...

  6. Codeforces Round #245 (Div. 2) A. Points and Segments (easy) 贪心

    A. Points and Segments (easy) Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...

  7. Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  8. CodeForces -Codeforces Round #496 (Div. 3) E2. Median on Segments (General Case Edition)

    参考:http://www.cnblogs.com/widsom/p/9290269.html 传送门:http://codeforces.com/contest/1005/problem/E2 题意 ...

  9. Codeforces Round #453 (Div. 1) 901C C. Bipartite Segments

    题 http://codeforces.com/contest/901/problem/C codeforces 901C 解 首先因为图中没有偶数长度的环,所以: 1.图中的环长度全是奇数,也就是说 ...

随机推荐

  1. java匿名内部类-细节

    1 package face_09; 2 3 public class InnerClassDemo50 { 4 static class Inner{ 5 6 } 7 public static v ...

  2. mesos是什么

    听过不少人在讨论 Mesos,然而并不是很明白 Mesos 到底能够解决什么问题,使用场景是怎样的,周伟涛(国内较早一批接触使用 Docker,Mesos 等技术的开发者)用一句话形容它, Mesos ...

  3. vue中清除路由缓存

    beforeRouteLeave (to, from, next) { if (to.name === 'pageA') { /* pageA是需要跳转的路由 */ // console.log('返 ...

  4. linux字符编码防止乱码

    一:linux字符编码 en_US.UTF-8 : 美式英文,utf-8 zh_CN.UTF-8 临时优化 export LANG=zh_CN.UTF-8 : 设置编码 永久优化 vim /etc/l ...

  5. 【存】008 Linux 文件查找 find

    01 一起来认识 find! 在 Linux 系统,find 毫无疑问是最强的文件查找工具.find 一般会与其他命令结合,将查找到的结果作为参数传入到后置命令中,进行删除.统计.复制迁移等操作. 0 ...

  6. static关键字的一些使用

    百度百科定义static关键字 通常情况下,类成员必须通过它的类的对象访问,但是可以创建这样一个成员,它能够被它自己使用,而不必引用特定的实例.在成员的声明前面加上关键字static(静态的)就能创建 ...

  7. JVM专题3: GC 垃圾回收

    合集目录 JVM专题3: GC 垃圾回收 什么是GC? 为什么要有 GC? Garbage Collection, 用于内存回收. 简述一下 Java 垃圾回收机制? 那些内存需要回收 虚拟机中程序计 ...

  8. Java中的wait方法 简单介绍。

    一 wait方法怎么用? package com.aaa.threaddemo; /* * 多线程中的wait方法? public final void wait() throws Interrupt ...

  9. c语言中数组的定义和java中数组定义的一些区别

    感谢原文:https://blog.csdn.net/gzwdz778/article/details/79799408 一维情况下: c中,数组的声明需要给出数组的维数,比如: int arr[5] ...

  10. VC 窗口置顶并激活为当前窗体

    转载请注明来源:https://www.cnblogs.com/hookjc/ if (this != GetForegroundWindow())                           ...