Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  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:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator