답안 #132458

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
132458 2019-07-18T21:22:43 Z egorlifar 우주 해적 (JOI14_space_pirate) C++17
80 / 100
451 ms 173092 KB
/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
 */
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>

using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left224
#define right right224
#define next next224
#define rank rank224
#define prev prev224
#define y1 y1224
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
const string FILENAME = "input";
const int MAXN = 100028;

int n;
long long k;
int a[MAXN];
long long cnt[MAXN];
int uk = 1;
int used[MAXN];
int pos[MAXN];
int jump[MAXN][65];
bool can[3028][3028];
int dist[3028][3028];

int go(int i, long long f) {
    for (int s = 62; s >= 0; s--) {
        if (f >= (1LL << s)) {
            f -= 1LL << s;
            i = jump[i][s];
        }
    }
    return i;
}


int fk[3028][3028];
int sk[3028][3028];


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //read(FILENAME);
    cin >> n >> k;
    vector<int> ggg;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
        jump[i][0] = a[i];
        ggg.pb(a[i]);
    }
    sort(all(ggg));
    ggg.resize(distance(ggg.begin(), unique(all(ggg))));
    int it = 0;
    pos[0] = 0;
    vector<int> g;
    int cur = 0;
    while (used[cur] != uk && it < k) {
        used[cur] = uk;
        pos[cur] = it;
        g.pb(cur);
        it++;
        cur = a[cur];
    }
    int cur1 = cur;
    assert(sz(g) <= n);
    if (it != k) {
        int f = it - pos[cur];
        for (int jj = 0; jj < (k - it) % f; jj++) {
            cur = a[cur];
        }
    }
    for (int i = 0; i < n; i++) {
        if (used[i] != uk) {
            cnt[cur] += n;
        }
    }
    for (int k = 1; k <= 63; k++) {
        for (int i = 0; i < n; i++) {
            jump[i][k] = jump[jump[i][k - 1]][k - 1];
        }
    }
    if (sz(ggg) == n) {
        for (int i = 0; i < sz(g); i++) {
            if (k - i >= 0) {
                cnt[g[i]]++;
            }
        }
        for (int i = 1; i <= sz(g) - 1; i++) {
            long long kk = k - 1;
            long long st = 0;
            while (st < min(1LL * sz(g), k) && kk >= 0) {
                long long pos = (st + i + kk % (sz(g) - i + 1)) % sz(g);
                long long gg = min(min(1LL * sz(g), k) - st, kk % (sz(g) - i + 1) + 1);
                cnt[g[pos]] += gg;
                st += kk % (sz(g) - i + 1);
                kk -= kk % (sz(g) - i + 1);
                st++;
                kk--;
            }
        }
        for (int i = 0; i < n; i++) {
            if (used[i] != uk) {
                cnt[i] += min(1LL * sz(g), k);
            }
        }
        for (int i = 0; i < n; i++) {
            cout << cnt[i] << '\n';
        }
        return 0;
    }
    for (int i = 0; i < n; i++) {
        int f = i;
        for (int j = 0; j < n; j++) {
            if (can[i][f]) break;
            can[i][f] = true;
            dist[i][f] = j;
            fk[i][j] = f;
            f = a[f];
        }
    }
    for (int i = 0; i < n; i++) {
        int t = go(i, k - sz(g));
        for (int f = 0; f < sz(g); f++) {
            sk[i][f] = t;
            t = a[t];
        }
    }
    long long ks = k;
    for (int j = 0; j < n; j++) {
        k = ks;
        for (int i = 0; i < sz(g); i++) {
            if (j == a[g[i]]) {
                cnt[cur]++;
            } else {
                if (k == 0) {
                    cnt[g[i]]++;
                    continue;
                }
                if (can[j][g[i]]) {
                    int fs = dist[j][g[i]] + 1;
                    long long kk = k;
                    kk--;
                    kk %= fs;
                    if (kk == 0) {
                        cnt[j]++;
                    } else {
                        cnt[fk[j][kk]]++;
                    }
                } else {
                    cnt[sk[j][k - 1 - (ks - sz(g))]]++;
                }
            }
            k--;
        }
    }
    for (int i = 0; i < n; i++) {
        cout << cnt[i] << '\n';
    }
    return 0;
}

Compilation message

space_pirate.cpp: In function 'int main()':
space_pirate.cpp:112:9: warning: unused variable 'cur1' [-Wunused-variable]
     int cur1 = cur;
         ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 3 ms 1912 KB Output is correct
4 Correct 4 ms 2040 KB Output is correct
5 Correct 3 ms 1912 KB Output is correct
6 Correct 3 ms 2040 KB Output is correct
7 Correct 3 ms 2040 KB Output is correct
8 Correct 3 ms 1912 KB Output is correct
9 Correct 3 ms 2040 KB Output is correct
10 Correct 3 ms 1912 KB Output is correct
11 Correct 3 ms 1912 KB Output is correct
12 Correct 4 ms 2040 KB Output is correct
13 Correct 3 ms 1912 KB Output is correct
14 Correct 3 ms 1912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 3 ms 1912 KB Output is correct
4 Correct 4 ms 2040 KB Output is correct
5 Correct 3 ms 1912 KB Output is correct
6 Correct 3 ms 2040 KB Output is correct
7 Correct 3 ms 2040 KB Output is correct
8 Correct 3 ms 1912 KB Output is correct
9 Correct 3 ms 2040 KB Output is correct
10 Correct 3 ms 1912 KB Output is correct
11 Correct 3 ms 1912 KB Output is correct
12 Correct 4 ms 2040 KB Output is correct
13 Correct 3 ms 1912 KB Output is correct
14 Correct 3 ms 1912 KB Output is correct
15 Correct 76 ms 71548 KB Output is correct
16 Correct 57 ms 65148 KB Output is correct
17 Correct 84 ms 72012 KB Output is correct
18 Correct 441 ms 116856 KB Output is correct
19 Correct 209 ms 103292 KB Output is correct
20 Correct 280 ms 105468 KB Output is correct
21 Correct 310 ms 109176 KB Output is correct
22 Correct 181 ms 96508 KB Output is correct
23 Correct 296 ms 108640 KB Output is correct
24 Correct 150 ms 94872 KB Output is correct
25 Correct 59 ms 70136 KB Output is correct
26 Correct 451 ms 116984 KB Output is correct
27 Correct 168 ms 91128 KB Output is correct
28 Correct 216 ms 95992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 29168 KB Output is correct
2 Correct 191 ms 29784 KB Output is correct
3 Correct 165 ms 29764 KB Output is correct
4 Correct 133 ms 29176 KB Output is correct
5 Correct 189 ms 29812 KB Output is correct
6 Correct 159 ms 29680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 3 ms 1912 KB Output is correct
4 Correct 4 ms 2040 KB Output is correct
5 Correct 3 ms 1912 KB Output is correct
6 Correct 3 ms 2040 KB Output is correct
7 Correct 3 ms 2040 KB Output is correct
8 Correct 3 ms 1912 KB Output is correct
9 Correct 3 ms 2040 KB Output is correct
10 Correct 3 ms 1912 KB Output is correct
11 Correct 3 ms 1912 KB Output is correct
12 Correct 4 ms 2040 KB Output is correct
13 Correct 3 ms 1912 KB Output is correct
14 Correct 3 ms 1912 KB Output is correct
15 Correct 76 ms 71548 KB Output is correct
16 Correct 57 ms 65148 KB Output is correct
17 Correct 84 ms 72012 KB Output is correct
18 Correct 441 ms 116856 KB Output is correct
19 Correct 209 ms 103292 KB Output is correct
20 Correct 280 ms 105468 KB Output is correct
21 Correct 310 ms 109176 KB Output is correct
22 Correct 181 ms 96508 KB Output is correct
23 Correct 296 ms 108640 KB Output is correct
24 Correct 150 ms 94872 KB Output is correct
25 Correct 59 ms 70136 KB Output is correct
26 Correct 451 ms 116984 KB Output is correct
27 Correct 168 ms 91128 KB Output is correct
28 Correct 216 ms 95992 KB Output is correct
29 Correct 135 ms 29168 KB Output is correct
30 Correct 191 ms 29784 KB Output is correct
31 Correct 165 ms 29764 KB Output is correct
32 Correct 133 ms 29176 KB Output is correct
33 Correct 189 ms 29812 KB Output is correct
34 Correct 159 ms 29680 KB Output is correct
35 Runtime error 283 ms 173092 KB Execution killed with signal 11 (could be triggered by violating memory limits)
36 Halted 0 ms 0 KB -