Codeforces Round #750 (Div. 2) E(dp)
Posted 吃花椒的妙酱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #750 (Div. 2) E(dp)相关的知识,希望对你有一定的参考价值。
E. Pchelyonok and Segments
传送门
题目大意:一个数组,将其划分为k个不想交的区间s1,s2…sk,使得sum1 < sum2 < …<sumk,且区间长度满足len1 = k,len2 = k-1,…lenk = 1,求最大的k
思路:dp
倒着考虑,维护的区间和需要减小,且区间长度每次+1
设dp[i][j]表示第i到n个数划分成了j个区间,则有转移方程
dp[i][j] = max(sum[i+j-1] - sum[i-1])
(sum[i+j-1] - sum[i-1] <dp[i+j][j-1])
最后看dp[1][i],最后输出i最大且dp[1][i]有值的i即可
#define _CRT_SECURE_NO_WARNINGS
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <list>
#include <queue>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _for(i,a,b) for(int i=(a) ;i<=(b) ;i++)
#define _rep(i,a,b) for(int i=(a) ;i>=(b) ;i--)
#define scd(v) scanf("%d",&v)
#define scdd(a,b) scanf("%d %d",&a,&b)
#define endl "\\n"
#define IOS ios::sync_with_stdio(false),cin.tie(0)
#define pb push_back
#define all(v) v.begin(),v.end()
#define mst(v,a) memset(v,a,sizeof(v))
#define ls p<<1
#define rs p<<1|1
#define int long long
#define inf 0x7f7f7f7f
#define fi first
#define se second
#define pii pair<int , int >
#define ls p<<1
#define rs p<<1|1
#define lson p<<1,l,mid
#define rson p<<1|1,mid+1,r
#define AC return 0
const int N = 1e5 + 100;
const int mod = 1e9 + 9;
const double eps = 1e-7;
int n, m, k;
int a[N], b[N];
int dp[N][505];
int sum[N];
void de()
{
}
void solve()
{
int k = 1;
while (k * (1 + k)/2 <= n) k++;
k--;
_for(i, 1, n) sum[i] = sum[i - 1] + a[i];
_rep(i, n, 1)
{
_for(j, 1, k) dp[i][j] = dp[i+1][j];
dp[i][1] = max(dp[i + 1][1], a[i]);
for (int j = 2; i + j <= n && j<=k; j++)
{
if (dp[i+j][j-1] && sum[i+j-1] - sum[i-1] < dp[i+j][j-1] )
{
dp[i][j] = max(dp[i][j], sum[i + j -1] - sum[i - 1]);
}
}
}
int ans = -1;
_rep(i, k, 1)
{
if (dp[1][i])
{
ans = i;
//cout << i << endl;
//return ;
break;
}
}
_for(i, 1, n)
{
_for(j, 1, k) dp[i][j] = 0;
}
cout << ans << endl;
}
signed main()
{
// freopen("data.txt","r",stdin);
IOS;
int T; cin>>T;
while (T--)
{
cin >> n;
_for(i, 1, n) cin >> a[i];
solve();
}
}
以上是关于Codeforces Round #750 (Div. 2) E(dp)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #750 (Div. 2) - B. Luntik and Subsequences - 题解
Codeforces Round #750 (Div. 2)
Codeforces Round #750 (Div. 2) E(dp)
Codeforces Round #436 E. Fire(背包dp+输出路径)