Submission #389441

# Submission time Handle Problem Language Result Execution time Memory
389441 2021-04-14T05:50:13 Z abc864197532 Permutation Recovery (info1cup17_permutation) C++17
60 / 100
4000 ms 286720 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define test(x) cout << #x << ' ' << x << endl
#define printv(x) { \
    for (auto a : x) cout << a << ' '; \
    cout << endl; \
}
#define pii pair<int, int>
#define pll pair<lli, lli>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int N = 512, abc = 864197532;

struct BigInt {
    vector <int> val;
    BigInt(string s = "0") {
        for (int i = s.length() - 1; ~i; --i) {
            val.pb(s[i] - '0');
        }
    }
    BigInt operator - (const BigInt &o) const {
        vector <int> result(max(val.size(), o.val.size()));
        for (int i = 0; i < result.size(); ++i) {
            int a = i < val.size() ? val[i] : 0;
            int b = i < o.val.size() ? o.val[i] : 0;
            result[i] = a - b;
        }
        for (int i = 0; i < result.size(); ++i) {
            if (result[i] < 0) {
                int add = (-result[i] + 9) / 10;
                result[i + 1] -= add;
                result[i] += add * 10;
            }
        }
        while (result.size() > 1 && result.back() == 0) result.pop_back();
        BigInt ans;
        ans.val.pop_back();
        for (int i : result) ans.val.pb(i);
        return ans;
    }
    BigInt operator + (const BigInt &o) const {
        vector <int> result(max(val.size(), o.val.size()));
        for (int i = 0; i < result.size(); ++i) {
            int a = i < val.size() ? val[i] : 0;
            int b = i < o.val.size() ? o.val[i] : 0;
            result[i] = a + b;
        }
        for (int i = 0; i < result.size(); ++i) {
            if (result[i] >= 10 && i == result.size() - 1) result.pb(0);
            result[i + 1] += result[i] / 10;
            result[i] %= 10;
        }
        while (result.size() > 1 && result.back() == 0) result.pop_back();
        BigInt ans;
        ans.val.pop_back();
        for (int i : result) ans.val.pb(i);
        return ans;
    }
    bool operator == (const BigInt &o) const {
        return val == o.val;
    }
    bool operator < (const BigInt &o) const {
        if (val.size() != o.val.size()) return val.size() < o.val.size();
        for (int i = val.size() - 1; ~i; --i) if (val[i] != o.val[i]) {
            return val[i] < o.val[i];
        }
        return false;
    }
} INF, one = BigInt("1"), zero;

struct Seg {
    int l, r, m;
    BigInt mn, lz;
    Seg* ch[2];
    Seg (int _l, int _r, vector <BigInt> &a) : l(_l), r(_r), m(l + r >> 1) {
        if (r - l > 1) {
            ch[0] = new Seg(l, m, a);
            ch[1] = new Seg(m, r, a);
            pull();
        } else {
            mn = a[l];
        }
    }
    void pull() {
        if (ch[0]->mn < ch[1]->mn) mn = ch[0]->mn;
        else mn = ch[1]->mn;
    }
    void give(BigInt i) {
        lz = lz + i;
        mn = mn - i;
    }
    void push() {
        if (lz == zero) return;
        ch[0]->give(lz), ch[1]->give(lz);
        lz = zero;
    }
    void modify(int a, int b, BigInt v) {
        if (a <= l && r <= b) give(v);
        else {
            push();
            if (a < m) ch[0]->modify(a, b, v);
            if (m < b) ch[1]->modify(a, b, v);
            pull();
        }
    }
    int query() {
        if (r - l == 1) {
            mn = INF;
            return l;
        }
        push();
        int ans;
        if (ch[1]->mn == one) ans = ch[1]->query();
        else ans = ch[0]->query();
        pull();
        return ans;
    }
};

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    INF.val.pop_back();
    while (INF.val.size() < 180) INF.val.pb(9);
    int n;
    cin >> n;
    vector <BigInt> a;
    string s;
    for (int i = 0; i < n; ++i) cin >> s, a.pb(BigInt(s));
    vector <BigInt> d(n);
    vector <int> p(n, 0);
    int now = 1;
    for (int i = 0; i < n; ++i) {
        d[i] = (i ? a[i] - a[i - 1] : a[i]);
    }
    Seg root(0, n, d);
    while (now <= n) {
        int i = root.query();
        p[i] = now++;
        root.modify(i, n, d[i]);
    }
    printv(p);
}

Compilation message

permutation.cpp: In member function 'BigInt BigInt::operator-(const BigInt&) const':
permutation.cpp:29:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |         for (int i = 0; i < result.size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~~
permutation.cpp:30:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |             int a = i < val.size() ? val[i] : 0;
      |                     ~~^~~~~~~~~~~~
permutation.cpp:31:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |             int b = i < o.val.size() ? o.val[i] : 0;
      |                     ~~^~~~~~~~~~~~~~
permutation.cpp:34:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |         for (int i = 0; i < result.size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~~
permutation.cpp: In member function 'BigInt BigInt::operator+(const BigInt&) const':
permutation.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |         for (int i = 0; i < result.size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~~
permutation.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |             int a = i < val.size() ? val[i] : 0;
      |                     ~~^~~~~~~~~~~~
permutation.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |             int b = i < o.val.size() ? o.val[i] : 0;
      |                     ~~^~~~~~~~~~~~~~
permutation.cpp:54:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |         for (int i = 0; i < result.size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~~
permutation.cpp:55:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |             if (result[i] >= 10 && i == result.size() - 1) result.pb(0);
      |                                    ~~^~~~~~~~~~~~~~~~~~~~
permutation.cpp: In constructor 'Seg::Seg(int, int, std::vector<BigInt>&)':
permutation.cpp:81:66: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   81 |     Seg (int _l, int _r, vector <BigInt> &a) : l(_l), r(_r), m(l + r >> 1) {
      |                                                                ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 9 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 9 ms 1228 KB Output is correct
3 Correct 20 ms 2892 KB Output is correct
4 Correct 19 ms 2764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 9 ms 1228 KB Output is correct
3 Correct 20 ms 2892 KB Output is correct
4 Correct 19 ms 2764 KB Output is correct
5 Correct 3925 ms 193420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 9 ms 1228 KB Output is correct
3 Correct 20 ms 2892 KB Output is correct
4 Correct 19 ms 2764 KB Output is correct
5 Correct 3925 ms 193420 KB Output is correct
6 Execution timed out 4081 ms 286720 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 9 ms 1228 KB Output is correct
3 Correct 20 ms 2892 KB Output is correct
4 Correct 19 ms 2764 KB Output is correct
5 Correct 3925 ms 193420 KB Output is correct
6 Execution timed out 4081 ms 286720 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 9 ms 1228 KB Output is correct
3 Correct 20 ms 2892 KB Output is correct
4 Correct 19 ms 2764 KB Output is correct
5 Correct 3925 ms 193420 KB Output is correct
6 Execution timed out 4081 ms 286720 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 9 ms 1228 KB Output is correct
3 Correct 20 ms 2892 KB Output is correct
4 Correct 19 ms 2764 KB Output is correct
5 Correct 3925 ms 193420 KB Output is correct
6 Execution timed out 4081 ms 286720 KB Time limit exceeded