Guest User

Untitled

a guest
Apr 26th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #pragma comment(linker, "/STACK:268435456")
  4.  
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <cstdio>
  8. #include <deque>
  9. #include <iostream>
  10. #include <map>
  11. #include <queue>
  12. #include <set>
  13. #include <string>
  14. #include <vector>
  15.  
  16. using namespace std;
  17.  
  18. //const string IN_NAME = "input.txt";
  19. //const string OUT_NAME = "output.txt";
  20.  
  21. const string NAME = "circle";
  22. const string IN_NAME = NAME + ".in";
  23. const string OUT_NAME = NAME + ".out";
  24.  
  25. template<class T> T abs(T &x)       { return ((x) >= 0) ? (x) : (-(x)); }
  26. template<class T> T sqr(T &x)       { return (x) * (x); }
  27. //template<class T> T min(T a, T b) { return ((a) < (b)) ? (a) : (b); }
  28. //template<class T> T max(T a, T b) { return ((a) > (b)) ? (a) : (b); }
  29.  
  30. #define sz(v) ((int) ((v).size()))
  31. #define all(v) (v).begin(), (v).end()
  32. #define fori(i,n) for (int i = 0; i < (n); i++)
  33.  
  34. typedef long long ll;
  35. typedef pair<int, int> ii;
  36.  
  37. //------------------------------------------------------------------------------
  38.  
  39. const ll INF = 1000LL*1000LL*1000LL*1000LL*1000LL*1000LL;
  40.  
  41. int n, k;
  42. bool a[2005002];
  43.  
  44. deque<bool> fd, bd;
  45.  
  46. int main()
  47. {
  48.     freopen(IN_NAME.c_str(), "r", stdin);
  49.     freopen(OUT_NAME.c_str(), "w", stdout);
  50.  
  51.     scanf("%d%d", &n, &k);
  52.     for (int i = 0; i < k; i++)
  53.     {
  54.         int x;
  55.         scanf("%d", &x);
  56.         x--;
  57.         a[x] = true;
  58.     }
  59.  
  60.     ll s = 0;
  61.     for (int i = 0; i < 2*n; i++)
  62.     {
  63.         if (a[i])
  64.         {
  65.             if (0 <= i && i < n)
  66.                 s += i;
  67.             else
  68.                 s += 2*n - i;
  69.         }
  70.     }
  71.  
  72.     ll best = s;
  73.  
  74.     for (int i = 1; i <= n; i++)
  75.         fd.push_back(a[i]);
  76.     for (int i = n+1; i < 2*n; i++)
  77.         bd.push_back(a[i]);
  78.     bd.push_back(a[0]);
  79.  
  80.     ll fTrue = 0;
  81.     ll bTrue = 0;
  82.     for (int i = 0; i < sz(fd); i++)
  83.         if (fd[i]) fTrue++;
  84.     for (int i = 0; i < sz(bd); i++)
  85.         if (bd[i]) bTrue++;
  86.  
  87.     for (int i = 1; i < 2*n; i++)
  88.     {
  89.         s -= fTrue;
  90.         s += bTrue;
  91.  
  92.         if (s < 0)
  93.         {
  94.             throw 0;
  95.         }
  96.  
  97.         if (s < best)
  98.         {
  99.             best = s;
  100.         }
  101.  
  102.         fd.push_back(bd.front());
  103.         bd.pop_front();
  104.         if (fd.back())
  105.         {
  106.             fTrue++;
  107.             bTrue--;
  108.         }
  109.  
  110.         bd.push_back(fd.front());
  111.         fd.pop_front();
  112.         if (bd.back())
  113.         {
  114.             fTrue--;
  115.             bTrue++;
  116.         }
  117.     }
  118.  
  119.     cout << best;
  120. }
Add Comment
Please, Sign In to add comment