Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.
Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n ≤ k) values b1, b2, ..., bn (it is guaranteed that all values bj are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn't announced.
Your task is to determine the number of options for the score the participant could have before the judges rated the participant.
The first line contains two integers k and n (1 ≤ n ≤ k ≤ 2 000) — the number of jury members and the number of scores Polycarp remembers.
The second line contains k integers a1, a2, ..., ak ( - 2 000 ≤ ai ≤ 2 000) — jury's marks in chronological order.
The third line contains n distinct integers b1, b2, ..., bn ( - 4 000 000 ≤ bj ≤ 4 000 000) — the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).
4 1
-5 5 0 20
10
3
2 2
-2000 -2000
3998000 4000000
1
The answer for the first example is 3 because initially the participant could have - 10, 10 or 15 points.
In the second example there is only one correct initial score equaling to 4 002 000.
题目大意 给定数组a和b, b[i]是a的某一个前缀和再加上一个x,问可能的x有多少个。
如果存在解,说明b[1]至少是a的一个前缀和加上x得来的。所以我们暴力去枚举b[1]是a的哪一个前缀和,然后遍历一下b数组,判断对应的前缀和是否存在。因为懒,直接lower_bound完事。
Code
1 /** 2 * Codeforces 3 * Problem#831C 4 * Accepted 5 * Time:78ms 6 * Memory:2100k 7 */ 8 #include <iostream> 9 #include <cstdio> 10 #include <ctime> 11 #include <cmath> 12 #include <cctype> 13 #include <cstring> 14 #include <cstdlib> 15 #include <fstream> 16 #include <sstream> 17 #include <algorithm> 18 #include <map> 19 #include <set> 20 #include <stack> 21 #include <queue> 22 #include <vector> 23 #include <stack> 24 #include <cassert> 25 #ifndef WIN32 26 #define Auto "%lld" 27 #else 28 #define Auto "%I64d" 29 #endif 30 using namespace std; 31 typedef bool boolean; 32 const signed int inf = (signed)((1u << 31) - 1); 33 const signed long long llf = (signed long long)((1ull << 61) - 1); 34 const double eps = 1e-6; 35 const int binary_limit = 128; 36 #define smin(a, b) a = min(a, b) 37 #define smax(a, b) a = max(a, b) 38 #define max3(a, b, c) max(a, max(b, c)) 39 #define min3(a, b, c) min(a, min(b, c)) 40 template<typename T> 41 inline boolean readInteger(T& u){ 42 char x; 43 int aFlag = 1; 44 while(!isdigit((x = getchar())) && x != '-' && x != -1); 45 if(x == -1) { 46 ungetc(x, stdin); 47 return false; 48 } 49 if(x == '-'){ 50 x = getchar(); 51 aFlag = -1; 52 } 53 for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0'); 54 ungetc(x, stdin); 55 u *= aFlag; 56 return true; 57 } 58 59 int n, m, k; 60 int* a; 61 int* sa; 62 int* b; 63 64 inline void init() { 65 readInteger(n); 66 readInteger(k); 67 a = new int[n + 1]; 68 b = new int[k + 1]; 69 sa = new int[n + 1]; 70 sa[0] = 0; 71 for(int i = 1; i <= n; i++) { 72 readInteger(a[i]); 73 sa[i] = sa[i - 1] + a[i]; 74 } 75 for(int i = 1; i <= k; i++) 76 readInteger(b[i]); 77 } 78 79 int res; 80 inline void solve() { 81 sort(sa + 1, sa + n + 1); 82 res = m = unique(sa + 1, sa + n + 1) - sa - 1; 83 for(int i = 1; i <= m; i++) { 84 int s = b[1] - sa[i]; 85 for(int j = 2; j <= k; j++) { 86 if(*lower_bound(sa + 1, sa + m + 1, b[j] - s) != b[j] - s) { 87 res--; 88 break; 89 } 90 } 91 } 92 printf("%d\n", res); 93 } 94 95 int main() { 96 init(); 97 solve(); 98 return 0; 99 }