f94ac98860 2016-12-29 kinaba: #include <iostream> f94ac98860 2016-12-29 kinaba: #include <sstream> f94ac98860 2016-12-29 kinaba: #include <iomanip> f94ac98860 2016-12-29 kinaba: #include <vector> f94ac98860 2016-12-29 kinaba: #include <string> f94ac98860 2016-12-29 kinaba: #include <map> f94ac98860 2016-12-29 kinaba: #include <set> f94ac98860 2016-12-29 kinaba: #include <algorithm> f94ac98860 2016-12-29 kinaba: #include <numeric> f94ac98860 2016-12-29 kinaba: #include <iterator> f94ac98860 2016-12-29 kinaba: #include <functional> f94ac98860 2016-12-29 kinaba: #include <complex> f94ac98860 2016-12-29 kinaba: #include <queue> f94ac98860 2016-12-29 kinaba: #include <stack> f94ac98860 2016-12-29 kinaba: #include <cmath> f94ac98860 2016-12-29 kinaba: #include <cassert> f94ac98860 2016-12-29 kinaba: #include <tuple> f94ac98860 2016-12-29 kinaba: using namespace std; f94ac98860 2016-12-29 kinaba: typedef long long LL; f94ac98860 2016-12-29 kinaba: typedef complex<double> CMP; f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: class TreeDistanceConstruction { public: f94ac98860 2016-12-29 kinaba: vector <int> construct(vector <int> d) f94ac98860 2016-12-29 kinaba: { f94ac98860 2016-12-29 kinaba: map<int, vector<int>> d2i; f94ac98860 2016-12-29 kinaba: for (int i = 0; i < d.size(); ++i) f94ac98860 2016-12-29 kinaba: d2i[d[i]].push_back(i); f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: const int m = d2i.begin()->first; f94ac98860 2016-12-29 kinaba: const int M = d2i.rbegin()->first; f94ac98860 2016-12-29 kinaba: vector<int> ans; f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: if (d2i[m].size() >= 3) f94ac98860 2016-12-29 kinaba: return vector<int>(); f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: int L = d2i[m].front(), R = d2i[m].back(); f94ac98860 2016-12-29 kinaba: if (d2i[m].size() == 1) { f94ac98860 2016-12-29 kinaba: if (2 * m != M) f94ac98860 2016-12-29 kinaba: return vector<int>(); f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: else { // d2i[m].size() == 2 f94ac98860 2016-12-29 kinaba: if (2*m-1 != M) f94ac98860 2016-12-29 kinaba: return vector<int>(); f94ac98860 2016-12-29 kinaba: ans.push_back(L); f94ac98860 2016-12-29 kinaba: ans.push_back(R); f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: for (int k = m + 1; k <= M; ++k) { f94ac98860 2016-12-29 kinaba: if (d2i[k].size() < 2) f94ac98860 2016-12-29 kinaba: return vector<int>(); f94ac98860 2016-12-29 kinaba: for (int i = 0; i < d2i[k].size(); ++i) { f94ac98860 2016-12-29 kinaba: ans.push_back(i == 0 ? L : R); f94ac98860 2016-12-29 kinaba: ans.push_back(d2i[k][i]); f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: L = d2i[k].front(); f94ac98860 2016-12-29 kinaba: R = d2i[k].back(); f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: return ans; f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: }; f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: // BEGIN CUT HERE f94ac98860 2016-12-29 kinaba: #include <ctime> f94ac98860 2016-12-29 kinaba: double start_time; string timer() f94ac98860 2016-12-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f94ac98860 2016-12-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f94ac98860 2016-12-29 kinaba: { os << "{ "; f94ac98860 2016-12-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f94ac98860 2016-12-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f94ac98860 2016-12-29 kinaba: void verify_case(const vector <int>& Expected, const vector <int>& Received) { f94ac98860 2016-12-29 kinaba: bool ok = (Expected == Received); f94ac98860 2016-12-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f94ac98860 2016-12-29 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } f94ac98860 2016-12-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f94ac98860 2016-12-29 kinaba: #define END verify_case(_, TreeDistanceConstruction().construct(d));} f94ac98860 2016-12-29 kinaba: int main(){ f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: CASE(0) f94ac98860 2016-12-29 kinaba: int d_[] = {3,2,2,3}; f94ac98860 2016-12-29 kinaba: vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); f94ac98860 2016-12-29 kinaba: int __[] = {1, 2, 1, 0, 2, 3 }; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: CASE(1) f94ac98860 2016-12-29 kinaba: int d_[] = {1,2,2,2}; f94ac98860 2016-12-29 kinaba: vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); f94ac98860 2016-12-29 kinaba: int __[] = {0, 1, 0, 2, 0, 3 }; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: CASE(2) f94ac98860 2016-12-29 kinaba: int d_[] = {1,1,1,1}; f94ac98860 2016-12-29 kinaba: vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); f94ac98860 2016-12-29 kinaba: vector <int> _; f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: CASE(3) f94ac98860 2016-12-29 kinaba: int d_[] = {1,1,1}; f94ac98860 2016-12-29 kinaba: vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); f94ac98860 2016-12-29 kinaba: vector <int> _; f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: CASE(4) f94ac98860 2016-12-29 kinaba: int d_[] = {1,1}; f94ac98860 2016-12-29 kinaba: vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); f94ac98860 2016-12-29 kinaba: int __[] = {0, 1 }; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: CASE(5) f94ac98860 2016-12-29 kinaba: int d_[] = { 2,2,3,3,3 }; f94ac98860 2016-12-29 kinaba: vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); f94ac98860 2016-12-29 kinaba: int __[] = { -1 }; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: CASE(6) f94ac98860 2016-12-29 kinaba: int d_[] = { 3,4,4,5,6,6 }; f94ac98860 2016-12-29 kinaba: vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); f94ac98860 2016-12-29 kinaba: int __[] = { -1 }; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: // END CUT HERE