Facebook Hacker Cup 2016 Qualification Round The Price is Correct

5 篇文章 0 订阅

The Price is Correct 25 points
Choose Output
No output file selected

You've managed to become a contestant on the hottest new game show, The Price is Correct!

After asking you to come on down to the stage, the show's host presents you with a row of N closed boxes, numbered from 1 to N in order, each containing a secret positive integer. A curtain opens to reveal a shiny, new tricycle — you recognize it as an expensive, top-of-the-line model.

The host then proceeds to explain the rules: you must select a contiguous sequence of the boxes (boxes a..b, for some 1 ≤ a ≤ b ≤ N). Your chosen boxes will then be opened, and if the sum of the numbers inside is no greater than the price of the tricycle, you win it!

You'd sure like to win that tricycle. Fortunately, not only are you aware that its price is exactly P, but you've paid off the host to let you in on the contents of the boxes! You know that each box icontains the number Bi.

How many different sequences of boxes can you choose such that you win the tricycle? Each sequence is defined by its starting and ending box indices (a and b).

Input

Input begins with an integer T, the number of times you appear on The Price is Correct. For each show, there is first a line containing the space-separated integers N and P. The next line contains N space-separated integers, B1 through BN in order.

Output

For the ith show, print a line containing "Case #i: " followed by the number of box sequences that will win you the tricycle.

Constraints

1 ≤ T ≤ 40 
1 ≤ N ≤ 100,000 
1 ≤ P ≤ 1,000,000,000 
1 ≤ Bi ≤ 1,000,000,000 

Explanation of Sample

In the first case no sequence adds up to more than 50, so all 10 sequences are winners. In the fourth case, you can select any single box, or the sequences (1, 2), (1, 3), and (2, 3), for 9 total winning sequences.

Example input  ·  Download
Example output  ·  Download
5
4 50
10 10 10 10
4 50
51 51 51 51
3 1000000000
1000000000 1000000000 1000000000
6 6
1 2 3 4 5 6
10 77
12 3 52 25 9 83 45 21 33 3
Case #1: 10
Case #2: 0
Case #3: 3
Case #4: 9
Case #5: 18

题意:给一串正整数,求有多少个区间的数值和小于p。

首先想到双指针扫描,但双指针最差2×N复杂度,而答案最大可以是N^2,直接求和扫描得到的结果一定会漏掉。

由于都是正整数,所以,对于刚扫到的某个区间[a, b],如果是合法的,那么就是以b为结尾合法区间中和

最大的一个区间,则以a+1~b为区间左端点的区间都是合法的,所以,对于扫到的合法区间要累加 b - a + 1。

#include<bits/stdc++.h>
const int N = 1e5 + 10;
typedef long long ll;
using namespace std;
int num[N];
void run()
{
  int n, p; 
  ll ans = 0;
  int sum = 0;
  scanf("%d%d", &n, &p);
  for (int i = 0; i < n; i++)
    scanf("%d", num + i);

  int l = 0;
  for (int i = 0; i < n; i++)
  {
    sum += num[i];
    if (sum <= p)//当前位置加了后,统计以i为区间右端点的合法区间数
      ans += i - l + 1;
    else
    {
      while (l <= i && sum > p)//l要直到i,因为可能本身就是大于p的。
      {
        sum -= num[l];
        l++;
      }
//如果l>i这里会加0,不影响答案,如果l<=i则sum<p成立,
//统计i为右端点的合法区间数
      ans += i - l + 1;
    }
  }
  printf("%lld\n", ans);
}
int main()
{
    int T, cas = 1;
    scanf("%d", &T);
    while (T--)
    {
      printf("Case #%d: ", cas++);
      run();
    }

    return 0; 
}


 





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值