-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChefAndWay.java
More file actions
104 lines (78 loc) · 2.54 KB
/
ChefAndWay.java
File metadata and controls
104 lines (78 loc) · 2.54 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
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.PriorityQueue;
/**
* Created by arpit on 22/12/16.
*/
public class ChefAndWay {
static int a[]=new int[100011];
static long dp[]=new long[100011];
static BigInteger []dp2=new BigInteger[100011];
static final int M=1000000007;
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,k;
String[]s;
s=br.readLine().split("\\s");
n=Integer.parseInt(s[0]);
k=Integer.parseInt(s[1]);
s=br.readLine().split("\\s");
for (int i = 0; i < n; i++)
a[i] = Integer.parseInt(s[i]);
System.out.println(solve(a, n, k));
}
private static long solve(int[] a, int n, int k) {
PriorityQueue<Pair>pq=new PriorityQueue<>();
pq.add(new Pair(Math.log(a[n-1]),n-1));
dp[n-1]=a[n-1];
for (int i =n-2; i>=0; i--) {
while (pq.peek().idx>(i+k))pq.poll();
long min=dp[pq.peek().idx];
dp[i]=(a[i]*min)%M;
pq.add(new Pair(pq.peek().logValue+Math.log(a[i]),i));
}
return dp[0];
}
//This solution is only for small sub task.
private static int solve1(int a[],int n,int k){
dp2[n-1]=new BigInteger(a[n-1]+"");
for (int i =n-2; i>=0 ; i--) {
BigInteger min=dp2[i+1];
dp2[i]=new BigInteger(a[i]+"");
for (int j = i+1; j<n && j<=(i+k); j++) {
if (dp2[j].compareTo(min)<0)min=dp2[j];
}
// System.out.println(i);
dp2[i]=dp2[i].multiply(min);
}
/*
for (int i = 0; i < n; i++) {
System.out.print(dp2[i] + " ");
}
System.out.println();*/
return dp2[0].mod(BigInteger.valueOf(1000000007)).intValue();
}
static class Pair implements Comparable<Pair>{
int idx;
double logValue;
public Pair(double logValue,int idx) {
this.idx = idx;
this.logValue = logValue;
}
@Override
public int compareTo(Pair pair) {
return Double.compare(this.logValue,pair.logValue);
}
@Override
public String toString() {
return "Pair{" +
"idx=" + idx +
", logValue=" + logValue +
'}';
}
}
}