-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACTIV.cpp
70 lines (65 loc) · 1.17 KB
/
ACTIV.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
vector<pii> a;
int n;
long long int dp[100010];
#define MOD 1000000000
//Binary search to find index of ending time of the process
int next(int target) {
int lo = 0;
int hi = n - 1;
while(lo < hi) {
int mid = (lo + hi) / 2;
if(a[mid].first >= target)
hi = mid;
else
lo = mid + 1;
}
if(a[n-1].first < target) {
return -1;
}
else
return lo;
}
//DP
long long int solve(int x) {
if(dp[x] != -1)
return dp[x]%MOD;
if(x >= n)
return 0;
int index = next(a[x].second);
if(index != -1)
dp[x] = (solve(x+1)%MOD + solve(index)%MOD + 1)%MOD;
else
dp[x] = (solve(x+1)%MOD + 1)%MOD;
return dp[x]%MOD;
}
//Strting point of program
int main() {
while(1) {
for (int i = 0; i < 100010; ++i)
{
dp[i] = -1;
}
scanf("%d",&n);
if(n == -1)
break;
for (int i = 0; i < n; ++i)
{
int x,y;
scanf("%d %d",&x, &y);
a.push_back(make_pair(x,y));
}
sort(a.begin(), a.end());
long long int ans = solve(0)%MOD;
string s;
for(int i = 0; i <= 7; i++) {
s += ans % 10 + '0';
ans = ans / 10;
}
reverse(s.begin(), s.end());
cout << s << endl;
a.clear();
}
}