Example #1
1
 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);
   }
 }
Example #2
0
  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);
    }
  }
Example #3
0
  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();
  }
Example #4
0
  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 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());
     }
   }
 }
Example #6
0
 private void scanData() {
   int citiesNumber = scanner.nextInt();
   maxPossibleCapacity = 0;
   adjacencyMatrix = new int[citiesNumber][citiesNumber];
   for (int startCity = 0; startCity < citiesNumber; startCity++) {
     for (int destinationCity = 0; destinationCity < citiesNumber; destinationCity++) {
       int nextCapacity = scanner.nextInt();
       adjacencyMatrix[startCity][destinationCity] = nextCapacity;
       if (nextCapacity > maxPossibleCapacity) {
         maxPossibleCapacity = nextCapacity;
       }
     }
   }
   graph = new CitiesFuelGraph(adjacencyMatrix);
 }
Example #7
0
 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");
 }
Example #8
0
 int nextInt() {
   return sc.nextInt();
 }