Submission #780477

# Submission time Handle Problem Language Result Execution time Memory
780477 2023-07-12T09:17:47 Z fatemetmhr Gondola (IOI14_gondola) C++17
100 / 100
31 ms 3144 KB
//  ~ Be Name Khoda ~  //

#include "gondola.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  1e6   + 10;
const int maxn5 =  5e5   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   + 9;
const ll  inf   =  1e18;

vector <int> av, num;


int valid(int n, int a[])
{
    for(int i =0 ; i < n; i++){
        a[i]--;
        num.pb(a[i]);
    }
    sort(all(num));
    num.resize(unique(all(num)) - num.begin());
    if(num.size() != n)
        return 0;
    for(int i = 0; i < n; i++) if(a[i] < n)
        av.pb(i);
    int mnid = 0;
    if(av.empty())
        return 1;
    for(int i = 0; i < av.size(); i++) if(a[av[i]] < a[av[mnid]])
        mnid = i;
    int keep = mnid;
    //cout << av.size() << endl;
    for(int i = 0; i < av.size(); i++){
        int nxt = mnid + 1;
        if(nxt == av.size())
            nxt = 0;
        int u = av[mnid], v = av[nxt];
        //cout << "here " << mnid << ' ' << nxt << ' ' << u << ' ' << v << endl;
        if((nxt != keep && a[u] > a[v]) || (n + a[v] - a[u]) % n != (v - u + n) % n)
            return 0;
        mnid = nxt;
    }
    return 1;
}

//----------------------

int replacement(int n, int a[], int ret[])
{   
    vector <pair<int, int>> av;
    for(int i = 0; i < n; i++)
        a[i]--;
    int mx = 0, base = -1;
    for(int i = 0; i < n; i++){
        if(a[i] > a[mx])
            mx = i;
        if(a[i] < n)
            base = i;
    }
    //cout << a[mx] << ' ' << n << endl;
    if(a[mx] == n - 1)
        return 0;
    for(int i = 0; i < n; i++) if(a[i] >= n && i != mx)
        av.pb({a[i], i});
    sort(all(av));
    int ind = 0;
    int pt = 0;
    int mxx = a[mx];
    a[mx] = (base == -1 ? mx : ((n + mx - base) + a[base]) % n);
    for(int x = n; x <= mxx; x++){
        if(ind < av.size() && av[ind].fi == x){
            ret[pt++] = 1 + (base == -1 ? av[ind].se : ((n + av[ind].se - base) + a[base]) % n);
            ind++;
        }
        else{
            ret[pt++] = 1 + a[mx];
            a[mx] = x;
        }
        //cout << pt << ' ' << x << endl;
    }
    return pt;
}

//----------------------

ll pw(ll a, ll b){
    ll ret = 1;
    for(; b; b >>= 1, a = a * a % mod)
        if(b & 1) ret = ret * a % mod;
    return ret;
}


int countReplacement(int n, int a[])
{
    if(!valid(n, a))
        return 0;
    bool ok = true;
    for(int i = 0; i < n; i++)
        ok &= (a[i] < n);
    if(ok)
        return 1;
    vector <pair<int, int>> av;
    for(int i = 0; i < n; i++) if(a[i] >= n)
        av.pb({a[i], i});
    ll rem = av.size(), ans = 1;
    if(av.size() == n)
        ans = n;
    sort(all(av));
    int ind = 0;
    int last = n;
    for(int i = 0; i < av.size(); i++){
        ans = ans * pw(int(av.size()) - i, av[i].fi - last) % mod;
        last = av[i].fi + 1;
    }
    return ans;

}


















Compilation message

gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:37:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |     if(num.size() != n)
      |        ~~~~~~~~~~~^~~~
gondola.cpp:44:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for(int i = 0; i < av.size(); i++) if(a[av[i]] < a[av[mnid]])
      |                    ~~^~~~~~~~~~~
gondola.cpp:48:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |     for(int i = 0; i < av.size(); i++){
      |                    ~~^~~~~~~~~~~
gondola.cpp:50:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         if(nxt == av.size())
      |            ~~~~^~~~~~~~~~~~
gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:86:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |         if(ind < av.size() && av[ind].fi == x){
      |            ~~~~^~~~~~~~~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:122:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  122 |     if(av.size() == n)
      |        ~~~~~~~~~~^~~~
gondola.cpp:127:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |     for(int i = 0; i < av.size(); i++){
      |                    ~~^~~~~~~~~~~
gondola.cpp:121:8: warning: unused variable 'rem' [-Wunused-variable]
  121 |     ll rem = av.size(), ans = 1;
      |        ^~~
gondola.cpp:125:9: warning: unused variable 'ind' [-Wunused-variable]
  125 |     int ind = 0;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 6 ms 1004 KB Output is correct
7 Correct 11 ms 1232 KB Output is correct
8 Correct 8 ms 1612 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
10 Correct 12 ms 1612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 6 ms 1008 KB Output is correct
7 Correct 11 ms 1232 KB Output is correct
8 Correct 9 ms 1568 KB Output is correct
9 Correct 4 ms 596 KB Output is correct
10 Correct 12 ms 1740 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 5 ms 724 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 13 ms 1384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 10 ms 596 KB Output is correct
12 Correct 6 ms 596 KB Output is correct
13 Correct 10 ms 1252 KB Output is correct
14 Correct 6 ms 596 KB Output is correct
15 Correct 16 ms 2252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 16 ms 1612 KB Output is correct
10 Correct 12 ms 1568 KB Output is correct
11 Correct 7 ms 852 KB Output is correct
12 Correct 9 ms 852 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 15 ms 1664 KB Output is correct
10 Correct 12 ms 1616 KB Output is correct
11 Correct 6 ms 852 KB Output is correct
12 Correct 6 ms 852 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 20 ms 2104 KB Output is correct
15 Correct 31 ms 3144 KB Output is correct
16 Correct 5 ms 980 KB Output is correct
17 Correct 15 ms 1968 KB Output is correct
18 Correct 9 ms 1492 KB Output is correct