This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.io.*;
import java.util.*;
class dna {
public static void main(String[] args) {
// TODO Auto-generated method stub
// two pointers
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int k = s.nextInt();
int r = s.nextInt();
int[] freq = new int[k+5];
int[] req = new int[k+5];
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = s.nextInt();
freq[arr[i]]++;
}
for(int i = 0; i < r; i++) {
int v1 = s.nextInt();
int v2 = s.nextInt();
req[v1] += v2;
}
for(int i = 0; i < k + 5; i++) {
if(freq[i] < req[i]) {
System.out.println("impossible");
return;
}
}
int left = 0;
int right = n;
while(left < right) {
int mid = left + (right - left)/2;
boolean works = false;
int[] curFreq = new int[k+5];
for(int i = 0; i < mid; i++) {
curFreq[arr[i]]++;
}
HashSet<Integer> invalid = new HashSet<>();
for(int i = 0; i < k+5; i++) {
if(curFreq[i] < req[i]) {
invalid.add(i);
}
}
if(invalid.size() == 0) works = true;
else {
for(int i = mid; i < n; i++) {
curFreq[arr[i]]++;
if(invalid.contains(arr[i]) && curFreq[arr[i]] >= req[arr[i]]) {
invalid.remove(arr[i]);
}
curFreq[arr[i-mid]]--;
if(curFreq[arr[i-mid]] < req[arr[i-mid]]) {
invalid.add(arr[i-mid]);
}
if(invalid.size() == 0) {
works = true;
break;
}
}
}
if(works) {
right = mid;
}
else {
left = mid + 1;
}
}
System.out.println(left);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |