当前位置: 代码迷 >> 综合 >> E. Pchelyonok and Segments(cf)dp
  详细解决方案

E. Pchelyonok and Segments(cf)dp

热度:87   发布时间:2023-11-23 05:43:59.0

 原题链接:Problem - E - Codeforces

每日一遍,dp鲨我~ 

思路晚上复习补

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef pair<int, int> PII;
const double pi = acos(-1.0);
#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define rrep(i, n) for (int i = n; i >= (1); --i)
typedef long long ll;
#define sqar(x) ((x)*(x))const int N = 1e5 + 10;
ll a[N], sum[N];
ll f[N][505];int main()
{int t, n;scanf("%d", &t);while(t--){scanf("%d", &n);rep(i, n) scanf("%lld", &a[i]);rep(i, n) sum[i] = sum[i - 1] + a[i];int ans = 1;for(int i = 1; i <= n; i++) for(int j = 0; j <= 500; j++) f[i][j] = 0;f[n][1] = a[n];for(int i = n - 1; i >= 1; i--){f[i][1] = max(f[i + 1][1], a[i]); //1单独拿出来考虑for(int j = 2; j <= 500 && i + j <= n; j++){f[i][j] = f[i + 1][j];if(f[i + j][j - 1] && sum[i + j - 1] - sum[i - 1] < f[i + j][j - 1]){f[i][j] = max(f[i][j], sum[i + j - 1] - sum[i - 1]);ans = max(ans ,j);}}}printf("%d\n", ans);}return 0;
}

  相关解决方案