Advertisement
Korotkodul

N4 100 из 100 stress

Oct 25th, 2022 (edited)
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int, int>
  11. #define pb(x) push_back(x)
  12. #include <ctime>
  13. using namespace std;
  14. using ll = long long;
  15. using ld = long double;
  16. using db = double;
  17. void cv(vector <int> &v) {
  18.     for (auto x : v) cout << x << ' ';
  19.     cout << "\n";
  20. }
  21.  
  22. void cvl(vector <ll> &v) {
  23.     for (auto x : v) cout << x << ' ';
  24.     cout << "\n";
  25. }
  26.  
  27.  
  28. void cvv(vector <vector <int> > &v) {
  29.     for (auto x : v) cv(x);
  30.     cout << "\n";
  31. }
  32.  
  33. void cvb(vector <bool> v) {
  34.     for (bool x : v) cout << x << ' ';
  35.     cout << "\n";
  36. }
  37.  
  38. void cvs(vector <string>  v) {
  39.     for (auto a : v) {
  40.         cout << a << "\n";
  41.     }
  42. }
  43.  
  44. void cvp(vector <pii> a) {
  45.     for (auto p : a) {
  46.         cout << p.first << ' ' << p.second << "\n";
  47.     }
  48.     cout << "\n";
  49. }
  50.  
  51. ll S(ll a1, ll d, ll n) {
  52.     return (2 * a1 + d * (n - 1) ) * n / 2;
  53. }
  54.  
  55.  
  56. ll frx, tox, fry, toy;
  57.  
  58. bool ok(int i, int j) {
  59.     return i >= frx && i <= tox && j >= fry && j <= toy;
  60. }
  61. vector <int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
  62.  
  63. bool sh = 0;
  64. ll n, m;
  65. // 10 11
  66. ll dull () {
  67.     ios::sync_with_stdio(0);
  68.     cin.tie(0);
  69.     cout.tie(0);
  70.     frx = -2;
  71.     tox = n - 1;
  72.     fry = 0;
  73.     toy = m - 1;
  74.     ll cnt = 1;
  75.     vector <vector <int> > a(n, vector <int> (m, 0));
  76.     /*if (min(n, m) == 1) {
  77.         cout << n * m;
  78.         exit(0);
  79.     }*/
  80.     a[0][0] = 1;
  81.     int i = 0, j = 0, to = 0;
  82.     ll k = 0;
  83.     while (1) {
  84.         //cnt +=
  85.         while ( ok(i + dx[to], j + dy[to]) ) {
  86.             i += dx[to];
  87.             j += dy[to];
  88.             a[i][j] = 1;
  89.             cnt++;
  90.         }
  91.         /*if (sh) {
  92.             cout << "to = " << to << "\n";
  93.             cout << "frx tox = " << frx << ' ' << tox << "\n";
  94.             cout << "fry toy = " << fry << ' ' << toy << "\n";
  95.         }*/
  96.         k++;
  97.         if (to == 0) {
  98.             frx += 2;
  99.             //tox -= 2;
  100.         }
  101.         else if (to == 2) {
  102.             tox -= 2;
  103.         }
  104.         else if (to == 1) {
  105.             fry += 2;
  106.         }
  107.         else if (to == 3) {
  108.             toy -= 2;
  109.         }
  110.         to++;
  111.         to %= 4;
  112.         /*if (sh) {
  113.             cout << "to = " << to << "\n";
  114.             cout << "i + dx[to] j + dy[to] = " << i + dx[to] << ' ' << j + dy[to] << "\n";
  115.         }*/
  116.         if (!ok(i + dx[to], j + dy[to])) {
  117.             break;
  118.         }
  119.     }
  120.     /*if (sh) {
  121.         cout << "i j = " << i << ' ' << j << "\n";
  122.         cout << "to = " << to << "\n";
  123.         cout << "ans\n";
  124.     }*/
  125.  
  126.     if (sh) {
  127.         cvv(a);
  128.     }
  129.     if (sh) {
  130.         cout << "dull k = " << k << "\n\n\n";
  131.     }
  132.     return cnt;
  133. }
  134.  
  135. bool ok(ll k) {
  136.     ll resx, resy;
  137.     ll gox = k / 2 + k % 2;
  138.     if (gox == 1) {
  139.         resx = n;
  140.     } else {
  141.         resx = n - (1 + 2 * (gox - 2));
  142.     }
  143.  
  144.     ll goy = k / 2;
  145.     resy = m - (1 + 2 * (goy - 1));
  146.     if (sh) {
  147.         cout << "check\n";
  148.         cout << "k = " << k << "\n";
  149.         cout << "gox goy = " << gox << ' ' << goy << "\n";
  150.         cout << "resx resy = " << resx << ' ' << resy << "\n";
  151.         cout << "\n";
  152.     }
  153.  
  154.     if (k % 2 == 0 && resx < 2) {
  155.         return 0;
  156.     }
  157.     if (k % 2 == 1 && resy < 2) {
  158.         return 0;
  159.     }
  160.  
  161.     bool r = min(resy, resx) >= 0;
  162.     return r;
  163. }
  164.  
  165. ll get(ll k) {
  166.     ll gox = k / 2 + k % 2;
  167.     ll goy = k / 2;
  168.     ll Sn, Sm;
  169.     Sn = S(n - 1, -2, gox - 1) + n;
  170.     Sm = S(m - 1, -2, goy);
  171.     ll r = Sn + Sm;
  172.     if (sh) {
  173.         cout << "get\n";
  174.         cout << "gox goy = " << gox << ' ' << goy << "\n";
  175.         cout << "Sn Sm = " << Sn << ' ' << Sm << "\n";
  176.         cout << "r = " << r << "\n";
  177.     }
  178.  
  179.     return r;
  180. }
  181.  
  182. ll my() {
  183.     if (min(n, m) == 1) {
  184.         return n * m;
  185.     }
  186.     ll L = 0, R = max(n, m) * 2, med;
  187.     while (L + 1 < R) {
  188.         med = (L + R) / 2;
  189.         if (sh) {
  190.             cout << "med = " << med << "\n";
  191.         }
  192.         if (ok(med)) {
  193.             L = med;
  194.         } else {
  195.             R = med;
  196.         }
  197.     }
  198.  
  199.     if (sh) {
  200.         cout << "L = " << L << "\n";
  201.         cout << "\n\n";
  202.     }
  203.  
  204.     ll ans = get(L);
  205.     return ans;
  206. }
  207.  
  208. bool random = 1;
  209. // 10 20
  210. int main() {
  211.     /*cin >> n >> m;
  212.     ll he = dull();
  213.     ll me = my();
  214.     cout << "n m = " << n << ' ' << m << "\n";
  215.     cout << "me he = " << me << ' '<< he << "\n";
  216.     if (me == he) {
  217.         cout << "OK\n";
  218.     }
  219.     else {
  220.  
  221.         cout << "WA\n";
  222.     }*/
  223.     if (random) {
  224.         srand(time(0));
  225.         bool al = 1;
  226.         int p=0;
  227.         while (p < 1e2) {
  228.             p++;
  229.             n = rand() % 30 + 1;
  230.             m = rand() % 30 + 1;
  231.             ll he = dull();
  232.             ll me = my();
  233.             if (me == he) {
  234.                 cout << "OK\n";
  235.                 cout << "n m = " << n << ' ' << m << "\n";
  236.             }
  237.             else {
  238.                 al = 0;
  239.                 cout << "WA\n";
  240.                 cout << "n m = " << n << ' ' << m << "\n";
  241.                 break;
  242.             }
  243.         }
  244.         cout << "al = " << al << "\n";
  245.     }
  246.     else {
  247.         cin >> n >> m;
  248.         ll he = dull();
  249.         ll me = my();
  250.         if (me == he) {
  251.             cout << "OK\n";
  252.         } else {
  253.             cout << "WA\n";
  254.             cout << "me he = " << me << ' ' << he << "\n";
  255.         }
  256.     }
  257. }
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement