Submission #337012

# Submission time Handle Problem Language Result Execution time Memory
337012 2020-12-17T21:29:22 Z SavicS A Huge Tower (CEOI10_tower) C++14
100 / 100
168 ms 37088 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
const int maxn = 1000005;
const int inf = 1e9 + 5;
const int mod = 1e9 + 9;

int n, d;
int niz[maxn];

ll ans[maxn];

vector<pii> vec;

bool cmp(int s1, int s2){
    return s1 > s2;
}

ll fak[maxn];
ll invz[maxn];
ll add(ll a, ll b){
    return (a + b) % mod;
}
ll mul(ll a, ll b){
    return (a * b) % mod;
}
ll power(ll a, ll b){
    if(!b)return 1;
    ll pola = power(a, b / 2);
    pola = mul(pola, pola);
    if(b % 2)pola = mul(pola, a);
    return pola;
}
ll inv(ll a){
    return power(a, mod - 2);
}
ll bin(int a, int b){
    return mul(fak[a], mul(invz[b], invz[a - b]));
}
void init(){
    fak[0] = 1;
    ff(i,1,maxn - 1)fak[i] = mul(fak[i - 1], i);
    invz[maxn - 1] = inv(fak[maxn - 1]);
    fb(i,maxn - 2, 0)invz[i] = mul(invz[i + 1], i + 1);
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> n >> d;
    init();
    ff(i,1,n)cin >> niz[i];
    sort(niz + 1, niz + n + 1);
    int j = 1;
    ll br = 0;
    ff(i,1,n){
        br++;
        while(niz[i] > niz[j] + d){
            br--;
            j++;
        }
        ans[i] = br;
    }
    ff(i,1,n){
        if(ans[i] >= ans[i + 1])vec.pb({(ll)i, ans[i]});
    }
//    cout << endl;
//    for(auto c : vec)cout << c.fi << " " << c.se << endl;
//    cout << endl;
    ll rez = fak[vec[0].se];
    int len = (int)vec.size();
    ff(i,1,len - 1){
        ll x = vec[i].fi;
        ll y = vec[i].se;
        rez = mul(rez, fak[y]);
        if(x - y + 1 <= vec[i - 1].fi){
            ll presek = vec[i - 1].fi - (x - y + 1) + 1;
            rez = mul(rez, invz[presek]);
        }
    }
    cout << rez << endl;
    return 0;
}
/**

7 4
1 2 3 4 5 6 7

4 1
1 2 3 100

6 9
10 20 20 10 10 20

// probati bojenje sahovski ili slicno

**/
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 16108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 16108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 16108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 16108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 17264 KB Output is correct
2 Correct 31 ms 16748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 22376 KB Output is correct
2 Correct 72 ms 21356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 28268 KB Output is correct
2 Correct 168 ms 37088 KB Output is correct