-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProjects.java
More file actions
90 lines (72 loc) · 2.42 KB
/
Projects.java
File metadata and controls
90 lines (72 loc) · 2.42 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Projects {
long mod = 1000000007;
static class project implements Comparable<project>{
int start;
int end;
long money;
project(int start, int end, long money){
this.start = start;
this.end = end;
this.money = money;
}
@Override
public int compareTo(project o) {
return this.end - o.end;
}
}
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
ArrayList<project> v = new ArrayList<>();
for(int i = 0; i < n; i++) {
String[] s = br.readLine().split(" ");
int start = Integer.parseInt(s[0]);
int end = Integer.parseInt(s[1]);
long price = Long.parseLong(s[2]);
v.add(new project(start, end, price));
}
TreeSet<Integer> set = new TreeSet<>();
Collections.sort(v);
for(project p : v) {
set.add(p.end);
}
long[] dp = new long[n+1];
dp[0] = 0;
for(int i = 1; i <= n; i++) {
project p = v.get(i-1);
long op1 = dp[i-1];
long op2 = p.money + dp[(int)findBest(set, p.start)];
dp[i] = Math.max(op1, op2);
}
System.out.println(dp[n]);
}
public static long findBest(TreeSet<Integer> set, int start) {
Integer floor = set.floor(start);
if(floor == null) return 0;
int first = set.first();
if(floor == first) return 0;
else {
return 1 + indexOf(set, floor);
}
}
private static int indexOf(TreeSet<Integer> set,
Integer element)
{
int index = -1;
// If the element exists in the TreeSet
if (set.contains(element)) {
// The element index will be equal to the
// size of the headSet for the element
index = set.headSet(element).size();
}
// Return the index of the element
// Value will be -1 if the element
// do not exist in the TreeSet
return index;
}
}