4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<typename To, typename From> 4fd800b3a8 2011-02-23 kinaba: To lexical_cast( const From& f ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: stringstream ss; 4fd800b3a8 2011-02-23 kinaba: ss << f; 4fd800b3a8 2011-02-23 kinaba: To t; 4fd800b3a8 2011-02-23 kinaba: ss >> t; 4fd800b3a8 2011-02-23 kinaba: return t; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class RailwayTickets { public: 4fd800b3a8 2011-02-23 kinaba: int minRejects(vector <string> travel, int seats) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: typedef pair<bool,int> event; // [true,A --- false,B) 4fd800b3a8 2011-02-23 kinaba: vector<event> evs; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<travel.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: stringstream ss(travel[i]); 4fd800b3a8 2011-02-23 kinaba: for(string s; ss>>s; ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int k = s.find('-'); 4fd800b3a8 2011-02-23 kinaba: int A = lexical_cast<int>( s.substr(0, k) ); 4fd800b3a8 2011-02-23 kinaba: int B = lexical_cast<int>( s.substr(k+1) ); 4fd800b3a8 2011-02-23 kinaba: evs.push_back( event(true,A) ); 4fd800b3a8 2011-02-23 kinaba: evs.push_back( event(false,B) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: sort( evs.begin(), evs.end() ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > minDeny(evs.size()+1, vector<int>(seats+1)); 4fd800b3a8 2011-02-23 kinaba: for(int ti=evs.size()-1; ti>=0; --ti) 4fd800b3a8 2011-02-23 kinaba: for(int si=0; si<=seats; ++si) 4fd800b3a8 2011-02-23 kinaba: if( evs[ti].first ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( si==0 ) 4fd800b3a8 2011-02-23 kinaba: minDeny[ti][si] = minDeny[ti+1][si]+1; 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: minDeny[ti][si] = min( minDeny[ti+1][si]+1, minDeny[ti+1][si-1] ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: minDeny[ti][si] = minDeny[ti+1][si+1]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return minDeny[0][seats]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, RailwayTickets().minRejects(travel, seats));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string travel_[] = {"1-3 3-5", 4fd800b3a8 2011-02-23 kinaba: "2-4 4-6", 4fd800b3a8 2011-02-23 kinaba: "1-2 2-3 3-4 4-5"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int seats = 2; 4fd800b3a8 2011-02-23 kinaba: int _ = 2; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string travel_[] = {"1-10000","2-10000","1-9999","2-9999","5000-5001"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int seats = 3; 4fd800b3a8 2011-02-23 kinaba: int _ = 2; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string travel_[] = {"1-2 2-5 2-8 234-236 56-878 6-34", 4fd800b3a8 2011-02-23 kinaba: "234-776 3242-8000 999-1000 3-14", 4fd800b3a8 2011-02-23 kinaba: "121-122 435-3455 123-987 77-999"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int seats = 1000; 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string travel_[] = {"1-2 2-3 3-4 4-5 5-6 6-7 1-3 2-7 1-2 1-4"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int seats = 1; 4fd800b3a8 2011-02-23 kinaba: int _ = 4; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string travel_[] = {"1-2","1-2"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int seats = 1; 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string travel_[] = {"1-2"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int seats = 1; 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE