Submission #317567

# Submission time Handle Problem Language Result Execution time Memory
317567 2020-10-30T02:30:28 Z casperwang DEL13 (info1cup18_del13) C++14
15 / 100
202 ms 131076 KB
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;

const int MAXN = 100000;
stack <int> ans;

bool solve(vector <pii> &arr) {
  vector <int> cnt(arr.size()+1);
  for (int i = 0; i < arr.size(); i++)
    cnt[i] = arr[i].ss - arr[i].ff + 1;
  for (int i = 0; i < arr.size(); i++) {
    if (cnt[i] % 2) {
      ans.push(arr[i].ss+1);
      cnt[i]--, cnt[i+1]--;
    }
  }
  if (cnt[arr.size()] % 2) return 0;
  stack <int> tmp;
  for (int i = 0; i <= arr.size(); i++) {
    if (cnt[i] && arr[i].ss - arr[i].ff + 1 == 2) {
      tmp.push(i);
    } else if (!cnt[i]) {
      while (tmp.size()) {
        ans.push(arr[tmp.top()].ff-1);
        ans.push(arr[tmp.top()].ff-1);
        cnt[tmp.top()] -= 2;
        if (tmp.size() > 1) {
          tmp.pop();
          cnt[tmp.top()] -= 2;
          tmp.pop();
        } else {
          if (!tmp.top() || !cnt[tmp.top()-1]) return 0;
          cnt[tmp.top()-1] -= 2;
          tmp.pop();
        }
      }
    } else {
      if (tmp.size() % 2) {
        ans.push(arr[i].ff-1);
        ans.push(arr[i].ff-1);
        cnt[i] -= 2;
        cnt[tmp.top()] -= 2;
        tmp.pop();
      }
      if (i < arr.size() && cnt[i] == arr[i].ss - arr[i].ff + 1) {
        if (i && arr[i-1].ss - arr[i-1].ff + 1 != 2) {
          ans.push(arr[i].ff-1);
          ans.push(arr[i].ff-1);
          cnt[i] -= 2;
          cnt[i-1] -= 2;
        } else if (cnt[i+1]) {
          ans.push(arr[i].ss+1);
          ans.push(arr[i].ss+1);
          cnt[i] -= 2;
          cnt[i+1] -= 2;
        } else if (tmp.size()) {
          ans.push(arr[i].ff-1);
          ans.push(arr[i].ff-1);
          cnt[i] -= 2;
          cnt[i-1] -= 2;
          tmp.pop();
          while (tmp.size()) {
            ans.push(arr[tmp.top()].ff-1);
            ans.push(arr[tmp.top()].ff-1);
            cnt[tmp.top()] -= 2;
            if (tmp.size() > 1) {
              tmp.pop();
              cnt[tmp.top()] -= 2;
              tmp.pop();
            } else {
              if (!tmp.top() || !cnt[tmp.top()-1]) return 0;
              cnt[tmp.top()-1] -= 2;
              tmp.pop();
            }
          }
        } else return 0;
      }
      while (tmp.size()) {
        ans.push(arr[tmp.top()].ff-1);
        ans.push(arr[tmp.top()].ff-1);
        cnt[tmp.top()] -= 2;
        tmp.pop();
        cnt[tmp.top()] -= 2;
        tmp.pop();
      }
    }
  }
  for (int i = 0; i < arr.size(); i++) {
    while (cnt[i]) {
      ans.push((arr[i].ff + arr[i].ss) / 2);
      cnt[i] -= 2;
    }
  }
  return 1;
}

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int a;
    vector <int> p(n+1);
    for (int i = 1; i <= k; i++)
      cin >> a, p[a] = 1;
    int l = 1, r = 0, now = 0;
    vector <vector<pii>> arr(1);
    for (int i = 1; i <= n; i++) {
      if (p[i]) {
        if (r >= l) arr[now].pb(pii(l, r));
        else arr.pb(vector<pii>()), now++;
        l = i+1;
      } else r = i;
    }
    if (r >= l) arr[now].pb(pii(l, r));
    bool tf = 1;
    for (vector <pii> v : arr) {
      if (!solve(v)) {
        tf = 0;
        break;
      }
    }
    if (!tf) {
      cout << -1 << "\n";
      while (ans.size()) ans.pop();
    } else {
      cout << ans.size() << "\n";
      while (ans.size()) {
        cout << ans.top(), ans.pop();
        cout << "\n "[ans.size()>0];
      }
    }
  }
}

Compilation message

del13.cpp: In function 'bool solve(std::vector<std::pair<int, int> >&)':
del13.cpp:13:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |   for (int i = 0; i < arr.size(); i++)
      |                   ~~^~~~~~~~~~~~
del13.cpp:15:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |   for (int i = 0; i < arr.size(); i++) {
      |                   ~~^~~~~~~~~~~~
del13.cpp:23:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |   for (int i = 0; i <= arr.size(); i++) {
      |                   ~~^~~~~~~~~~~~~
del13.cpp:49:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |       if (i < arr.size() && cnt[i] == arr[i].ss - arr[i].ff + 1) {
      |           ~~^~~~~~~~~~~~
del13.cpp:92:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |   for (int i = 0; i < arr.size(); i++) {
      |                   ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 196 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Runtime error 196 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Runtime error 196 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Runtime error 196 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Runtime error 193 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
4 Runtime error 202 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Correct 8 ms 512 KB Output is correct
2 Correct 4 ms 784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 196 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Runtime error 196 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Runtime error 193 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
4 Runtime error 202 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
5 Runtime error 194 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
6 Runtime error 197 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Runtime error 199 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Runtime error 196 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Runtime error 196 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Runtime error 193 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
4 Runtime error 202 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
5 Runtime error 194 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
6 Runtime error 197 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Runtime error 199 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
8 Runtime error 197 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
9 Runtime error 192 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
10 Runtime error 198 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
11 Runtime error 193 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)