Hdu 1003 Max Sum

Source: Internet
Author: User

First, what we did at the beginning is messy:
[Cpp]
/* The program is made by pyy */
/*----------------------------------------------------------------------------//
Copyright (c) 2012 panyanyany All rights reserved.
 
URL: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1003
Name: 1003 Max Sum
 
Date: Thursday, July 05,201 2
Time Stage: 1 hour
 
Result:
6132517 11:39:32 Accepted 1003
0 MS 1008 K 1950 B
C ++ pyy
 
 
Test Data:
 
Review:
 
//----------------------------------------------------------------------------*/
 
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <math. h>
# Include <vector>
 
# Include <algorithm>
# Include <iostream>
# Include <queue>
# Include <set>
# Include <string>
 
Using namespace std;
 
# Define MEM (a, v) memset (a, v, sizeof (a) // a for address, v for value
# Define max (x, y) (x)> (y )? (X): (y ))
# Define min (x, y) (x) <(y )? (X): (y ))
 
# Define INF (0x3f3f3f)
# Define maxn100009
 
# Define L (x) <1)
# Define R (x) <1) | 1)
# Define M (x, y) (x) + (y)> 1)
 
# Define DB /##/
Typedef _ int64 LL;
 
Int sum [MAXN], v [MAXN], s, t, head, tail, n, tcase;
 
 
Int main ()
{
Int I, j, MaxSum, FinSum;
 
While (scanf ("% d", & tcase )! = EOF)
{
For (j = 1; j <= tcase; ++ j)
{
Scanf ("% d", & n );
For (I = 0; I <n; ++ I)
{
Scanf ("% d", v + I );
If (0 = I)
{
FinSum = sum [I] = v [I];
S = t = head = tail = I;
}
Else
{
If (sum [I-1] <0)
{
Head = tail = I;
Sum [I] = v [I];
}
Else
{
Int tmp = sum [I-1] + v [I];
// If (tmp> = 0) // only a value greater than 0 is possible. + the subsequent number is larger.
//{
Sum [I] = tmp;
Tail = I;
//}
/* Else // if the sum is negative, it is better not to add
{
Sum [I] = v [I];
Head = tail = I;
}
*/
}
If (FinSum <sum [I])
{
S = head;
T = tail;
FinSum = sum [I];
}
}
}
Printf ("Case % d: \ n % d \ n", j, FinSum, s + 1, t + 1 );
If (j <tcase)
Putchar ('\ n ');
}
}
 
Return 0;
}


Second, the modified ones refer to others' code:
[Cpp]
/* The program is made by pyy */
/*----------------------------------------------------------------------------//
Copyright (c) 2012 panyanyany All rights reserved.
 
URL:
Name:
 
Date: Thursday, July 05,201 2
Time Stage:
 
Result:
6132607 12:00:59 Accepted 1003
0 MS 616 K 1623 B
C ++ pyy
 
 
Test Data:
 
Review:
 
//----------------------------------------------------------------------------*/
 
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <math. h>
# Include <vector>
 
# Include <algorithm>
# Include <iostream>
# Include <queue>
# Include <set>
# Include <string>
 
Using namespace std;
 
# Define MEM (a, v) memset (a, v, sizeof (a) // a for address, v for value
# Define max (x, y) (x)> (y )? (X): (y ))
# Define min (x, y) (x) <(y )? (X): (y ))
 
# Define INF (0x3f3f3f)
# Define maxn100009
 
# Define L (x) <1)
# Define R (x) <1) | 1)
# Define M (x, y) (x) + (y)> 1)
 
# Define DB /##/
Typedef _ int64 LL;
 
Int v [MAXN], s, t, head, n, tcase;
 
 
Int main ()
{
Int I, j, FinSum, CurSum;
 
While (scanf ("% d", & tcase )! = EOF)
{
For (j = 1; j <= tcase; ++ j)
{
Scanf ("% d", & n );
FinSum =-100000000;
CurSum = head = 0;
For (I = 0; I <n; ++ I)
{
Scanf ("% d", v + I );
CurSum + = v [I];
// The following two if values cannot be exchanged; otherwise, only one negative number cannot be processed.
If (FinSum <CurSum)
{
FinSum = CurSum;
S = head;
T = I;
}
If (CurSum <0)
{
CurSum = 0;
Head = I + 1;
}
}
Printf ("Case % d: \ n % d \ n", j, FinSum, s + 1, t + 1 );
If (j <tcase)
Putchar ('\ n ');
}
}
 
Return 0;
}
Author: panyanyany

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.