Submission #18580511


Source Code Expand

Copy
// #define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

template<typename T, size_t N>
struct fenwick {
  T c[N];
  fenwick() {
    fill(c + 1, c + N, 0);
  }
  void add(int x, T y) {
    for (; x < N; x += x & -x) {
      c[x] += y;
    }
  }
  T query(int x) {
    T ret = 0;
    for (; x; x -= x & -x) {
      ret += c[x];
    }
    return ret;
  }
};

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  for (int i = 0; i < n; ++i) cin >> p[i];
  const int N = 2e5 + 10;
  fenwick<int, N> F;
  int64 sum = 0;
  for (int i = n - 1; i >= 0; --i) {
    sum += F.query(p[i]);
    F.add(p[i], 1);
  }
  if (sum != n - 1) {
    cout << -1 << '\n';
    return 0;
  }
  for (int i = 0; i < n; ++i) --p[i];
  vector<int> pos(n);
  for (int i = 0; i < n; ++i) pos[p[i]] = i;
  vector<ii> ret;
  for (int i = 0; i < n; ++i) {
    if (p[i] <= i) continue;
    ret.push_back({i, p[i]});
  }
  sort(ret.begin(), ret.end());
  trace(ret);
  for (int i = 1; i < SZ(ret); ++i) {
    if (ret[i].first != ret[i - 1].second) {
      cout << -1 << '\n';
      return 0;
    }
  }
  vector<int> op;
  for (int i = n - 1; i >= 0; --i) {
    if (pos[i] >= i) continue;
    for (int j = pos[i]; j < i; ++j) op.push_back(j);
  }
  for (int i = 0; i < SZ(op); ++i) {
    cout << op[i] + 1 << '\n';
  }
  return 0;
}

Submission Info

Submission Time
Task C - Exoswap
User cuiaoxiang
Language C++ (Clang 10.0.0)
Score 500
Code Size 3841 Byte
Status AC
Exec Time 224 ms
Memory 8480 KB

Compile Error

./Main.cpp:78:42: warning: unused variable 'inf' [-Wunused-const-variable]
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
                                         ^
./Main.cpp:79:11: warning: unused variable 'MOD' [-Wunused-const-variable]
const int MOD = 1e9 + 7;
          ^
2 warnings generated.

Judge Result

Set Name All Sample
Score / Max Score 500 / 500 0 / 0
Status
AC × 59
AC × 2
Set Name Test Cases
All sample_01.txt, sample_02.txt, testcase_1.txt, testcase_10.txt, testcase_11.txt, testcase_12.txt, testcase_13.txt, testcase_14.txt, testcase_15.txt, testcase_16.txt, testcase_17.txt, testcase_18.txt, testcase_19.txt, testcase_2.txt, testcase_20.txt, testcase_21.txt, testcase_22.txt, testcase_23.txt, testcase_24.txt, testcase_25.txt, testcase_26.txt, testcase_27.txt, testcase_28.txt, testcase_29.txt, testcase_3.txt, testcase_30.txt, testcase_31.txt, testcase_32.txt, testcase_33.txt, testcase_34.txt, testcase_35.txt, testcase_36.txt, testcase_37.txt, testcase_38.txt, testcase_39.txt, testcase_4.txt, testcase_40.txt, testcase_41.txt, testcase_42.txt, testcase_43.txt, testcase_44.txt, testcase_45.txt, testcase_46.txt, testcase_47.txt, testcase_48.txt, testcase_49.txt, testcase_5.txt, testcase_50.txt, testcase_51.txt, testcase_52.txt, testcase_53.txt, testcase_54.txt, testcase_55.txt, testcase_56.txt, testcase_57.txt, testcase_6.txt, testcase_7.txt, testcase_8.txt, testcase_9.txt
Sample sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
sample_01.txt AC 5 ms 3900 KB
sample_02.txt AC 4 ms 3812 KB
testcase_1.txt AC 2 ms 3948 KB
testcase_10.txt AC 19 ms 3856 KB
testcase_11.txt AC 66 ms 3816 KB
testcase_12.txt AC 52 ms 3844 KB
testcase_13.txt AC 58 ms 3948 KB
testcase_14.txt AC 224 ms 7388 KB
testcase_15.txt AC 153 ms 7420 KB
testcase_16.txt AC 155 ms 7436 KB
testcase_17.txt AC 154 ms 7612 KB
testcase_18.txt AC 154 ms 7336 KB
testcase_19.txt AC 153 ms 7440 KB
testcase_2.txt AC 12 ms 3808 KB
testcase_20.txt AC 153 ms 7576 KB
testcase_21.txt AC 156 ms 7464 KB
testcase_22.txt AC 154 ms 7464 KB
testcase_23.txt AC 152 ms 7332 KB
testcase_24.txt AC 154 ms 7340 KB
testcase_25.txt AC 151 ms 7464 KB
testcase_26.txt AC 154 ms 7408 KB
testcase_27.txt AC 186 ms 7576 KB
testcase_28.txt AC 109 ms 6588 KB
testcase_29.txt AC 15 ms 3944 KB
testcase_3.txt AC 3 ms 3980 KB
testcase_30.txt AC 153 ms 7404 KB
testcase_31.txt AC 133 ms 6976 KB
testcase_32.txt AC 45 ms 4372 KB
testcase_33.txt AC 129 ms 7024 KB
testcase_34.txt AC 110 ms 4364 KB
testcase_35.txt AC 111 ms 4352 KB
testcase_36.txt AC 110 ms 4404 KB
testcase_37.txt AC 109 ms 4320 KB
testcase_38.txt AC 113 ms 4352 KB
testcase_39.txt AC 109 ms 4372 KB
testcase_4.txt AC 7 ms 3884 KB
testcase_40.txt AC 111 ms 4348 KB
testcase_41.txt AC 40 ms 3884 KB
testcase_42.txt AC 87 ms 4052 KB
testcase_43.txt AC 80 ms 4148 KB
testcase_44.txt AC 184 ms 4236 KB
testcase_45.txt AC 109 ms 4312 KB
testcase_46.txt AC 112 ms 4284 KB
testcase_47.txt AC 109 ms 4180 KB
testcase_48.txt AC 111 ms 4332 KB
testcase_49.txt AC 113 ms 4288 KB
testcase_5.txt AC 9 ms 3808 KB
testcase_50.txt AC 116 ms 4348 KB
testcase_51.txt AC 36 ms 3808 KB
testcase_52.txt AC 34 ms 3896 KB
testcase_53.txt AC 36 ms 3976 KB
testcase_54.txt AC 150 ms 8480 KB
testcase_55.txt AC 149 ms 6152 KB
testcase_56.txt AC 108 ms 4184 KB
testcase_57.txt AC 106 ms 4344 KB
testcase_6.txt AC 5 ms 3812 KB
testcase_7.txt AC 3 ms 3792 KB
testcase_8.txt AC 5 ms 3948 KB
testcase_9.txt AC 121 ms 4236 KB