-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChefAndFrog.java
More file actions
143 lines (113 loc) · 3.65 KB
/
ChefAndFrog.java
File metadata and controls
143 lines (113 loc) · 3.65 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* Created by arpit on 9/12/16.
*
* Problem link:-https://www.codechef.com/problems/FROGV
*/
public class ChefAndFrog {
final static int MAX=100001;
static int parent[]=new int[MAX];
static Data a[]=new Data[MAX];
static int maxDistance[]=new int[MAX];//Used for dp approach
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,k,p;
String s[];
s=br.readLine().split("\\s");
n=Integer.parseInt(s[0]);
k=Integer.parseInt(s[1]);
p=Integer.parseInt(s[2]);
initParent(n);
s=br.readLine().split("\\s");
for (int i = 0; i < n; i++) {
a[i]=new Data(Integer.parseInt(s[i]),i);
}
Arrays.sort(a,0,n);
//This is the DSU approach
/* constructDSU(n, k);
pathCompression(n);
while (p-->0){
s=br.readLine().split("\\s");
System.out.println(solveQuery(Integer.parseInt(s[0])-1,Integer.parseInt(s[1])-1));
}*/
//This is the DP approach
preCompute(n,k);
while (p-->0){
s=br.readLine().split("\\s");
System.out.println(queryUsingDp(Integer.parseInt(s[0])-1,Integer.parseInt(s[1])-1));
}
for (int i = 0; i < n; i++) {
System.out.print(maxDistance[a[i].index] + " ");
}
System.out.println();
}
private static void pathCompression(int n) {
for (int i = 0; i < n; i++) {
findSet(i);
}
}
private static String solveQuery(int a, int b) {
if (findSet(a)==findSet(b))return "Yes";
return "No";
}
private static void initParent(int n) {
for (int i = 0; i < n; i++) {
parent[i]=i;
}
}
private static void constructDSU(int n, int k) {
for (int i = 0; i < n; i++) {
for (int j=i+1;j<n;j++){
if ((a[j].x-a[i].x)<=k){
union(a[i].index,a[j].index);
i=j-1;
}
else break;
}
}
}
private static void union(int a, int b) {
parent[findSet(a)]=findSet(b);
}
private static int findSet(int a) {
if (parent[a]==a)return a;
parent[a]=findSet(parent[a]);
return parent[a];
}
//The problem can also be solved simply using dynamic programming,
// The idea behind dp approach is that two frogs can only communicate if there maximum distance is equal.
// We compute the maximum coordinate(distance) up to which frog can communicate.
static void preCompute(int n,int k){
maxDistance[a[n-1].index]=a[n-1].x+k;
for (int i =n-2; i>=0; i--) {
if ((a[i+1].x-a[i].x)<=k)maxDistance[a[i].index]=maxDistance[a[i+1].index];
else maxDistance[a[i].index]=a[i].x+k;
}
}
static String queryUsingDp(int p,int q){
if (maxDistance[p]==maxDistance[q])return "Yes";
return "No";
}
static class Data implements Comparable<Data>{
int x,index;
public Data(int x, int index) {
this.x = x;
this.index = index;
}
@Override
public int compareTo(Data data) {
return ((Integer)this.x).compareTo(data.x);
}
@Override
public String toString() {
return "Data{" +
"x=" + x +
", index=" + index +
'}';
}
}
}