Advertisement
matheus_monteiro

Clock Pictures

Nov 4th, 2023 (edited)
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. #include <iomanip>
  3.  
  4. /*
  5.  * Author: Matheus Monteiro
  6.  */
  7.  
  8. using namespace std;
  9.  
  10. // #ifdef DEBUGMONTEIRO
  11. #define _ << " , " <<
  12. #define bug(x) cout << #x << "  >>>>>>>  " << x << endl;
  13. // #else
  14. // #define bug(x)
  15. // #endif
  16.  
  17. #define int long long
  18. #define Max(a, b) (a > b ? a : b)
  19. #define Min(a, b) (a < b ? a : b)
  20. #define ii pair<int, int>
  21. #define f first
  22. #define s second
  23. #define vi vector<int>
  24. #define vii vector<ii>
  25. #define LSOne(S) ((S) & (-S))
  26. #define UNTIL(t) while (clock() < (t) * CLOCKS_PER_SEC)
  27. #define SZ(a) (int)a.size()
  28. #define fastio std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);
  29.  
  30. const int MAX = 200005;
  31. const int MOD = 1000000007; //10^9 + 7
  32. const int OO = 0x3f3f3f3f; // 0x3f3f3f3f;
  33. const double EPS = 1e-9;  //10^-9
  34. std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
  35.  
  36. vector<int> kmpPreProcess(const vector<int> &t) {
  37.   int i = 0;
  38.   int j = -1;
  39.  
  40.   vector<int> b(t.size() + 1);
  41.   b[0] = -1;
  42.  
  43.   while(i < t.size()) {
  44.     while(j >= 0 && t[i] != t[j]) j = b[j];
  45.     ++i; ++j;
  46.     b[i] = j;
  47.   }
  48.  
  49.   return b;
  50. }
  51.  
  52. bool kmp(const vector<int> &s, const vector<int> &t) {
  53.   vector<int> b = kmpPreProcess(t);
  54.   int i = 0;
  55.   int j = 0;
  56.  
  57.   while(i < s.size()) {
  58.     while(j >= 0 && s[i] != t[j]) j = b[j];
  59.     ++i; ++j;
  60.     if(j == t.size()) return true;
  61.   }
  62.   return false;
  63. }
  64.  
  65. void solve() {
  66.   int n;
  67.   cin >> n;
  68.  
  69.   vector<int> firstClock(n);
  70.   for(int &w : firstClock) cin >> w;
  71.  
  72.   vector<int> secondClock(n);
  73.   for(int &w : secondClock) cin >> w;
  74.  
  75.   sort(firstClock.begin(), firstClock.end());
  76.   sort(secondClock.begin(), secondClock.end());
  77.  
  78.   vector<int> firstDegreeDiff;
  79.   vector<int> secondDegreeDiff;
  80.  
  81.   for(int i = 0; i < n; ++i)
  82.     firstDegreeDiff.push_back( ((firstClock[(i + 1) % n] - firstClock[i]) + 360000) % 360000  );
  83.  
  84.   for(int i = 0; i < n; ++i)
  85.     secondDegreeDiff.push_back( ((secondClock[(i + 1) % n] - secondClock[i]) + 360000) % 360000  );
  86.  
  87.   for(int i = 0; i < n; ++i) firstDegreeDiff.push_back(firstDegreeDiff[i]);
  88.  
  89.   if(kmp(firstDegreeDiff, secondDegreeDiff)) {
  90.     cout << "possible\n";
  91.   } else {
  92.     cout << "impossible\n";
  93.   }
  94. }
  95.  
  96. int32_t main() {
  97.   fastio;
  98.  
  99.   int t = 1;
  100.   //std::cin >> t;
  101.   for(int caso = 1; caso <= t; ++caso) {
  102.     //cout << "Case #" << caso << ": ";
  103.     solve();
  104.   }
  105.  
  106.   return 0;
  107. }
  108.  
  109. /*
  110. Before submit:
  111.     Check the corners cases
  112.     Check solution restrictions
  113.  
  114. For implementation solutions:
  115.     Check the flow of the variables
  116.  
  117. For intervals problems:
  118.     Think about two pointers
  119.  
  120. For complete search:
  121.     Think about cuts
  122.  
  123. If you get WA:
  124.     Reread your code looking for stupid typos
  125.     Try various manual testcases
  126.     Recheck the correctness of your algorithm
  127.     Reread the statement
  128.     Write a straightforward solution, create lots of testcases by yourself, and compare both solutions
  129.     Generate random test cases and perform a stress test, comparing your solution with the one that you are sure is correct
  130.     Change the coder (if you're with a team)
  131.     Give up. You may have other tasks to solve.
  132. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement