Submission #315989

# Submission time Handle Problem Language Result Execution time Memory
315989 2020-10-24T17:08:07 Z phathnv Boat (APIO16_boat) C++11
9 / 100
476 ms 4352 KB
#include <bits/stdc++.h>

#define mp make_pair
#define X first
#define Y second
#define taskname "Boat"

using namespace std;

typedef long long ll;
typedef pair <int, int> ii;

const int N = 502;
const int MOD = 1e9 + 7;

int n, a[N], b[N];
int nX, x[2 * N], inv[N], dp[2][N * 2][N], sumDp[N * 2];

void readInput(){
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i] >> b[i];
}

void compress(){
    for(int i = 1; i <= n; i++){
        x[++nX] = a[i];
        x[++nX] = ++b[i];
    }
    sort(x + 1, x + 1 + nX);
    nX = unique(x + 1, x + 1 + nX) - (x + 1);
    for(int i = 1; i <= n; i++){
        a[i] = lower_bound(x + 1, x + 1 + nX, a[i]) - x;
        b[i] = lower_bound(x + 1, x + 1 + nX, b[i]) - x;
    }
}

int powMod(int x, int k){
    if (k == 0)
        return 1;
    int res = powMod(x, k >> 1);
    res = (ll) res * res % MOD;
    if (k & 1)
        res = (ll) res * x % MOD;
    return res;
}

void prepare(){
    for(int i = 1; i <= n; i++)
        b[i]--;
    inv[0] = 1;
    for(int i = 1; i <= n; i++)
        inv[i] = powMod(i, MOD - 2);
}

void add(int &x, int y){
    x += y;
    x -= (x >= MOD) * MOD;
}

void solve(){
    int res = 0;
    sumDp[0] = 1;
    for(int i = 0; i < n; i++){
        int id = i & 1;
        for(int j = 1; j < nX; j++){
            int len = min(n, x[j + 1] - x[j]);
            for(int k = 1; k <= len; k++)
                dp[id ^ 1][j][k] = 0;
        }

        int sum = 0;
        for(int j = 1; j < nX; j++){
            int len = min(n, x[j + 1] - x[j]);
            add(sum, sumDp[j - 1]);
            if (a[i + 1] <= j && j <= b[i + 1])
                add(dp[id ^ 1][j][1], (ll) sum * len % MOD);
            for(int k = 1; k <= len; k++){
                add(sumDp[j], dp[id][j][k]);
                if (a[i + 1] <= j && j <= b[i + 1] && k < len)
                    add(dp[id ^ 1][j][k + 1], (ll) dp[id][j][k] * (len - k) % MOD * inv[k + 1] % MOD);
            }
        }
        for(int j = 1; j < nX; j++){
            int len = min(n, x[j + 1] - x[j]);
            for(int k = 1; k <= len; k++)
                add(res, dp[id ^ 1][j][k]);
        }
    }

    cout << res;
}

int main(){
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    readInput();
    compress();
    prepare();
    solve();
    return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   96 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
boat.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   97 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 474 ms 4344 KB Output is correct
2 Correct 471 ms 4344 KB Output is correct
3 Correct 470 ms 4300 KB Output is correct
4 Correct 474 ms 4344 KB Output is correct
5 Correct 470 ms 4344 KB Output is correct
6 Correct 473 ms 4344 KB Output is correct
7 Correct 468 ms 4344 KB Output is correct
8 Correct 470 ms 4224 KB Output is correct
9 Correct 466 ms 4344 KB Output is correct
10 Correct 476 ms 4352 KB Output is correct
11 Correct 468 ms 4224 KB Output is correct
12 Correct 467 ms 4344 KB Output is correct
13 Correct 465 ms 4344 KB Output is correct
14 Correct 475 ms 4296 KB Output is correct
15 Correct 466 ms 4344 KB Output is correct
16 Correct 81 ms 1144 KB Output is correct
17 Correct 89 ms 1024 KB Output is correct
18 Correct 83 ms 1024 KB Output is correct
19 Correct 87 ms 1024 KB Output is correct
20 Correct 87 ms 1144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 474 ms 4344 KB Output is correct
2 Correct 471 ms 4344 KB Output is correct
3 Correct 470 ms 4300 KB Output is correct
4 Correct 474 ms 4344 KB Output is correct
5 Correct 470 ms 4344 KB Output is correct
6 Correct 473 ms 4344 KB Output is correct
7 Correct 468 ms 4344 KB Output is correct
8 Correct 470 ms 4224 KB Output is correct
9 Correct 466 ms 4344 KB Output is correct
10 Correct 476 ms 4352 KB Output is correct
11 Correct 468 ms 4224 KB Output is correct
12 Correct 467 ms 4344 KB Output is correct
13 Correct 465 ms 4344 KB Output is correct
14 Correct 475 ms 4296 KB Output is correct
15 Correct 466 ms 4344 KB Output is correct
16 Correct 81 ms 1144 KB Output is correct
17 Correct 89 ms 1024 KB Output is correct
18 Correct 83 ms 1024 KB Output is correct
19 Correct 87 ms 1024 KB Output is correct
20 Correct 87 ms 1144 KB Output is correct
21 Incorrect 31 ms 3968 KB Output isn't correct
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 1168 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 474 ms 4344 KB Output is correct
2 Correct 471 ms 4344 KB Output is correct
3 Correct 470 ms 4300 KB Output is correct
4 Correct 474 ms 4344 KB Output is correct
5 Correct 470 ms 4344 KB Output is correct
6 Correct 473 ms 4344 KB Output is correct
7 Correct 468 ms 4344 KB Output is correct
8 Correct 470 ms 4224 KB Output is correct
9 Correct 466 ms 4344 KB Output is correct
10 Correct 476 ms 4352 KB Output is correct
11 Correct 468 ms 4224 KB Output is correct
12 Correct 467 ms 4344 KB Output is correct
13 Correct 465 ms 4344 KB Output is correct
14 Correct 475 ms 4296 KB Output is correct
15 Correct 466 ms 4344 KB Output is correct
16 Correct 81 ms 1144 KB Output is correct
17 Correct 89 ms 1024 KB Output is correct
18 Correct 83 ms 1024 KB Output is correct
19 Correct 87 ms 1024 KB Output is correct
20 Correct 87 ms 1144 KB Output is correct
21 Incorrect 31 ms 3968 KB Output isn't correct
22 Halted 0 ms 0 KB -