f33265e95c 2014-09-05 kinaba: #include <iostream> f33265e95c 2014-09-05 kinaba: #include <sstream> f33265e95c 2014-09-05 kinaba: #include <iomanip> f33265e95c 2014-09-05 kinaba: #include <vector> f33265e95c 2014-09-05 kinaba: #include <string> f33265e95c 2014-09-05 kinaba: #include <map> f33265e95c 2014-09-05 kinaba: #include <set> f33265e95c 2014-09-05 kinaba: #include <algorithm> f33265e95c 2014-09-05 kinaba: #include <numeric> f33265e95c 2014-09-05 kinaba: #include <iterator> f33265e95c 2014-09-05 kinaba: #include <functional> f33265e95c 2014-09-05 kinaba: #include <complex> f33265e95c 2014-09-05 kinaba: #include <queue> f33265e95c 2014-09-05 kinaba: #include <stack> f33265e95c 2014-09-05 kinaba: #include <cmath> f33265e95c 2014-09-05 kinaba: #include <cassert> f33265e95c 2014-09-05 kinaba: #include <tuple> f33265e95c 2014-09-05 kinaba: using namespace std; f33265e95c 2014-09-05 kinaba: typedef long long LL; f33265e95c 2014-09-05 kinaba: typedef complex<double> CMP; f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: class CatsOnTheLineDiv1 { public: f33265e95c 2014-09-05 kinaba: int getNumber(vector <int> position, vector <int> count, int time) f33265e95c 2014-09-05 kinaba: { f33265e95c 2014-09-05 kinaba: const int N = position.size(); f33265e95c 2014-09-05 kinaba: vector<pair<int,int>> pc; f33265e95c 2014-09-05 kinaba: for(int i=0; i<N; ++i) pc.emplace_back(position[i], count[i]); f33265e95c 2014-09-05 kinaba: sort(pc.begin(), pc.end()); f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: map<LL,int> dp; f33265e95c 2014-09-05 kinaba: dp[(-400000000LL)<<32] = 0; f33265e95c 2014-09-05 kinaba: for(auto& pci: pc) { f33265e95c 2014-09-05 kinaba: const int p = pci.first; f33265e95c 2014-09-05 kinaba: const int c = pci.second; f33265e95c 2014-09-05 kinaba: map<LL,int> dp2; f33265e95c 2014-09-05 kinaba: for(auto& tk: dp) { f33265e95c 2014-09-05 kinaba: const LL t = tk.first; f33265e95c 2014-09-05 kinaba: const int k = tk.second; f33265e95c 2014-09-05 kinaba: const int occ_x = int(t>>32); f33265e95c 2014-09-05 kinaba: const int has_wan = t&0x80000000; f33265e95c 2014-09-05 kinaba: const int wan_x = int(t&0x3fffffff); f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: int myleft = max(p-time, occ_x+1); f33265e95c 2014-09-05 kinaba: int myright = myleft + c - 1; f33265e95c 2014-09-05 kinaba: if(myright <= p+time) { f33265e95c 2014-09-05 kinaba: LL key = (LL(myright)<<32) | has_wan | wan_x; f33265e95c 2014-09-05 kinaba: int v = k; f33265e95c 2014-09-05 kinaba: cerr << "#" << myright << " " << (has_wan?1:0) << " " << wan_x << endl; f33265e95c 2014-09-05 kinaba: if(dp2.count(key)) dp2[key] = min(dp2[key], v); else dp2[key] = v; f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: if(has_wan && p-time <= wan_x) { f33265e95c 2014-09-05 kinaba: LL key = LL(occ_x)<<32 | 0x80000000 | wan_x; f33265e95c 2014-09-05 kinaba: int v = k; f33265e95c 2014-09-05 kinaba: cerr << "$" << occ_x << " " << 1 << " " << wan_x << endl; f33265e95c 2014-09-05 kinaba: if(dp2.count(key)) dp2[key] = min(dp2[key], v); else dp2[key] = v; f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: { f33265e95c 2014-09-05 kinaba: LL key = LL(occ_x)<<32 | 0x80000000 | (p+time); f33265e95c 2014-09-05 kinaba: int v = k + (has_wan?1:0); f33265e95c 2014-09-05 kinaba: cerr << "!" << occ_x << " " << 1 << " " << p+time << endl; f33265e95c 2014-09-05 kinaba: if(dp2.count(key)) dp2[key] = min(dp2[key], v); else dp2[key] = v; f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: dp = std::move(dp2); f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: int best = N; f33265e95c 2014-09-05 kinaba: for(auto& tk: dp) { f33265e95c 2014-09-05 kinaba: const LL t = tk.first; f33265e95c 2014-09-05 kinaba: const int k = tk.second; f33265e95c 2014-09-05 kinaba: const int occ_x = int(t>>32); f33265e95c 2014-09-05 kinaba: const int has_wan = t&0x80000000; f33265e95c 2014-09-05 kinaba: const int wan_x = int(t&0x3fffffff); f33265e95c 2014-09-05 kinaba: best = min(best, k + (has_wan?1:0)); f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: return best; f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: }; f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: // BEGIN CUT HERE f33265e95c 2014-09-05 kinaba: #include <ctime> f33265e95c 2014-09-05 kinaba: double start_time; string timer() f33265e95c 2014-09-05 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f33265e95c 2014-09-05 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f33265e95c 2014-09-05 kinaba: { os << "{ "; f33265e95c 2014-09-05 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f33265e95c 2014-09-05 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f33265e95c 2014-09-05 kinaba: void verify_case(const int& Expected, const int& Received) { f33265e95c 2014-09-05 kinaba: bool ok = (Expected == Received); f33265e95c 2014-09-05 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f33265e95c 2014-09-05 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } f33265e95c 2014-09-05 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f33265e95c 2014-09-05 kinaba: #define END verify_case(_, CatsOnTheLineDiv1().getNumber(position, count, time));} f33265e95c 2014-09-05 kinaba: int main(){ f33265e95c 2014-09-05 kinaba: f33265e95c 2014-09-05 kinaba: CASE(0) f33265e95c 2014-09-05 kinaba: int position_[] = {0}; f33265e95c 2014-09-05 kinaba: vector <int> position(position_, position_+sizeof(position_)/sizeof(*position_)); f33265e95c 2014-09-05 kinaba: int count_[] = {7}; f33265e95c 2014-09-05 kinaba: vector <int> count(count_, count_+sizeof(count_)/sizeof(*count_)); f33265e95c 2014-09-05 kinaba: int time = 3; f33265e95c 2014-09-05 kinaba: int _ = 0; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(1) f33265e95c 2014-09-05 kinaba: int position_[] = {0}; f33265e95c 2014-09-05 kinaba: vector <int> position(position_, position_+sizeof(position_)/sizeof(*position_)); f33265e95c 2014-09-05 kinaba: int count_[] = {6}; f33265e95c 2014-09-05 kinaba: vector <int> count(count_, count_+sizeof(count_)/sizeof(*count_)); f33265e95c 2014-09-05 kinaba: int time = 2; f33265e95c 2014-09-05 kinaba: int _ = 1; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(2) f33265e95c 2014-09-05 kinaba: int position_[] = {4, 7, 47}; f33265e95c 2014-09-05 kinaba: vector <int> position(position_, position_+sizeof(position_)/sizeof(*position_)); f33265e95c 2014-09-05 kinaba: int count_[] = {4, 7, 4}; f33265e95c 2014-09-05 kinaba: vector <int> count(count_, count_+sizeof(count_)/sizeof(*count_)); f33265e95c 2014-09-05 kinaba: int time = 1; f33265e95c 2014-09-05 kinaba: int _ = 3; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(3) f33265e95c 2014-09-05 kinaba: int position_[] = {3, 0, 7, 10}; f33265e95c 2014-09-05 kinaba: vector <int> position(position_, position_+sizeof(position_)/sizeof(*position_)); f33265e95c 2014-09-05 kinaba: int count_[] = {3, 7, 4, 5}; f33265e95c 2014-09-05 kinaba: vector <int> count(count_, count_+sizeof(count_)/sizeof(*count_)); f33265e95c 2014-09-05 kinaba: int time = 2; f33265e95c 2014-09-05 kinaba: int _ = 2; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(4) f33265e95c 2014-09-05 kinaba: int position_[] = {-5, 0, 7}; f33265e95c 2014-09-05 kinaba: vector <int> position(position_, position_+sizeof(position_)/sizeof(*position_)); f33265e95c 2014-09-05 kinaba: int count_[] = {47, 85, 10}; f33265e95c 2014-09-05 kinaba: vector <int> count(count_, count_+sizeof(count_)/sizeof(*count_)); f33265e95c 2014-09-05 kinaba: int time = 6; f33265e95c 2014-09-05 kinaba: int _ = 1; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(5) f33265e95c 2014-09-05 kinaba: int position_[] = {-8, 12, -15, -20, 17, -5, 7, 10}; f33265e95c 2014-09-05 kinaba: vector <int> position(position_, position_+sizeof(position_)/sizeof(*position_)); f33265e95c 2014-09-05 kinaba: int count_[] = {20, 10, 7, 9, 2, 8, 11, 10}; f33265e95c 2014-09-05 kinaba: vector <int> count(count_, count_+sizeof(count_)/sizeof(*count_)); f33265e95c 2014-09-05 kinaba: int time = 2; f33265e95c 2014-09-05 kinaba: int _ = 5; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: /* f33265e95c 2014-09-05 kinaba: CASE(6) f33265e95c 2014-09-05 kinaba: int position_[] = ; f33265e95c 2014-09-05 kinaba: vector <int> position(position_, position_+sizeof(position_)/sizeof(*position_)); f33265e95c 2014-09-05 kinaba: int count_[] = ; f33265e95c 2014-09-05 kinaba: vector <int> count(count_, count_+sizeof(count_)/sizeof(*count_)); f33265e95c 2014-09-05 kinaba: int time = ; f33265e95c 2014-09-05 kinaba: int _ = ; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: CASE(7) f33265e95c 2014-09-05 kinaba: int position_[] = ; f33265e95c 2014-09-05 kinaba: vector <int> position(position_, position_+sizeof(position_)/sizeof(*position_)); f33265e95c 2014-09-05 kinaba: int count_[] = ; f33265e95c 2014-09-05 kinaba: vector <int> count(count_, count_+sizeof(count_)/sizeof(*count_)); f33265e95c 2014-09-05 kinaba: int time = ; f33265e95c 2014-09-05 kinaba: int _ = ; f33265e95c 2014-09-05 kinaba: END f33265e95c 2014-09-05 kinaba: */ f33265e95c 2014-09-05 kinaba: } f33265e95c 2014-09-05 kinaba: // END CUT HERE