51Nod 1065 Minimum positive subsegment sum

A sequence of N integers a[1],a[2],a[3],…,a[n], from which a subsequence (a[i],a[i+1],…a[ j]), so that the sum of this subsequence > 0, and this sum is the smallest among all subsequences whose sum > 0.
For example: 4, -1, 5, -2, -1, 2, 6, -2. -1, 5, -2, -1, the sequence sum is 1, which is the smallest.
Input
Line 1: Length N of integer sequences (2 <= N <= 50000)
Lines 2 - N+1: N integers
Output
Output the minimum positive subsegment sum.
Input example
8
4
-1
5
-2
-1
2
6
-2
Output example
1
For each digit, calculate the sum of the digits from the first digit to that position. Then sort these sums.

If the nodes in similar positions satisfy the contextual relationship of their pos, the minimum value is compared. Because they are similar, they are already candidates for the minimum value, and the rest are absolutely impossible, because if A to B cannot form a queue, A to B If C forms a queue, then B to C must be smaller than the value of A to C, and it must be able to form a queue (A and B cannot form a queue, indicating that posA>posB, A and C can form a queue, indicating that posA<posC , then there must be posB<posC).
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
struct node
{
    ll value,pos;
    bool operator<(const node a)
    {
        return a.value>value;
    }
}e[50006];
ll n,x;
intmain ()
{
    scanf("%lld",&n);
    e[0].value=e[0].pos=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&x);
        e[i].value=e[i-1].value+x;
        e[i].pos=i;
    }
    sort(e,e +n+ 1 );
    ll ans=INF;
    for(int i=1;i<=n;i++)
    {
        if(e[i].pos-e[i-1].pos>0 && e[i].value-e[i-1].value>0)
            ans=min(ans,(e[i].value-e[i-1].value));
    }
    printf("%lld\n",ans);
    return 0;
}

 

Guess you like

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