Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## 代码

Posted by liumeng at 2012-03-24 19:34:57 on Problem 3195
```#include <iostream>
#include <string>
#include <fstream>
#include <stack>
#include <sstream>
using namespace std;
#define M 10000
int a[M];
int n;

bool judge() {
int sum;
stack<int> st;
int t;
for (int i = 0; i < n; i++) {
t = a[i];
if (t < 0) {
st.push(t);
} else {
sum = 0;
while (!st.empty() && st.top() != -t) {
if(st.top()<0){
return false;
}
sum += st.top();
st.pop();
}
if (st.empty()) {
return false;
}
if(sum>=t){
return false;
}
st.pop();
st.push(t);

}

}
if(st.size()!=1)
return false;
if(st.top()<0)
return false;
return true;
}

int main() {
//cout << "Hello World!!!" << endl; // prints Hello World!!!
//ifstream cin("a");
int t;
string s;
int i;
while(getline(cin,s)){
stringstream is(s);
i=0;
while(is>>t){
a[i++]=t;
}
n=i;

if(judge()){
cout<<":-) Matrioshka!"<<endl;
}
else{
cout<<":-( Try again."<<endl;
}
}

return 0;
}```

Followed by: