This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |