Facebook Hacker Cup 2016 Qualification Round The Price is Correct
2016-10-21 14:11
417 查看
The Price is Correct25 points
Download
Input
Choose Output
No output file selected
Submit
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).
The next line contains N space-separated integers, B1 through BN in order.
1 ≤ N ≤ 100,000
1 ≤ P ≤ 1,000,000,000
1 ≤ Bi ≤ 1,000,000,000
Example input · Download
Example output · Download
题意:给一串正整数,求有多少个区间的数值和小于p。
首先想到双指针扫描,但双指针最差2×N复杂度,而答案最大可以是N^2,直接求和扫描得到的结果一定会漏掉。
由于都是正整数,所以,对于刚扫到的某个区间[a, b],如果是合法的,那么就是以b为结尾合法区间中和
最大的一个区间,则以a+1~b为区间左端点的区间都是合法的,所以,对于扫到的合法区间要累加 b - a + 1。
Download
Input
Choose Output
No output file selected
Submit
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 ≤ 401 ≤ 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 ; 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; }
相关文章推荐
- Facebook Hacker Cup 2016 Qualification Round Text Editor
- Facebook Hacker cup Qualification round Problem 1
- Facebook Hacker Cup 2016 Round 2
- Facebook Hacker Cup 2016 Round 1 Boomerang Tournament
- Facebook Hacker Cup 2016 Round 1 Laundro, Matt
- Facebook hacker cup qualification round 题解
- FaceBook Hacker Cup 2016 Qualification - Text Editor (dp)
- FaceBook Hacker Cup 2016 Qualification - High Security (贪心)
- 2016 CCPC-Final A - The Third Cup is Free
- Facebook Hacker Cup 2015 Round 1--Winning at Sports(动态规划)
- Facebook Hacker Cup 2016 Qualification Round第一题
- Facebook Hacker Cup 2015 Round 1--Autocomplete(字典树新建与查询)
- Facebook Hacker Cup 2015 Round 1--Corporate Gifting(树动态规划)
- Facebook Hacker Cup 2015 Round 1--Winning at Sports(动态规划)
- Facebook Hacker Cup 2017 Round 1 Pie Progress
- Facebook Hacker Cup 2016 Qualification Round 解题报告
- FB Hacker Cup 2016 Qualification - D Text Editor
- Facebook Hacker Cup 2016 Qualification Round High Security
- Facebook Hacker Cup 2015 Round 1 解题报告
- Facebook Hacker Cup 2015 Round 1 Homework(附带测试数据)