jainbot27

milkpailssilver

Jul 21st, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target("sse4")
  3. #include <bits/stdc++.h>
  4. #define f first
  5. #define s second
  6. #define pb push_back
  7. #define mp make_pair
  8. #define ts to_string
  9. #define ub upper_bound
  10. #define lb lower_bound
  11. const char nl = '\n';
  12. using namespace std;
  13. using ll = long long;
  14. using vi = vector<int>;
  15. using vl = vector<ll>;
  16. using pii = pair<int, int>;
  17. using pll = pair<ll, ll>;
  18. using str = string;
  19. //The following debugging code was taken from Benjamin Qi's template :)
  20. str ts(char c) { return str(1,c); }
  21. str ts(bool b) { return b ? "true" : "false"; }
  22. str ts(const char* s) { return (str)s; }
  23. str ts(str s) { return s; }
  24. template<class A> str ts(complex<A> c) {
  25.     stringstream ss; ss << c; return ss.str(); }
  26. str ts(vector<bool> v) {
  27.     str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
  28.     res += "}"; return res; }
  29. template<size_t SZ> str ts(bitset<SZ> b) {
  30.     str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
  31.     return res; }
  32. template<class A, class B> str ts(pair<A,B> p);
  33. template<class T> str ts(T v) {
  34.     bool fst = 1; str res = "{";
  35.     for (const auto& x: v) {
  36.         if (!fst) res += ", ";
  37.         fst = 0; res += ts(x);
  38.     }
  39.     res += "}"; return res;
  40. }
  41. template<class A, class B> str ts(pair<A,B> p) {
  42.     return "("+ts(p.f)+", "+ts(p.s)+")"; }
  43. void DBG() { cerr << "]" << endl; }
  44. template<class H, class... T> void DBG(H h, T... t) {
  45.     cerr << ts(h); if (sizeof...(t)) cerr << ", ";
  46.     DBG(t...); }
  47.  
  48. #ifdef LOCAL
  49. #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  50. #else
  51. #define dbg(...) 0
  52. #endif
  53. //CODE STARTS HERE-----------------------------------------------------------------------------------------//
  54. int x,y,k,m,d[205][205];
  55. queue<array<int,3>> q;
  56. bool check(int a, int b){
  57.     if(d[a][b] != -1)
  58.         return false;
  59.     return 1;
  60. }
  61. signed main(){
  62.     cin.tie(nullptr);
  63.     ios_base::sync_with_stdio(false);
  64.     freopen("pails.in", "r", stdin);
  65.     freopen("pails.out", "w", stdout);
  66.     memset(d, -1, sizeof(d));
  67.     cin >> x >> y >> k >> m;
  68.     q.push({0,0,0});
  69.     while(!q.empty()){
  70.         array<int, 3> cur = q.front();
  71.         if(cur[2] > k) break;
  72.         q.pop();
  73.         d[cur[0]][cur[1]] = cur[2];
  74.         if(check(cur[0], 0)){
  75.             q.push({cur[0], 0, cur[2] + 1});
  76.         }
  77.         if(check(0, cur[1])){
  78.             q.push({0, cur[1], cur[2] + 1});
  79.         }
  80.         if(check(cur[0], y)){
  81.             q.push({cur[0], y, cur[2] + 1});
  82.         }
  83.         if(check(x, cur[1])){
  84.             q.push({x, cur[1], cur[2] + 1});
  85.         }
  86.         int nextx = cur[0] + cur[1];
  87.         int nexty = (cur[0] + cur[1])-x;
  88.         nextx = (nextx>x)?x:nextx;
  89.         nexty = (nexty >= 0)?nexty:0;
  90.         if(check(nextx, nexty)){
  91.             q.push({nextx, nexty, cur[2] + 1 });
  92.         }
  93.         nextx = (cur[0] + cur[1])-y;
  94.         nexty = (cur[0] + cur[1]);
  95.         nexty = (nexty>y)?y:nexty;
  96.         nextx = (nextx >= 0)?nextx:0;
  97.         if(check(nextx, nexty)){
  98.             q.push({nextx, nexty, cur[2] + 1 });
  99.         }
  100.     }
  101.     int ans = 1e9;
  102.     for(int i =0; i<=100; i++){
  103.         for(int j = 0; j <=100; j++){
  104.             if(d[i][j] != -1 && d[i][j] <= k){
  105.                 ans = min(ans, abs(m-i-j));
  106.             }
  107.         }
  108.     }
  109.     cout << ans << nl;
  110. }
Add Comment
Please, Sign In to add comment