Submission #555112

# Submission time Handle Problem Language Result Execution time Memory
555112 2022-04-30T07:36:13 Z kwongweng Boat (APIO16_boat) C++17
31 / 100
2000 ms 490128 KB
/*
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;
}
 
ll inverse(ll n){
	return 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);
}

void solve(){
    int n; cin >> n;
    vector<ll> a(n), b(n); FOR(i,0,n) cin >> a[i] >> b[i];
    vector<ll> dp[n];
    vector<ll> pre[n];
    map<ll, ll> mp;
    ll ans = 0;
    
    FOR(i,0,n){
        dp[i].pb(1);
        FOR(j,0,i){
            if (a[i] < a[j]) continue;
            dp[i][0] += pre[j][min(b[j]-a[j]+1, a[i]-a[j])];
            dp[i][0] %= MOD;
        }
        FOR(j,0,b[i]-a[i]+1){
            ll val = (dp[i][j] + mp[j+a[i]]) % MOD;
            dp[i].pb(val);
        }
        pre[i].pb(0);
        FOR(j,0,b[i]-a[i]+1){
            ll val = (pre[i][j] + dp[i][j]) % MOD;
            pre[i].pb(val);
            ll val2 = (mp[j+a[i]] + dp[i][j]) % MOD;
            mp[j+a[i]] = val2;
        }
        ans = (ans + pre[i][b[i]-a[i]+1]) % MOD;
    }
    
    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 'int main()':
boat.cpp:90:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |   freopen("input.txt", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
boat.cpp:91:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |   freopen("output.txt", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 328 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 328 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
21 Correct 132 ms 17296 KB Output is correct
22 Correct 132 ms 17144 KB Output is correct
23 Correct 130 ms 16504 KB Output is correct
24 Correct 130 ms 17700 KB Output is correct
25 Correct 130 ms 17940 KB Output is correct
26 Correct 130 ms 16548 KB Output is correct
27 Correct 125 ms 16620 KB Output is correct
28 Correct 130 ms 16520 KB Output is correct
29 Correct 133 ms 16588 KB Output is correct
30 Correct 350 ms 80768 KB Output is correct
31 Correct 344 ms 79516 KB Output is correct
32 Correct 355 ms 80816 KB Output is correct
33 Correct 340 ms 78396 KB Output is correct
34 Correct 347 ms 79472 KB Output is correct
35 Correct 311 ms 75656 KB Output is correct
36 Correct 320 ms 78248 KB Output is correct
37 Correct 328 ms 79288 KB Output is correct
38 Correct 309 ms 77000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2107 ms 490128 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 328 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
21 Correct 132 ms 17296 KB Output is correct
22 Correct 132 ms 17144 KB Output is correct
23 Correct 130 ms 16504 KB Output is correct
24 Correct 130 ms 17700 KB Output is correct
25 Correct 130 ms 17940 KB Output is correct
26 Correct 130 ms 16548 KB Output is correct
27 Correct 125 ms 16620 KB Output is correct
28 Correct 130 ms 16520 KB Output is correct
29 Correct 133 ms 16588 KB Output is correct
30 Correct 350 ms 80768 KB Output is correct
31 Correct 344 ms 79516 KB Output is correct
32 Correct 355 ms 80816 KB Output is correct
33 Correct 340 ms 78396 KB Output is correct
34 Correct 347 ms 79472 KB Output is correct
35 Correct 311 ms 75656 KB Output is correct
36 Correct 320 ms 78248 KB Output is correct
37 Correct 328 ms 79288 KB Output is correct
38 Correct 309 ms 77000 KB Output is correct
39 Execution timed out 2107 ms 490128 KB Time limit exceeded
40 Halted 0 ms 0 KB -