Submission #102795

# Submission time Handle Problem Language Result Execution time Memory
102795 2019-03-27T14:25:02 Z CorneliuVadimTudor Boat (APIO16_boat) C++14
100 / 100
730 ms 4472 KB
#include <bits/stdc++.h>
#define MAXN 1000
#define MAXG 2000

struct Event{
    int time, node, tip;
    bool operator < (const Event &aux) const{
        return time < aux.time;
    }
};
std::vector <Event> V;
std::set <int> S;
std::vector <int> X;

const int MOD = 1000000007;
int d[1 + MAXG][1 + MAXN];
int C[1 + MAXN], D[1 + MAXN];
int inv[1 + MAXN];

inline int logPow(int a, int n){
    int p = 1;
    while(n){
        if(n % 2) p = 1LL * p * a % MOD;
        a = 1LL * a * a % MOD;
        n /= 2;
    }
    return p;
}

int main(){
    FILE*fi,*fo;
    //fi = fopen("a.in","r");
    //fo = fopen("a.out","w");
    fi = stdin;
    fo = stdout;

    int n;
    fscanf(fi,"%d", &n);
    for(int i = 1; i <= n; i++){
        int a, b;
        fscanf(fi,"%d%d", &a, &b);
        V.push_back({a, i, 1});
        V.push_back({b + 1, i, -1});
    }
    std::sort(V.begin(), V.end());

    for(int i = 1; i <= n; i++)
        inv[i] = logPow(i, MOD - 2);

    int j = 0, group = 0;
    d[group][0] = 1;
    for(int i = 0; i < V.size(); i = j){
        j = i;
        while(j < V.size() && V[j].time == V[i].time){
            if(V[j].tip == 1) S.insert(V[j].node);
            else S.erase(V[j].node);
            j++;
        }
        if(!S.size()) continue;

        X.clear();
        for(auto y: S) X.push_back(y);

        int len = V[j].time - V[i].time;
        C[0] = 1;
        int lim = std::min((int)X.size(), len);
        for(int p = 1; p <= lim; p++){
            C[p] = 1LL * C[p - 1] * (len - p + 1) % MOD;
            C[p] = 1LL * C[p] * inv[p] % MOD;
        }

        for(int x = 0; x <= X.size(); x++){
            int Cxp = 1, lim = std::min(x, len);
            D[x] = 0;
            for(int p = 1; p <= lim; p++){
                Cxp = 1LL * Cxp * (x - p + 1) % MOD;
                Cxp = 1LL * Cxp * inv[p] % MOD;
                D[x] = (D[x] + 1LL * C[p] * Cxp) % MOD;
            }
        }

        group++;
        int first = 0;
        for(int prev = 0; prev <= n; prev++){
            while(first < X.size() && X[first] <= prev) first++;
            int ind = first;

            int x = 0;
            while(ind < X.size()){
                x++;
                d[group][X[ind]] = (d[group][X[ind]] + 1LL * d[group - 1][prev] * D[x]) % MOD;
                ind++;
            }
        }

        int ind = 0;
        int prev = d[group][X[ind]];
        ind++;
        while(ind < X.size()){
            int aux = d[group][X[ind]];
            d[group][X[ind]] = (d[group][X[ind]] - prev + MOD) % MOD;
            prev = aux;
            ind++;
        }

        for(int prev = 0; prev <= n; prev++)
            d[group][prev] = (d[group][prev] + d[group - 1][prev]) % MOD;
    }

    int ans = 0;
    for(int prev = 1; prev <= n; prev++)
        ans = (ans + d[group][prev]) % MOD;

    fprintf(fo,"%lld", ans);

    return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:52:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < V.size(); i = j){
                    ~~^~~~~~~~~~
boat.cpp:54:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(j < V.size() && V[j].time == V[i].time){
               ~~^~~~~~~~~~
boat.cpp:72:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int x = 0; x <= X.size(); x++){
                        ~~^~~~~~~~~~~
boat.cpp:85:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(first < X.size() && X[first] <= prev) first++;
                   ~~~~~~^~~~~~~~~~
boat.cpp:89:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(ind < X.size()){
                   ~~~~^~~~~~~~~~
boat.cpp:99:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(ind < X.size()){
               ~~~~^~~~~~~~~~
boat.cpp:114:27: warning: format '%lld' expects argument of type 'long long int', but argument 3 has type 'int' [-Wformat=]
     fprintf(fo,"%lld", ans);
                           ^
boat.cpp:38:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fi,"%d", &n);
     ~~~~~~^~~~~~~~~~~~~
boat.cpp:41:15: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf(fi,"%d%d", &a, &b);
         ~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 2304 KB Output is correct
2 Correct 7 ms 2304 KB Output is correct
3 Correct 7 ms 2304 KB Output is correct
4 Correct 7 ms 2304 KB Output is correct
5 Correct 7 ms 2304 KB Output is correct
6 Correct 6 ms 2304 KB Output is correct
7 Correct 8 ms 2304 KB Output is correct
8 Correct 5 ms 2304 KB Output is correct
9 Correct 8 ms 2304 KB Output is correct
10 Correct 7 ms 2304 KB Output is correct
11 Correct 5 ms 2304 KB Output is correct
12 Correct 7 ms 2304 KB Output is correct
13 Correct 7 ms 2304 KB Output is correct
14 Correct 6 ms 2304 KB Output is correct
15 Correct 7 ms 2432 KB Output is correct
16 Correct 4 ms 768 KB Output is correct
17 Correct 4 ms 768 KB Output is correct
18 Correct 4 ms 768 KB Output is correct
19 Correct 3 ms 768 KB Output is correct
20 Correct 4 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 2304 KB Output is correct
2 Correct 7 ms 2304 KB Output is correct
3 Correct 7 ms 2304 KB Output is correct
4 Correct 7 ms 2304 KB Output is correct
5 Correct 7 ms 2304 KB Output is correct
6 Correct 6 ms 2304 KB Output is correct
7 Correct 8 ms 2304 KB Output is correct
8 Correct 5 ms 2304 KB Output is correct
9 Correct 8 ms 2304 KB Output is correct
10 Correct 7 ms 2304 KB Output is correct
11 Correct 5 ms 2304 KB Output is correct
12 Correct 7 ms 2304 KB Output is correct
13 Correct 7 ms 2304 KB Output is correct
14 Correct 6 ms 2304 KB Output is correct
15 Correct 7 ms 2432 KB Output is correct
16 Correct 4 ms 768 KB Output is correct
17 Correct 4 ms 768 KB Output is correct
18 Correct 4 ms 768 KB Output is correct
19 Correct 3 ms 768 KB Output is correct
20 Correct 4 ms 768 KB Output is correct
21 Correct 128 ms 3944 KB Output is correct
22 Correct 150 ms 3996 KB Output is correct
23 Correct 149 ms 3960 KB Output is correct
24 Correct 126 ms 3832 KB Output is correct
25 Correct 111 ms 3828 KB Output is correct
26 Correct 174 ms 3884 KB Output is correct
27 Correct 173 ms 3836 KB Output is correct
28 Correct 184 ms 3832 KB Output is correct
29 Correct 181 ms 3784 KB Output is correct
30 Correct 10 ms 4224 KB Output is correct
31 Correct 9 ms 4224 KB Output is correct
32 Correct 12 ms 4252 KB Output is correct
33 Correct 10 ms 4224 KB Output is correct
34 Correct 13 ms 4352 KB Output is correct
35 Correct 9 ms 4224 KB Output is correct
36 Correct 10 ms 4224 KB Output is correct
37 Correct 10 ms 4224 KB Output is correct
38 Correct 11 ms 4224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1212 KB Output is correct
2 Correct 6 ms 1152 KB Output is correct
3 Correct 6 ms 1152 KB Output is correct
4 Correct 6 ms 1152 KB Output is correct
5 Correct 6 ms 1152 KB Output is correct
6 Correct 9 ms 1152 KB Output is correct
7 Correct 9 ms 1152 KB Output is correct
8 Correct 9 ms 1152 KB Output is correct
9 Correct 10 ms 1152 KB Output is correct
10 Correct 10 ms 1152 KB Output is correct
11 Correct 6 ms 1152 KB Output is correct
12 Correct 5 ms 1152 KB Output is correct
13 Correct 7 ms 1152 KB Output is correct
14 Correct 6 ms 1152 KB Output is correct
15 Correct 7 ms 1152 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 5 ms 768 KB Output is correct
18 Correct 4 ms 768 KB Output is correct
19 Correct 5 ms 768 KB Output is correct
20 Correct 4 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 2304 KB Output is correct
2 Correct 7 ms 2304 KB Output is correct
3 Correct 7 ms 2304 KB Output is correct
4 Correct 7 ms 2304 KB Output is correct
5 Correct 7 ms 2304 KB Output is correct
6 Correct 6 ms 2304 KB Output is correct
7 Correct 8 ms 2304 KB Output is correct
8 Correct 5 ms 2304 KB Output is correct
9 Correct 8 ms 2304 KB Output is correct
10 Correct 7 ms 2304 KB Output is correct
11 Correct 5 ms 2304 KB Output is correct
12 Correct 7 ms 2304 KB Output is correct
13 Correct 7 ms 2304 KB Output is correct
14 Correct 6 ms 2304 KB Output is correct
15 Correct 7 ms 2432 KB Output is correct
16 Correct 4 ms 768 KB Output is correct
17 Correct 4 ms 768 KB Output is correct
18 Correct 4 ms 768 KB Output is correct
19 Correct 3 ms 768 KB Output is correct
20 Correct 4 ms 768 KB Output is correct
21 Correct 128 ms 3944 KB Output is correct
22 Correct 150 ms 3996 KB Output is correct
23 Correct 149 ms 3960 KB Output is correct
24 Correct 126 ms 3832 KB Output is correct
25 Correct 111 ms 3828 KB Output is correct
26 Correct 174 ms 3884 KB Output is correct
27 Correct 173 ms 3836 KB Output is correct
28 Correct 184 ms 3832 KB Output is correct
29 Correct 181 ms 3784 KB Output is correct
30 Correct 10 ms 4224 KB Output is correct
31 Correct 9 ms 4224 KB Output is correct
32 Correct 12 ms 4252 KB Output is correct
33 Correct 10 ms 4224 KB Output is correct
34 Correct 13 ms 4352 KB Output is correct
35 Correct 9 ms 4224 KB Output is correct
36 Correct 10 ms 4224 KB Output is correct
37 Correct 10 ms 4224 KB Output is correct
38 Correct 11 ms 4224 KB Output is correct
39 Correct 6 ms 1212 KB Output is correct
40 Correct 6 ms 1152 KB Output is correct
41 Correct 6 ms 1152 KB Output is correct
42 Correct 6 ms 1152 KB Output is correct
43 Correct 6 ms 1152 KB Output is correct
44 Correct 9 ms 1152 KB Output is correct
45 Correct 9 ms 1152 KB Output is correct
46 Correct 9 ms 1152 KB Output is correct
47 Correct 10 ms 1152 KB Output is correct
48 Correct 10 ms 1152 KB Output is correct
49 Correct 6 ms 1152 KB Output is correct
50 Correct 5 ms 1152 KB Output is correct
51 Correct 7 ms 1152 KB Output is correct
52 Correct 6 ms 1152 KB Output is correct
53 Correct 7 ms 1152 KB Output is correct
54 Correct 5 ms 768 KB Output is correct
55 Correct 5 ms 768 KB Output is correct
56 Correct 4 ms 768 KB Output is correct
57 Correct 5 ms 768 KB Output is correct
58 Correct 4 ms 768 KB Output is correct
59 Correct 345 ms 4316 KB Output is correct
60 Correct 320 ms 4344 KB Output is correct
61 Correct 346 ms 4344 KB Output is correct
62 Correct 354 ms 4344 KB Output is correct
63 Correct 357 ms 4216 KB Output is correct
64 Correct 695 ms 4284 KB Output is correct
65 Correct 685 ms 4344 KB Output is correct
66 Correct 730 ms 4472 KB Output is correct
67 Correct 666 ms 4376 KB Output is correct
68 Correct 699 ms 4448 KB Output is correct
69 Correct 295 ms 4232 KB Output is correct
70 Correct 283 ms 4356 KB Output is correct
71 Correct 276 ms 4264 KB Output is correct
72 Correct 283 ms 4312 KB Output is correct
73 Correct 279 ms 4280 KB Output is correct
74 Correct 44 ms 1144 KB Output is correct
75 Correct 42 ms 1144 KB Output is correct
76 Correct 49 ms 1144 KB Output is correct
77 Correct 46 ms 1144 KB Output is correct
78 Correct 47 ms 1144 KB Output is correct