QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#251397#7678. The GameLainWA 5ms3604kbC++232.9kb2023-11-14 17:47:222023-11-14 17:47:23

Judging History

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

  • [2023-11-14 17:47:23]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:3604kb
  • [2023-11-14 17:47:22]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

  int tt;
  cin >> tt;
  while(tt--) {
    int n, m;
    cin >> n >> m;
    vi a(n), b(m);
    map<int,int> ma, mb;
    for (auto& x : a) cin >> x, ma[x]++;
    for (auto& x : b) cin >> x, mb[x]++;
    sort(all(a));
    sort(all(b));
    if (n < m) {
      cout << -1 << '\n';
      continue;
    } else if (n == m) {
      if (a != b) {
        cout << -1 << '\n';
        continue;
      } else {
        cout << 0 << '\n';
        continue;
      }
    }
    vi all_values;
    all_values.insert(all_values.end(), all(a));
    all_values.insert(all_values.end(), all(b));
    sort(all(all_values));
    all_values.erase(unique(all(all_values)), all_values.end());

    vi sub, add;
    for (auto& x : all_values) {
      int diff = ma[x] - mb[x];
      if (diff > 0) {
        for (int i =0; i < abs(diff); i++)
          sub.push_back(x);
      } else if (diff < 0) {
        for (int i =0 ; i < abs(diff); i++)
          add.push_back(x);
      }
    }

    int max_removal = min(b[0], (add.size()?add[0]:INT_MAX));
    multiset<int> S;
    multiset<pii> T;
    int last_added = sz(sub);
    int64_t leader_add = 0;
    for (int i = sz(add) - 1l, j = sz(sub) - 1; i >= 0; i--, j--) {
      assert(j >= 0);
      T.insert({sub[j], add[i] - sub[j]});
      leader_add += add[i] - sub[j];
      last_added = j;
    }
    rep(i, 0, last_added)
      S.insert(sub[i]);
    // The numbers that need to be totally removed - the rest can just be added
    // to the leaders.
    int64_t to_remove = last_added - leader_add;
    if (to_remove < 0) {
      cout << -1 << '\n';
      continue;
    }

    // debug
    /* for (auto&x : S) cout << x<< " "; */
    /* cout << '\n'; */
    /* for (auto& [x, y] : T) cout << x << "," << y << ' '; */
    /* cout << '\n'; */

    bool ok = true;
    vi ans;
    while(S.size()) {
      int minT = (T.size() ? T.begin()->first: INT_MAX);
      int maxS = *S.rbegin();
      int minS = *S.begin();
      if (to_remove > 0 && minS < minT && minS < max_removal) {
        to_remove--;
        ans.push_back(minS);
        S.erase(S.begin());
        S.insert(minS+1);
        S.erase(S.begin());
      } else if (T.size()) {
        auto [x, y] = *T.begin();
        ans.push_back(x);
        T.erase(T.begin());
        if (y != 1)
          T.insert({x+1, y-1});
        S.erase(S.begin());
      } else {
        ok = false;
        break;
      }
    }
    if (!ok) {
      cout << -1 << '\n';
      continue;
    }
    cout << sz(ans) << '\n';
    for (auto& x : ans) cout << x << " ";
    cout << '\n';
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6
5 3
1 2 2 3 3
2 3 4
4 2
1 2 2 4
2 4
5 2
2 3 3 4 4
5 5
6 1
1 1 1 1 1 1
4
4 2
1 1 1 2
2 2
4 1
1 1 1 1
2

output:

2
1 3 
-1
3
2 4 4 
5
1 1 1 2 3 
2
1 1 
-1

result:

ok ok (6 test cases)

Test #2:

score: -100
Wrong Answer
time: 5ms
memory: 3540kb

input:

7056
4 3
1 1 1 1
1 1 1
4 3
1 1 1 1
1 1 2
4 3
1 1 1 1
1 1 3
4 3
1 1 1 1
1 1 4
4 3
1 1 1 1
1 1 5
4 3
1 1 1 1
1 1 6
4 3
1 1 1 1
1 2 2
4 3
1 1 1 1
1 2 3
4 3
1 1 1 1
1 2 4
4 3
1 1 1 1
1 2 5
4 3
1 1 1 1
1 2 6
4 3
1 1 1 1
1 3 3
4 3
1 1 1 1
1 3 4
4 3
1 1 1 1
1 3 5
4 3
1 1 1 1
1 3 6
4 3
1 1 1 1
1 4 4
4 3
1 1...

output:

-1
1
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1
2 
-1
-1
-1
1
1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

wrong answer Wrong answer, the final sequence does not equal to B (test case 114)