4fd800b3a8 2011-02-23 kinaba: //----------------------------------------------------------------------------- 4fd800b3a8 2011-02-23 kinaba: // >>Code Template<< (for Visual C++) 4fd800b3a8 2011-02-23 kinaba: 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 <string> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <map> 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 <complex> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 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: #define cout os 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: void END_OF_INPUT_FOR_THIS_TEST_CASE(); // stub for multi-threading 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: //----------------------------------------------------------------------------- 4fd800b3a8 2011-02-23 kinaba: // >>Main<< 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void case_main( ostream& os ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: END_OF_INPUT_FOR_THIS_TEST_CASE(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: //----------------------------------------------------------------------------- 4fd800b3a8 2011-02-23 kinaba: // >>Code Template<< (Multi-Thread Solver) 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: #if 1 4fd800b3a8 2011-02-23 kinaba: #undef cout 4fd800b3a8 2011-02-23 kinaba: #include <windows.h> 4fd800b3a8 2011-02-23 kinaba: #include <process.h> 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int THREAD_NUM = 2; 4fd800b3a8 2011-02-23 kinaba: volatile int g_id; 4fd800b3a8 2011-02-23 kinaba: int g_nCase; 4fd800b3a8 2011-02-23 kinaba: CRITICAL_SECTION g_cs; 4fd800b3a8 2011-02-23 kinaba: vector<string> g_output; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: unsigned __stdcall thread_main( void* t_id ) { 4fd800b3a8 2011-02-23 kinaba: for(;;) { 4fd800b3a8 2011-02-23 kinaba: EnterCriticalSection(&g_cs); 4fd800b3a8 2011-02-23 kinaba: const int id = ++g_id; 4fd800b3a8 2011-02-23 kinaba: if(id>g_nCase) { LeaveCriticalSection(&g_cs); break; } 4fd800b3a8 2011-02-23 kinaba: cerr << setw(4) << id << " @ " << (int)t_id << " start" << endl; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: ostringstream ss; 4fd800b3a8 2011-02-23 kinaba: ss << "Case #" << id << ": "; 4fd800b3a8 2011-02-23 kinaba: case_main( ss ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: EnterCriticalSection(&g_cs); 4fd800b3a8 2011-02-23 kinaba: if(g_output.size()<id) g_output.resize(id); 4fd800b3a8 2011-02-23 kinaba: g_output[id-1] = ss.str(); 4fd800b3a8 2011-02-23 kinaba: cerr << setw(4) << id << " @ " << (int)t_id << " end" << endl; 4fd800b3a8 2011-02-23 kinaba: LeaveCriticalSection(&g_cs); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void END_OF_INPUT_FOR_THIS_TEST_CASE() { LeaveCriticalSection(&g_cs); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int main() { 4fd800b3a8 2011-02-23 kinaba: cin >> g_nCase; 4fd800b3a8 2011-02-23 kinaba: string dummy; getline(cin, dummy); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: InitializeCriticalSection(&g_cs); 4fd800b3a8 2011-02-23 kinaba: vector<HANDLE> thread; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<THREAD_NUM; ++i) 4fd800b3a8 2011-02-23 kinaba: thread.push_back( (HANDLE)_beginthreadex(0, 0, &thread_main, (void*)i, 0, 0) ); 4fd800b3a8 2011-02-23 kinaba: WaitForMultipleObjects( thread.size(), &thread[0], TRUE, INFINITE ); 4fd800b3a8 2011-02-23 kinaba: DeleteCriticalSection(&g_cs); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: copy( g_output.begin(), g_output.end(), ostream_iterator<string>(cout) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: //----------------------------------------------------------------------------- 4fd800b3a8 2011-02-23 kinaba: // >>Code Template<< (Single-Thread Solver) 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: #else 4fd800b3a8 2011-02-23 kinaba: #undef cout 4fd800b3a8 2011-02-23 kinaba: void END_OF_INPUT_FOR_THIS_TEST_CASE() {} 4fd800b3a8 2011-02-23 kinaba: int main() { 4fd800b3a8 2011-02-23 kinaba: int nCase; cin >> nCase; 4fd800b3a8 2011-02-23 kinaba: string dummy; getline(cin, dummy); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int id=1; id<=nCase; ++id) { 4fd800b3a8 2011-02-23 kinaba: cout << "Case #" << id << ": "; 4fd800b3a8 2011-02-23 kinaba: case_main( cout ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: #endif