Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize ("O3")
- #pragma GCC target("sse4")
- #include <bits/stdc++.h>
- #define f first
- #define s second
- #define pb push_back
- #define mp make_pair
- #define ts to_string
- #define ub upper_bound
- #define lb lower_bound
- const char nl = '\n';
- using namespace std;
- using ll = long long;
- using vi = vector<int>;
- using vl = vector<ll>;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using str = string;
- //The following debugging code was taken from Benjamin Qi's template :)
- str ts(char c) { return str(1,c); }
- str ts(bool b) { return b ? "true" : "false"; }
- str ts(const char* s) { return (str)s; }
- str ts(str s) { return s; }
- template<class A> str ts(complex<A> c) {
- stringstream ss; ss << c; return ss.str(); }
- str ts(vector<bool> v) {
- str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
- res += "}"; return res; }
- template<size_t SZ> str ts(bitset<SZ> b) {
- str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
- return res; }
- template<class A, class B> str ts(pair<A,B> p);
- template<class T> str ts(T v) {
- bool fst = 1; str res = "{";
- for (const auto& x: v) {
- if (!fst) res += ", ";
- fst = 0; res += ts(x);
- }
- res += "}"; return res;
- }
- template<class A, class B> str ts(pair<A,B> p) {
- return "("+ts(p.f)+", "+ts(p.s)+")"; }
- void DBG() { cerr << "]" << endl; }
- template<class H, class... T> void DBG(H h, T... t) {
- cerr << ts(h); if (sizeof...(t)) cerr << ", ";
- DBG(t...); }
- #ifdef LOCAL
- #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
- #else
- #define dbg(...) 0
- #endif
- //CODE STARTS HERE-----------------------------------------------------------------------------------------//
- int x,y,k,m,d[205][205];
- queue<array<int,3>> q;
- bool check(int a, int b){
- if(d[a][b] != -1)
- return false;
- return 1;
- }
- signed main(){
- cin.tie(nullptr);
- ios_base::sync_with_stdio(false);
- freopen("pails.in", "r", stdin);
- freopen("pails.out", "w", stdout);
- memset(d, -1, sizeof(d));
- cin >> x >> y >> k >> m;
- q.push({0,0,0});
- while(!q.empty()){
- array<int, 3> cur = q.front();
- if(cur[2] > k) break;
- q.pop();
- d[cur[0]][cur[1]] = cur[2];
- if(check(cur[0], 0)){
- q.push({cur[0], 0, cur[2] + 1});
- }
- if(check(0, cur[1])){
- q.push({0, cur[1], cur[2] + 1});
- }
- if(check(cur[0], y)){
- q.push({cur[0], y, cur[2] + 1});
- }
- if(check(x, cur[1])){
- q.push({x, cur[1], cur[2] + 1});
- }
- int nextx = cur[0] + cur[1];
- int nexty = (cur[0] + cur[1])-x;
- nextx = (nextx>x)?x:nextx;
- nexty = (nexty >= 0)?nexty:0;
- if(check(nextx, nexty)){
- q.push({nextx, nexty, cur[2] + 1 });
- }
- nextx = (cur[0] + cur[1])-y;
- nexty = (cur[0] + cur[1]);
- nexty = (nexty>y)?y:nexty;
- nextx = (nextx >= 0)?nextx:0;
- if(check(nextx, nexty)){
- q.push({nextx, nexty, cur[2] + 1 });
- }
- }
- int ans = 1e9;
- for(int i =0; i<=100; i++){
- for(int j = 0; j <=100; j++){
- if(d[i][j] != -1 && d[i][j] <= k){
- ans = min(ans, abs(m-i-j));
- }
- }
- }
- cout << ans << nl;
- }
Add Comment
Please, Sign In to add comment