# topcoder srm 553

div1 250pt:

题意：。。。

解法：先假设空出来的位置是0,然后模拟一次看看是不是满足，如果不行的话，我们只需要关心最后栈顶的元素取值是不是受空白处的影响，于是还是模拟一下。

 1 // BEGIN CUT HERE
2
3 // END CUT HERE
4 #line 5 "Suminator.cpp"
5 #include<cstdio>
6 #include<cstring>
7 #include<cstdlib>
8 #include<ctime>
9 #include<cmath>
10 #include<cassert>
11 #include<iostream>
12 #include<string>
13 #include<sstream>
14 #include<vector>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<stack>
19 #include<algorithm>
20 using namespace std;
21 typedef long long ll;
22 typedef pair<int,int> pii;
23 const int N = 55;
24 ll stk[N];
25 class Suminator
26 {
27     public:
28     int findMissing(vector <int> program, int wantedResult){
29     //$CARETPOSITION$
30         int top=0;
31         int n=program.size();
32         for(int i=0;i<n;i++){
33             if(program[i] == 0 || program[i] == -1){
34                 if(top >= 2){
35                     ll res = stk[top]+stk[top-1];
36                     stk[--top]=res;
37             //        if(res > wantedResult)return -1;
38                 }else if(top == 0){
39                     stk[++top] = 0;
40                 }
41             }else{
42                 stk[++top] = program[i];
43             }
44         }
45         if(stk[top] == wantedResult)return 0;
46         bool have[N];memset(have,0,sizeof(have));
47         top = 0;
48         for(int i=0;i<n;i++){
49             if(program[i] == 0){
50                 if(top >= 2){
51                     ll res = stk[top] + stk[top-1];
52                     bool ok = have[top] || have[top-1];
53                     stk[--top] = res;
54     //                if(res > wantedResult)return -1;
55                     have[top] = ok;
56                 }else if(top == 0){
57                     stk[++top] = 0;
58                     have[top] = 0;
59                 }
60             }else if(program[i] == -1){
61                 stk[++top] = 0;
62                 have[top] = 1;
63             }else{
64                 stk[++top] = program[i];
65                 have[top] = 0;
66             }
67         }
68         if(have[top] == 0)return -1;
69         else if(stk[top] >= wantedResult)return -1;
70         else return wantedResult - stk[top];
71
72     }
73
74 // BEGIN CUT HERE
75     public:
76     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
77     private:
78     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
79     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
80     void test_case_0() { int Arr0[] = {7,-1,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10; int Arg2 = 3; verify_case(0, Arg2, findMissing(Arg0, Arg1)); }
81     void test_case_1() { int Arr0[] = {100, 200, 300, 0, 100, -1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 600; int Arg2 = 0; verify_case(1, Arg2, findMissing(Arg0, Arg1)); }
82     void test_case_2() { int Arr0[] = {-1, 7, 3, 0, 1, 2, 0, 0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 13; int Arg2 = 0; verify_case(2, Arg2, findMissing(Arg0, Arg1)); }
83     void test_case_3() { int Arr0[] = {-1, 8, 4, 0, 1, 2, 0, 0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 16; int Arg2 = -1; verify_case(3, Arg2, findMissing(Arg0, Arg1)); }
84     void test_case_4() { int Arr0[] = {1000000000, 1000000000, 1000000000,  1000000000, -1, 0, 0, 0, 0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1000000000; int Arg2 = -1; verify_case(4, Arg2, findMissing(Arg0, Arg1)); }
85     void test_case_5() { int Arr0[] = {7, -1, 3, 0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = -1; verify_case(5, Arg2, findMissing(Arg0, Arg1)); }
86
87 // END CUT HERE
88
89 };
90 // BEGIN CUT HERE
91 int main(){
92     Suminator ___test;
93     ___test.run_test(-1);
94     return 0;
95 }
96 // END CUT HERE
250pt

div1 500pt

posted @ 2014-03-08 15:14  silver__bullet  阅读(463)  评论(0编辑  收藏  举报