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

怎么总是WA

Posted by vitacy at 2009-05-16 00:59:45 on Problem 1010
#include <iostream>
#include <string>
#include <cstdlib>
#include <set>
#include <vector>

using namespace std;

bool tie=false;
vector<int> answer;
int total=0;
void process(multiset<int> &stamps,int customer);
void backtrack(multiset<int>::reverse_iterator b,
                multiset<int>::reverse_iterator e,		
								int c,int s,vector<int> &tmp);	 
								

int main()
{
	multiset<int> stamps;
	vector<int>	  customers;
	int i;
	while(cin >>i)
	{
              
              do{
                  if(i==0) break;
                  stamps.insert(i);
              }while(cin >>i);
              
              while(cin>>i){
	 						    if(i==0) break;
	 						    customers.push_back(i);
              }
              for(i=0;i<customers.size();i++)
              {
  							   
 							    process(stamps,customers[i]);
							}
							stamps.clear();
							customers.clear();
    }
    
	return 0;
}
void process(multiset<int> &stamps,int customer)
{
 		 vector<int> tmp;
 		 total=0;
 		 tie=false;
 		 answer.clear();
 		 
 		 backtrack(stamps.rbegin(),stamps.rend(),customer,4,tmp);
 		 cout <<customer<<" ";
 		 if(total==0){
		 		 cout<<"---- none"<<endl;
    } else{
			cout<<"("<<(answer.size()>>1)<<"): ";
			if(tie) cout <<"tie"<<endl;
			else {
					 for(int i=answer.size()-2;i>=0;i-=2)
					 	{			 while(answer[i+1]--){
									 cout <<answer[i];
					 				 if(i!=0) cout<<" ";
					 				 else cout<<endl;
									 }
						 }
		 }
		}
}
void backtrack(multiset<int>::reverse_iterator b,
                multiset<int>::reverse_iterator e,		
								int c,int s,vector<int> &tmp)
{
 								int i,j;
 								
				 				if(c==0){
											for(j=1;j<tmp.size();j+=2)
											     c += tmp[j];
			 						    if(answer.size()>tmp.size())
			 						    {
											}else if(answer.size()<tmp.size())
											{
											            answer=tmp;
											 						total=c;
											 						tie=false;
											}else if(answer.size()==tmp.size()&&total>c)
											{
											 						answer=tmp;
											 						total=c;
											 						tie=false;
											}	else if(total==c)
											{
											 						if(answer[0]<tmp[0])
											 						{
							 										    answer=tmp;
											 						    total=c;
											 						    tie=false;
																	}
																	if(answer[0]==tmp[0])
																	{
		 		 										 			    tie=true;	    
																	}
											}
											return;
								 }
								 
                if(b==e) return;
 								if(s==0) return;
 								
				 				int stamp=*b; 
				 				int count=c/stamp;
				 				if(count>s) return;
								 ++b;
								 backtrack(b,e,c,s,tmp);		 
								 tmp.push_back(stamp);
								 for(i=1;i<=count;i++)
								 {
								        tmp.push_back(i);
								        //cout<<"count:"<<i<<" stamp:"<<stamp<<" c-count*stamp:"<<c-i*stamp<<endl;
								 				backtrack(b,e,c-i*stamp,s-i,tmp);	
												tmp.pop_back();				
								 }
								 tmp.pop_back();								 
}


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