public static void main(String[] args) { FastScanner in = new FastScanner(System.in); int sum = in.nextInt(); int limit = in.nextInt(); int cnt = 0; StringBuilder sb = new StringBuilder(); Point[] points = new Point[limit + 1]; points[0] = new Point(0, 0); for (int i = 1; i <= limit; i++) points[i] = new Point(i, lb(i)); Arrays.sort( points, new Comparator<Point>() { public int compare(Point a, Point b) { return a.y - b.y; } }); // System.out.println(Arrays.toString(points)); for (int i = limit; i >= 1; i--) { if (sum >= points[i].y) { sum -= points[i].y; sb.append(" "); sb.append(points[i].x); cnt++; } } if (sum == 0) { System.out.println(cnt); System.out.println(sb.toString().trim()); } else { System.out.println(-1); } }
public void solve() throws IOException { int n = in.nextInt(); int m = in.nextInt(); graph = new ArrayList[n + 1]; color = new int[n + 1]; Arrays.fill(color, 0); for (int i = 0; i < m; i++) { int from = in.nextInt(); int to = in.nextInt(); if (graph[from] == null) { graph[from] = new ArrayList<Integer>(); } graph[from].add(to); } topSort(); if (hasCycle) { out.print(-1); return; } for (Integer anAnswer : answer) { out.printf("%d ", anAnswer); } }
protected static void calcTransferCombinations() throws IOException { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); final int PRISONERS_NUMBER = in.nextInt(); final int MAX_AGGRESSION = in.nextInt(); final int RANK_LENGTH = in.nextInt(); int combinations = PRISONERS_NUMBER - RANK_LENGTH + 1; int distanceToLastDangerous = RANK_LENGTH; int itr = 1; while (itr <= PRISONERS_NUMBER) { int aggression = in.nextInt(); if (aggression > MAX_AGGRESSION) { if (PRISONERS_NUMBER - itr < RANK_LENGTH) { combinations -= Math.min(PRISONERS_NUMBER - itr + 1, distanceToLastDangerous); break; } int left = min(itr, RANK_LENGTH, distanceToLastDangerous); combinations -= left; distanceToLastDangerous = 0; } distanceToLastDangerous++; itr++; } out.print(combinations); out.flush(); }
public static void main(String[] args) { FastScanner in = new FastScanner(System.in); int n = in.nextInt(); int[] s = new int[n + 5]; int[] s2 = new int[n + 5]; for (int i = 1; i <= n; i++) { s[i] = in.nextInt(); s2[n - i + 1] = s[i]; } int[] stack = new int[n + 5]; int size = 0; int answer = 0; for (int i = 1; i <= n; i++) { while (size > 0 && stack[size - 1] < s[i]) size--; stack[size++] = s[i]; if (size >= 2) answer = Math.max(answer, stack[size - 1] ^ stack[size - 2]); } size = 0; for (int i = 1; i <= n; i++) { while (size > 0 && stack[size - 1] < s2[i]) size--; stack[size++] = s2[i]; if (size >= 2) answer = Math.max(answer, stack[size - 1] ^ stack[size - 2]); } System.out.println(answer); }
public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int u = 1; while (u <= n) { String s = sc.next(); TreeSet<Long> set = new TreeSet<Long>(); set.add(Long.parseLong(s)); for (int i = 0; i < s.length(); i++) { for (int j = i + 1; j < s.length(); j++) { String next = s.substring(0, i) + s.charAt(j) + s.substring(i + 1, j) + s.charAt(i) + s.substring(j + 1); if (next.charAt(0) != '0') { set.add(Long.parseLong(next)); } } } min = set.first(); max = set.last(); out.println("Case #" + u + ": " + min + " " + max); u++; } out.close(); }
private Queue<Integer> readJobs() throws IOException { int m = in.nextInt(); Queue<Integer> jobs = new LinkedList<>(); for (int i = 0; i < m; ++i) { jobs.add(in.nextInt()); } return jobs; }
public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); int t = sc.nextInt(); while (t > 0) { t--; int[] line = sc.nextIntArray(); System.out.println(solve(line[0], line[1], line[2])); } }
public void solve() throws IOException { /* MAIN CODE GOES HERE!!! */ MyQueueOnMyLinkedList q = new MyQueueOnMyLinkedList(); int n = in.nextInt(); for (int i = 0; i < n; i++) { String s = in.next(); if (s.charAt(0) == '-') { out.println(q.pop()); } else if (s.charAt(0) == '+') { q.push(in.nextInt()); } } }
public static void main(String[] args) { FastScanner scanner = new FastScanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); assert 1 <= n && n <= 1e6 : "out of range, n: " + n; assert 2 <= m && m <= 1e3 : "out of range, m: " + m; int[] A = new int[n]; for (int i = 0; i < A.length; i++) { A[i] = scanner.nextInt(); assert 0 <= A[i] && A[i] <= 1e9 : "out of range, A[i]: " + A[i]; if (A[i] == 0) { System.out.println("YES"); return; } } Bravo o = new Bravo(n, m, A); System.out.println((o.solve()) ? "YES" : "NO"); }
public void solve() throws IOException { in = new FastScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int numWorkers = in.nextInt(); Queue<Integer> jobs = readJobs(); List<Pair> result = assignJobs(jobs, numWorkers); writeResponse(result); out.close(); }
public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); out.println(getSubstrings(sc.next())); // int t = sc.nextInt(); // while (t > 0) { // t--; // int n = sc.nextInt(); // int k = sc.nextInt(); // int q = sc.nextInt(); // String s = sc.nextLine(); // while (q > 0) { // q--; // int left = sc.nextInt(); // int right = sc.nextInt(); // out.println(solve(s, k, left, right)); // } // } out.close(); }
public void run() { Locale.setDefault(Locale.US); try { sc = new FastScanner("access.in"); out = new PrintWriter("access.out"); solve(); sc.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } }
BigInteger nextBigInteger() { return sc.nextBigInteger(); }
int nextInt() { return sc.nextInt(); }
double nextDouble() { return sc.nextDouble(); }
long nextLong() { return sc.nextLong(); }
String nextToken() { return sc.nextToken(); }