Beispiel #1
1
 public int minimumPrice(long[] dread, int[] price) {
   int n = dread.length;
   int m = n << 1;
   long[] maximum = new long[m + 1];
   Arrays.fill(maximum, 0);
   for (int i = 0; i < n; ++i) {
     long[] new_maximum = new long[m + 1];
     Arrays.fill(new_maximum, -1);
     for (int j = 0; j <= m; ++j) {
       if (maximum[j] != -1) {
         if (maximum[j] >= dread[i]) {
           new_maximum[j] = Math.max(new_maximum[j], maximum[j]);
         }
         if (j + price[i] <= m) {
           new_maximum[j + price[i]] = Math.max(new_maximum[j + price[i]], maximum[j] + dread[i]);
         }
       }
     }
     maximum = new_maximum;
   }
   int answer = 0;
   while (maximum[answer] == -1) {
     answer++;
   }
   return answer;
 }
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    int t = in.nextInt();
    TreeMap<Long, Long> tmap = new TreeMap<>();
    long res = 0;
    while (t-- > 0) {
      res = 0;
      int n = in.nextInt();
      long m = in.nextLong();
      tmap.clear();
      long[] arr = new long[n];
      res = in.nextLong();
      arr[0] = res % m;
      res = Long.MIN_VALUE;
      tmap.put(arr[0], arr[0]);
      for (int i = 1; i < arr.length; i++) {
        arr[i] = in.nextLong();
        arr[i] %= m;
        arr[i] += arr[i - 1];
        arr[i] %= m;

        if (tmap.higherEntry(arr[i]) == null) {
          res = Math.max(res, arr[i]);
          tmap.put(arr[i], arr[i]);
          continue;
        }
        long val = tmap.higherEntry(arr[i]).getValue();
        res = Math.max(res, (arr[i] - val + m) % m);
        tmap.put(arr[i], arr[i]);
      }

      System.out.println(res);
    }
  }
  // Constructor::TextContext
  //
  // Generates a TextContext Object.
  //
  // Generates before and after contexts based on given length and values
  //
  // Parameters
  //
  // * primary -- TextPrimary object to be tested against context
  // * constraint -- TextConstraint object attached to this context
  // * checkSumType -- Hash for checksum
  // * contextLength - length (in chars) of the context used for testing
  //
  public TextContext(
      TextPrimary primary, TextConstraint constraint, HashType checkSumType, int contextLength) {
    super();
    this.checkSumType = checkSumType;
    //	Testing if content matches the bit-checksum tests

    this.checkSum = checkSum(primary.getContent(), checkSumType);

    int beforeStart = constraint.getStartPos() - contextLength;
    beforeStart = Math.max(0, beforeStart);
    int beforeEnd = constraint.getStartPos();

    int afterStart = constraint.getEndPos();
    int afterEnd = constraint.getEndPos() + contextLength;
    afterEnd = Math.min(primary.getContent().length(), afterEnd);

    // Evaluating how much of selected text to store
    this.totalSelectionLength = primary.getContent().length();
    int cLength = this.totalSelectionLength;
    if (this.totalSelectionLength > DEFAULT_CONTEXTLENGTH) {
      double half = (double) (this.totalSelectionLength / 2);
      cLength = (int) (Math.floor(half * percentStorage));
      this.beginSel = primary.getContent().substring(beforeEnd, (beforeEnd + cLength));
      this.endSel = primary.getContent().substring((afterStart - cLength), afterStart);
      this.totalSelection = this.beginSel.concat(this.endSel);
    } else {
      // Use the entire selection
      this.beginSel = "";
      this.endSel = "";
      this.totalSelection = primary.getContent();
    }

    this.beforeContext = primary.getContent().substring(beforeStart, beforeEnd);
    this.afterContext = primary.getContent().substring(afterStart, afterEnd);
  }
Beispiel #4
0
 public int count(
     String[] s1000,
     String[] s100,
     String[] s10,
     String[] s1,
     String[] t1000,
     String[] t100,
     String[] t10,
     String[] t1) {
   String S1000 = concate(s1000);
   String S100 = concate(s100);
   String S10 = concate(s10);
   String S1 = concate(s1);
   String T1000 = concate(t1000);
   String T100 = concate(t100);
   String T10 = concate(t10);
   String T1 = concate(t1);
   int n = S1000.length();
   Interval[] intervals = new Interval[n];
   for (int i = 0; i < n; ++i) {
     intervals[i] =
         new Interval(
             Integer.parseInt(
                 S1000.substring(i, i + 1) + S100.charAt(i) + S10.charAt(i) + S1.charAt(i)),
             Integer.parseInt(
                 T1000.substring(i, i + 1) + T100.charAt(i) + T10.charAt(i) + T1.charAt(i)));
   }
   Arrays.sort(intervals);
   int leftmost = Integer.MAX_VALUE;
   int rightmost = Integer.MIN_VALUE;
   for (int i = 0; i < n; ++i) {
     leftmost = Math.min(leftmost, intervals[i].b);
     rightmost = Math.max(rightmost, intervals[i].a);
   }
   int answer = 0;
   for (int i = 0; i < n; ++i) {
     int total = 0;
     int now = leftmost;
     int j = 0;
     while (true) {
       if (intervals[i].a <= now) {
         now = Math.max(now, intervals[i].b);
       }
       if (now >= rightmost) {
         break;
       }
       int best = now;
       while (j < n && intervals[j].a <= now) {
         best = Math.max(best, intervals[j++].b);
       }
       if (best <= now) {
         return -1;
       }
       total++;
       now = best;
     }
     answer += total;
   }
   return answer;
 }
 public int minimumMoves(String[] boardString) {
   n = boardString.length;
   m = boardString[0].length();
   board = new int[n];
   for (int i = 0; i < n; ++i) {
     for (int j = 0; j < m; ++j) {
       if (boardString[i].charAt(j) == 'W') {
         board[i] |= 1 << j;
       }
     }
   }
   int answer = Integer.MAX_VALUE;
   all = (1 << m) - 1;
   for (int type = 0; type < 1 << m; ++type) {
     answer = Math.min(answer, solve(type, 0));
     for (int first = type; first > 0; first = (first - 1) & type) {
       answer = Math.min(answer, solve(type, first));
     }
     answer = Math.min(answer, solve(type, all ^ type));
     for (int first = all ^ type; first > 0; first = (first - 1) & (all ^ type)) {
       answer = Math.min(answer, solve(type, first));
     }
   }
   return answer == Integer.MAX_VALUE ? -1 : answer;
 }
Beispiel #6
0
  void run() {
    InputReader in = new InputReader(System.in);
    PrintWriter out = new PrintWriter(System.out);

    int n = in.nextInt(), q = in.nextInt();
    int[] v = new int[n], c = new int[n];
    for (int i = 0; i < n; ++i) v[i] = in.nextInt();
    for (int i = 0; i < n; ++i) c[i] = in.nextInt();

    for (int i = 0; i < q; ++i) {
      int a = in.nextInt(), b = in.nextInt();
      long[] dp = new long[n + 1];
      Node[] heap = new Node[2];
      Arrays.fill(dp, -INF);
      for (int j = 0; j < 2; ++j) heap[j] = new Node(-INF, -1);
      for (int j = 0; j < n; ++j) {
        long val = v[j], maxv = val * b;
        int color = c[j];
        maxv = Math.max(dp[color] + val * a, maxv);
        maxv = Math.max(choose(heap, color) + val * b, maxv);
        dp[color] = Math.max(maxv, dp[color]);
        update(heap, dp[color], color);
      }

      long ret = 0;
      for (int j = 1; j <= n; ++j) ret = Math.max(ret, dp[j]);
      out.println(ret);
    }

    out.close();
  }
Beispiel #7
0
  /** Returns angle in degrees from specified origin to specified destination. */
  public static int getAngle(Point origin, Point destination) // origin != destination
      {
    double distance = getDistance(origin, destination);
    int add = 0;
    int x = destination.getx() - origin.getx();
    int y = origin.gety() - destination.gety(); // Java flips things around

    double angleRad = Math.asin(Math.abs(y) / distance);
    double angleDeg = Math.toDegrees(angleRad);

    if ((x >= 0) && (y >= 0)) // Quadrant 1
    {
      angleDeg = angleDeg;
    }
    if ((x < 0) && (y > 0)) // Quadrant 2
    {
      angleDeg = 180 - angleDeg;
    }
    if ((x <= 0) && (y <= 0)) // Quadrant 3
    {
      angleDeg = 180 + angleDeg;
    }
    if ((x > 0) && (y < 0)) // Quadrant 4
    {
      angleDeg = 360 - angleDeg;
    }

    float angleFloat = Math.round(angleDeg);
    int angleInt = Math.round(angleFloat);

    return (angleInt);
  }
Beispiel #8
0
  public static void testICDE() {
    // Number of total operations
    int numberOfTests = 5;
    // Length of the p, note that n=p.q
    int lengthp = 512;

    Paillier esystem = new Paillier();
    Random rd = new Random();
    PaillierPrivateKey key = KeyGen.PaillierKey(lengthp, 122333356);
    esystem.setDecryptEncrypt(key);
    // let's test our algorithm by encrypting and decrypting few instances

    long start = System.currentTimeMillis();
    for (int i = 0; i < numberOfTests; i++) {
      BigInteger m1 = BigInteger.valueOf(Math.abs(rd.nextLong()));
      BigInteger m2 = BigInteger.valueOf(Math.abs(rd.nextLong()));
      BigInteger c1 = esystem.encrypt(m1);
      BigInteger c2 = esystem.encrypt(m2);
      BigInteger c3 = esystem.multiply(c1, m2);
      c1 = esystem.add(c1, c2);
      c1 = esystem.add(c1, c3);

      esystem.decrypt(c1);
    }
    long stop = System.currentTimeMillis();
    System.out.println(
        "Running time per comparison in milliseconds: " + ((stop - start) / numberOfTests));
  }
Beispiel #9
0
 /**
  * Returns the date in seconds.
  *
  * @return seconds
  */
 final BigDecimal seconds() {
   int z = tz;
   if (z == Short.MAX_VALUE) {
     // [CG] XQuery, DateTime: may be removed
     final long n = System.currentTimeMillis();
     z = Calendar.getInstance().getTimeZone().getOffset(n) / 60000;
   }
   return (sec == null ? BigDecimal.ZERO : sec)
       .add(BigDecimal.valueOf(Math.max(0, hou) * 3600 + Math.max(0, min) * 60 - z * 60));
 }
Beispiel #10
0
 /**
  * Adds the time zone to the specified token builder.
  *
  * @param tb token builder
  */
 void zone(final TokenBuilder tb) {
   if (tz == Short.MAX_VALUE) return;
   if (tz == 0) {
     tb.add('Z');
   } else {
     tb.add(tz > 0 ? '+' : '-');
     prefix(tb, Math.abs(tz) / 60, 2);
     tb.add(':');
     prefix(tb, Math.abs(tz) % 60, 2);
   }
 }
Beispiel #11
0
 static double[] PDF() {
   double normal[] = new double[256];
   for (int x = 0; x < 256; x++) {
     float sigma = 50;
     float m = 128;
     double sigmasqr2pi = sigma * Math.sqrt(2 * Math.PI);
     float twosigma2 = 2 * sigma * sigma;
     normal[x] = Math.exp(-(Math.pow((x - m), 2)) / twosigma2) / (sigmasqr2pi);
   }
   return normal;
 }
Beispiel #12
0
 private double evaluateAt(double a[], int start, double where) {
   int deg = a.length - 1;
   if (Math.abs(where) <= 1.0) {
     double result = a[deg];
     for (int i = deg - 2; i >= start; i--) result = result * where + a[i];
     return result;
   } else {
     double result = a[0 + start];
     for (int i = 1 + start; i < deg; i++) result = result / where + a[i];
     return result * Math.pow(where, deg - start);
   }
 }
  private int findClosestIndexOf(String context, int oldIndex, String content) {

    Matcher matcher = Pattern.compile(Pattern.quote(context)).matcher(content);
    int index = 0;

    while (matcher.find()) {
      if (Math.abs(oldIndex - matcher.start()) < (Math.abs(oldIndex - index))) {
        index = matcher.start();
      }
    }

    return index;
  }
  public static void main(String[] args) throws java.lang.Exception {
    // let_me_start

    String s = s();
    String t = s();
    int c[] = new int[150];
    int d[] = new int[150];
    String S = s.toUpperCase();
    String T = t.toUpperCase();
    int C[] = new int[150];
    int D[] = new int[150];

    int n = s.length();
    int m = t.length();

    // int arr[] = is((int)n);

    long ans = 0;
    for (int i = 1; i <= n; i++) {
      c[s.charAt(i - 1)]++;
    }

    for (int i = 1; i <= m; i++) {
      d[t.charAt(i - 1)]++;
    }
    for (int i = 1; i <= n; i++) {
      C[S.charAt(i - 1)]++;
    }

    for (int i = 1; i <= m; i++) {
      D[T.charAt(i - 1)]++;
    }

    int b = 0;
    int min = 1000000000;
    for (int i = 1; i <= 149; i++) {
      b += Math.min(c[i], d[i]);
      d[i] -= Math.min(c[i], d[i]);
    }
    int a = 0;
    min = 1000000000;
    for (int i = 1; i <= 149; i++) {
      a += Math.min(C[i], D[i]);
      D[i] -= Math.min(C[i], D[i]);
    }
    a = a - b;
    out.write("" + b + " " + a + "\n");
    out.flush();
    return;
  }
Beispiel #15
0
  /** Dummy test method. */
  public static boolean dan(Point p1, Point p2) {
    //		Point p1 = mech1.getLocation();
    //		Point p2 = mech2.getLocation();
    int p1x = p1.getx();
    int p1y = p1.gety();
    int p2x = p2.getx();
    int p2y = p2.gety();
    int dx = p2x - p1x;
    int dy = p2y - p1y;
    int xdir = sign(dx);
    int ydir = sign(dy);
    //		double dist = Math.sqrt(dx*dx+dy*dy);
    double changex = (double) Math.abs(dx); // Diff in x
    double changey = (double) Math.abs(dy); // Diff in y

    double currentx = p1x;
    double currenty = p1y;
    int nextx = p1x;
    int nexty = p1y;
    double xdist, ydist;
    double xcost, ycost;

    int width = 5;

    while ((p2x - nextx) * xdir > 0 || (p2y - nexty) * ydir > 0) {
      xdist =
          dist(
              currentx, width,
              xdir); // total distance need to encounter next terrain object in x direction
      ydist = dist(currenty, width, ydir);

      xcost = (changex == 0) ? 10000 : xdist / changex;
      ycost = (changey == 0) ? 10000 : ydist / changey;

      if (xcost <= ycost) ydist = xdist * (changey / changex);
      else xdist = ydist * (changex / changey);

      currentx += xdir * xdist;
      currenty += ydir * ydist;

      nextx = (int) round(currentx, ydir);
      nexty = (int) round(currenty, ydir);

      //			if (Map.getNearestGrid(nextx,nexty).getType() !=0) return false;
    }

    System.out.println("Nextx: " + nextx + " Nexty: " + nexty);
    System.out.println("Currentx: " + currentx + " Currenty: " + currenty);
    return true;
  }
Beispiel #16
0
 public static int rules(
     String top, Vector<String> first, Vector<String> second, Vector<String> afia) {
   while (!first.isEmpty() && !second.isEmpty()) {
     if (count % 2 == 0) {
       boolean check = false;
       for (int i = 0; i < first.size(); i++) {
         check =
             matcher(
                 top,
                 first.elementAt(
                     i)); // performs a linear search on player1's whot to see if any matches the
         // top card
         if (check == true) {
           place.addElement(first.elementAt(i));
           first.removeElementAt(i);
           break;
         }
       }
       if (check == false) {
         Random rd = new Random();
         int pick = Math.abs(rd.nextInt(afia.size() - 1)) % (afia.size() - 1);
         first.addElement(afia.elementAt(pick));
         afia.removeElementAt(pick);
       }
     } else {
       boolean check = false;
       for (int i = 0; i < second.size(); i++) {
         check =
             matcher(
                 top,
                 second.elementAt(
                     i)); // performs a linear search on player1's whot to see if any matches the
         // top card
         if (check == true) {
           place.addElement(second.elementAt(i));
           second.removeElementAt(i);
           break;
         }
       }
       if (check == false) {
         Random rd = new Random();
         int pick = Math.abs(rd.nextInt(afia.size() - 1)) % (afia.size() - 1);
         second.addElement(afia.elementAt(pick));
         afia.removeElementAt(pick);
       }
     }
   }
   return 1;
 }
 // BEGIN KAWIGIEDIT TESTING
 // Generated by KawigiEdit-pf 2.3.0
 private static boolean KawigiEdit_RunTest(
     int testNum, int[] p0, int[] p1, boolean hasAnswer, double p2) {
   System.out.print("Test " + testNum + ": [" + "{");
   for (int i = 0; p0.length > i; ++i) {
     if (i > 0) {
       System.out.print(",");
     }
     System.out.print(p0[i]);
   }
   System.out.print("}" + "," + "{");
   for (int i = 0; p1.length > i; ++i) {
     if (i > 0) {
       System.out.print(",");
     }
     System.out.print(p1[i]);
   }
   System.out.print("}");
   System.out.println("]");
   GreaterGame obj;
   double answer;
   obj = new GreaterGame();
   long startTime = System.currentTimeMillis();
   answer = obj.calc(p0, p1);
   long endTime = System.currentTimeMillis();
   boolean res;
   res = true;
   System.out.println("Time: " + (endTime - startTime) / 1000.0 + " seconds");
   if (hasAnswer) {
     System.out.println("Desired answer:");
     System.out.println("\t" + p2);
   }
   System.out.println("Your answer:");
   System.out.println("\t" + answer);
   if (hasAnswer) {
     res = answer == answer && Math.abs(p2 - answer) <= 1e-9 * Math.max(1.0, Math.abs(p2));
   }
   if (!res) {
     System.out.println("DOESN'T MATCH!!!!");
   } else if ((endTime - startTime) / 1000.0 >= 2) {
     System.out.println("FAIL the timeout");
     res = false;
   } else if (hasAnswer) {
     System.out.println("Match :-)");
   } else {
     System.out.println("OK, but is it right?");
   }
   System.out.println("");
   return res;
 }
Beispiel #18
0
  public void FFT(int len, int oper) {
    int bit_len = 1;
    while ((1 << bit_len) < len) bit_len++;

    Vector thrds = new Vector();

    try {

      for (int i = 0; i < len; i += len / PROCESS_NUM) {
        // processBitReverse(i, i + len / PROCESS_NUM, bit_len);
        Thread t = new Thread(new BitReverseProcessor(this, i, i + len / PROCESS_NUM, bit_len));
        thrds.addElement(t);
        t.start();
      }

      for (int i = 0; i < thrds.size(); i++) {
        ((Thread) thrds.get(i)).join();
      }
      thrds.clear();

      // memcpy(data, tmp, sizeof(tmp));
      double t[][] = tmp;
      tmp = data;
      data = t;

      for (int i = 1; (1 << i) <= len; i++) {
        int m = 1 << i;
        double wm[] = {Math.cos(pi * 2 / (double) m), oper * Math.sin(pi * 2 / (double) m)};

        for (int j = 1; j < PROCESS_NUM; j++) {
          // processFFT(j * len / PROCESS_NUM, (j + 1) * len / PROCESS_NUM, m, wm);
          Thread t1 =
              new Thread(
                  new FFTProcessor(
                      this, j * len / PROCESS_NUM, (j + 1) * len / PROCESS_NUM, m, wm));
          thrds.addElement(t1);
          t1.start();
        }

        for (int k = 0; k < thrds.size(); k++) {
          ((Thread) thrds.get(k)).join();
        }
        thrds.clear();
      }
    } catch (Exception e) {
      System.out.println(" catch exception from FFT");
    }
  }
 public static void main(String[] args) {
   Scanner inp = new Scanner(System.in);
   int n = inp.nextInt();
   String[] store = new String[n];
   for (int i = 0; i < n; i++) store[i] = inp.next();
   int[] cnt = new int[n];
   Arrays.fill(cnt, 0);
   String str = inp.next();
   for (int j = 0; j < n; j++) {
     int l1 = store[j].length();
     for (int k = 0; k <= (str.length() - l1); k++) {
       if (str.substring(k, k + l1).equals(store[j])) {
         cnt[j] = cnt[j] + 1;
       }
     }
   }
   int y = 0;
   for (int m = 0; m < n; m++) {
     y = Math.max(y, cnt[m]);
   }
   System.out.println(y);
   for (int h = 0; h < n; h++) {
     if (cnt[h] == y) System.out.println(store[h]);
   }
 }
Beispiel #20
0
  public static int compare(Record o1, Record o2) {
    ArrayList<String> mKeys = new ArrayList<String>(o1.keySet());
    ArrayList<String> tKeys = new ArrayList<String>(o2.keySet());
    Collections.sort(mKeys);
    Collections.sort(tKeys);

    for (int i = 0; i != Math.min(mKeys.size(), tKeys.size()); ++i) {
      String mk = mKeys.get(i);
      String tk = tKeys.get(i);
      int c = mk.compareTo(tk);
      if (c != 0) {
        return c;
      }
      String mv = o1.get(mk).toString();
      String tv = o2.get(tk).toString();
      c = mv.compareTo(tv);
      if (c != 0) {
        return c;
      }
    }

    if (mKeys.size() < tKeys.size()) {
      return -1;
    } else if (mKeys.size() > tKeys.size()) {
      return 1;
    } else {
      return 0;
    }
  }
Beispiel #21
0
 public void makeSHButtonActionPerformed(ActionEvent evt) {
   String name = "Stakeholder " + (this.stakeholders.size());
   boolean a, b, c, d, e;
   a = b = c = d = e = false;
   int random = (int) (Math.random() * 32);
   System.out.println(random);
   if (random >= 16) {
     a = true;
     random -= 16;
   }
   if (random >= 8) {
     b = true;
     random -= 8;
   }
   if (random >= 4) {
     c = true;
     random -= 4;
   }
   if (random >= 2) {
     d = true;
     random -= 2;
   }
   if (random >= 1) {
     e = true;
     random -= 1;
   }
   System.out.printf("Stakeholder(%s, ,%b,%b,%b,%b,%b)%n", name, a, b, c, d, e);
   Stakeholder s = new Stakeholder(name, "", a, b, c, d, e);
   stakeholders.add(s);
   pushAllSHButton.setText("Push " + stakeholders.size() + " to View");
 }
 // To add/remove operators change evaluateOperator() and registration
 public double evaluateOperator(double lft, char opr, double rgt) {
   switch (opr) {
     case '=':
       return rgt; // simple assignment, used as the final operation, must be maximum precedence
     case '^':
       return Math.pow(lft, rgt); // power
     case '±':
       return -rgt; // unary negation
     case '*':
       return lft * rgt; // multiply (classical)
     case '×':
       return lft * rgt; // multiply (because it's a Unicode world out there)
     case '·':
       return lft * rgt; // multiply (because it's a Unicode world out there)
     case '(':
       return lft * rgt; // multiply (implicit due to brackets, e.g "(a)(b)")
     case '/':
       return lft / rgt; // divide (classical computing)
     case '÷':
       return lft / rgt; // divide (because it's a Unicode world out there)
     case '%':
       return lft % rgt; // remainder
     case '+':
       return lft + rgt; // add/unary-positive
     case '-':
       return lft - rgt; // subtract/unary-negative
     default:
       throw new UnsupportedOperationException(
           "MathEval internal operator setup is incorrect - internal operator \""
               + opr
               + "\" not handled");
   }
 }
Beispiel #23
0
  /** Returns cost of moving specified distance in specified map grid. */
  public static int getTerrainCost(MapGrid grid, int distance, int currentElevation) {
    int cost = 0;

    if (grid.getType() == 1) // Rough terrain cost
    {
      cost = cost + (distance * 2);
    } else if (grid.getType() == 2) // Light woods cost
    {
      cost = cost + (distance * 2);
    } else if (grid.getType() == 3) // Heavy woods cost
    {
      cost = cost + (distance * 3);
    } else if ((grid.getType() == 4) && (grid.getElevation() == 1)) // Depth 1 Water cost
    {
      cost = cost + (distance * 2);
    } else if ((grid.getType() == 4) && (grid.getElevation() >= 2)) // Depth 2+ Water cost
    {
      cost = cost + (distance * 4);
    } else {
      cost = cost + distance;
    } // Clear terrain

    cost = cost * grid.getSize();

    if (grid.getElevation() != currentElevation) {
      cost = cost + (Math.abs(grid.getElevation() - currentElevation) * 60);
    } // Elevation change cost

    return (cost);
  }
Beispiel #24
0
  public static void main(String[] args) throws IOException {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(br.readLine());
    int a[] = new int[n];
    int c[] = new int[n];
    for (int i = 0; i < n; i++) {
      a[i] = Integer.parseInt(br.readLine());
    }

    for (int i = 0; i < n; i++) {
      c[i] = 1;
    }

    for (int j = 1; j < n; j++) {
      if (a[j] > a[j - 1]) c[j] = c[j - 1] + 1;
    }

    for (int k = n - 2; k >= 0; k--) {
      if (a[k] > a[k + 1]) c[k] = Math.max(c[k + 1] + 1, c[k]);
    }
    int count = 0;
    for (int i = 0; i < n; i++) {
      count += c[i];
    }

    System.out.println(count);
  }
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int arr[][] = new int[6][6];
    int max = -10000; // lowest possible value is -9*6=-54
    int sum;
    for (int arr_i = 0; arr_i < 6; arr_i++) {
      for (int arr_j = 0; arr_j < 6; arr_j++) {
        arr[arr_i][arr_j] = in.nextInt();
      }
    }

    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        sum =
            arr[i][j]
                + arr[i][j + 1]
                + arr[i][j + 2]
                + arr[i + 1][j + 1]
                + arr[i + 2][j]
                + arr[i + 2][j + 1]
                + arr[i + 2][j + 2];
        max = Math.max(sum, max);
      }
    }
    System.out.println(max);
  }
Beispiel #26
0
  /** Returns distance between specified coordinates. */
  public static double getDistance(Point origin, Point destination) {
    int x = destination.getx() - origin.getx();
    int y = destination.gety() - origin.gety();
    double distance = Math.sqrt(x * x + y * y);

    return distance;
  }
Beispiel #27
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   n = in.nextInt();
   l = new BigInteger(in.next());
   k = in.nextInt();
   m = in.nextInt();
   BigInteger w = BigInteger.TEN.pow(m);
   for (int i = 0; i <= n; ++i) a[i] = new BigInteger(in.next());
   for (int i = 0; i < Math.min(k, n + 1); ++i) {
     t = a[0];
     for (int j = 1; j <= n; ++j) {
       t = t.multiply(l);
       t = t.add(a[j]);
     }
     t = t.mod(w);
     q[n][i] = t.mod(w);
     int ret = 0;
     c = t.toString().toCharArray();
     int ll = c.length;
     for (int j = 0; j < Math.min(m, ll); ++j) {
       ret += (c[ll - 1 - j] - '0') * (c[ll - 1 - j] - '0');
     }
     System.out.println(ret);
     l = l.add(BigInteger.ONE);
   }
   if (k > n) {
     for (int i = n - 1; i >= 0; --i) {
       for (int j = 0; j <= i; j++) q[i][j] = q[i + 1][j + 1].subtract(q[i + 1][j]).mod(w);
     }
     for (int i = 1; i <= n; ++i) q[0][i] = q[0][0];
     int po = 1;
     for (int i = n + 1; i < k; ++i) {
       for (int j = 1; j <= n; ++j)
         q[j][(po + j) % (n + 1)] =
             q[j - 1][(po + j - 1) % (n + 1)].add(q[j][(po + j - 1) % (n + 1)]).mod(w);
       int ret = 0;
       c = q[n][(po + n) % (n + 1)].mod(w).toString().toCharArray();
       int ll = c.length;
       for (int j = 0; j < Math.min(m, ll); ++j) {
         ret += (c[ll - 1 - j] - '0') * (c[ll - 1 - j] - '0');
       }
       System.out.println(ret);
       l = l.add(BigInteger.ONE);
       ++po;
     }
   }
 }
 public static boolean isPrime(long n) throws Exception {
   if (n == 1) return false;
   if (n == 2 || n == 3) return true;
   for (int i = 2; i <= Math.sqrt(n); i++) {
     if (n % i == 0) return false;
   }
   return true;
 }
Beispiel #29
0
 /**
  * Adds the time to the specified token builder.
  *
  * @param tb token builder
  */
 final void time(final TokenBuilder tb) {
   if (sec.remainder(DAYSECONDS).signum() == 0) return;
   tb.add('T');
   final long h = hou();
   if (h != 0) {
     tb.addLong(Math.abs(h));
     tb.add('H');
   }
   final long m = min();
   if (m != 0) {
     tb.addLong(Math.abs(m));
     tb.add('M');
   }
   final BigDecimal sc = sec();
   if (sc.signum() == 0) return;
   tb.add(Token.chopNumber(Token.token(sc.abs().toPlainString()))).add('S');
 }
Beispiel #30
0
  /** Returns distance to next map grid. */
  public static double dist(double num, int width, int dir) {
    // distance to next terrain square
    double next = num + width * dir;
    int square = (int) (round((next / width), dir)) * width;
    double dist = (square - num) - dir * ((double) width / 2);

    return Math.abs(dist);
  }