Advertisement
TrickmanOff

Untitled

Mar 27th, 2020
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.02 KB | None | 0 0
  1. #pragma optimization_level 3
  2. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
  3. #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <fstream>
  7. #include <vector>
  8. #include <queue>
  9. #include <functional>
  10. #include <set>
  11. #include <map>
  12. #include <math.h>
  13. #include <cmath>
  14. #include <string>
  15. #include <random>
  16. #include <unordered_set>
  17. #include <unordered_map>
  18. #include <bitset>
  19. #include <string.h>
  20. #include <stack>
  21. #include <assert.h>
  22. #include <list>
  23. #include <time.h>
  24. #include <memory>
  25. #include <chrono>
  26. using namespace std;
  27. //
  28. #define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
  29. //#define cin in
  30. //#define cout out
  31. #define ll long long
  32. #define db double
  33. #define ld long double
  34. #define uset unordered_set
  35. #define umap unordered_map
  36. #define F first
  37. #define S second
  38. #define ms multiset
  39. #define pb push_back
  40. #define pq priority_queue
  41. #define umap unordered_map
  42. #define uset unordered_set
  43. #define ull unsigned long long
  44. #define pii pair<int, int>
  45. #define pll pair<ll, ll>
  46. #define pdd pair<ld, ld>
  47. #define pnn pair<Node*, Node*>
  48. #define uid uniform_int_distribution
  49. #define PI acos(-1.0)
  50. //#define sort(a, b) sort(a.begin(), a.end(), b())
  51. //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  52. ifstream in("input.txt");
  53. ofstream out("output.txt");
  54.  
  55. struct Q {
  56.     int l, r, pos;
  57. };
  58.  
  59. vector<Q> f_arcs;
  60. vector<int> ans;
  61. int n, m;
  62.  
  63. bool check() {
  64.     //1/-1 - '0', 2/-2 - '1'
  65.     //{pos, -2, -1/+1, +2}
  66.     vector<pii> line;
  67.     for (int i = 0; i < m; i++) {
  68.         Q q = f_arcs[i];
  69.         int t = (ans[i] == 0 ? 1 : 2);
  70.         if (q.l > q.r)
  71.             line.push_back({ 1, t });
  72.         line.push_back({ q.l, t });
  73.         line.push_back({ q.r + 1, -t });
  74.     }
  75.     sort(line.begin(), line.end());
  76.     int cnt0 = 0, cnt1 = 0;
  77.     for (int i = 1, p = 0; i <= n; i++) {
  78.         while (p < line.size() && line[p].first == i) {
  79.             if (abs(line[p].second) == 2)
  80.                 cnt1 += 0.5 * line[p].second;
  81.             else
  82.                 cnt0 += line[p].second;
  83.             p++;
  84.         }
  85.         if (cnt0 == 0 || cnt1 == 0) return 0;
  86.     }
  87.     return 1;
  88. }
  89.  
  90. bool is_bit(int a, int p) {
  91.     return (a >> p) % 2;
  92. }
  93.  
  94. bool solve() {
  95.     for (int mask = 0; mask < (1 << m); mask++) {
  96.         for (int i = 0; i < m; i++)
  97.             ans[i] = is_bit(mask, i);
  98.         if (check()) {
  99.            // for (int x : ans)
  100.                 //cout << x;
  101.            // cout << '\n';
  102.             return 1;
  103.         }
  104.     }
  105.     return 0;
  106. }
  107.  
  108. vector<Q> arcs;
  109.  
  110. void gen() {
  111.     n = rand() % 5 + 2;
  112.     m = rand() % 5 + 2;
  113.     arcs.clear();
  114.     f_arcs.clear();
  115.     for (int i = 0; i < m; i++)
  116.         arcs.push_back({ rand() % n + 1, rand() % n + 1, i });
  117. }
  118.  
  119. bool my_sol() {
  120.     int b_dist = -1, b_num = -1;
  121.     for (int i = 0; i < m; i++) {
  122.         int l = arcs[i].l, r = arcs[i].r;
  123.         int dist = (l > r ? n - l + r : r - l);
  124.         if (dist > b_dist) {
  125.             b_dist = dist;
  126.             b_num = i;
  127.         }
  128.     }
  129.     ans.resize(m, 0);
  130.  
  131.     ////////
  132.     int d = arcs[b_num].l;
  133.     for (Q& q : arcs) {
  134.         q.l = ((q.l - d + n) % n) + 1;
  135.         q.r = ((q.r - d + n) % n) + 1;
  136.     }
  137.     //
  138.     f_arcs = arcs;
  139.  
  140.     vector<Q> spec;
  141.  
  142.     for (Q q : arcs) {
  143.         if (q.l > q.r)
  144.             spec.push_back(q);
  145.     }
  146.  
  147.     int m1 = m;
  148.     {
  149.         int p = 0;
  150.         for (int i = 0; i < m; i++) {
  151.             if (arcs[i].l <= arcs[i].r)
  152.                 arcs[p++] = arcs[i];
  153.         }
  154.         arcs.resize(p);
  155.         m1 = p;
  156.     }
  157.     //
  158.     sort(arcs.begin(), arcs.end(), [&](const Q a, const Q b) {return a.l < b.l; });
  159.  
  160.     for (int ch = -1; ch < (int)spec.size(); ch++) {
  161.         fill(ans.begin(), ans.end(), 0);
  162.         ans[b_num] = 1;
  163.  
  164.         int p[2] = { 0, 0 }, fin[2] = { n, n };
  165.         for (int i = 0; i < spec.size(); i++) {
  166.             ans[spec[i].pos] = (i == ch);
  167.             p[i == ch] = max(spec[i].r, p[i == ch]);
  168.             fin[i == ch] = min(spec[i].l - 1, fin[i == ch]);
  169.         }
  170.  
  171.         p[1] = max(p[1], f_arcs[b_num].r);
  172.  
  173.         int vp = 0;
  174.         //{r, num}
  175.         pq<pii> q;
  176.         while (p[0] < fin[0] || p[1] < fin[1]) {
  177.             int k;
  178.             if (p[0] >= fin[0]) k = 1;
  179.             else if (p[1] >= fin[1]) k = 0;
  180.             else k = (p[0] <= p[1] ? 0 : 1);
  181.  
  182.             while (vp < m1 && arcs[vp].l <= p[k] + 1) {
  183.                 if (arcs[vp].pos != b_num)
  184.                     q.push({ arcs[vp].r, arcs[vp].pos });
  185.                 vp++;
  186.             }
  187.  
  188.             if (q.empty()) break;
  189.  
  190.             pii x;
  191.             if (q.size() >= 2) {
  192.                 pii tmp = q.top();
  193.                 q.pop();
  194.                 x = q.top();
  195.                 if (x.first >= fin[k]) {
  196.                     q.pop();
  197.                     q.push(tmp);
  198.                     ans[x.second] = k;
  199.                     p[k] = x.first;
  200.                     continue;
  201.                 }
  202.                 else
  203.                     q.push(tmp);
  204.             }
  205.  
  206.             if (p[!k] < fin[!k] && fin[!k] > fin[k] && q.top().first >= fin[!k] && q.size() >= 2) {
  207.                 if (q.size() == 1) break;
  208.                 pii tmp = q.top();
  209.                 q.pop();
  210.                 x = q.top();
  211.                 q.pop();
  212.                 q.push(tmp);
  213.             }
  214.             else {
  215.                 x = q.top();
  216.                 q.pop();
  217.             }
  218.  
  219.             ans[x.second] = k;
  220.             p[k] = x.first;
  221.         }
  222.         if (p[0] >= fin[0] && p[1] >= fin[1])
  223.             return 1;
  224.     }
  225.     return 0;
  226. }
  227.  
  228. int main() {
  229.     srand(time(0));
  230.     for (int cnt = 0;; cnt++) {
  231.         gen();
  232.         if (cnt % 10000 == 0)
  233.             cout << "=";
  234.        
  235.         if (!my_sol() && solve()) {
  236.             cout << "\n";
  237.             cout << n << ' ' << m << '\n';
  238.             for (Q q : f_arcs)
  239.                 cout << q.l << ' ' << q.r << '\n';
  240.             return 0;
  241.         }
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement