Advertisement
ec1117

Untitled

Feb 5th, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.22 KB | None | 0 0
  1. Employment - 1214F
  2.  
  3. First, let's notice that the optimal answer can be achieved without changing the relative order of candidates. That means that if we order candidates by circle clockwise, the second candidate will work at the next clockwise workplace from the first candidate's workplace, the third candidate will work at the next clockwise workplace from the second candidate's workplace and so on. Let's prove it. If in optimal answer the order has changed, then there should be 2 candidates, so that the first of them lives earlier clockwise then the second and works at workplace, which is further. If we swap their workplaces, the distance between home and workplace for each of them will either stay the same or decrease. So, doing this swaps, we can achieve the situation, when the relative order of candidates stay the same.
  4.  
  5. Now we can come up with simple O(n2) solution. Let's first sort all candidates and workplaces by their city number. Let's select some workplace for the first candidate. Because in the optimal answer the order of candidates will not change, for each candidate we know his workplace. Now in O(n) time we can calculate the total distance. And because there are n possible workplaces for the first candidate, the solution works in O(n2) time.
  6.  
  7. To solve problem faster, let's notice, that if some candidate lives in city with number x and his workplace has number y, the the total distance from home to work for him will be:
  8.  
  9. −x+y+m if y<x−m/2
  10. x−y if x−m/2≤y<x
  11. −x+y if x≤y<x+m/2
  12. x−y+m if x+m/2≤y
  13. So for each candidate we have at most 4 intervals of workplaces positions, at which the sign before the candidate's home position in the distance formula stays the same. The same way for each workplace we have at most 4 intervals of candidates positions, where the sign before the workplace position in distance formula stays the same. Also, there are 4 intervals of candidates positions, where we need to add m to the distance formula. Because the relative order of candidates stays the same, we can iterate over all possible workplaces for the first candidate and check the total distance in each variant. When we move first candidate workplace to the next, for some candidates and workplaces their distance formula can change, but for each of them it can change no more then 4 times. So we will totally do no more then 8n changes. All in all we will check all distances in O(nlogn) time (we have additional logarithm because of sorting).
  14.  
  15. #include "bits/stdc++.h"
  16. using namespace std;
  17. using ll = long long;
  18. using uint = unsigned int;
  19. using db = long double;
  20. using ii = pair<int,int>;
  21. const int N =1e6+5, MOD = 1e9 + 7;
  22. ll sum[N];
  23. int ans[N], m, n;
  24. void add(int l, int r, ll val){
  25.   if(l <= r){
  26.     sum[l] += val;
  27.     sum[r+1] -= val;
  28.   }
  29. }
  30. int32_t main(){
  31. #ifdef ONLINE_JUDGE
  32.     ios_base::sync_with_stdio(0);
  33.     cin.tie(0);
  34. #endif
  35.  
  36.   cin >> m >> n;
  37.  
  38.   vector<pair<ll, ll>> a(n), b;
  39.  
  40.   for(int i = 0; i < n; i++){
  41.     cin >> a[i].first;
  42.     a[i].second = i + 1;
  43.   }
  44.   for(int i = 0, x; i < n; i++){
  45.     cin >> x;
  46.     b.push_back(make_pair(x,i + 1));
  47.     b.push_back(make_pair(x-m,i + 1));
  48.     b.push_back(make_pair(x+m,i + 1));
  49.   }
  50.  
  51.   sort(b.begin(),b.end());
  52.   sort(a.begin(), a.end());
  53.   b.insert(b.begin(), make_pair(-1e16, 0));
  54.   a.insert(a.begin(), make_pair(-1e16, 0));
  55.   b.push_back(make_pair(1e16,0));
  56.   a.push_back(make_pair(1e16,0));
  57.  
  58.   for(int i = 1; i <= n; i++){
  59.     int j = upper_bound(b.begin(), b.end(), make_pair(a[i].first, LLONG_MAX)) - b.begin();
  60.     int d = j - i;
  61.         add(0, d - 1, a[i].first);
  62.  
  63.         add(d, 2 * n, -a[i].first);
  64.   }
  65.   for(int i = 1; i <= n+n+n; i++){
  66.     int j = lower_bound(a.begin(), a.end(), make_pair(b[i].first, LLONG_MIN)) - a.begin() - 1;
  67.     int d = i-j;
  68.         add(max(0, i - n), min(d - 1, i - 1), -b[i].first);
  69.         add(max(d, i - n), min(2 * n, i - 1), b[i].first);
  70.   }
  71.  
  72.   ll mn = LLONG_MAX, cur = 0;
  73.   int idx = -1;
  74.   for(int i = 0; i <= n + n; i++){
  75.     cur += sum[i];
  76.     if(cur < mn){
  77.       mn = cur;
  78.       idx = i;
  79.     }
  80.   }
  81.  
  82.   cout << mn << '\n';
  83.   for(int i = 1; i <= n; i++)
  84.     ans[a[i].second] = b[i + idx].second;
  85.   for(int i = 1; i <= n; i++)
  86.     cout << ans[i] << " \n"[i==n];
  87.  
  88.  
  89.   return 0;
  90. }
  91.  
  92. #include <cstdio>
  93. #include <algorithm>
  94.  
  95. using namespace std;
  96.  
  97. using ll = long long;
  98. const int MN = 2e5 + 100;
  99.  
  100. int N, M;
  101. ll F = 1e18, f;
  102. ll p[MN*3];//equality = b-a
  103. int g;
  104. struct W
  105. {
  106. public:
  107.     ll v; int i;
  108.     void in(int i) {scanf("%lld", &v); W::i=i;}
  109.     bool operator < (W o) const {return v < o.v;}
  110.     bool operator < (ll x) const {return v < x;}
  111. } A[MN], B[MN];
  112. bool operator < (ll x, W y) {return x < y.v;}
  113. int z[MN];
  114.  
  115. int main(void)
  116. {
  117.     scanf("%d%d", &M, &N);
  118.     for(int i = 0;i < N;++i) A[i].in(i);
  119.     for(int i = 0;i < N;++i) B[i].in(i);
  120.     sort(A, A+N), sort(B, B+N);
  121.     for(int i = 0;i < N;++i)
  122.     {
  123.         p[0] += M-A[i].v;
  124.         p[i+1] -= M;
  125.         p[i+N-(std::lower_bound(B, B+N, A[i].v)-B)+1] += A[i].v*2;
  126.         p[i+N+1] += M;
  127.     }
  128.     for(int i = 0;i < N;++i)
  129.         p[0] += B[i].v, p[N-i+(std::upper_bound(A, A+N, B[i].v)-A)]-=B[i].v*2;
  130.     for(int i = 0;i <= N*2;++i)
  131.         if((f+=p[i])<F)
  132.             F=f, g=i;
  133.     printf("%lld\n", F);
  134.     for(int i = 0;i < N;++i)
  135.         z[A[i].i] = B[(i+N*4-g)%N].i;
  136.     for(int i = 0;i < N;i++)
  137.         printf("%d%c", z[i]+1, " \n"[i==N-1]);
  138.     return 0;
  139. }
  140.  
  141.  
  142. int m,n,ans[MX];
  143. vpi a, b;
  144.  
  145. ll eval(int x) {
  146.     ll ans = 0;
  147.     F0R(i,n) ans += abs(a[x+i].f-b[i].f);
  148.     return ans;
  149. }
  150.  
  151. int main() {
  152.     setIO(); re(m,n); a.rsz(n); b.rsz(n);
  153.     F0R(i,n) {
  154.         re(a[i].f);
  155.         a[i].s = i+1;
  156.     }
  157.     F0R(i,n) {
  158.         re(b[i].f);
  159.         b[i].s = i+1;
  160.     }
  161.     sort(all(a));
  162.     sort(all(b));
  163.     a.rsz(3*n);
  164.     F0R(i,3*n) {
  165.         if (i < n) a[i].f -= m;
  166.         else {
  167.             a[i] = a[i-n];
  168.             a[i].f += m;
  169.         }
  170.     }
  171.     int lo = 0, hi = 2*n;
  172.     while (lo < hi) {
  173.         int mid = (lo+hi)/2;
  174.         if (eval(mid) < eval(mid+1)) hi = mid;
  175.         else lo = mid+1;
  176.     }
  177.     ps(eval(lo));
  178.     F0R(i,n) ans[a[lo+i].s] = b[i].s;
  179.     FOR(i,1,n+1) pr(ans[i],' ');
  180.     /*F0R(i,2*n+1) ps(eval(i));
  181.     ps(a,b);*/
  182. }
  183.  
  184. #include <bits/stdc++.h>
  185. using namespace std;
  186.  
  187. using ll = long long;
  188.  
  189. int main(){
  190.     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  191.     ll m;
  192.     ll n;
  193.     cin >> m >> n;
  194.     vector<ll> _a(n);
  195.     vector<ll> _b(n);
  196.     vector<ll> a(n); vector<ll> b(n);
  197.     map<ll, set<ll> > ax, bx;
  198.     for(int i = 0; i < n; i++){
  199.         cin >> a[i];
  200.         a[i]--;
  201.         a[i] *= 2;
  202.         _a[i] = a[i];
  203.         ax[_a[i]].insert(i);
  204.     }
  205.     for(int i = 0; i < n; i++){
  206.         cin >> b[i];
  207.         b[i]--;
  208.         b[i] *= 2;
  209.         _b[i] = b[i];
  210.         bx[_b[i]].insert(i);
  211.     }
  212.     m *= 2;
  213.     sort(a.begin(), a.end());
  214.     sort(b.begin(), b.end());
  215.     vector<pair<ll, ll> > events;
  216.     for(int i = 0; i < n; i++){
  217.         events.push_back({a[i], 0});
  218.         events.push_back({b[i], 1});
  219.         events.push_back({a[i]+m, 0});
  220.         events.push_back({b[i]+m, 1});
  221.     }
  222.     sort(events.begin(), events.end());
  223.  
  224.     ll pos_sum = 0;
  225.     ll neg_sum = 0;
  226.     ll cur_ans = 0;
  227.  
  228.     map<ll, ll> freq;
  229.     ll counter = 0;
  230.  
  231.     ll cpsum = 0;
  232.     for(int i = 0; i < 2*n; i++){
  233.         if(events[i].second == 0){
  234.             cpsum += 1;
  235.         } else {
  236.             cpsum -= 1;
  237.         }
  238.         ll diff = events[i+1].first - events[i].first;
  239.         freq[cpsum] += diff;
  240.         if(cpsum > 0){
  241.             pos_sum += diff;
  242.         }
  243.         if(cpsum < 0){
  244.             neg_sum += diff;
  245.         }
  246.         cur_ans += abs(cpsum) * diff;
  247.     }
  248.     assert(cpsum == 0);
  249.     ll ans = 2e18;
  250.     ll ans_idx = -1;
  251.     for(int i = 0; i < 2*n; i++){
  252.         if(cur_ans < ans){
  253.             ans = cur_ans;
  254.             ans_idx = i;
  255.         }
  256.         if(events[i].second == 0){
  257.             // subtract one from all
  258.             neg_sum += freq[counter];
  259.             cur_ans += neg_sum;
  260.             counter += 1;
  261.             cur_ans -= pos_sum;
  262.             pos_sum -= freq[counter];
  263.         } else {
  264.             // add one from all
  265.             pos_sum += freq[counter];
  266.             cur_ans += pos_sum;
  267.             counter -= 1;
  268.             cur_ans -= neg_sum;
  269.             neg_sum -= freq[counter];
  270.         }
  271.     }
  272.     cout << ans / 2 << '\n';
  273.     vector<ll> as, bs;
  274.     for(ll _i = ans_idx; _i < ans_idx + 2*n; _i++){
  275.         ll i = _i % (2 * n);
  276.         if(events[i].second == 0){
  277.             ll r = events[i].first % m;
  278.             as.push_back(*ax[r].begin());
  279.             ax[r].erase(ax[r].begin());
  280.         } else {
  281.             ll r = events[i].first % m;
  282.             bs.push_back(*bx[r].begin());
  283.             bx[r].erase(bx[r].begin());
  284.         }
  285.     }
  286.     vector<ll> pp(n, -1);
  287.     for(ll i = 0; i < n; i++){
  288.         pp[as[i]] = bs[i];
  289.     }
  290.     for(ll i = 0; i < n; i++){
  291.         cout << pp[i] + 1 << ' ';
  292.     }
  293.     cout << '\n';
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement