Luogu P1982 Children's Numbers

Topic description

There are n children lined up in a row. Each child has a number on their hands, which can be positive or negative. It is stipulated that the characteristic value of each child is equal to the maximum value of the sum of the numbers in the hands of several consecutive (at least one) children in the children in front of him (including himself).

As the teacher of these children, you need to give each child a score. The score is stipulated as follows: the score of the first child is his eigenvalue, and the scores of other children are all the children in front of him (excluding him I), the child's score plus the maximum value of its eigenvalues.

Please calculate the maximum value of all the children's scores, keep the sign of the maximum value when outputting, and output the absolute value modulo p .

Input and output format

Input format:

The input file is number.in .

The first line contains two positive integers n , p, separated by a space.

The second line contains n numbers, separated by a space between each two integers, representing the number on each child's hand.

Output format:

The output file is named number.out .

The output is only one line containing an integer representing the result of taking the maximum score modulo p .

Input and output example

Input sample #1 : 

5 997
1 2 3 4 5

Sample output #1 : 

21

Input example #2 : 

5 7
-1 -1 -1 -1 -1

Sample output #2 : 

-1

illustrate

Case 1:

The eigenvalues ​​of the children are 1 , 3, 6, 10, 15, the scores are 1, 2, 5, 11, 21, and the maximum value is 21

The modulo for 997 is 21.

Case 2:

The eigenvalues ​​of the children are -1 , -1, -1, -1, -1, respectively, and the scores are -1, -2, -2, -2, -2, and the maximum value is

The modulo of -1 to 7 is -1, which outputs -1.

For 50% of the data, 1 ≤ n ≤ 1,000, 1 ≤ p ≤ 1,000 and the absolute value of all numbers does not exceed 1000;

For 100% of the data, 1 ≤ n ≤ 1,000,000, 1 ≤ p ≤ 10^9, and the absolute value of all other numbers does not exceed 10^9.

Problem solving ideas:

     When you see the "maximum value of several consecutive ..." in the title, you know that it is mostly done with DP. After reading the title, I used DP to do it~

      The key to this question is that the final answer must be produced in the first position or the last position. Then pay attention to the process of doing it, don't let the answer exceed 1 billion, and %p if it exceeds. Knowing this, you can start coding this question.

Code: (Please do not copy directly)

#include <cstdio>
#include <algorithm>
long long a[1000005][3],n,p,maxn,x;//Use long long to prevent explosion
bool bo = 0;
using namespace std;
intmain()
{
    scanf("%lld%lld",&n,&p);
    p=abs(p);
    scanf("%lld",&x);
    a[1][1]=a[1][0]=a[1][2]=x;//Process the first bit
    maxn=x;//as the current maximum value
    for(long long i=2;i<=n;i++)
	{
        scanf("%lld",&x);
        if (a[i-1][1]>0) a[i][1]=a[i-1][1]+x;//The maximum field sum when i bits are calculated
          else a[i][1]=x;
        a[i][0]=max(maxn,a[i][1]);
        maxn=a[i][0];
    }
    a[2][2]=a[1][0]*2;
    if (a[2][2]>=a[1][2]) bo=1;
    for (long long i=3;i<=n;i++)
      if (a[i-1][0]<0) a[i][2]=a[i-1][2]; else//计算分数
      {
      	a[i][2]=a[i-1][2]+a[i-1][0];
      	if (a[i][2]>a[1][2]) bo=1;
      	if (a[i][2]>1000000000) a[i][2]%=p;
      }
    if (bo) printf("%lld\n",a[n][2]%p); else
      printf("%lld\n",a[1][2]%p);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389728&siteId=291194637