# NYOJ-422 string and Difference

Source: Internet
Author: User

Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 422

Solution:

After reading this question, you should know that it is a dynamic planning question. Similar to the questions you have done, you can find the maximum number of consecutive numbers (positive and negative ). This question has one more condition, that is, the absolute value, and the absolute value of the maximum and minimum values.

My method is to open two DP arrays. DP [I] indicates the maximum or minimum value until the I element. Then, use Max and min to record the absolute values of the maximum and minimum values. After a traversal, the maximum and minimum values are obtained .. But one thing to note is that the maximum value may be the maximum value of a positive number, or the maximum value of a negative number (absolute value), such as-1,-1,-1,-1. DP [I] is both-1, while dp1 [3] =-4. Therefore, when comparing the maximum value, compare the DP and dp1 arrays at the same time. But the minimum value does not happen...

The above deleted my original incorrect ideas !~ I thought it was wrong. Now let's talk about the correct idea .......

This question requires continuous substrings with the largest absolute values and continuous substrings with the smallest absolute values. If (A [I-1] + A> = A [I]) A [I] = A [I-1] + A; this DP process is implemented, but the absolute values are different. First, find the continuous substring with the maximum absolute value:

We first obtain the maximum value of this string from the first element to the I element, expressed by DP [I.

After DP [] is obtained, it is found that the maximum value of the absolute value only appears in DP [0] (the negative number is relatively large), and DP [n-1] (the positive number is relatively large ), or DP [n-1]-DP [0] (one positive and one negative ).

The minimum value requires a little trouble:

You can sort the values first. After sorting, the difference between the two adjacent elements must be the smallest. Then, we can traverse the difference values of all adjacent elements to find the smallest absolute value...

I used a little optimization and added a 0 element. When all values are negative, 0-DP [0] is the maximum value. All values are positive DP [n-1]-0 and the maximum value, if positive and negative DP [n-1]-DP [0] is the maximum value .. You don't need to compare it three times...

The Code is as follows:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int dp[1000010];int main(){int ncase;int num, temp;int max1, min1;scanf("%d", &ncase);while(ncase--){memset(dp, 0, sizeof(dp));scanf("%d", &num);dp[0] = 0;for(int i = 1; i <= num; ++i){scanf("%d", &temp);dp[i] = dp[i - 1] + temp;}sort(dp, dp + num + 1);max1 = dp[num] - dp[0];min1 = 99999;for(int i = 0; i < num; ++i)min1 = min(min1, dp[i + 1] - dp[i]);printf("%d\n", max1 - min1);}return 0;}

Related Keywords:

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.