public static void main(String[] args) { FScanner input = new FScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); Instrument[] instr = new Instrument[input.nextInt()]; int k = input.nextInt(); for (int i = 0; i < instr.length; i++) { instr[i] = new Instrument(input.nextInt(), i + 1); } Arrays.sort( instr, new Comparator<Instrument>() { @Override public int compare(Instrument o1, Instrument o2) { return new Integer(o1.cost).compareTo(o2.cost); } }); ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < instr.length; i++) { if (instr[i].cost <= k) { k -= instr[i].cost; list.add(instr[i].id); } else { break; } } out.println(list.size()); for (int i = 0; i < list.size(); i++) { if (i == 0) { out.print(list.get(i)); } else { out.print(" " + list.get(i)); } } out.close(); }
public static void main(String[] args) { FScanner input = new FScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); String current = input.nextLine(); long[] cache = new long[current.length()]; for (int i = 1; i < cache.length; i++) { cache[i] = current.charAt(i) == current.charAt(i - 1) ? cache[i - 1] + 1 : cache[i - 1]; } int count = input.nextInt(); for (int i = 0; i < count; i++) { int left = input.nextInt(); int right = input.nextInt(); out.println(cache[right - 1] - cache[left - 1]); } out.close(); }
public static void main(String[] args) { FScanner input = new FScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); int p = input.nextInt(); int q = input.nextInt(); HashSet<Integer> indexSet = new HashSet<>(); int currentMessageIndex = 1; int totalMessage = 0; int threshold = 1; HashMap<Integer, Integer>[] messages = new HashMap[p]; int[] messagePtrs = new int[p]; int[] readIndexPtrs = new int[p]; for (int i = 0; i < messagePtrs.length; i++) { messagePtrs[i] = 0; } for (int i = 0; i < readIndexPtrs.length; i++) { readIndexPtrs[i] = 0; } for (int i = 0; i < messages.length; i++) { messages[i] = new HashMap<>(); } for (int i = 0; i < q; i++) { int type = input.nextInt(); int app = input.nextInt(); if (type == 1) { totalMessage++; messages[app - 1].put(messagePtrs[app - 1], currentMessageIndex); messagePtrs[app - 1]++; currentMessageIndex++; } else if (type == 2) { HashMap<Integer, Integer> map = messages[app - 1]; for (int j = readIndexPtrs[app - 1]; j < map.size(); j++) { indexSet.add(map.get(j)); } readIndexPtrs[app - 1] = map.size(); } else { if (app >= threshold) { for (int j = app; j >= threshold; j--) { indexSet.add(j); } threshold = app; } } out.println(totalMessage - indexSet.size()); } out.close(); }
public static void main(String[] args) { FScanner input = new FScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); int[] array = new int[input.nextInt()]; int m = input.nextInt(); for (int i = 0; i < array.length; i++) { array[i] = input.nextInt(); } HashSet<Integer> set = new HashSet<>(); int[] solutions = new int[array.length]; for (int i = solutions.length - 1; i >= 0; i--) { int current = array[i]; set.add(current); solutions[i] = set.size(); } for (int i = 0; i < m; i++) { out.println(solutions[input.nextInt() - 1]); } out.close(); }
public static void main(String[] args) { FScanner input = new FScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); int n = input.nextInt(); String specialString = "1"; long total = 0; for (int i = 0; i < n; i++) { String current = input.next(); char[] array = current.toCharArray(); if (array.length == 1 && array[0] == '0') { out.println(0); System.exit(0); } int countOne = 0; int countZero = 0; int countOthers = 0; for (int j = 0; j < array.length; j++) { if (array[j] == '1') { countOne++; } else if (array[j] == '0') { countZero++; } else { countOthers++; } } if (countOne > 1 || countOthers > 0) { specialString = current; } else { total += countZero; } } StringBuilder answer = new StringBuilder(specialString); for (int i = 0; i < total; i++) { answer = answer.append('0'); } out.println(answer.toString()); out.close(); }
public static void main(String[] args) { FScanner input = new FScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); String current = input.nextLine(); int k = input.nextInt(); int[] array = new int[26]; for (int i = 0; i < 26; i++) { array[i] = input.nextInt(); } int total = 0; int currentCounter = 1; for (int i = 0; i < current.length(); i++) { total += currentCounter * array[current.charAt(i) - 'a']; currentCounter++; } Arrays.sort(array); for (int i = 1; i <= k; i++) { total += currentCounter * array[array.length - 1]; currentCounter++; } out.println(total); out.close(); }
public static void main(String[] args) { FScanner input = new FScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); int n = input.nextInt(); HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { int k = input.nextInt(); if (!map.containsKey(k)) { map.put(k, 1); } else { map.put(k, map.get(k) + 1); } } int movies = input.nextInt(); int[] audio = new int[movies]; int[] subs = new int[movies]; for (int i = 0; i < movies; i++) { audio[i] = input.nextInt(); } for (int i = 0; i < movies; i++) { subs[i] = input.nextInt(); } ArrayList<Integer> list = new ArrayList<>(map.keySet()); Collections.sort( list, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return map.get(o2).compareTo(map.get(o1)); } }); int maxMovieIndex = -1; int maxMovieA = -1; int maxMovieS = -1; for (int i = 0; i < movies; i++) { int currentA = audio[i]; int currentS = subs[i]; if (map.containsKey(currentA)) { if (map.get(currentA) > maxMovieA) { maxMovieA = map.get(currentA); maxMovieS = map.get(currentS) == null ? 0 : map.get(currentS); maxMovieIndex = i + 1; } else if (map.get(currentA) == maxMovieA) { if (map.containsKey(currentS)) { if (map.get(currentS) > maxMovieS) { maxMovieA = map.get(currentA); maxMovieS = map.get(currentS); maxMovieIndex = i + 1; } } } } } if (maxMovieIndex == -1) { for (int i = 0; i < movies; i++) { int currentS = subs[i]; if (map.containsKey(currentS)) { if (map.get(currentS) > maxMovieS) { maxMovieS = map.get(currentS); maxMovieIndex = i + 1; } } } } if (maxMovieIndex == -1) { out.println(1); } else { out.println(maxMovieIndex); } out.close(); }