div1 250pt:
题意:。。。
解法:先假设空出来的位置是0,然后模拟一次看看是不是满足,如果不行的话,我们只需要关心最后栈顶的元素取值是不是受空白处的影响,于是还是模拟一下。
// BEGIN CUT HERE // END CUT HERE
#line 5 "Suminator.cpp"
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cassert>
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = ;
ll stk[N];
class Suminator
{
public:
int findMissing(vector <int> program, int wantedResult){
//$CARETPOSITION$
int top=;
int n=program.size();
for(int i=;i<n;i++){
if(program[i] == || program[i] == -){
if(top >= ){
ll res = stk[top]+stk[top-];
stk[--top]=res;
// if(res > wantedResult)return -1;
}else if(top == ){
stk[++top] = ;
}
}else{
stk[++top] = program[i];
}
}
if(stk[top] == wantedResult)return ;
bool have[N];memset(have,,sizeof(have));
top = ;
for(int i=;i<n;i++){
if(program[i] == ){
if(top >= ){
ll res = stk[top] + stk[top-];
bool ok = have[top] || have[top-];
stk[--top] = res;
// if(res > wantedResult)return -1;
have[top] = ok;
}else if(top == ){
stk[++top] = ;
have[top] = ;
}
}else if(program[i] == -){
stk[++top] = ;
have[top] = ;
}else{
stk[++top] = program[i];
have[top] = ;
}
}
if(have[top] == )return -;
else if(stk[top] >= wantedResult)return -;
else return wantedResult - stk[top]; } // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); if ((Case == -) || (Case == )) test_case_5(); }
private:
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(); }
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; } }
void test_case_0() { int Arr0[] = {,-,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, findMissing(Arg0, Arg1)); }
void test_case_1() { int Arr0[] = {, , , , , -}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, findMissing(Arg0, Arg1)); }
void test_case_2() { int Arr0[] = {-, , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, findMissing(Arg0, Arg1)); }
void test_case_3() { int Arr0[] = {-, , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = -; verify_case(, Arg2, findMissing(Arg0, Arg1)); }
void test_case_4() { int Arr0[] = {, , , , -, , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = -; verify_case(, Arg2, findMissing(Arg0, Arg1)); }
void test_case_5() { int Arr0[] = {, -, , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = -; verify_case(, Arg2, findMissing(Arg0, Arg1)); } // END CUT HERE };
// BEGIN CUT HERE
int main(){
Suminator ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
250pt
div1 500pt
有点复杂的dp,有待实现