被系统弄挂了,问题出在最大值没处理好。Problem Statement |
|||||||||||||
This problem statement contains superscripts and/or subscripts. It may not display properly outside the applet. Little Rudolph had an important sequence of positive integers. The sequence consisted of N positive integers a0, a1, .., aN-1. Rudolph wrote the sequence onto the blackboard in the classroom. While Rudolph had gone out, little Arthur came into the classroom and saw the sequence. Arthur likes to play with numbers as much as he likes to give his friends puzzles. So he did the following:
1 2 3 4 -> 1 + 2 - 3 + 4 -> 1 + 2 - 3 + 4 -> + - + 3 -1 7 3 -1 7 When Rudolph returned, he was shocked as his important sequence had disappeared. Arthur quickly told him what operations he had performed and that Rudolph has to simply reconstruct the orginal sequence. Unfortunately, little Arthur did not realize that it is not necessarily possible to determine the original sequence uniquely. For example, both original sequences {1, 2, 3, 4} and {2, 1, 2, 5} lead to the same sequence {3, -1, 7} when operator sequence is {+, -, +}. The only thing Rudolph remembers about his original sequence is that all the integers were positive. Rudolph now wants to count all sequences of positive integers that match the blackboard. You are given vector <int> B and string operators both containing N-1 elements. The i-th element of B is the number bi and i-th element of operators will be '+' or '-', meaning that the i-th operator is + or -, respectively. Return the number of different positive integer sequences A that lead to sequence B when operators operators are used in the way described. If there are infinitely many such sequences, return -1. Note that there may be test cases where no valid sequence A exists. For such test cases the correct return value is 0. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Notes |
|||||||||||||
- | It is guaranteed that the correct answer will always fit into the 32-bit signed integer type. | ||||||||||||
- | The integer 0 (zero) is not positive. It may not occur in Rudolph's original sequence. | ||||||||||||
Constraints |
|||||||||||||
- | B will contain between 1 and 50 elements, inclusive. | ||||||||||||
- | operators will contain the same number of characters as the number of elements in B. | ||||||||||||
- | Each element of B will be between -1000000000 (-109) and 1000000000 (109), inclusive. | ||||||||||||
- | Each character in operators will be either '+' or '-' (quotes for clarity). | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
/****************************************************************************************************** ** Copyright (C) 2011.07.01-2013.07.01 ** Author: famousDT <[email protected]> ** Edit date: 2012-04-11 ******************************************************************************************************/ #include <stdio.h> #include <stdlib.h>//abs,atof(string to float),atoi,atol,atoll #include <math.h>//atan,acos,asin,atan2(a,b)(a/b atan),ceil,floor,cos,exp(x)(e^x),fabs,log(for E),log10 #include <vector> #include <queue> #include <deque>//two direction queue #include <map> #include <time.h> #include <set> #include <list> #include <stack> #include <string> #include <iostream> #include <fstream> #include <assert.h> #include <bitset> #include <iterator>//C++Primer #include <string.h>//memcpy(to,from,count #include <ctype.h>//character process:isalpha,isdigit,islower,tolower,isblank,iscntrl,isprll #include <numeric> #include <utility>//pair #include <functional> #include <algorithm> using namespace std; typedef long long ll; #define MY_E exp(1.0) #define MY_PI acos(-1.0) #define MY_MAX(a, b) ((a) > (b) ? (a) : (b)) #define MY_MIN(a, b) ((a) < (b) ? (a) : (b)) #define MY_MALLOC(n, type) ((type *)malloc((n) * sizeof(type))) #define MY_ABS(a) (((a) >= 0) ? (a) : (-(a))) #define MY_ll_MAX 0x7fffffff #define CLEAR(a) memset(a, 0, sizeof(a)) /*==========================================================*\ | \*==========================================================*/ class ImportantSequence { public: int getCount(vector <int> B, string operators) { int l = B.size(), i, j; double min = 1, max = 500000000000.0;//changed double a = 0, b = 1;//changed int flag = 0; for (i = l - 1; i >= 0; --i) { if (operators[i] == '+') { a = B[i] - a; b = -b; if (b == -1) { if (a <= 1) { flag = 1; break; } else { if (a - 1 < max) { max = a - 1; } } } else { if (a >= 0) ; else { if (min < -(a - 1)) { min = -(a - 1); } } } } else { a = B[i] + a; b = b; if (b == -1) { if (a <= 1) { flag = 1; break; } else { if (a - 1 < max) { max = a - 1; } } } else { if (a >= 0) ; else { if (min < -(a - 1)) { min = -(a - 1); } } } } } if (flag == 1) return 0; if (min <= max) { if (max == 500000000000.0) return -1;//changed return max - min + 1; } return 0; } };