Non-negative Partial Sums

来源:互联网 发布:js关闭微信内置浏览器 编辑:程序博客网 时间:2024/06/02 02:40

这个题我自己还没搞定,看了好久,最后没核心部分还真是清楚,知道的麻烦留言一下下,或者加我 qq:997930294给我解释一下,万分感谢

描述

    You are given a sequence of n numbers a0,....,an-1. A cyclic shift by k positions (0≤k≤n-1) results in the following sequence : ak,ak+1,...,an-1,a0,a1,...,ak-1. How many of the n cyclic shifts satisfy the condition that the sum of the first i numbers is greater than or equal to zero for all i with 1≤i≤n?

输入

    Each test case consists of two lines. The first contains the number n(1≤n≤106),the number if integers in the sequence. The second contains n integers a0,...,an-1(-1000≤ai≤1000) representing the sequence of numbers. The input will finish with a line containing 0.

输出

    Each test case consists of two lines. The first contains the number n(1≤n≤106),the number if integers in the sequence. The second contains n integers a0,...,an-1(-1000≤ai≤1000) representing the sequence of numbers. The input will finish with a line containing 0.

样例输入

3
2 2 1
3
-1 1 1
1
-1
0

 

样例输出

3
2
0

 

#include <stdio.h>#include<iostream>#include <string.h>using namespace std;#define MAX 2100000struct node {     int sum,id; }qu[MAX]; int n,head,tail,ans; int sum[MAX],arr[MAX]; int main() {     int i,j,k;     while (scanf("%d",&n) != EOF)    {         if (n == 0) break;         ans = head = tail = 0;         for (i = 1; i <= n; ++i)        {             scanf("%d",&arr[i]);             sum[i] = sum[i-1] + arr[i];         }       //  for(i=1;i<=n;i++)cout<<sum[i]<<" ";         for (i = 1; i <= n; ++i)             sum[i+n] = sum[i+n-1] + arr[i];        //     for(i=1;i<=2*n;i++)cout<<sum[i]<<" ";         for (i = 1; i <= 2 * n; ++i)//这个for循环里的各种语句不懂,知道的请留言解释一下         {             while (tail < head && sum[i] < qu[head-1].sum)                 head--;             qu[head].sum = sum[i];             qu[head].id = i;             head++;             if (i > n && qu[tail].sum >= sum[i-n])                ans++;             while (tail < head && qu[tail].id <= i - n + 1)                tail++;         }         printf("%d\n",ans);     }     return 0; }