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 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; }
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(); }
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); } }
/** * 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)); }
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]); } }
// 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); }
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); }
public int solve(int start, int maxV, int be, int[] ivl) { if (start < 0 || start > maxV) return -1; if (be == ivl.length) return start; if (DP[be][start] != -2) return DP[be][start]; return DP[be][start] = Math.max( solve(start + ivl[be], maxV, be + 1, ivl), solve(start - ivl[be], maxV, be + 1, ivl)); }
/** * Adds the specified dayTime duration. * * @param add value to be added */ private void add(final BigDecimal add) { // normalized modulo: sc % 60 vs. (-sc + sc % 60 + 60 + sc) % 60 final BigDecimal sc = sec().add(add); sec = sc.signum() >= 0 ? sc.remainder(BD60) : sc.negate().add(sc.remainder(BD60)).add(BD60).add(sc).remainder(BD60); final long mn = Math.max(min(), 0) + div(sc.longValue(), 60); min = (byte) mod(mn, 60); final long ho = Math.max(hou, 0) + div(mn, 60); hou = (byte) mod(ho, 24); final long da = div(ho, 24); final long[] ymd = ymd(days().add(BigDecimal.valueOf(da))); yea = ymd[0]; mon = (byte) ymd[1]; day = (byte) ymd[2]; }
public void actionPerformed(ActionEvent e) { // Invoked when an action occurs. if (e.getSource().equals(singleSizeUp)) { singleSize = singleSize * 2; singleSizeLabel.setText("Image height=" + singleSize); } if (e.getSource().equals(singleSizeDown)) { singleSize = Math.max(64, singleSize / 2); singleSizeLabel.setText("Image height=" + singleSize); } if (e.getSource().equals(seqSizeUp)) { seqSize = seqSize * 2; seqSizeLabel.setText("Image height=" + seqSize); } if (e.getSource().equals(seqSizeDown)) { seqSize = Math.max(64, seqSize / 2); seqSizeLabel.setText("Image height=" + seqSize); } }
public static void main(String[] args) { while (scan.hasNextDouble()) { double t1 = scan.nextDouble(); double t2 = scan.nextDouble(); int i1, i2; for (i1 = 0; i1 < s1.length; i1++) { if (t1 < s1[i1]) break; } for (i2 = 0; i2 < s2.length; i2++) { if (t2 < s2[i2]) break; } System.out.println(r[Math.max(i1, i2)]); } }
// 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; }
void solve() { int n = nextInt(); int m = nextInt(); int l = 1; int r = n; for (int i = 0; i < m; i++) { nextToken(); nextToken(); String s = nextToken(); nextToken(); int k = nextInt(); if (s.equals("left")) { r = Math.min(r, k - 1); } else { l = Math.max(l, k + 1); } } if (l > r) { out.println(-1); } else { out.println(r - l + 1); } }
public int selectAction() { if (currentPath != -1) { if (nodePaths[currentPath].contains(currentNode)) { currentIndex = nodePaths[currentPath].indexOf(currentNode); } } if (firstStep) { pathLength = 0; // currentPath=generator.nextInt(paths.length); currentPath = selectCycle(); previousPath = currentPath; System.out.println("New path is generated"); System.out.println(currentPath); System.out.println(currentNode); firstStep = false; if (nodePaths[currentPath].contains(currentNode)) { pathLength++; currentIndex = nodePaths[currentPath].indexOf(currentNode); startTime = System.currentTimeMillis(); cycleReward = cycleReward + payout - travelCost; // currentUtilitiesforVisitingNodes[currentNode]; return paths[currentPath].get(currentIndex); } else { cycleReward = 0; return 0; } } else { if (currentIndex == 0 && nodePaths[currentPath].contains(currentNode)) { currentPath = selectCycle(); previousPath = currentPath; pathLength = 0; System.out.println("New path is generated"); System.out.println(currentPath); System.out.println(currentNode); firstStep = false; if (nodePaths[currentPath].contains(currentNode)) { pathLength++; currentIndex = nodePaths[currentPath].indexOf(currentNode); startTime = System.currentTimeMillis(); cycleReward = cycleReward + payout - travelCost; // currentUtilitiesforVisitingNodes[currentNode]; return paths[currentPath].get(currentIndex); } else { cycleReward = 0; return 0; } } // if(currentIndex==paths[currentPath].size()-1){ // firstStep=true; // } if (pathLength == paths[currentPath].size() - 1) { firstStep = true; endTime = System.currentTimeMillis(); Rtmp = cycleReward * 1000 / (endTime - startTime); if (t < N) { Rmax = Math.max(Rmax, Rtmp); } if (t == N) { alpha = Rmax; } if (t > N) { alpha = lambda * alpha + (1 - lambda) * Rtmp; } t = t + 1; } return paths[currentPath].get(currentIndex); } }
private int clamp(int val, int min, int max) { return Math.max(Math.min(val, max), min); }
public String winner(int[] x, int[] y) { int n = x.length; for (int i = 0; i < n; ++i) { x[i] += MAX_ABS; y[i] += MAX_ABS; } ArrayList<Point> points = new ArrayList<Point>(); int oneCount = 0; int twoCount = 0; for (int x0 = 0; x0 <= MAX_ABS * 2; ++x0) { boolean found = false; double yMin = Double.MAX_VALUE; double yMax = Double.MIN_VALUE; for (int i = 0; i < n; ++i) { int x1 = x[i]; int y1 = y[i]; int x2 = x[(i + 1) % n]; int y2 = y[(i + 1) % n]; if ((long) (x1 - x0) * (x2 - x0) <= 0) { found = true; if (x1 == x2) { yMin = Math.min(yMin, Math.min(y1, y2)); yMax = Math.max(yMax, Math.max(y1, y2)); } else { double y0 = y1 + (double) (y2 - y1) / (x2 - x1) * (x0 - x1); yMin = Math.min(yMin, y0); yMax = Math.max(yMax, y0); } } } if (found) { int count = 0; int y0 = (int) Math.ceil(yMin); while (count < 2 && y0 <= yMax) { boolean valid = true; for (int i = 0; i < n; ++i) { valid &= x[i] != x0 || y[i] != y0; } if (valid) { count++; points.add(new Point(x0, y0)); } y0++; } if (count >= 1) { oneCount++; } if (count >= 2) { twoCount++; } } } if (twoCount > 0) { return oneCount > 1 ? YES : NO; } if (points.size() <= 2) { return NO; } Point o = points.get(0); for (int i = 2; i < points.size(); ++i) { if (points.get(1).subtract(o).det(points.get(i).subtract(o)) != 0) { return YES; } } return NO; }
/** * Returns a day count. * * @return days */ final BigDecimal days() { final long y = yea == Long.MAX_VALUE ? 1 : yea; return days(y + ADD_NEG, Math.max(mon, 0), Math.max(day, 0)); }
/** * Analyzes the specified patterns. * * @param patterns patterns * @return picture variables */ private Picture[] analyze(final byte[][] patterns) { // pictures final int picL = patterns.length; final Picture[] pics = new Picture[picL]; // analyze patterns for (int p = 0; p < picL; p++) { final byte[] pt = patterns[p]; final Picture pic = new Picture(); // position (integer/fractional) int pos = 0; // active character found boolean act = false; // number of characters after exponent int exp = -1; // number of optional characters final int[] opt = new int[2]; // loop through all characters final int pl = pt.length; for (int i = 0, cl; i < pl; i += cl) { final int ch = ch(pt, i); cl = cl(pt, i); boolean active = contains(actives, ch); if (ch == decimal) { ++pos; act = false; } else if (ch == optional) { opt[pos]++; } else if (ch == exponent) { if (act && containsActive(pt, i + cl)) { exp = 0; } else { active = false; } } else if (ch == grouping) { if (pos == 0) pic.group[pos] = Array.add(pic.group[pos], pic.min[pos] + opt[pos]); } else if (contains(digits, ch)) { if (exp == -1) pic.min[pos]++; else exp++; } if (active) { act = true; } else { // passive characters pic.pc |= ch == percent; pic.pm |= ch == permille; // prefixes/suffixes pic.prefSuf[pos == 0 && act ? pos + 1 : pos].add(ch); } } // finalize integer-part-grouping-positions final int[] igp = pic.group[0]; final int igl = igp.length; for (int g = 0; g < igl; ++g) igp[g] = pic.min[0] + opt[0] - igp[g]; // check if integer-part-grouping-positions are regular // if yes, they are replaced with a single position if (igl > 1) { boolean reg = true; final int i = igp[igl - 1]; for (int g = igl - 2; g >= 0; --g) reg &= i * igl == igp[g]; if (reg) pic.group[0] = new int[] {i}; } pic.maxFrac = pic.min[1] + opt[1]; pic.minExp = Math.max(0, exp); pics[p] = pic; } return pics; }
// To add/remove functions change evaluateOperator() and registration public double evaluateFunction(String fncnam, ArgParser fncargs) throws ArithmeticException { switch (Character.toLowerCase(fncnam.charAt(0))) { case 'a': { if (fncnam.equalsIgnoreCase("abs")) { return Math.abs(fncargs.next()); } if (fncnam.equalsIgnoreCase("acos")) { return Math.acos(fncargs.next()); } if (fncnam.equalsIgnoreCase("asin")) { return Math.asin(fncargs.next()); } if (fncnam.equalsIgnoreCase("atan")) { return Math.atan(fncargs.next()); } } break; case 'c': { if (fncnam.equalsIgnoreCase("cbrt")) { return Math.cbrt(fncargs.next()); } if (fncnam.equalsIgnoreCase("ceil")) { return Math.ceil(fncargs.next()); } if (fncnam.equalsIgnoreCase("cos")) { return Math.cos(fncargs.next()); } if (fncnam.equalsIgnoreCase("cosh")) { return Math.cosh(fncargs.next()); } } break; case 'e': { if (fncnam.equalsIgnoreCase("exp")) { return Math.exp(fncargs.next()); } if (fncnam.equalsIgnoreCase("expm1")) { return Math.expm1(fncargs.next()); } } break; case 'f': { if (fncnam.equalsIgnoreCase("floor")) { return Math.floor(fncargs.next()); } } break; case 'g': { // if(fncnam.equalsIgnoreCase("getExponent" )) { return // Math.getExponent(fncargs.next()); } needs Java 6 } break; case 'l': { if (fncnam.equalsIgnoreCase("log")) { return Math.log(fncargs.next()); } if (fncnam.equalsIgnoreCase("log10")) { return Math.log10(fncargs.next()); } if (fncnam.equalsIgnoreCase("log1p")) { return Math.log1p(fncargs.next()); } } break; case 'm': { if (fncnam.equalsIgnoreCase("max")) { return Math.max(fncargs.next(), fncargs.next()); } if (fncnam.equalsIgnoreCase("min")) { return Math.min(fncargs.next(), fncargs.next()); } } break; case 'n': { // if(fncnam.equalsIgnoreCase("nextUp" )) { return Math.nextUp // (fncargs.next()); } needs Java 6 } break; case 'r': { if (fncnam.equalsIgnoreCase("random")) { return Math.random(); } // impure if (fncnam.equalsIgnoreCase("round")) { return Math.round(fncargs.next()); } if (fncnam.equalsIgnoreCase("roundHE")) { return Math.rint(fncargs.next()); } // round half-even } break; case 's': { if (fncnam.equalsIgnoreCase("signum")) { return Math.signum(fncargs.next()); } if (fncnam.equalsIgnoreCase("sin")) { return Math.sin(fncargs.next()); } if (fncnam.equalsIgnoreCase("sinh")) { return Math.sinh(fncargs.next()); } if (fncnam.equalsIgnoreCase("sqrt")) { return Math.sqrt(fncargs.next()); } } break; case 't': { if (fncnam.equalsIgnoreCase("tan")) { return Math.tan(fncargs.next()); } if (fncnam.equalsIgnoreCase("tanh")) { return Math.tanh(fncargs.next()); } if (fncnam.equalsIgnoreCase("toDegrees")) { return Math.toDegrees(fncargs.next()); } if (fncnam.equalsIgnoreCase("toRadians")) { return Math.toRadians(fncargs.next()); } } break; case 'u': { if (fncnam.equalsIgnoreCase("ulp")) { return Math.ulp(fncargs.next()); } } break; // no default } throw new UnsupportedOperationException( "MathEval internal function setup is incorrect - internal function \"" + fncnam + "\" not handled"); }