Facebook Hacker Cup 2016 Qualification Round The Price is Correct

The Price is Correct25 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; 
}








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章