Submission #670568

# Submission time Handle Problem Language Result Execution time Memory
670568 2022-12-09T14:38:30 Z omikron123 A Huge Tower (CEOI10_tower) C++14
100 / 100
160 ms 9364 KB
// https://oj.uz/problem/view/CEOI10_tower

#include <cstdio>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
using namespace std;

// 这个有点数学题:
// 假设前i-1个块(从小到大排序)可以组成的方案有ans_i-1个,设b_i是满足以下式子的j数量:
//     j < i, a_i <= a_j + d
// 可以证明对于任何一个i-1个块的方案,都有b_i+1种办法,可以把第i个块插入进去。
// 具体说:一种办法是放最下面,另外b_i种是放在满足这个条件的石头的上面,其它地方都不合适。
// 所以这个题就可以通过递推来解决了。

int n, d;
vector<int> a;

typedef long long ll;

ll ans = 1;

int main() {
    scanf("%d %d", &n, &d);
    for (int i = 0; i < n; i++) {
        int v;
        scanf("%d", &v);
        a.push_back(v);
    }
    sort(a.begin(), a.end());
    int j = 0;
    for (int i = 0; i < n; i++) {
        while (j < i && a[j] + d < a[i]) {
            j++;
        }
        ans = ans * (i-j+1) % ((ll)1e9 + 9);
    }

    printf("%lld", ans);

    return 0;
}

Compilation message

tower.cpp: In function 'int main()':
tower.cpp:25:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |     scanf("%d %d", &n, &d);
      |     ~~~~~^~~~~~~~~~~~~~~~~
tower.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         scanf("%d", &v);
      |         ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 588 KB Output is correct
2 Correct 11 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 2608 KB Output is correct
2 Correct 63 ms 3744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 8712 KB Output is correct
2 Correct 160 ms 9364 KB Output is correct