Exemplo n.º 1
3
Arquivo: 4540.cpp Projeto: ACLB/ACM
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        for(int i = 1;i<=n;i++) scanf("%d",&a[i]);

        GetRmq(); Bo = (int)sqrt(n);
    
        for(int i = 1;i <= m; i++) scanf("%d %d",&S[i].L,&S[i].R),S[i].Id = i;
        
        sort(S+1,S+m+1);

        St.push(0);

        for(int i = 1;i <= n;i++)
        {
            while(St.size() > 1 && a[St.top()] > a[i]) St.pop();

            R[i] = St.top(); St.push(i);
        }

        while(!St.empty()) St.pop();

        St.push(n+1);

        for(int i = n;i>=1;i--)
        {
            while(St.size() > 1 && a[St.top()] > a[i]) St.pop();

            L[i] = St.top(); St.push(i);
        }

        while(!St.empty()) St.pop();

        memset(sumL,0,sizeof(sumL));

        memset(sumR,0,sizeof(sumR));

        sumR[1] =a[1];

        for(int i = 2;i<=n;i++) sumR[i] = sumR[R[i]]+(1LL)*(i-R[i])*a[i];

        sumL[n] = a[n];

        for(int i = n-1;i >= 1;i--) sumL[i] = sumL[L[i]]+(1LL)*(L[i]-i)*a[i];

        LL sum = 0;

        int l = 0,r = 1;

        for(int i = 1;i<=m;i++)
        {
            while(r <= S[i].R) sum +=Rcal(l,r),r++;

            while(r > S[i].R+1) r --,sum-=Rcal(l,r);

            while(l < S[i].L-1) l++,sum-=Lcal(l,r);
            
            while(l >= S[i].L) sum+=Lcal(l,r),l--;

            ans[S[i].Id] =sum;
        }

        for(int i = 1;i<=m;i++) printf("%lld\n",ans[i]);
    }
    return 0;
}
Exemplo n.º 2
2
 void pop() {
     if (s_1.empty()) {
         transfere();
     }
     s_1.pop();
 }
Exemplo n.º 3
2
bool isb(stack<int> &vi, stack<int> &vj) {
    printf("DEBUG isb\n");
    return !vi.empty() && (vj.empty() || vj.top() > vi.top());
}
 // Removes the element from in front of queue.
 void pop(void) {
     pushIntoQueue();
     if (!queue.empty())
         queue.pop();
 }
int main()
{
    int n;
    char ch;
    while(cin>>n)
    {
        int i,j,k=0;
        arry[0]=n;
        ch=getchar();
        while(ch!='\n')
        {
            cin>>arry[++k];
            ch=getchar();
        }
        k++;
        int flag=1,s;
        for(i=0;i<k;i++)
        {
            if(arry[i]<0)
            {
                team1.push(arry[i]);
                team2.push(arry[i]*(-1));
            }
            else if(arry[i]>0)
            {
                if(team2.empty())
                {
                    flag=0;
                    break;
                }
                if(arry[i]!=team2.top())
                {
                    flag=0;
                    break;
                }
                else
                {
                    team2.pop();
                    s=0;
                    while(team1.top()>0)
                    {
                        s=s+team1.top();
                        team1.pop();
                    }
                    if(s>=arry[i])
                    {
                        flag=0;
                        break;
                    }
                    team1.pop();
                    team1.push(arry[i]);
                }
            }
        }
        if(!team2.empty()) flag=0;
        if(flag) cout<<":-) Matrioshka!"<<endl;
        else cout<<":-( Try again."<<endl;
        while(!team1.empty())
            team1.pop();
        while(!team2.empty())
            team2.pop();
    }
    return 0;
}
Exemplo n.º 6
0
 bool empty(){
     return s1.empty() && s2.empty();
 }
 bool empty(void) {
     return input.empty() && output.empty();
 }
Exemplo n.º 8
0
int main()
{
	#ifdef fn
		freopen(fn ".in", "r", stdin);
		freopen(fn ".out", "w", stdout);
	#endif
	scanf("%s", s + 1);
	n = strlen(s + 1);
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == '(' || s[i] == '[')
			st.push(i);
		if (s[i] == ')')
		{
			if (!st.empty() && s[st.top()] == '(')
			{
				pr[i] = st.top(), st.pop();
				pr[pr[i]] = i;
			}
			else
			{
				while (!st.empty())
					pr[st.top()] = -1, st.pop();
				pr[i] = -1;
			}
		}
		if (s[i] == ']')
		{
			if (!st.empty() && s[st.top()] == '[')
			{
				pr[i] = st.top(), st.pop();
				pr[pr[i]] = i;
			}
			else
			{
				while (!st.empty())
					pr[st.top()] = -1, st.pop();
				pr[i] = -1;
			}
		}
	}
	while (!st.empty())
		pr[st.top()] = -1, st.pop();
//	for (int i = 1; i <= n; i++)
//		printf("%d ", pr[i]);
//	puts("");
	for (int i = 1; i <= n; i++)
		a[i] = a[i - 1] + (s[i] == '[');
	int L = 0, R = -1, l = -1, r = -1, ans = 0;
	for (int i = 1; i <= n;)
	{
		if (pr[i] == -1)
		{
			if (l != -1 && umax(ans, a[r] - a[l - 1]))
				L = l, R = r;
			l = -1, r = -1;
			i++;
		}
		else
		{
			if (l == -1)
				l = i;
			r = pr[i];
			i = pr[i] + 1;
		}
	}
	if (l != -1 && umax(ans, a[r] - a[l - 1]))
		L = l, R = r;
	printf("%d\n", ans);
	for (int i = L; i <= R; i++)
		putchar(s[i]);
}
Exemplo n.º 9
0
 void test() {
         while (!min_stack.empty()) {
                 cout << min_stack.top() << endl;
                 min_stack.pop();
         }
 }
Exemplo n.º 10
0
void reset(){
    while(!s.empty()) s.pop();
    while(!q.empty()) q.pop();
    while(!pq.empty()) pq.pop();
    isStack=1, isQueue=1, isPQueue=1;
}
Exemplo n.º 11
0
void gol() {
    while (!stk.empty())
        stk.pop();
}
Exemplo n.º 12
0
 /** @return whether we have a next smallest number */
 bool hasNext() {
     if(s.empty())
         return false;
     return true;
 }
Exemplo n.º 13
0
void MWinDeviceImpl::Restore()
{
	assert(not mState.empty());
	mRenderTarget->RestoreDrawingState(mState.top());
	mState.pop();
}
Exemplo n.º 14
0
 int front() {
     if (s_1.empty()) {
         transfere();
     }
     return s_1.top();
 }
Exemplo n.º 15
0
void findnearest(FILE *fout)
{
	int x = root;
	int nearest = -1;
	double mind = 0x7FFFFFFF;
	dfs2(x, nearest, mind);
	if (nearest == -1)
	{
		nearest = stk.top();
		mind = dist(kd[nearest].d, target);
	}
	while (!q.empty()) q.pop();
	q.push(T(kd[nearest].d[0], kd[nearest].d[1], mind));
	used[nearest] = nowi;
	nown = 1;
	while (!stk.empty())
	{
		int bp = stk.top();
		stk.pop();
		double maxd = q.top().dis;
		if (kd[bp].lc == 0 && kd[bp].rc == 0)
		{			
			if (used[bp]!=nowi && (nown < kmin || cmps(dist(kd[bp].d, target) - maxd) < 0))
			{
				used[bp] = nowi;
				if (nown == kmin) q.pop();
				q.push(T(kd[bp].d[0], kd[bp].d[1], dist(kd[bp].d, target)));
				if (nown<kmin) nown++;
			}			   
		}
		else
		{
			int s = kd[bp].split;
			int psearch;
			if (target[s] <= kd[bp].d[s])
			{
				psearch = kd[bp].lc;
				if (psearch && vis[psearch] != nowi) stk.push(psearch);
				if ( (nown<kmin || cmps(abs(kd[bp].d[s] - target[s]) - maxd) < 0))
				{
					psearch = kd[bp].rc;
					if (psearch && vis[psearch] != nowi) stk.push(psearch);
				}
			}
			else
			{
				psearch = kd[bp].rc;
				if (psearch && vis[psearch] != nowi) stk.push(psearch);
				if ( (nown<kmin || cmps(abs(kd[bp].d[s] - target[s]) - maxd) < 0))
				{
					psearch = kd[bp].lc;
					if (psearch && vis[psearch] != nowi) stk.push(psearch);
				}
			}
			if (used[bp]!=nowi && (nown<kmin || cmps(dist(kd[bp].d, target) - maxd) < 0))
			{
				used[bp] = nowi;
				if (nown == kmin) q.pop();
				q.push(T(kd[bp].d[0], kd[bp].d[1], dist(kd[bp].d, target)));
				if (nown<kmin) nown++;
			}
		}
	}	
	int cnt = 0;
	while (!q.empty())
	{
		ans[++cnt] = q.top();		
		q.pop();
	}
	fprintf(fout, "%d\n",cnt);
	fprintf(fout, "%d %d\n", target[0], target[1]);
	for (int i = cnt; i >= 1; i--)
	{
		fprintf(fout, "%d %d %.8f\n", ans[i].d[0], ans[i].d[1], ans[i].dis);
	}
}
Exemplo n.º 16
0
 /** @return whether we have a next smallest number */
 bool hasNext() {
     return !path.empty();
 }
Exemplo n.º 17
0
vector<Tuple> Qtree::exec(bool print, string *table_name){
	vector<Tuple> ret ;
	#ifdef DEBUG
	this->print(0);
	#endif
	if(this->type == INS){
		vector<Tuple> temp = this->left->exec( false, NULL ) ;
		if(temp.size() != 0){
			Schema sins_from  =  temp[0].getSchema() ;
			vector<enum FIELD_TYPE> field_types_from = sins_from.getFieldTypes() ;
			vector<string> field_names_from = sins_from.getFieldNames() ;
			if(field_types_from.size() == this->info.size() - 1){
				Schema sins_to = p->schema_manager.getSchema( this->info[0] ) ;
				vector<enum FIELD_TYPE> field_types_to ;
				vector<union Field> fields ;
				vector<string>::iterator it0 = this->info.begin() ;
				vector<enum FIELD_TYPE>::iterator it1 = field_types_from.begin();
				vector<string>::iterator it2 = field_names_from.begin();
				vector<string> STRv;
				vector<int> INTv ;
				string table_n = (*it0)  ;
				vector<string> field_names_to ;
				it0 ++ ;
				for( ; it0 != this->info.end()  ; it0 ++, it1++){
					unsigned long found = it0->rfind('.')  ;
					string s_table ;
					if(found == std::string::npos){
						s_table = string( table_n + "." + (*it0) ) ;
					}else{
						s_table = string( it0->substr( it0->rfind('.') + 1 )  ) ;
					}
					if( sins_to.fieldNameExists( *it0 ) ){
						field_names_to.push_back(string(  *it0)  ) ;
						if(sins_to.getFieldType( *it0) == *it1 ){
						}else{
							perror(  ": Type mismatch");
							return ret; 
						}
					}else{
						if(sins_to.fieldNameExists(s_table) ) {
							field_names_to.push_back(string(  s_table ) ) ;
							if(sins_to.getFieldType( s_table) == *it1 ){
							}else{
								perror( ": Type mismatch");
								return ret; 
							}
						} else{
								perror( "exec: No such field");
						}
					}
				}	
				for(vector<Tuple>::iterator it_tuple = temp.begin(); it_tuple != temp.end(); it_tuple ++) {
					for(it1 = field_types_from.begin(), it2 = field_names_from.begin()  ; 
					it1 != field_types_from.end()  ; it1++, it2++){
						if(*it1 == INT){
							INTv.push_back( it_tuple->getField( *it2).integer ) ;
						}else{
							STRv.push_back( *(it_tuple->getField( *it2).str) ) ;
						}
					}
					p->insert(table_n, field_names_to, STRv, INTv) ;
					INTv.clear();
					STRv.clear() ;
				}
			}else{
				perror("Size mismatch");
				return ret;
			}
		}else{
			return ret;
		}
	}else if(this->type == TAU){
		string table_n;
		if(this->left->type == TABLE && (output_s.empty() || output_s.top() == NULL) ){
			Schema s = p->schema_manager.getSchema( this->left->info[0] ) ;
			string s_table ;
			unsigned long found = this->info[0].rfind('.')  ;
			table_n = this->left->info[0] ;
			if(found == std::string::npos){
				s_table = string( table_n + "." + this->info[0]  ) ;
			}else{
				s_table = string( this->info[0].substr( this->info[0].rfind('.') + 1 )  ) ;
			}
			if( s.fieldNameExists( this->info[0] ) ){
				ret = p->SortTwoPass(table_n, this->info[0])  ;
			}else if(s.fieldNameExists(s_table) ) {
				ret = p->SortTwoPass(table_n, s_table)  ;
			}else{
				perror("No such field");
				return ret ;
			}
		}else{
			vector<Tuple> temp = this->left->exec( false, &table_n ) ;
			if(table_name != NULL) { (*table_name ) = string( this->info[0] ) ;}
			if(temp.size() != 0){
				Schema s  =  temp[0].getSchema() ;
				string temp_table_name = "temp_table" ;
				while(p->schema_manager.relationExists(temp_table_name) ){
					temp_table_name += "-a" ;
				}
				p->CreateTable(temp_table_name, temp ) ;
				temp_relations.push_back( temp_table_name ) ;
				unsigned long found = this->info[0].rfind('.')  ;
				string s_table ;
				if(found == std::string::npos){
					s_table = string( table_n + "." + this->info[0]  ) ;
				}else{
					s_table = string( this->info[0].substr( this->info[0].rfind('.') + 1 )  ) ;
				}
				if( s.fieldNameExists( this->info[0] ) ){
					ret = p->SortTwoPass(temp_table_name, this->info[0])  ;
				}else if(s.fieldNameExists(s_table) ) {
					ret = p->SortTwoPass(temp_table_name, s_table)  ;
				}else{
					perror("No such field");
					return ret ;
				}
			}else{
				return ret;
			}
		}
	}else if(this->type == DELTA ){
		string table_n;
		if(this->left->type == TABLE){
			table_n = this->left->info[0] ;
			ret = p->dupTwoPass(table_n) ;
		}else{
			vector<Tuple> temp = this->left->exec( false , &table_n) ;
			if(table_name != NULL) { (*table_name ) = string( this->info[0] ) ;}
	
			if(temp.size() != 0){
				Schema s  =  temp[0].getSchema() ;
				string temp_table_name = "temp_table" ;
				while(p->schema_manager.relationExists(temp_table_name) ){
					temp_table_name += "-a" ;
				}
				p->CreateTable(temp_table_name, temp );
				temp_relations.push_back(temp_table_name  ) ;
				ret = p->dupTwoPass(temp_table_name) ;
			}else{
				return ret;
			}
		}
	}else if(this->type == PI ){
		string table_n;
		vector<Tuple> temp = this->left->exec( false, &table_n ) ;
		if(table_name != NULL) { (*table_name ) = string( this->info[0] ) ;}
		if(temp.size() != 0){
			Schema s  =  temp[0].getSchema() ;
			vector<string> field_names ;
			vector<enum FIELD_TYPE> field_types  ;
			for(vector<string>::iterator it= this->info.begin(); it != this->info.end(); it++){
				unsigned long found = it->rfind('.')  ;
				string s_table ;
				if(found == std::string::npos){
					s_table = string( table_n + "." + (*it) ) ;
				}else{
					s_table = string( it->substr( it->rfind('.') + 1 )  ) ;
				}
				if( s.fieldNameExists( *it ) ){
					field_names.push_back(string(*it) ) ;
					field_types.push_back(s.getFieldType( *it) ) ;
				}else{
					if(s.fieldNameExists(s_table) ) {
						field_names.push_back(string( s_table ) ) ;
						field_types.push_back( s.getFieldType( s_table )  );
					} else{
						perror( "exec: No such field");
					}
				}
			}
			string temp_table_name = "temp_table" ;
			Relation *rlt = NULL;
			while(p->schema_manager.relationExists(temp_table_name) ){
				temp_table_name += "-a" ;
			}
			rlt = p->CreateTable(temp_table_name, field_names, field_types) ;
			temp_relations.push_back(temp_table_name  ) ;
			for(vector<Tuple>::iterator tit = temp.begin(); tit != temp.end(); tit++){
				Tuple t = rlt->createTuple() ;
	
				for(vector<string>::iterator it = field_names.begin(); it != field_names.end() ; it++){
					union Field f= tit->getField(*it) ;
					if( s.getFieldType(*it) == INT ){
						t.setField(  *it,  f.integer ) ;
					}else{
						t.setField( *it, *(f.str)) ;
					}
				}
				ret.push_back( t ) ;
			}
		}else{
			return ret;
		}
	}else if(this->type == PRODUCT){
		vector<string> ptables;
		vector<Relation *> relations ;
		map<string, Qexpression *> sigma_operation ;
		vector<string> commons ;
		map<string, bool> joined_keys;

		vector<string>::iterator it = ptables.begin();

		ptables.insert(ptables.end(), this->info.begin(), this->info.end() );
		
		if(output_s.empty() ){
		}else if(output_s.top()->type == INTEGER || output_s.top()->type == LITERAL ){
			Tuple *t = NULL;
			if(output_s.top()->judge(*t) ){
				/* WHERE clasuse always true */
		 		 while(! output_s.empty() ){ output_s.top()->free() ;output_s.pop();}
			}else{
				/* empty results */
				return ret; 
			}
		}else{
			Qexpression *optimized = output_s.top()->optimize_sigma(&sigma_operation) ;
			output_s.pop(); if(optimized != NULL){ output_s.push(optimized) ;}
			
			#ifdef DEBUG
			for(map<string, Qexpression *>::iterator it = sigma_operation.begin(); it != sigma_operation.end(); it ++){
				cout << it->first << "->" << endl;
				it->second->print(0);
			}
			#endif

			if( ! output_s.empty() ){
				optimized = output_s.top()->optimize_join(commons, joined_keys) ;
				output_s.pop(); 
				if(optimized != NULL){ 
					output_s.push(optimized) ;
				}else{
					while(! output_s.empty() ){output_s.top()->free() ; output_s.pop();}
				}

				if(! output_s.empty()){
					#ifdef DEBUG
					output_s.top()->print(0); 
					#endif
				}
			}
			#ifdef DEBUG
			cerr << "commons: ";
			for(vector<string>::iterator it = commons.begin(); it != commons.end(); it++){
				cerr<< *it << " " ;
			}
			cerr << endl ;
			#endif
		}
		vector<string> to_drop ;
		for(vector<string>::iterator it = ptables.begin(); it != ptables.end(); ){
			if(sigma_operation[*it] == NULL){
				it++;
			}else{
				Relation *temp_relation;
				vector<Tuple> tuples  =  p->singleTableSelect( *it  , sigma_operation[*it] ) ;
				if(tuples.size() != 0){
					temp_relation = p->CreateTable( ( *it) + "-SIGMA",  tuples) ;
				}else{
					vector<string> field_names = p->schema_manager.getRelation(*it)->getSchema().getFieldNames(); 
					vector<enum FIELD_TYPE> field_types =  p->schema_manager.getRelation(*it)->getSchema().getFieldTypes() ;
					temp_relation = p->CreateTable( (*it) + "-SIGMA" , field_names, field_types ) ;
				}
				to_drop.push_back( temp_relation->getRelationName() ) ;
				it = ptables.erase(it) ;ptables.insert( it, temp_relation->getRelationName() ) ;
			}
		}
		if(ptables.size() == 2){
			if(ptables[0] <= ptables[1]){
				ret = p->JoinTwoPass(ptables[0], ptables[1], commons ) ;
			}else{
				ret = p->JoinTwoPass(ptables[1], ptables[0], commons ) ;
			}
		}else{
			ret = p->JoinTables(ptables, commons) ;
		}
		for(vector<string>::iterator it = to_drop.begin(); it != to_drop.end(); it++){
			p->DropTable(*it) ;
		}
		if(output_s.empty() ){
		}else{
			string temp_table_name = "temp_table";
			while(p->schema_manager.relationExists(temp_table_name)) {
				temp_table_name += "-a";
			}
			p->CreateTable( temp_table_name, ret ) ;
			temp_relations.push_back(temp_table_name) ;

			ret = p->singleTableSelect(temp_table_name, output_s.top() ) ;
		}
	}else if(this->type == TABLE){
		if(table_name != NULL) { (*table_name ) = string( this->info[0] ) ;}
		ret = p->singleTableSelect(this->info[0], output_s.empty() ? NULL : output_s.top() );
	}else{
		return ret;
	}
	if(ret.size() != 0 && print){
		vector<string> field_names = 
			ret[0].getSchema( ).getFieldNames() ;
		cout <<  "-----------------" << endl ;
		for(vector<string>::iterator it = field_names.begin(); it != field_names.end(); it++){
			cout<< *it << ' ' ;
		} cout << endl << "-----------------" << endl ;
		for(vector<Tuple>::iterator it = ret.begin(); it != ret.end(); it ++ ){
			cout << (*it) << endl;
		}cout <<  "-----------------" << endl ;
	}
	return  ret;
}
Exemplo n.º 18
0
void print_stack(stack<StackType> s)
{
	while(!s.empty())
	{
		StackType b = s.top();
		s.pop();
		switch(b.code)
		{
			case add: cout<<"+"<<" "; break;
			case multy: cout<<"*"<<" "; break;
			case in:  cout<<">>"<<" "; break;
			case out: cout<<"<<"<<" ";  break;
			case sub: cout<<"-"<<" "; break;
			case DEV: cout<<"/"<<" "; break;
			case MOD: cout<<"%"<<" "; break;
			case Equal: cout<<"=="<<" "; break;
			case not_equal: cout<<"!="<<" "; break;
			case less_or_equal: cout<<"<="<<" "; break;
			case more_or_equal: cout<<">="<<" "; break;
			case more: cout<<">"<<" "; break;
			case Less: cout<<"<"<<" "; break;
			case scolar: cout<<"~"<<" "; break;
			case conc: cout<<"@"<<" "; break;
			case AND: cout<<"&"<<" "; break;
			case OR: cout<<"|"<<" "; break;
			case sp: cout<<"#"<<" "; break;
			case ssh: cout<<"\\"<<" "; break;
			case sl: cout<<"::"<<" "; break;
			case Xor: cout<<"^"<<" "; break;
			case ls: cout<<"--"<<" "; break;
			case rs: cout<<"++"<<" "; break;
			case otr: cout<<"!"<<" "; break;
			case id: cout<<"id "<<" "; break;
			case keyword: cout<<b.name<<" "; break;
			case semicolon: cout<<";"<<" "; break;
			case coma: cout<<","<<" "; break;
			case lable: cout<<"lable:  "<<b.name<<" "; break;
			case lbr: cout<<"("<<" "; break;
			case rbr: cout<<")"<<" "; break;
			case rfs: cout<<"}"<<" "; break;
			case lfs: cout<<"{"<<" "; break;
			case colon: cout<<":"<<" "; break;
			case S: cout<<"S"<<" "; break;
			case P: cout<<"P"<<" "; break;
			case G: cout<<"G"<<" "; break;
			case X: cout<<"X"<<" "; break;
			case X1: cout<<"X1"<<" "; break;
			case TYPE: cout<<"TYPE"<<" "; break;	
			case O: cout<<"O"<<" "; break;
			case W: cout<<"W"<<" "; break;		
			case W1: cout<<"W1"<<" "; break;
			case T: cout<<"T"<<" "; break;		
			case T1: cout<<"T1"<<" "; break;
			case K: cout<<"K"<<" "; break;
			case E: cout<<"E"<<" "; break;
			case I: cout<<"I"<<" "; break;
			case U: cout<<"U"<<" "; break;
			case A: cout<<"A"<<" "; break;
			case B: cout<<"B"<<" "; break;
			case C: cout<<"C"<<" "; break;
			case D: cout<<"D"<<" "; break;
			case F: cout<<"F"<<" "; break;
			case L: cout<<"L"<<" "; break;
			case SH: cout<<"SH"<<" "; break;
			case H: cout<<"H"<<" "; break;
			case J: cout<<"J"<<" "; break;
			case Z: cout<<"Z"<<" "; break;
			case G1: cout<<"G1"<<" "; break;
			case ID: cout<<"ID"<<" "; break;
			case CONST: cout<<"const"<<" "; break;
			case W2: cout<<"W2"<<" "; break;							


		}	
	}
	cout<<endl;
}
Exemplo n.º 19
0
 bool isEmpty() {
     return s1.empty();
 }
Exemplo n.º 20
0
Arquivo: 33.cpp Projeto: archer811/ACM
int main()
{
    int cas;
    while(~scanf("%d",&cas))
    {
        while(cas--)
        {
            scanf("%d%d%d",&day,&n,&w);
            for(int i=1; i<=day; i++) scanf("%d",&w_boy[i]);
            for(int i=0; i<n; i++) for(int j=0; j<n; j++) scanf("%lf",&place[i][j]);
            for(int i=0; i<n; i++) for(int j=0; j<w; j++) scanf("%lf",&weather[i][j]);
            for(int i=0; i<=day; i++) for(int j=0; j<n; j++) dp[i][j]=0;
            dp[0][0]=1.00;
            for(int i=0; i<day; i++)
            {
                for(int j=0; j<n; j++)
                {
                    double tmp=dp[i][j];
                    for(int k=0; k<n; k++)
                    {
                        double t1=place[j][k];
                        double t2=weather[k][w_boy[i+1]];
                        if(dp[i+1][k]<tmp*t1*t2)
                        {
                            dp[i+1][k]=tmp*t1*t2;
                            pre[i+1][k]=j;
                        }
                    }
                }
                int flag=0;
                for(int j=0; j<n; j++)
                {
                    if(dp[i+1][j]>inf)
                    {
                        flag=1;
                        break;
                    }
                }
                if(!flag)
                {
                    for(int j=0; j<n; j++)
                    {
                        dp[i+1][j]*=inf;
                    }
                }
            }
            double tmax=0.0;
            int flag=0;
            for(int i=0; i<n; i++)
            {
                if(dp[day][i]>tmax)
                {
                    tmax=dp[day][i];
                    flag=i;
                }
            }
            q1.push(flag);
            flag=pre[day][flag];
            int num=day-1;
            while(num)
            {
                q1.push(flag);
                flag=pre[num][flag];
                num--;
            }
            int flag2=0;
            while(!q1.empty())
            {
                int t1=q1.top();
                q1.pop();
                if(flag2) printf(" %d",t1);
                else
                {
                    flag2=1;
                    printf("%d",t1);
                }
            }
            puts("");
        }
    }
    return 0;
}
 // Return whether the queue is empty.
 bool empty(void) {
     return s_in.empty()&&s_out.empty();
 }
Exemplo n.º 22
0
bool Queue::empty(){
	return second.empty();
}
 // Return whether the queue is empty.
 bool empty(void) {
     return push_stack.empty() && queue.empty();
 }
 // Return whether the queue is empty.
 bool empty(void) {
     return in.empty();
 }
Exemplo n.º 25
0
    void push(int x) {
        arr.push(x);
		if (minValue.empty() || x <= minValue.top())
			minValue.push(x);
    }
Exemplo n.º 26
0
int main()
{

    ios::sync_with_stdio(false);cin.tie(0);     // decrease the time of cin, cout

    int t, n , k;

    cin >> t;

    while(t--){

        cin >> n;

        while(!first.empty()) first.pop();      // clear stacks in every test case
        while(!last.empty()) last.pop();

        for(int i = 1; i <= n; i++) cin >> arr[i];

        backwardStore[1] = 0;                   // first vehicle doesn't have a previous one
        first.push(1);                          // first vehicle has an index (1)

        for(int i = 2; i <= n; i++){

            while(!first.empty() && arr[i] > arr[first.top()])      // pop all less height vehicles
                first.pop();

            if(!first.empty()) backwardStore[i] = i - first.top();      // this driver can see backward up to first.top()
            else backwardStore[i] = i - 1;                         // this is the highest vehicle until now

            first.push(i);                                          // push vehicles indexes
        }

        forwardStore[n] = 0;                                        //last vehicle doesn't have a follower vehicle
        last.push(n);

        for(int i = n - 1; i >= 1; i--){

            while(!last.empty() && arr[i] > arr[last.top()]) last.pop();

            if(!last.empty()) forwardStore[i] = last.top() - i;
            else forwardStore[i] = n - i;

            last.push(i);
        }

        long long maxi = -1;        // initialize the maximum SIGHT
        int index = -1;

	//for(int i=1;i<=n;i++){
		//cout << "height : " << arr[i] << " left : " << backwardStore[i] << " right : " <<  forwardStore[i] << endl;
	//}

        for(int i = 1; i <= n; i++){

            long long sum = backwardStore[i] + forwardStore[i];

            sum = ( (sum % mod) *  (i) ) % mod;

            if(sum > maxi){                 // update maximum and index of the driver who have the maximum SIGHT until now

                maxi = sum;
                index = i;
            }
        }

        cout << index << "\n";
    }

    return 0;
}
Exemplo n.º 27
0
void print(stack<int> my_tour) {
    while (!my_tour.empty()) {
        int top = my_tour.top(); cout << top << ' ';
        my_tour.pop();
    }
}
	/** @return whether we have a next smallest number */
	bool hasNext() {
		if (!sta.empty())
			return true;
		else
			return false;
	}
Exemplo n.º 29
0
void push(int x)
{
    s1.push(x);
    if(s2.empty()||x<=s2.top())
        s2.push(x);
}
Exemplo n.º 30
0
 void transfere() {
     while (!s_2.empty()) {
         s_1.push(s_2.top());
         s_2.pop();
     }
 }