Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Point;
- import java.io.*;
- import java.math.*;
- import java.util.*;
- public class Main implements Runnable {
- int maxn = (int)1e5+200;
- int mod = (int)1e9+7;
- int inf = (int)1e9;
- int n,m,k;
- int a[] = new int[maxn];
- void solve() throws Exception {
- ArrayList<Integer> list2 = new ArrayList<>();
- ArrayList<Integer> list1 = new ArrayList<>();
- list1.addAll(list2);
- int a1[] = {1,2,3,-99,0};
- int a2[] = {7, 1, 5, 0, 9, -1};
- int a3[] = {7, 1, 5, 0, 9, -1};
- int a4[] = {7, 1, 5, 0, 9, -1};
- out.println(getDuplicates(a1, 1, 0));
- out.println(getDuplicates(a2, 2, 3));
- out.println(getDuplicates(a3, 2, 4));
- out.println(getDuplicates(a4, 3, 1));
- }
- public class Chapter {
- List<Chapter> options;
- public Chapter(List<Chapter> options) {
- this.options = options;
- }
- public int getNumberOfStories() {
- if (options==null || options.isEmpty()) return 1;
- int numberOfStories = 0;
- for (Chapter chapter : options) {
- numberOfStories+=chapter.getNumberOfStories();
- }
- return numberOfStories;
- }
- public List<List<Chapter>> getAllPossibleStories() {
- List<List<Chapter>> list = new ArrayList<>();
- if (options==null || options.isEmpty()) {
- ArrayList<Chapter> story = new ArrayList<>();
- story.add(this);
- list.add(story);
- return list;
- }
- // we can add memorization here by
- for (Chapter chapter: options) {
- List<List<Chapter>> allStories = chapter.getAllPossibleStories();
- for (List<Chapter> oneStory: allStories) { // creates all possible stories with current chapter
- List<Chapter> currentStory = new ArrayList<>();
- currentStory.add(this);
- currentStory.addAll(oneStory);
- list.add(currentStory);
- }
- }
- return list;
- }
- }
- //7, 1, 5, 0, 9, -1 2 4
- public boolean getDuplicates (int a[], int k, int d) {
- TreeMap<Integer, Integer> treeMap = new TreeMap<>();
- int l = 0;
- for (int r=0; r<a.length; r++) {
- while (r-l>=k) {
- treeMap.put(a[l], treeMap.get(a[l])-1);
- if (treeMap.get(a[l])<=0) {
- treeMap.remove(a[l]);
- }
- l++;
- }
- Integer ceiling = treeMap.ceilingKey(a[r]-d);
- if (ceiling!=null && Math.abs(a[r]-ceiling)<=d) {
- return true;
- }
- treeMap.putIfAbsent(a[r], 0);
- treeMap.put(a[r], treeMap.get(a[r])+1);
- }
- return false;
- }
- class Pair implements Comparable<Pair>{
- int x;
- int y;
- public Pair (int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public int compareTo(Pair p) {
- if (x>p.x) return 1;
- else if (x==p.x) {
- if (y>p.y) return 1;
- else if (y==p.y) return 0;
- else return -1;
- } else {
- return -1;
- }
- }
- }
- String fileInName = "network";
- boolean file = false;
- boolean isAcmp = true;
- static Throwable throwable;
- public static void main (String [] args) throws Throwable {
- Locale.setDefault(new Locale("en", "US"));
- Thread thread = new Thread(null, new Main(), "", (1 << 26));
- thread.start();
- thread.join();
- thread.run();
- if (throwable != null)
- throw throwable;
- }
- FastReader in;
- PrintWriter out;
- public void run() {
- String fileIn = "input.txt";
- String fileOut = "output.txt";
- try {
- if (isAcmp) {
- if (file) {
- in = new FastReader(new BufferedReader(new FileReader(fileIn)));
- out = new PrintWriter (fileOut);
- } else {
- in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
- out = new PrintWriter(System.out);
- }
- } else if (file) {
- in = new FastReader(new BufferedReader(new FileReader(fileInName+".in")));
- out = new PrintWriter (fileInName + ".out");
- } else {
- in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
- out = new PrintWriter(System.out);
- }
- solve();
- } catch(Exception e) {
- throwable = e;
- } finally {
- out.close();
- }
- }
- }
- class FastReader {
- BufferedReader bf;
- StringTokenizer tk = null;
- public FastReader(BufferedReader bf) {
- this.bf = bf;
- }
- public String nextToken () throws Exception {
- if (tk==null || !tk.hasMoreTokens()) {
- tk = new StringTokenizer(bf.readLine());
- }
- if (!tk.hasMoreTokens()) return nextToken();
- else
- return tk.nextToken();
- }
- public int nextInt() throws Exception {
- return Integer.parseInt(nextToken());
- }
- public long nextLong() throws Exception {
- return Long.parseLong(nextToken());
- }
- public double nextDouble() throws Exception {
- return Double.parseDouble(nextToken());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement