Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #pragma comment(linker, "/STACK:268435456")
- #include <algorithm>
- #include <cmath>
- #include <cstdio>
- #include <deque>
- #include <iostream>
- #include <map>
- #include <queue>
- #include <set>
- #include <string>
- #include <vector>
- using namespace std;
- //const string IN_NAME = "input.txt";
- //const string OUT_NAME = "output.txt";
- const string NAME = "circle";
- const string IN_NAME = NAME + ".in";
- const string OUT_NAME = NAME + ".out";
- template<class T> T abs(T &x) { return ((x) >= 0) ? (x) : (-(x)); }
- template<class T> T sqr(T &x) { return (x) * (x); }
- //template<class T> T min(T a, T b) { return ((a) < (b)) ? (a) : (b); }
- //template<class T> T max(T a, T b) { return ((a) > (b)) ? (a) : (b); }
- #define sz(v) ((int) ((v).size()))
- #define all(v) (v).begin(), (v).end()
- #define fori(i,n) for (int i = 0; i < (n); i++)
- typedef long long ll;
- typedef pair<int, int> ii;
- //------------------------------------------------------------------------------
- const ll INF = 1000LL*1000LL*1000LL*1000LL*1000LL*1000LL;
- int n, k;
- bool a[2005002];
- deque<bool> fd, bd;
- int main()
- {
- freopen(IN_NAME.c_str(), "r", stdin);
- freopen(OUT_NAME.c_str(), "w", stdout);
- scanf("%d%d", &n, &k);
- for (int i = 0; i < k; i++)
- {
- int x;
- scanf("%d", &x);
- x--;
- a[x] = true;
- }
- ll s = 0;
- for (int i = 0; i < 2*n; i++)
- {
- if (a[i])
- {
- if (0 <= i && i < n)
- s += i;
- else
- s += 2*n - i;
- }
- }
- ll best = s;
- for (int i = 1; i <= n; i++)
- fd.push_back(a[i]);
- for (int i = n+1; i < 2*n; i++)
- bd.push_back(a[i]);
- bd.push_back(a[0]);
- ll fTrue = 0;
- ll bTrue = 0;
- for (int i = 0; i < sz(fd); i++)
- if (fd[i]) fTrue++;
- for (int i = 0; i < sz(bd); i++)
- if (bd[i]) bTrue++;
- for (int i = 1; i < 2*n; i++)
- {
- s -= fTrue;
- s += bTrue;
- if (s < 0)
- {
- throw 0;
- }
- if (s < best)
- {
- best = s;
- }
- fd.push_back(bd.front());
- bd.pop_front();
- if (fd.back())
- {
- fTrue++;
- bTrue--;
- }
- bd.push_back(fd.front());
- fd.pop_front();
- if (bd.back())
- {
- fTrue--;
- bTrue++;
- }
- }
- cout << best;
- }
Add Comment
Please, Sign In to add comment