74d1f45c5c 2014-10-14 kinaba: #include <iostream> 74d1f45c5c 2014-10-14 kinaba: #include <sstream> 74d1f45c5c 2014-10-14 kinaba: #include <iomanip> 74d1f45c5c 2014-10-14 kinaba: #include <vector> 74d1f45c5c 2014-10-14 kinaba: #include <string> 74d1f45c5c 2014-10-14 kinaba: #include <map> 74d1f45c5c 2014-10-14 kinaba: #include <set> 74d1f45c5c 2014-10-14 kinaba: #include <algorithm> 74d1f45c5c 2014-10-14 kinaba: #include <numeric> 74d1f45c5c 2014-10-14 kinaba: #include <iterator> 74d1f45c5c 2014-10-14 kinaba: #include <functional> 74d1f45c5c 2014-10-14 kinaba: #include <complex> 74d1f45c5c 2014-10-14 kinaba: #include <queue> 74d1f45c5c 2014-10-14 kinaba: #include <stack> 74d1f45c5c 2014-10-14 kinaba: #include <cmath> 74d1f45c5c 2014-10-14 kinaba: #include <cassert> 74d1f45c5c 2014-10-14 kinaba: #include <tuple> 74d1f45c5c 2014-10-14 kinaba: using namespace std; 74d1f45c5c 2014-10-14 kinaba: typedef long long LL; 74d1f45c5c 2014-10-14 kinaba: typedef complex<double> CMP; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: class StoryFromTCO { public: 74d1f45c5c 2014-10-14 kinaba: int minimumChanges(vector <int> places, vector <int> cutoff) 74d1f45c5c 2014-10-14 kinaba: { 74d1f45c5c 2014-10-14 kinaba: const int N = places.size(); 74d1f45c5c 2014-10-14 kinaba: vector<pair<int,int>> cp; 74d1f45c5c 2014-10-14 kinaba: for(int i=0; i<N; ++i) 74d1f45c5c 2014-10-14 kinaba: cp.emplace_back(cutoff[i], places[i]); 74d1f45c5c 2014-10-14 kinaba: sort(cp.begin(), cp.end()); 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: vector<int> lm(N); // i-th place candidate can be put at >=lm[i] 74d1f45c5c 2014-10-14 kinaba: for(int i=0; i<N; ++i) { 74d1f45c5c 2014-10-14 kinaba: int p=cp[i].second, k=0; 74d1f45c5c 2014-10-14 kinaba: for(; k<N; ++k) 74d1f45c5c 2014-10-14 kinaba: if(p <= cp[k].first) 74d1f45c5c 2014-10-14 kinaba: break; 74d1f45c5c 2014-10-14 kinaba: if(k == N) 74d1f45c5c 2014-10-14 kinaba: return -1; 74d1f45c5c 2014-10-14 kinaba: lm[i] = k; 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: return solve(lm); 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: // reorder v so that v[i]<=i for all i. 74d1f45c5c 2014-10-14 kinaba: int solve(vector<int> v) 74d1f45c5c 2014-10-14 kinaba: { 74d1f45c5c 2014-10-14 kinaba: const int N = v.size(); 74d1f45c5c 2014-10-14 kinaba: const int HOLE = 0x7fffffff; 74d1f45c5c 2014-10-14 kinaba: int ans = 0; 74d1f45c5c 2014-10-14 kinaba: multiset<int> stock; 74d1f45c5c 2014-10-14 kinaba: for(int i=0; i<N; ++i) 74d1f45c5c 2014-10-14 kinaba: if(!(v[i]<=i)) { 74d1f45c5c 2014-10-14 kinaba: if(stock.empty() || !(*stock.begin()<=i)) { 74d1f45c5c 2014-10-14 kinaba: // find right most fitting elem. 74d1f45c5c 2014-10-14 kinaba: for(int k=N-1; k>i; --k) 74d1f45c5c 2014-10-14 kinaba: if(v[k]<=i) { 74d1f45c5c 2014-10-14 kinaba: stock.insert(v[k]); 74d1f45c5c 2014-10-14 kinaba: v[k] = HOLE; 74d1f45c5c 2014-10-14 kinaba: break; 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: if(stock.empty() || !(*stock.begin()<=i)) 74d1f45c5c 2014-10-14 kinaba: return -1; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: ++ans; 74d1f45c5c 2014-10-14 kinaba: if(v[i] != HOLE) stock.insert(v[i]); 74d1f45c5c 2014-10-14 kinaba: v[i] = *stock.begin(); 74d1f45c5c 2014-10-14 kinaba: stock.erase(stock.begin()); 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: return ans; 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: }; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: // BEGIN CUT HERE 74d1f45c5c 2014-10-14 kinaba: #include <ctime> 74d1f45c5c 2014-10-14 kinaba: double start_time; string timer() 74d1f45c5c 2014-10-14 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 74d1f45c5c 2014-10-14 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 74d1f45c5c 2014-10-14 kinaba: { os << "{ "; 74d1f45c5c 2014-10-14 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 74d1f45c5c 2014-10-14 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 74d1f45c5c 2014-10-14 kinaba: void verify_case(const int& Expected, const int& Received) { 74d1f45c5c 2014-10-14 kinaba: bool ok = (Expected == Received); 74d1f45c5c 2014-10-14 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 74d1f45c5c 2014-10-14 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 74d1f45c5c 2014-10-14 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 74d1f45c5c 2014-10-14 kinaba: #define END verify_case(_, StoryFromTCO().minimumChanges(places, cutoff));} 74d1f45c5c 2014-10-14 kinaba: int main(){ 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: CASE(0) 74d1f45c5c 2014-10-14 kinaba: int places_[] = {20,100,500,50}; 74d1f45c5c 2014-10-14 kinaba: vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 74d1f45c5c 2014-10-14 kinaba: int cutoff_[] = {7500,2250,150,24}; 74d1f45c5c 2014-10-14 kinaba: vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 3; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(1) 74d1f45c5c 2014-10-14 kinaba: int places_[] = {5,4,3,2,1}; 74d1f45c5c 2014-10-14 kinaba: vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 74d1f45c5c 2014-10-14 kinaba: int cutoff_[] = {5,4,3,2,1}; 74d1f45c5c 2014-10-14 kinaba: vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 0; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(2) 74d1f45c5c 2014-10-14 kinaba: int places_[] = {1,5,5,5}; 74d1f45c5c 2014-10-14 kinaba: vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 74d1f45c5c 2014-10-14 kinaba: int cutoff_[] = {8,6,4,2}; 74d1f45c5c 2014-10-14 kinaba: vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 74d1f45c5c 2014-10-14 kinaba: int _ = -1; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(3) 74d1f45c5c 2014-10-14 kinaba: int places_[] = {3,1,5}; 74d1f45c5c 2014-10-14 kinaba: vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 74d1f45c5c 2014-10-14 kinaba: int cutoff_[] = {6,4,2}; 74d1f45c5c 2014-10-14 kinaba: vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 3; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(4) 74d1f45c5c 2014-10-14 kinaba: int places_[] = {14,11,42,9,19}; 74d1f45c5c 2014-10-14 kinaba: vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 74d1f45c5c 2014-10-14 kinaba: int cutoff_[] = {11,16,37,41,47} 74d1f45c5c 2014-10-14 kinaba: ; 74d1f45c5c 2014-10-14 kinaba: vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 4; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(5) 74d1f45c5c 2014-10-14 kinaba: int places_[] = {4,1,3,3,2,4,5,5,4,4}; 74d1f45c5c 2014-10-14 kinaba: vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 74d1f45c5c 2014-10-14 kinaba: int cutoff_[] = {3,3,5,2,4,4,5,4,3,5}; 74d1f45c5c 2014-10-14 kinaba: vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 6; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(6) 74d1f45c5c 2014-10-14 kinaba: int places_[] = {213,177,237,444,497,315,294,104,522,635,13,26,22,276,88,249,374,17,42,245,80,553,121,625,62,105, 74d1f45c5c 2014-10-14 kinaba: 53,132,178,250,18,210,492,181,2,99,29,21,62,218,188,584,702,63,41,79,28,452,2}; 74d1f45c5c 2014-10-14 kinaba: vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 74d1f45c5c 2014-10-14 kinaba: int cutoff_[] = {33,40,41,48,74,84,117,125,126,164,197,197,201,218,222,231,244,277,290,309,321,321,360,376,440, 74d1f45c5c 2014-10-14 kinaba: 442,465,477,491,513,639,645,647,675,706,710,726,736,736,765,801,838,845,854,863,865,887,902,908}; 74d1f45c5c 2014-10-14 kinaba: vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 23; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: /* 74d1f45c5c 2014-10-14 kinaba: CASE(7) 74d1f45c5c 2014-10-14 kinaba: int places_[] = ; 74d1f45c5c 2014-10-14 kinaba: vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 74d1f45c5c 2014-10-14 kinaba: int cutoff_[] = ; 74d1f45c5c 2014-10-14 kinaba: vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 74d1f45c5c 2014-10-14 kinaba: int _ = ; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(8) 74d1f45c5c 2014-10-14 kinaba: int places_[] = ; 74d1f45c5c 2014-10-14 kinaba: vector <int> places(places_, places_+sizeof(places_)/sizeof(*places_)); 74d1f45c5c 2014-10-14 kinaba: int cutoff_[] = ; 74d1f45c5c 2014-10-14 kinaba: vector <int> cutoff(cutoff_, cutoff_+sizeof(cutoff_)/sizeof(*cutoff_)); 74d1f45c5c 2014-10-14 kinaba: int _ = ; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: */ 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: // END CUT HERE