Non-negative Partial Sums

21 篇文章 0 订阅

这个题我自己还没搞定,看了好久,最后没核心部分还真是清楚,知道的麻烦留言一下下,或者加我 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 2100000
struct 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;
 }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值