Submission #557332

# Submission time Handle Problem Language Result Execution time Memory
557332 2022-05-05T06:58:14 Z kwongweng Boat (APIO16_boat) C++17
100 / 100
1765 ms 3276 KB
/*
Solution for APIO 2016 - Boat
Tags : dp
Complexity O(n^3)
Remark 1 : dp optimisation required for n = 500
Remark 2 : splitting intervals with size <= 10^9 into O(n) compartments
*/
 
#include <bits/stdc++.h>
using namespace std;
 
#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
 
ll MOD = 1000000007;
 
ll power(ll base, ll n){
	if (n == 0) return 1;
	if (n == 1) return base;
	ll halfn = power(base, n/2);
	if (n % 2 == 0) return (halfn * halfn) % MOD;
	return (((halfn * halfn) % MOD) * base) % MOD;
}
 
vector<ll> inv(501,-1);
ll inverse(ll n){
    if (inv[n] != -1) return inv[n];
	return inv[n] = power(n, MOD-2);
}
 
ll add(ll a, ll b){
	return (a+b) % MOD;
}
 
ll mul(ll a, ll b){
	a %= MOD;
	return (a*b) % MOD;
}
 
ll gcd(ll a, ll b){
    if (a == 1) return 1;
    if (a == 0) return b;
    return gcd(b%a, a);
}
 
vector<ll> cnt[1000];
// cnt[i][j] = # solution for first ith school with last student sent is in ith compartment from left, and there are j students in the compartment
 
void solve(){
    int n; cin >> n;
    vector<ll> a(n), b(n); FOR(i,0,n) cin >> a[i] >> b[i];
    vector<pair<ll,int>> pos;
    FOR(i,0,n){
        b[i]++;
        pos.pb({a[i], i});
        pos.pb({b[i], i});
    }
    sort(pos.begin(), pos.end());
    vi l(n,-1), r(n,-1);
    FOR(i,0,2*n){
        int j = pos[i].se;
        if (l[j] == -1) l[j] = i;
        else r[j] = i;
    }
    FOR(i,0,2*n-1){
        cnt[i].pb(0);
    }
    FOR(i,0,n){
        ll total = 1;
        FOR(j,0,l[i]){
            for (ll val2 : cnt[j]){
                total += val2;
                total %= MOD;
            }
        }
        //compartment from l[i] to r[i]
        vector<ll> val(i+2);
        FOR(j,0,r[i]-l[i]){
            val[0] = 0;
            ll interval = pos[j+l[i]+1].fi - pos[j+l[i]].fi;
            val[1] = mul(interval,total);
            for (ll val2 : cnt[j+l[i]]){
                total += val2;
                total %= MOD;
            }
            FOR(k,2,i+2){
                ll new_val = 0;
                if (cnt[j+l[i]].size()>=k) new_val = cnt[j+l[i]][k-1];
                if (new_val == 0){
                    val[k] = 0;
                    continue;
                }
                ll binom = mul(interval - k + 1, inverse(k)) ; // 
                //binom = (interval)C(k) / (interval)C(k-1)
                new_val = mul(new_val, binom);
                val[k] = new_val;
                //#solution with k ending elements in l[i]
            }
            FOR(k,1,i+2){
                if (k>=cnt[j+l[i]].size()){
                    if (val[k] == 0) break;
                    // dp optimisation
                    // saves space and time since val[k] = 0 ==> val[k+1] = 0 too
                    cnt[j+l[i]].pb(val[k]);
                    continue;
                } 
                cnt[j+l[i]][k] = (cnt[j+l[i]][k] + val[k]) % MOD;
            }
        }
    }
    ll ans = 0;
    FOR(i,0,2*n-1){
        for (ll val2 : cnt[i]){
            ans = add(ans, val2);
        }
    }
    cout << ans;
}
 
int main() {
    ios::sync_with_stdio(false);
    if (fopen("input.txt", "r")) {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
    int TC = 1;
    //cin >> TC;
    FOR(i, 1, TC+1){
        //cout << "Case #" << i << ": ";
        solve();
    }
}

Compilation message

boat.cpp:13: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   13 | #pragma GCC optimization ("Ofast")
      | 
boat.cpp:14: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   14 | #pragma GCC optimization ("unroll-loops")
      | 
boat.cpp: In function 'void solve()':
boat.cpp:102:39: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  102 |                 if (cnt[j+l[i]].size()>=k) new_val = cnt[j+l[i]][k-1];
      |                     ~~~~~~~~~~~~~~~~~~^~~
boat.cpp:114:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |                 if (k>=cnt[j+l[i]].size()){
      |                     ~^~~~~~~~~~~~~~~~~~~~
boat.cpp: In function 'int main()':
boat.cpp:137:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |   freopen("input.txt", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
boat.cpp:138:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |   freopen("output.txt", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 3 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 356 KB Output is correct
5 Correct 3 ms 360 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 4 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 4 ms 340 KB Output is correct
15 Correct 3 ms 352 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 4 ms 340 KB Output is correct
18 Correct 4 ms 456 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 3 ms 356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 3 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 356 KB Output is correct
5 Correct 3 ms 360 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 4 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 4 ms 340 KB Output is correct
15 Correct 3 ms 352 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 4 ms 340 KB Output is correct
18 Correct 4 ms 456 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 3 ms 356 KB Output is correct
21 Correct 75 ms 496 KB Output is correct
22 Correct 69 ms 496 KB Output is correct
23 Correct 71 ms 468 KB Output is correct
24 Correct 71 ms 504 KB Output is correct
25 Correct 70 ms 500 KB Output is correct
26 Correct 89 ms 468 KB Output is correct
27 Correct 92 ms 468 KB Output is correct
28 Correct 97 ms 468 KB Output is correct
29 Correct 89 ms 476 KB Output is correct
30 Correct 4 ms 356 KB Output is correct
31 Correct 4 ms 468 KB Output is correct
32 Correct 4 ms 468 KB Output is correct
33 Correct 4 ms 360 KB Output is correct
34 Correct 4 ms 356 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 3 ms 352 KB Output is correct
37 Correct 3 ms 340 KB Output is correct
38 Correct 4 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 460 KB Output is correct
2 Correct 6 ms 348 KB Output is correct
3 Correct 7 ms 444 KB Output is correct
4 Correct 7 ms 468 KB Output is correct
5 Correct 8 ms 472 KB Output is correct
6 Correct 15 ms 480 KB Output is correct
7 Correct 14 ms 468 KB Output is correct
8 Correct 14 ms 484 KB Output is correct
9 Correct 13 ms 432 KB Output is correct
10 Correct 14 ms 468 KB Output is correct
11 Correct 8 ms 468 KB Output is correct
12 Correct 6 ms 340 KB Output is correct
13 Correct 8 ms 468 KB Output is correct
14 Correct 8 ms 340 KB Output is correct
15 Correct 8 ms 340 KB Output is correct
16 Correct 5 ms 340 KB Output is correct
17 Correct 4 ms 468 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 3 ms 344 KB Output is correct
20 Correct 4 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 3 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 356 KB Output is correct
5 Correct 3 ms 360 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 4 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 4 ms 340 KB Output is correct
15 Correct 3 ms 352 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 4 ms 340 KB Output is correct
18 Correct 4 ms 456 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 3 ms 356 KB Output is correct
21 Correct 75 ms 496 KB Output is correct
22 Correct 69 ms 496 KB Output is correct
23 Correct 71 ms 468 KB Output is correct
24 Correct 71 ms 504 KB Output is correct
25 Correct 70 ms 500 KB Output is correct
26 Correct 89 ms 468 KB Output is correct
27 Correct 92 ms 468 KB Output is correct
28 Correct 97 ms 468 KB Output is correct
29 Correct 89 ms 476 KB Output is correct
30 Correct 4 ms 356 KB Output is correct
31 Correct 4 ms 468 KB Output is correct
32 Correct 4 ms 468 KB Output is correct
33 Correct 4 ms 360 KB Output is correct
34 Correct 4 ms 356 KB Output is correct
35 Correct 4 ms 340 KB Output is correct
36 Correct 3 ms 352 KB Output is correct
37 Correct 3 ms 340 KB Output is correct
38 Correct 4 ms 340 KB Output is correct
39 Correct 7 ms 460 KB Output is correct
40 Correct 6 ms 348 KB Output is correct
41 Correct 7 ms 444 KB Output is correct
42 Correct 7 ms 468 KB Output is correct
43 Correct 8 ms 472 KB Output is correct
44 Correct 15 ms 480 KB Output is correct
45 Correct 14 ms 468 KB Output is correct
46 Correct 14 ms 484 KB Output is correct
47 Correct 13 ms 432 KB Output is correct
48 Correct 14 ms 468 KB Output is correct
49 Correct 8 ms 468 KB Output is correct
50 Correct 6 ms 340 KB Output is correct
51 Correct 8 ms 468 KB Output is correct
52 Correct 8 ms 340 KB Output is correct
53 Correct 8 ms 340 KB Output is correct
54 Correct 5 ms 340 KB Output is correct
55 Correct 4 ms 468 KB Output is correct
56 Correct 3 ms 340 KB Output is correct
57 Correct 3 ms 344 KB Output is correct
58 Correct 4 ms 348 KB Output is correct
59 Correct 766 ms 2388 KB Output is correct
60 Correct 697 ms 2240 KB Output is correct
61 Correct 667 ms 2420 KB Output is correct
62 Correct 778 ms 2644 KB Output is correct
63 Correct 728 ms 2300 KB Output is correct
64 Correct 1765 ms 3252 KB Output is correct
65 Correct 1730 ms 3228 KB Output is correct
66 Correct 1712 ms 3152 KB Output is correct
67 Correct 1714 ms 3276 KB Output is correct
68 Correct 1715 ms 3148 KB Output is correct
69 Correct 712 ms 2136 KB Output is correct
70 Correct 741 ms 2216 KB Output is correct
71 Correct 719 ms 2280 KB Output is correct
72 Correct 761 ms 2204 KB Output is correct
73 Correct 770 ms 2308 KB Output is correct
74 Correct 115 ms 672 KB Output is correct
75 Correct 108 ms 552 KB Output is correct
76 Correct 113 ms 672 KB Output is correct
77 Correct 107 ms 668 KB Output is correct
78 Correct 119 ms 680 KB Output is correct