Submission #411988

# Submission time Handle Problem Language Result Execution time Memory
411988 2021-05-26T11:44:42 Z losmi247 Gondola (IOI14_gondola) C++14
100 / 100
91 ms 13676 KB
#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;
typedef long long ll;
const int N = 1e5+34;
const int mod = 1000000009;

ll n,a[N];
map <int,int> pos,bio;
int treba[N];

int valid(int br,int *inputseq){
    n = br;
    for(int i = 1; i <= n; i++) a[i] = inputseq[i-1];

    for(int i = 1; i <= n; i++){
        if(a[i] <= n) pos[a[i]] = i;
        if(bio[a[i]]) return 0;
        bio[a[i]] = 1;
    }
    int lst = 0;
    bool ok = 1;
    for(int i = 1; i <= n; i++){
        if(!pos[i]) continue;
        if(!lst){
            lst = i;
            continue;
        }
        if(pos[lst] < pos[i]){
            if(pos[i]-pos[lst] != i-lst){
                ok = 0;
                break;
            }
        }
        else{
            if(pos[lst]-pos[i]-1 != lst-1+n-i){
                ok = 0;
                break;
            }
        }
        lst = i;
    }

    return ok;
}

int replacement(int br,int *gondolaSeq,int *replacementSeq){
    n = br;
    for(int i = 1; i <= n; i++) a[i] = gondolaSeq[i-1];



    for(int i = 1; i <= n; i++){
        if(a[i] <= n){
            treba[i] = a[i];
        }
        else if(i > 1){
            if(treba[i-1] == n) treba[i] = 1;
            else if(treba[i-1]) treba[i] = treba[i-1]+1;
        }
    }

    for(int i = n; i >= 1; i--){
        if(treba[i]) continue;
        if(treba[i+1] == 1) treba[i] = n;
        else treba[i] = treba[i+1]-1;
    }

    bool svi = 1;
    for(int i = 1; i <= n; i++){
        if(a[i] <= n) svi = 0;
    }
    if(svi){
        for(int i = 1; i <= n; i++){
            treba[i] = i;
        }
    }

    for(int i = 1; i <= n; i++){
        assert(1 <= treba[i] && treba[i] <= n);
    }

    vector <pair<int,int>> v;
    for(int i = 1; i <= n; i++){
        if(treba[i] != a[i]) v.push_back({a[i],treba[i]});
    }

    sort(v.begin(),v.end());

    /*cout << v.size() << " evo" << endl;
    for(auto f : v){
        cout << f.first << " " << f.second << endl;
    }
    cout << endl;*/

    int cnt = 0,stigo = n;
    for(auto f : v){
        for(int j = stigo; j < f.first; j++) replacementSeq[cnt++] = (j == stigo ? f.second : j);
        stigo = f.first;
    }

    return cnt;
}

ll fp(ll x,ll y){
    if(y == 0){
        return 1;
    }
    ll p = fp(x,y/2);
    p = (p*p)%mod;
    if(y&1){
        p = (p*x)%mod;
    }
    return p;
}

int countReplacement(int br,int *inputSeq){
    n = br;
    for(int i = 1; i <= n; i++) a[i] = inputSeq[i-1];

    if(!valid(br,inputSeq)){
        return 0;
    }

    for(int i = 1; i <= n; i++){
        if(a[i] <= n){
            treba[i] = a[i];
        }
        else if(i > 1){
            if(treba[i-1] == n) treba[i] = 1;
            else if(treba[i-1]) treba[i] = treba[i-1]+1;
        }
    }

    for(int i = n; i >= 1; i--){
        if(treba[i]) continue;
        if(treba[i+1] == 1) treba[i] = n;
        else treba[i] = treba[i+1]-1;
    }

    bool svi = 1;
    for(int i = 1; i <= n; i++){
        if(a[i] <= n) svi = 0;
    }
    if(svi){
        for(int i = 1; i <= n; i++){
            treba[i] = i;
        }
    }

    for(int i = 1; i <= n; i++){
        assert(1 <= treba[i] && treba[i] <= n);
    }

    vector <pair<ll,ll>> v;
    for(int i = 1; i <= n; i++){
        if(treba[i] != a[i]) v.push_back({a[i],treba[i]});
    }

    sort(v.begin(),v.end());

    ll k = v.size();
    ll sol = 1;
    ll prosli = n;
    for(ll i = 0; i < v.size(); i++){
        if(i == 0){
            ll pom = fp(k,v[i].first-prosli-1);
            sol *= pom;
            sol %= mod;
            prosli = v[i].first;
        }
        else{
            ll pom = fp(k-i,v[i].first-prosli-1);
            sol *= pom;
            sol %= mod;
            prosli = v[i].first;
        }
    }

    if(svi){
        ll fak = n;
        sol *= fak;
        sol %= mod;
    }

    return sol;
}

/*int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int a;
    cin >> a;
    int *niz = (int*)malloc(sizeof(int)*a);
    for(int i = 0; i < a; i++) cin >> niz[i];
    cout << valid(a,niz) << endl;

    int a;
    cin >> a;
    int *niz = (int*)malloc(sizeof(int)*a);
    for(int i = 0; i < a; i++) cin >> niz[i];
    int *drugi = (int*)malloc(sizeof(int)*200);
    int len = replacement(a,niz,drugi);
    cout << len << endl;
    for(int i = 0; i < len; i++){
        cout << drugi[i] << " ";
    }
    cout << endl;

    int a;
    cin >> a;
    int *niz = (int*)malloc(sizeof(int)*a);
    for(int i = 0; i < a; i++) cin >> niz[i];
    cout << countReplacement(a,niz) << endl;
}*/

Compilation message

gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:165:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |     for(ll i = 0; i < v.size(); i++){
      |                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 38 ms 4400 KB Output is correct
7 Correct 13 ms 1908 KB Output is correct
8 Correct 91 ms 8240 KB Output is correct
9 Correct 22 ms 2776 KB Output is correct
10 Correct 90 ms 9464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 38 ms 4404 KB Output is correct
7 Correct 11 ms 1900 KB Output is correct
8 Correct 81 ms 8220 KB Output is correct
9 Correct 22 ms 2756 KB Output is correct
10 Correct 90 ms 9520 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 18 ms 2644 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 65 ms 8848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 308 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 10 ms 1944 KB Output is correct
12 Correct 14 ms 2132 KB Output is correct
13 Correct 20 ms 1816 KB Output is correct
14 Correct 11 ms 1868 KB Output is correct
15 Correct 26 ms 2468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 83 ms 9880 KB Output is correct
10 Correct 57 ms 8464 KB Output is correct
11 Correct 19 ms 3476 KB Output is correct
12 Correct 23 ms 4080 KB Output is correct
13 Correct 5 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 74 ms 9932 KB Output is correct
10 Correct 59 ms 8416 KB Output is correct
11 Correct 19 ms 3500 KB Output is correct
12 Correct 23 ms 4020 KB Output is correct
13 Correct 5 ms 1228 KB Output is correct
14 Correct 83 ms 12416 KB Output is correct
15 Correct 89 ms 13676 KB Output is correct
16 Correct 15 ms 2996 KB Output is correct
17 Correct 58 ms 9000 KB Output is correct
18 Correct 31 ms 5744 KB Output is correct