Codeforces 75D Big Maximum Sum 最大子段和 dp

题目链接:点击打开链接

题意:

第一行 n m

n个vector

下面n行 第一个数字u表示vector 的大小,然后后面u个数字给出这个vector

最后一行m个数字

表示把上面的vector拼接起来

得到一个大序列,求这个大序列的最大子段和

先预处理出每个vector的最大子段和,左起连续最大,右起连续最大,所有数的和

然后dp 一下。。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
const long long inf = 1e18;
#define ll long long
#define N 250010
inline ll Max(ll n, ll x[]){
    ll ans = x[0], sum = x[0];
    for(ll i = 1; i < n; i++)
    {
       sum += x[i];
       ans = max(ans, sum);
    }
    return ans;
}

vector<ll>G[N];
ll n, m;
ll l[N],r[N],cop[N],sum[N], w[N];
ll work(ll x){
	ll ans = G[x][0], sum = 0;
	for(ll i = 0; i < G[x].size(); i++)
	{
		if(sum + G[x][i] >= 0){
			sum += G[x][i];
			ans = max(ans, sum);
		}
		else sum = 0;
		ans = max(ans, G[x][i]);
	}
	return ans;
}
void input(){
    for(ll i = 1; i <= n; i++){
        G[i].clear();
		sum[i] = 0;
        ll u, v; scanf("%I64d",&u);
        while(u--)
        {
            scanf("%I64d",&v);
            G[i].push_back(v);
			sum[i] += v;
        }
        for(ll j = 0; j < G[i].size(); j++)cop[j] = G[i][j];
        l[i] = Max((ll)G[i].size(), cop);
        for(ll j = 0; j < G[i].size(); j++)cop[G[i].size()-j-1] = G[i][j];
        r[i] = Max((ll)G[i].size(), cop);
		w[i] = work(i);
    }
}

ll dp[250010][2];
int main() {
    ll u;
    while(~scanf("%I64d %I64d",&n,&m)) {
        input();
        ll ans = -inf;
        dp[0][0] = dp[0][1] = -inf;
        for(ll i = 1; i <= m; i++)
        {
            scanf("%I64d", &u);
            dp[i][0] = dp[i-1][1] + max(sum[u], l[u]);
			dp[i][0] = max(dp[i][0], max(sum[u], l[u]));
			dp[i][1] = dp[i-1][1] + sum[u];
			dp[i][1] = max(dp[i][1], sum[u]);
			dp[i][1] = max(dp[i][1], r[u]);
			ans = max(ans, dp[i][0]);
			ans = max(ans, dp[i][1]);
			ans = max(ans, w[u]);
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
3 4
8 -10 1 9 9 -10 2 -10 -9
7 3 -10 -10 -6 3 -7 0
1 -3
1 3 2 3

*/

Codeforces 75D Big Maximum Sum 最大子段和 dp

时间: 2024-12-09 19:27:55

Codeforces 75D Big Maximum Sum 最大子段和 dp的相关文章

codeforces #319 B - Modulo Sum (抽屉原理,dp)

B - Modulo Sum Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choose a non-empty subsequence aij 

[ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33363   Accepted: 10330 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o

Codeforces Round #169 (Div. 2)C. Little Girl and Maximum Sum

传送门 Description The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries,

POJ2479 Maximum sum[DP|最大子段和]

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o

Little Girl and Maximum Sum CodeForces - 276C

---恢复内容开始--- The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, eac

Codeforces Round #169 (Div. 2)---C. Little Girl and Maximum Sum(简单贪心)

The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defi

Maximum sum(最大子段和)

Maximum sum AC_Code 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <string> 6 #include <cmath> 7 #include <cstdlib> 8 #include <algorithm> 9 using namespace std

hdu 5586 Sum 最大子段和

Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5586 Description There is a number sequence A1,A2....An,you can select a interval [l,r] or not,all the numbers Ai(l≤i≤r) will become f(Ai).f(x)=(1890x+143)mod1

ural 1146. Maximum Sum

1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this p