1. 程式人生 > >poj-2479 Maximum sum 【最大字串和】

poj-2479 Maximum sum 【最大字串和】

Maximum sum
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 34697 Accepted: 10752

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 of T(<=30) test cases. The number of test cases (T) is given in the first line of the input. 
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.

Output

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input

1

10
1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

13

Hint

In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer. 

Huge input,scanf is recommended.

Source

POJ Contest,Author:[email protected]

求兩段的最大和,前後進行兩次dp;

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>

using namespace std;

int t, n, p[100010], ans[100010];

int main()
{
	scanf("%d",&t);
	while (t--)
	{
		memset(ans,0,sizeof(ans));
		scanf("%d",&n);
		int b = 0, sum = -1000000000;
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &p[i]);
			b += p[i];
			sum = max(b, sum);
			ans[i] = sum;
			if (b < 0) 
				b = 0;	
		}

		b = 0, sum = -1000000000;
		int tmp = -1000000000;

		for (int i = n - 1; i >= 1; i--)
		{
			b += p[i];
			sum = max(b, sum);
			tmp = max(tmp, ans[i - 1] + sum);
			if (b < 0) 
				b = 0;
		}
		printf("%d\n",tmp);
	}
	return 0;
}


相關推薦

poj-2479 Maximum sum 字串

Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34697 Accepted: 10752 Description Given a set of n integer

程式設計網格無水題動態規劃篇字串

Written By MorrowWind,csdnicewing 可以到洛谷的P115上去練習  https://www.luogu.org/problemnew/show/P1115 題目描述         給出資料個

Maximum sum欄位變式

【題目描述】 對於給定的整數序列A={a1,a2,…,an}A={a1,a2,…,an},找出兩個不重合連續子段,使得兩子段中所有數字的和最大。我們如下定義函式 d(A): 我們的目標就是求出d(A)。 【輸入】 第一行是一個整數T(≤30),代表一

POJ 1273 Drainage Ditches流模版

ems 操作 AC LG 尋找 In size 兩種 ret 題意:現在有m個池塘(從1到m開始編號,1為源點,m為匯點),及n條有向水渠,給出這n條水渠所連接的點和所能流過的最大流量,求從源點到匯點能流過的最大流量 Dinic 1 #include<ios

PAT - 1007 Maximum Subsequence Sum大子列

題目 Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N

序列清華大學考研複試上機——序列

題目描述給出一個整數序列S,其中有N個數,定義其中一個非空連續子序列T中所有數的和為T的“序列和”。 對於S的所有非空連續子序列T,求最大的序列和。 變數條件:N為正整數,N≤1000000,結果序列和

poj 2479 Maximum sum(遞推)

宋體 思路 freopen sim ace [1] clu mat while ?? 題意:給定n個數,求兩段連續不重疊子段的最大和。 思路非常easy。把原串劃為兩段。求兩段的連續最大子串和之和,這裏要先預處理一下,用lmax數組表示1到i的最大連續子串和,用rmax

POJ 1459 &amp;&amp; ZOJ 1734--Power Network流dinic

問題 -m memory ret wid bsp man 最大 中轉 Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions:

poj 2391 Ombrophobic Bovines

print void += else ace dinic obi 同時 mes 我%……&(¥……,調了一下午,最後發現P賦值1e5能過,賦值1e6就會TLE致死。改了一下午加一晚上然而這是為什麽??? 一種常見的建圖套路,首先二分答案,註意上界要取大一點,1e9是

poj 1637 Sightseeing tour流+歐拉路

tps spa tour ble 條件 ons emp possible != 參考:https://www.cnblogs.com/kuangbin/p/3537525.html 這篇講的挺好的 首先分清歐拉路和歐拉環: 歐拉路:圖中經過每條邊一次且僅一次的路徑,要求只有

POJ 2771 Guardian of Decency 獨立集

傳送門:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K

poj 2516 Minimum Cost小費用流

題目連結:http://poj.org/problem?id=2516; 題目大意:有 n 個商店,m 個倉庫,k 種商品,問滿足供貨商要求的最小花費是多少,如果不能滿足輸出-1;前 n 行為每個商店需要商品的數量,接著 m 行為每個倉庫能放的多少個每種商品,然後是 k 個 n*m 的矩陣,表示

2019雅禮集訓費用流模型轉換D2T3 sum

目錄 題意 輸入格式 輸出格式 思路 程式碼 題意 現在你有一個集合{1,2,3,...,n},要求你從中取出一些元素,使得這些元素兩兩互質。問你能夠取出的元素總和最多是多少? 輸入格式 一個整數n 輸出格式 一個整數表示能夠選出的最大的元素總和。 思路 這道題居然

(線性dp)POJ 2479 Maximum sum

代碼 UNC bsp space orm second task 技術 time Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 44459

1305. [CQOI2009]跳舞流+二分

else edge fine string 男女 nbsp CP ios clu Description 一次舞會有n個男孩和n個女孩。每首曲子開始時,所有男孩和女孩恰好配成n對跳交誼舞。每個男孩都不會和同一個女孩跳兩首(或更多)舞曲。有一些男孩女孩相互喜歡,而其他相互不

UOJ274 [清華集訓2016] 溫暖會指引我們前行 LCT生成樹

生成 size from down print truct ack pty play 題目分析: 差評,最大生成樹裸題。hack數據還卡常。 代碼: 1 #include<bits/stdc++.h> 2 using namespace std;

maximum-subarray 序列連續 貪心

subarray span odin sin pub ive question more pid Find the contiguous subarray within an array (containing at least one number) which has

Pseudoforest HDU - 3367生成樹Kruskal

題目連結   題意:好坑的題啊,我一開始看了測試樣例以為是放進一棵樹中問最大的邊,每棵樹上最多成一個環,沒想可以放進多棵樹中,但是要求的是最大的邊權總和就行,那麼就是Kruskal的思想了,以降序排序,然後逐步存入邊,遇到已經成環的就放棄掉這條邊,因為它一定不是最優解。 #

POJ 3020 Antenna Placement 小邊覆蓋

傳送門:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K

POJ 2584 T-Shirt Gumbo 多重匹配

T-Shirt Gumbo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2488 Accepted: 1158 Description Boudreaux and Thibodeaux are student volunt