Created
April 15, 2019 12:58
-
-
Save yak1ex/3ec70964e4be69263f0aea2190c1a53b to your computer and use it in GitHub Desktop.
GCJ2019 R1A A and another C++ matters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iomanip> | |
#include <iostream> | |
#include <sstream> | |
#include <algorithm> | |
#include <numeric> | |
#include <limits> | |
#include <bitset> | |
#include <map> | |
#include <set> | |
#include <queue> | |
#include <stack> | |
#include <unordered_map> | |
#include <unordered_set> | |
#include <cmath> | |
#include <cassert> | |
#include <functional> | |
typedef unsigned long long ULL; | |
typedef long long LL; | |
typedef unsigned long UL; | |
typedef unsigned int UI; | |
typedef unsigned short US; | |
typedef unsigned char UC; | |
// Integer range for range-based for loop | |
template<typename T,T D=1>struct irange{struct it{it(T v_):v(v_){}T operator*()const{return v;}it&operator++(){v+=D;return*this;}friend bool operator!=(const it&it1, const it&it2){return it1.v!=it2.v;}private:T v;};it begin()const{return b;}it end()const{return e;}irange(T b_,T e_):b(b_),e(e_){}irange<T,-D>rev()const{return {e-1,b-1};}private:T b,e;}; | |
#define IR(b,e) irange<std::common_type_t<decltype(b),decltype(e)>>(b,e) | |
// reverse range | |
template<typename T>struct rrange{T&t;rrange(T&t_):t(t_){}auto begin()const{return rbegin(t);}auto end()const{return rend(t);}};template<typename T>auto rev(T&t){return rrange<T>(t);}template<typename T,T D>auto rev(const irange<T,D>&t){return t.rev();} | |
// mvec for flat style | |
template<typename T,typename U>auto make_mvec(const T&t,const U&u){return std::vector<T>(u,t);}template<typename T,typename U0,typename...U>auto make_mvec(const T&t,const U0&u0,const U&...u){return std::vector<decltype(make_mvec<T>(t,u...))>(u0,make_mvec<T>(t,u...));} | |
#define REC(f, r, a) std::function< r a > f = [&] a -> r | |
#define RNG(v) std::begin(v), std::end(v) | |
using namespace std; | |
bool check(UI lr, UI lc, UI i, UI j) | |
{ | |
return i != lr && j != lc && i+j != lr+lc && lr+j != i+lc; | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(false); | |
UI cases; cin >> cases; | |
for(UI casenum : IR(0, cases)) { | |
UI R, C; cin >> R >> C; | |
bool sw = false; | |
if(R < C) { | |
swap(R,C); | |
sw = true; | |
} | |
auto table = make_mvec<UI>(0, R, C); | |
vector<pair<UI, UI>> result; | |
REC(moves, bool, (UI r, UI c, UI rd, UI cd, UI RR, UI CC, UI num)) { | |
if(!table[r+rd][c+cd]) { | |
result.emplace_back(r+rd, c+cd); | |
table[r+rd][c+cd] = 1; | |
for(auto i: IR(0, RR)) { | |
for(auto j: IR(0,CC)) { | |
if(check(r, c, i, j)) | |
if(moves(i, j, rd, cd, RR, CC, num + 1)) return true; | |
} | |
} | |
table[r+rd][c+cd] = 0; | |
result.pop_back(); | |
return false; | |
} else return num == RR*CC; | |
}; | |
auto move = [&]()->bool { | |
UI rr = R/(R/5), rd = 0; | |
for(auto i: IR(0, R/rr)) { | |
UI cd = 0; | |
for(auto j: IR(0, C/2)) { | |
UI cc = (C%2 && j == 0) ? 3 : 2; | |
bool flag = false; | |
for(auto ii: IR(0, rr)) { | |
for(auto jj: IR(0, cc)) { | |
if(result.size() == 0 || check(result.back().first, result.back().second, ii + rd, jj + cd)) { | |
if(moves(ii, jj, rd, cd, i == R/rr-1 ? R - rd : rr, cc, 0)) { | |
flag = true; break; | |
} | |
} | |
} | |
if(flag) break; | |
} | |
if(!flag) return false; | |
cd += cc; | |
} | |
rd += rr; | |
} | |
return true; | |
}; | |
auto ver = C <= 5 ? moves(0, 0, 0, 0, R, C, 0) : move(); | |
cout << "Case #" << casenum+1 << ": "; | |
if(ver) { | |
cout << "POSSIBLE" << endl; | |
for(auto &v: result) { | |
if(sw) cout << v.second + 1 << ' ' << v.first + 1 << endl; | |
else cout << v.first + 1 << ' ' << v.second + 1 << endl; | |
} | |
} | |
else cout << "IMPOSSIBLE" << endl; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iomanip> | |
#include <iostream> | |
#include <sstream> | |
#include <algorithm> | |
#include <numeric> | |
#include <limits> | |
#include <bitset> | |
#include <map> | |
#include <set> | |
#include <queue> | |
#include <stack> | |
#include <unordered_map> | |
#include <unordered_set> | |
#include <cmath> | |
#include <cassert> | |
#include <random> | |
#include <functional> | |
typedef unsigned long long ULL; | |
typedef long long LL; | |
typedef unsigned long UL; | |
typedef unsigned int UI; | |
typedef unsigned short US; | |
typedef unsigned char UC; | |
// Integer range for range-based for loop | |
template<typename T,T D=1>struct irange{struct it{it(T v_):v(v_){}T operator*()const{return v;}it&operator++(){v+=D;return*this;}friend bool operator!=(const it&it1, const it&it2){return it1.v!=it2.v;}private:T v;};it begin()const{return b;}it end()const{return e;}irange(T b_,T e_):b(b_),e(e_){}irange<T,-D>rev()const{return {e-1,b-1};}private:T b,e;}; | |
#define IR(b,e) irange<std::common_type_t<decltype(b),decltype(e)>>(b,e) | |
// reverse range | |
template<typename T>struct rrange{T&t;rrange(T&t_):t(t_){}auto begin()const{return rbegin(t);}auto end()const{return rend(t);}};template<typename T>auto rev(T&t){return rrange<T>(t);}template<typename T,T D>auto rev(const irange<T,D>&t){return t.rev();} | |
// mvec for flat style | |
template<typename T,typename U>auto make_mvec(const T&t,const U&u){return std::vector<T>(u,t);}template<typename T,typename U0,typename...U>auto make_mvec(const T&t,const U0&u0,const U&...u){return std::vector<decltype(make_mvec<T>(t,u...))>(u0,make_mvec<T>(t,u...));} | |
#define REC(f, r, a) std::function< r a > f = [&] a -> r | |
#define RNG(v) std::begin(v), std::end(v) | |
using namespace std; | |
bool check(UI lr, UI lc, UI i, UI j) | |
{ | |
return i != lr && j != lc && i+j != lr+lc && lr+j != i+lc; | |
} | |
mt19937 gen; | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(false); | |
UI cases; cin >> cases; | |
for(UI casenum : IR(0, cases)) { | |
UI R, C; cin >> R >> C; | |
vector<pair<UI, UI>> v; | |
if(R <= 4 || C <= 4) { | |
bool sw = false; | |
if(R < C) { | |
swap(R, C); sw = true; | |
} | |
auto table = make_mvec(0, R, C); | |
REC(moves, bool, (UI r, UI c)) { | |
if(!table[r][c]) { | |
v.emplace_back(r, c); | |
table[r][c] = 1; | |
for(auto i: IR(0, R)) { | |
for(auto j: IR(0,C)) { | |
if(check(r, c, i, j)) | |
if(moves(i, j)) return true; | |
} | |
} | |
table[r][c] = 0; | |
v.pop_back(); | |
return false; | |
} else return v.size() == R*C; | |
}; | |
auto ver = moves(0, 0); | |
if(ver) { | |
cout << "Case #" << casenum+1 << ": POSSIBLE" << endl; | |
for(const auto& val: v) { | |
if(sw) cout << val.second+1 << ' ' << val.first+1 << endl; | |
else cout << val.first+1 << ' ' << val.second+1 << endl; | |
} | |
} else cout << "Case #" << casenum+1 << ": IMPOSSIBLE" << endl; | |
} else { | |
for(auto i: IR(0, R)) for(auto j: IR(0, C)) v.emplace_back(i, j); | |
std::uniform_int_distribution<> dis(0, R*C-1); | |
bool flag = false; | |
while(!flag) { | |
flag = true; | |
UI idx; | |
for(auto i: IR(0, R*C-1)) { | |
if(!check(v[i].first, v[i].second, v[i+1].first, v[i+1].second)) { | |
idx = i; flag = false; | |
break; | |
} | |
} | |
if(!flag) { | |
swap(v[idx], v[dis(gen)]); | |
} | |
} | |
cout << "Case #" << casenum+1 << ": POSSIBLE" << endl; | |
for(const auto& val: v) { cout << val.first+1 << ' ' << val.second+1 << endl; } | |
} | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <utility> | |
#include <tuple> | |
using namespace std; | |
struct t | |
{ | |
int n; | |
t(int n_ = 0):n(n_) { cout << "def ctor" << endl; } | |
t(const t& t_):n(t_.n) { cout << "copy ctor" << endl; } | |
t(t&& t_):n(t_.n) { cout << "move ctor" << endl; } | |
t& operator=(const t& t_) { n = t_.n; cout << "copy assign" << endl; } | |
t& operator=(t&& t_) { n = t_.n; cout << "move assign" << endl; } | |
~t() { cout << "dtor" << endl; } | |
}; | |
pair<t,t> f1() | |
{ | |
return { t{1}, t{2} }; | |
} | |
pair<t,t> f1_() | |
{ | |
pair<t,t> p{ t{1}, t{2} }; | |
return p; | |
} | |
pair<t,t> f1__() | |
{ | |
pair<t,t> p{ piecewise_construct, forward_as_tuple(1), forward_as_tuple(2) }; | |
return p; | |
} | |
t f2(t t_) | |
{ | |
return t_; | |
} | |
t f3(const t &t_) | |
{ | |
t t__(t_); | |
return t__; | |
} | |
int main(void) | |
{ | |
t t0; | |
{ | |
cout << "f1()" << endl; | |
cout << f1().first.n << endl; | |
} | |
{ | |
cout << "f1_()" << endl; | |
cout << f1_().first.n << endl; | |
} | |
{ | |
cout << "f1__()" << endl; | |
cout << f1__().first.n << endl; | |
} | |
{ | |
cout << "f2()" << endl; | |
cout << f2(t0).n << endl; | |
} | |
{ | |
cout << "f3()" << endl; | |
cout << f3(t0).n << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment