QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#378501#8574. Swirly Sortucup-team087#WA 8ms4116kbC++142.9kb2024-04-06 13:14:302024-04-06 13:14:32

Judging History

你现在查看的是最新测评结果

  • [2024-04-06 13:14:32]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:4116kb
  • [2024-04-06 13:14:30]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


vector<int> uf;
int root(int u) {
  return (uf[u] < 0) ? u : (uf[u] = root(uf[u]));
}
bool connect(int u, int v) {
  u = root(u);
  v = root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


constexpr Int INF = 1001001001001001001LL;

int N, K;
vector<Int> A;

Int solve() {
  priority_queue<Int> que;
  Int ret = 0;
  for (int i = 0; i < N; ++i) {
    que.push(A[i]);
    que.push(A[i]);
    ret += (que.top() - A[i]);
    que.pop();
  }
  return ret;
}

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d%d", &N, &K);
    A.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%lld", &A[i]);
    }
    
    Int ans = INF;
    if (K == 1) {
      ans = solve();
    } else if (K == N) {
      for (int i = 0; i < N; ++i) {
        chmin(ans, solve());
        rotate(A.begin(), A.begin() + 1, A.end());
      }
    } else if (K % 2 != 0) {
      vector<int> is(N);
      for (int i = 0; i < N; ++i) is[i] = i;
      sort(is.begin(), is.end(), [&](int i, int j) -> bool {
        return (A[i] < A[j]);
      });
      int sig = N;
      uf.assign(N, -1);
      for (int i = 0; i < N; ++i) {
        if (connect(i, is[i])) {
          --sig;
        }
      }
// cerr<<"is = "<<is<<", sig = "<<sig<<endl;
      sig %= 2;
      if (sig) {
        for (int i = 0; i < N - 1; ++i) {
          chmin(ans, A[is[i + 1]] - A[is[i]]);
        }
      }
    } else {
      ans = 0;
    }
    printf("%lld\n", ans);
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3880kb

input:

4
4 1
6 4 3 7
4 2
6 4 3 7
4 3
6 4 3 7
4 4
6 4 3 7

output:

3
0
1
2

result:

ok 4 number(s): "3 0 1 2"

Test #2:

score: -100
Wrong Answer
time: 8ms
memory: 4116kb

input:

10000
4 3
524728 254456 277709 19127
15 11
360089 525234 862619 897281 336644 910706 75922 708901 754517 734744 94169 326125 746826 846063 159956
4 2
140105 792522 40264 514789
12 2
270333 888927 500833 9065 936673 982631 332435 751429 607700 840339 804685 416612
8 7
119416 689632 517277 673646 8262...

output:

23253
1001001001001001001
0
0
15986
278544
0
0
0
0
0
2022
14023
0
0
9260
0
0
51255
0
0
277173
480146
0
1001001001001001001
1001001001001001001
25495
0
1928
0
0
266148
0
767231
5853
0
0
121885
0
788638
0
0
0
779611
0
5881
0
0
0
0
517074
0
5586
0
210836
454586
662851
0
781542
0
0
864957
175421
0
0
0
0...

result:

wrong answer 2nd numbers differ - expected: '7691', found: '1001001001001001001'