/*
Solution for APIO 2016 - Boat
Tags :
*/
#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)) ; //
new_val = mul(new_val, binom);
val[k] = new_val;
}
FOR(k,1,i+2){
if (k>=cnt[j+l[i]].size()){
if (val[k] == 0) break;
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:10: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
10 | #pragma GCC optimization ("Ofast")
|
boat.cpp:11: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
11 | #pragma GCC optimization ("unroll-loops")
|
boat.cpp: In function 'void solve()':
boat.cpp:99:39: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
99 | if (cnt[j+l[i]].size()>=k) new_val = cnt[j+l[i]][k-1];
| ~~~~~~~~~~~~~~~~~~^~~
boat.cpp:109:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | if (k>=cnt[j+l[i]].size()){
| ~^~~~~~~~~~~~~~~~~~~~
boat.cpp: In function 'int main()':
boat.cpp:130:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
boat.cpp:131:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
340 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 |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
340 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 |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
21 |
Correct |
72 ms |
488 KB |
Output is correct |
22 |
Correct |
72 ms |
500 KB |
Output is correct |
23 |
Correct |
66 ms |
496 KB |
Output is correct |
24 |
Correct |
95 ms |
508 KB |
Output is correct |
25 |
Correct |
72 ms |
496 KB |
Output is correct |
26 |
Correct |
87 ms |
468 KB |
Output is correct |
27 |
Correct |
86 ms |
472 KB |
Output is correct |
28 |
Correct |
113 ms |
596 KB |
Output is correct |
29 |
Correct |
87 ms |
484 KB |
Output is correct |
30 |
Correct |
4 ms |
484 KB |
Output is correct |
31 |
Correct |
4 ms |
476 KB |
Output is correct |
32 |
Correct |
4 ms |
468 KB |
Output is correct |
33 |
Correct |
4 ms |
468 KB |
Output is correct |
34 |
Correct |
4 ms |
464 KB |
Output is correct |
35 |
Correct |
3 ms |
356 KB |
Output is correct |
36 |
Correct |
4 ms |
340 KB |
Output is correct |
37 |
Correct |
4 ms |
468 KB |
Output is correct |
38 |
Correct |
3 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
468 KB |
Output is correct |
2 |
Correct |
5 ms |
340 KB |
Output is correct |
3 |
Correct |
6 ms |
468 KB |
Output is correct |
4 |
Correct |
7 ms |
464 KB |
Output is correct |
5 |
Correct |
7 ms |
476 KB |
Output is correct |
6 |
Correct |
14 ms |
404 KB |
Output is correct |
7 |
Correct |
14 ms |
480 KB |
Output is correct |
8 |
Correct |
14 ms |
468 KB |
Output is correct |
9 |
Correct |
15 ms |
488 KB |
Output is correct |
10 |
Correct |
14 ms |
444 KB |
Output is correct |
11 |
Correct |
7 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
8 ms |
340 KB |
Output is correct |
14 |
Correct |
7 ms |
444 KB |
Output is correct |
15 |
Correct |
8 ms |
448 KB |
Output is correct |
16 |
Correct |
4 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
412 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
4 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
340 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 |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
21 |
Correct |
72 ms |
488 KB |
Output is correct |
22 |
Correct |
72 ms |
500 KB |
Output is correct |
23 |
Correct |
66 ms |
496 KB |
Output is correct |
24 |
Correct |
95 ms |
508 KB |
Output is correct |
25 |
Correct |
72 ms |
496 KB |
Output is correct |
26 |
Correct |
87 ms |
468 KB |
Output is correct |
27 |
Correct |
86 ms |
472 KB |
Output is correct |
28 |
Correct |
113 ms |
596 KB |
Output is correct |
29 |
Correct |
87 ms |
484 KB |
Output is correct |
30 |
Correct |
4 ms |
484 KB |
Output is correct |
31 |
Correct |
4 ms |
476 KB |
Output is correct |
32 |
Correct |
4 ms |
468 KB |
Output is correct |
33 |
Correct |
4 ms |
468 KB |
Output is correct |
34 |
Correct |
4 ms |
464 KB |
Output is correct |
35 |
Correct |
3 ms |
356 KB |
Output is correct |
36 |
Correct |
4 ms |
340 KB |
Output is correct |
37 |
Correct |
4 ms |
468 KB |
Output is correct |
38 |
Correct |
3 ms |
360 KB |
Output is correct |
39 |
Correct |
7 ms |
468 KB |
Output is correct |
40 |
Correct |
5 ms |
340 KB |
Output is correct |
41 |
Correct |
6 ms |
468 KB |
Output is correct |
42 |
Correct |
7 ms |
464 KB |
Output is correct |
43 |
Correct |
7 ms |
476 KB |
Output is correct |
44 |
Correct |
14 ms |
404 KB |
Output is correct |
45 |
Correct |
14 ms |
480 KB |
Output is correct |
46 |
Correct |
14 ms |
468 KB |
Output is correct |
47 |
Correct |
15 ms |
488 KB |
Output is correct |
48 |
Correct |
14 ms |
444 KB |
Output is correct |
49 |
Correct |
7 ms |
468 KB |
Output is correct |
50 |
Correct |
5 ms |
340 KB |
Output is correct |
51 |
Correct |
8 ms |
340 KB |
Output is correct |
52 |
Correct |
7 ms |
444 KB |
Output is correct |
53 |
Correct |
8 ms |
448 KB |
Output is correct |
54 |
Correct |
4 ms |
340 KB |
Output is correct |
55 |
Correct |
4 ms |
412 KB |
Output is correct |
56 |
Correct |
3 ms |
340 KB |
Output is correct |
57 |
Correct |
4 ms |
340 KB |
Output is correct |
58 |
Correct |
4 ms |
340 KB |
Output is correct |
59 |
Correct |
775 ms |
2412 KB |
Output is correct |
60 |
Correct |
700 ms |
2396 KB |
Output is correct |
61 |
Correct |
664 ms |
2264 KB |
Output is correct |
62 |
Correct |
780 ms |
2824 KB |
Output is correct |
63 |
Correct |
721 ms |
2236 KB |
Output is correct |
64 |
Correct |
1772 ms |
3200 KB |
Output is correct |
65 |
Correct |
1813 ms |
3076 KB |
Output is correct |
66 |
Correct |
1810 ms |
3244 KB |
Output is correct |
67 |
Correct |
1861 ms |
3260 KB |
Output is correct |
68 |
Correct |
1852 ms |
3132 KB |
Output is correct |
69 |
Correct |
749 ms |
2188 KB |
Output is correct |
70 |
Correct |
782 ms |
2308 KB |
Output is correct |
71 |
Correct |
774 ms |
2164 KB |
Output is correct |
72 |
Correct |
781 ms |
2132 KB |
Output is correct |
73 |
Correct |
814 ms |
2228 KB |
Output is correct |
74 |
Correct |
125 ms |
672 KB |
Output is correct |
75 |
Correct |
116 ms |
668 KB |
Output is correct |
76 |
Correct |
121 ms |
644 KB |
Output is correct |
77 |
Correct |
115 ms |
640 KB |
Output is correct |
78 |
Correct |
121 ms |
740 KB |
Output is correct |