exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
3e6437c4168bf75d2de87174d16090b5
train_002.jsonl
1443430800
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
256 megabytes
import java.util.*; import java.awt.Point; import java.io.*; import java.math.BigInteger; public class CodeForces { FastScanner in; PrintWriter out; class pair implements Comparable { int min, max; char symb; pair (int a, int b, char ch) { min = Math.min(a, b); max = Math.max(a, b); symb = ch; } public int compareTo(Object obj) { pair cur = (pair) obj; if (cur.max - max == 0) return (cur.min - min); return (cur.max - max); } } public void solve() throws IOException { pair mas[] = new pair [3]; int square = 0; for (int i = 0; i < 3; i++) { int a = in.nextInt(); int b = in.nextInt(); char ch = 'A'; square += a*b; ch += i; mas[i] = new pair(a, b, ch); } Arrays.sort(mas); if (mas[0].max * mas[0].max != square) { System.out.println("-1"); return; } int size_n = square / mas[0].max; int size_m = mas[0].max; int tmp_n = size_n; char res[][] = new char [size_n][size_m]; // 1 for (int i = 0; i < mas[0].min; i++) for (int j = 0; j < size_m; j++) res[i][j] = mas[0].symb; tmp_n -= mas[0].min; // 2 if (mas[1].max == size_m) { for (int i = mas[0].min; i < mas[0].min + mas[1].min; i++) for (int j = 0; j < size_m; j++) res[i][j] = mas[1].symb; } else if (mas[1].max <= tmp_n) { for (int i = mas[0].min; i < mas[0].min + mas[1].max; i++) for (int j = 0; j < mas[1].min; j++) res[i][j] = mas[1].symb; } else { for (int i = mas[0].min; i < mas[0].min + mas[1].min; i++) for (int j = 0; j < mas[1].max; j++) res[i][j] = mas[1].symb; } System.out.println(size_m); // 3 for (int i = 0; i < size_n; i++) { for (int j = 0; j < size_m; j++) { if (res[i][j] != 0) System.out.print(res[i][j]); else System.out.print(mas[2].symb); } System.out.println(); } } public void run() { try { in = new FastScanner(); out = new PrintWriter(System.out); //in = new FastScanner(new File("knapsack.in")); //out = new PrintWriter(new File("knapsack.out")); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } String nextLine() { String ret = null; try { ret = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return ret; } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nextIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long[] nextLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } BigInteger nextBigInteger() { return new BigInteger(next()); } Point nextIntPoint() { int x = nextInt(); int y = nextInt(); return new Point(x, y); } Point[] nextIntPointArray(int size) { Point[] array = new Point[size]; for (int index = 0; index < size; ++index) { array[index] = nextIntPoint(); } return array; } List<Integer>[] readGraph(int vertexNumber, int edgeNumber, boolean undirected) { List<Integer>[] graph = new List[vertexNumber]; for (int index = 0; index < vertexNumber; ++index) { graph[index] = new ArrayList<Integer>(); } while (edgeNumber-- > 0) { int from = nextInt() - 1; int to = nextInt() - 1; graph[from].add(to); if(undirected) graph[to].add(from); } return graph; } } public static void main(String[] arg) { new CodeForces().run(); } }
Java
["5 1 2 5 5 2", "4 4 2 6 4 2"]
1 second
["5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC", "6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC"]
null
Java 7
standard input
[ "geometry", "constructive algorithms", "bitmasks", "math", "implementation", "brute force" ]
2befe5da2df57d23934601cbe4d4f151
The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.
1,700
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company, Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.
standard output
PASSED
cacd2054b5db0b0be3e23e9066c75455
train_002.jsonl
1443430800
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
256 megabytes
import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class P581D { private void run() { int x1 = nextInt(); int y1 = nextInt(); int x2 = nextInt(); int y2 = nextInt(); int x3 = nextInt(); int y3 = nextInt(); int totalArea = (x1 * y1) + (x2 * y2) + (x3 * y3); int ta = (int) Math.sqrt(totalArea); if (ta * ta != totalArea) { System.out.println("-1"); } else { if ((x1 == ta || y1 == ta) && (x2 == ta || y2 == ta) && (x3 == ta || y3 == ta)) { //all vertical or all horizontal int h1 = x1 == ta ? y1 : x1; int h2 = x2 == ta ? y2 : x2; int h3 = x3 == ta ? y3 : x3; if (h1 + h2 + h3 == ta) { System.out.println(ta); write(ta, h1, 'A'); write(ta, h2, 'B'); write(ta, h3, 'C'); } else { System.out.println("-1"); } } else { //one vertical and 2 horizontal int countTa = 0; countTa = countTa + (x1 == ta ? 1 : 0); countTa = countTa + (x2 == ta ? 1 : 0); countTa = countTa + (x3 == ta ? 1 : 0); countTa = countTa + (y1 == ta ? 1 : 0); countTa = countTa + (y2 == ta ? 1 : 0); countTa = countTa + (y3 == ta ? 1 : 0); if (countTa == 1) { if (x1 == ta || y1 == ta) { writeToSide(x1, y1, 'A', x2, y2, 'B', x3, y3, 'C', ta); } else if (x2 == ta || y2 == ta) { writeToSide(x2, y2, 'B', x1, y1, 'A', x3, y3, 'C', ta); } else if (x3 == ta || y3 == ta) { writeToSide(x3, y3, 'C', x2, y2, 'B', x1, y1, 'A', ta); } } else { System.out.println("-1"); } } } } private void writeToSide(int x1, int y1, char c1, int x2, int y2, char c2, int x3, int y3, char c3, int ta) { int height = Math.min(x1, y1); int pendingHeight = ta - height; if (x2 + x3 == ta && pendingHeight == y2 && pendingHeight == y3) { System.out.println(ta); write(ta, height, c1); writeSides(x2, c2, x3, c3, pendingHeight, ta); } else if (x2 + y3 == ta && pendingHeight == y2 && pendingHeight == x3) { System.out.println(ta); write(ta, height, c1); writeSides(x2, c2, y3, c3, pendingHeight, ta); } else if (y2 + x3 == ta && pendingHeight == x2 && pendingHeight == y3) { System.out.println(ta); write(ta, height, c1); writeSides(y2, c2, x3, c3, pendingHeight, ta); } else if (y2 + y3 == ta && pendingHeight == x2 && pendingHeight == x3) { System.out.println(ta); write(ta, height, c1); writeSides(y2, c2, y3, c3, pendingHeight, ta); } else { System.out.println("-1"); } } private void writeSides(int x2, char c2, int x3, char c3, int pendingHeight, int ta) { for (int i = 0; i < pendingHeight; i++) { for (int j = 0; j < ta; j++) { System.out.print(j < x2 ? c2 : c3); } System.out.println(); } } private void write(int ta, int h, char c) { for (int i = 0; i < h; i++) { for (int j = 0; j < ta; j++) { System.out.print(c); } System.out.println(); } } private int getMaxSatisfaction( Map<Integer, Map<Integer, Long>> increaseSatisfaction, Map<Integer, Long> satisfaction, Set<Integer> visited, Long currentSatisfaction) { return 0; } private int countPaths(Map<Integer, Boolean> vertexes, Map<Integer, Set<Integer>> edges, Integer currentVertex, int catsCount, int maxCatsCount, HashSet<Integer> visited) { if (visited.contains(currentVertex)) { return 0; } visited.add(currentVertex); Set<Integer> vertexEdges = new HashSet<Integer>(edges.get(currentVertex)); vertexEdges.removeAll(visited); int newCatsCount = vertexes.get(currentVertex).booleanValue() ? catsCount + 1 : 0; if (vertexEdges == null || vertexEdges.size() == 0) { //leaf if (newCatsCount > maxCatsCount) { return 0; } else { if (currentVertex.intValue() != 1) { return 1; } else { return 0; } } } if (newCatsCount > maxCatsCount) { return 0; } int count = 0; for (Integer nextVertex : vertexEdges) { count = count + countPaths(vertexes, edges, nextVertex, newCatsCount, maxCatsCount, visited); } visited.remove(currentVertex); return count; } public boolean isTriangle(int a, int b, int c) { return (a + b > c) && (a + c > b) && (b + c > a); } private char nextChar() { try { return (char) System.in.read(); } catch (IOException e) { throw new RuntimeException(e); } } private char[] incrementWord(char[] input, char maxLetter) { int currentIndex = input.length - 1; while (currentIndex >= 0 && input[currentIndex] == maxLetter) { currentIndex--; } if (currentIndex < 0) { return input; } input[currentIndex] = (char) (input[currentIndex] + 1); for (int i = currentIndex + 1; i < input.length; i++) { input[i] = 'a'; } return input; } private int getFree(Integer currentFree, Map<Integer, Integer> count) { while (count.containsKey(currentFree)) { currentFree = currentFree + 1; } return currentFree; } private double computeArea(double side1, double side2, double side3) { double p = (side1 + side2 + side3) / 2d; return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3)); } private int greaterThan(List<Integer> indexesP2, Integer j) { for (int i = 0; i < indexesP2.size(); i++) { Integer number = indexesP2.get(i); if (number > j) { return indexesP2.size() - i; } } return 0; } public static void main(String[] args) { P581D notes = new P581D(); notes.run(); notes.close(); } private Scanner sc; private P581D() { this.sc = new Scanner(System.in); } private int[] asInteger(String[] values) { int[] ret = new int[values.length]; for (int i = 0; i < values.length; i++) { String val = values[i]; ret[i] = Integer.valueOf(val).intValue(); } return ret; } private String nextLine() { return sc.nextLine(); } private long nextLong() { return sc.nextLong(); } private int nextInt() { return sc.nextInt(); } private String readLine() { if (sc.hasNextLine()) { return (sc.nextLine()); } else { return null; } } private void close() { try { sc.close(); } catch (Exception e) { //log } } } /** BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCC */
Java
["5 1 2 5 5 2", "4 4 2 6 4 2"]
1 second
["5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC", "6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC"]
null
Java 7
standard input
[ "geometry", "constructive algorithms", "bitmasks", "math", "implementation", "brute force" ]
2befe5da2df57d23934601cbe4d4f151
The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.
1,700
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company, Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.
standard output
PASSED
0a0f9169f8939beac01a70e8b71e2951
train_002.jsonl
1443430800
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
256 megabytes
import java.io.*; import java.util.*; public class C322D { public static StringTokenizer st; public static void nextLine(BufferedReader br) throws IOException { st = new StringTokenizer(br.readLine()); } public static String next() { return st.nextToken(); } public static int nextInt() { return Integer.parseInt(st.nextToken()); } public static long nextLong() { return Long.parseLong(st.nextToken()); } public static double nextDouble() { return Double.parseDouble(st.nextToken()); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); nextLine(br); int x1 = nextInt(); int y1 = nextInt(); int x2 = nextInt(); int y2 = nextInt(); int x3 = nextInt(); int y3 = nextInt(); int area = x1 * y1 + x2 * y2 + x3 * y3; int side = (int)Math.sqrt(area); if (side * side != area) { System.out.println(-1); return; } if (x1 > y1) { int tmp = x1; x1 = y1; y1 = tmp; } if (x2 > y2) { int tmp = x2; x2 = y2; y2 = tmp; } if (x3 > y3) { int tmp = x3; x3 = y3; y3 = tmp; } if (y1 == side && y2 == side && y3 == side) { System.out.println(side); for (int i = 0; i < x1; i++) { for (int j = 0; j < side; j++) { System.out.print("A"); } System.out.println(); } for (int i = 0; i < x2; i++) { for (int j = 0; j < side; j++) { System.out.print("B"); } System.out.println(); } for (int i = 0; i < x3; i++) { for (int j = 0; j < side; j++) { System.out.print("C"); } System.out.println(); } } else if (y1 == side) { int rem = side - x1; if (x2 == rem) { x2 = y2; y2 = rem; } else if (y2 != rem) { System.out.println(-1); return; } if (x3 == rem) { x3 = y3; y3 = rem; } else if (y3 != rem) { System.out.println(-1); return; } System.out.println(side); for (int i = 0; i < x1; i++) { for (int j = 0; j < side; j++) { System.out.print("A"); } System.out.println(); } for (int i = 0; i < rem; i++) { for (int j = 0; j < x2; j++) { System.out.print("B"); } for (int j = 0; j < x3; j++) { System.out.print("C"); } System.out.println(); } } else if (y2 == side) { int rem = side - x2; if (x1 == rem) { x1 = y1; y1 = rem; } else if (y1 != rem) { System.out.println(-1); return; } if (x3 == rem) { x3 = y3; y3 = rem; } else if (y3 != rem) { System.out.println(-1); return; } System.out.println(side); for (int i = 0; i < x2; i++) { for (int j = 0; j < side; j++) { System.out.print("B"); } System.out.println(); } for (int i = 0; i < rem; i++) { for (int j = 0; j < x1; j++) { System.out.print("A"); } for (int j = 0; j < x3; j++) { System.out.print("C"); } System.out.println(); } } else if (y3 == side) { int rem = side - x3; if (x1 == rem) { x1 = y1; y1 = rem; } else if (y1 != rem) { System.out.println(-1); return; } if (x2 == rem) { x2 = y2; y2 = rem; } else if (y2 != rem) { System.out.println(-1); return; } System.out.println(side); for (int i = 0; i < x3; i++) { for (int j = 0; j < side; j++) { System.out.print("C"); } System.out.println(); } for (int i = 0; i < rem; i++) { for (int j = 0; j < x1; j++) { System.out.print("A"); } for (int j = 0; j < x2; j++) { System.out.print("B"); } System.out.println(); } } else { System.out.println(-1); return; } } }
Java
["5 1 2 5 5 2", "4 4 2 6 4 2"]
1 second
["5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC", "6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC"]
null
Java 7
standard input
[ "geometry", "constructive algorithms", "bitmasks", "math", "implementation", "brute force" ]
2befe5da2df57d23934601cbe4d4f151
The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.
1,700
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company, Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.
standard output
PASSED
d82a0856ff3a6ac6d862846de34a7b58
train_002.jsonl
1443430800
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces { private static class Rect { int x; int y; char color; boolean inv; private Rect(int x, int y, char color) { this.color = color; if (x < y) { int temp = x; x = y; y = temp; } this.x = x; this.y = y; } @Override public String toString() { return "Rect{" + "x=" + x + ", y=" + y + ", color=" + color + ", inv=" + inv + '}'; } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int x1 = in.nextInt(); int y1 = in.nextInt(); int x2 = in.nextInt(); int y2 = in.nextInt(); int x3 = in.nextInt(); int y3 = in.nextInt(); int totalSpace = x1 * y1 + x2 * y2 + x3 * y3; List<Rect> rects = new ArrayList<>(); rects.add(new Rect(x1, y1, 'A')); rects.add(new Rect(x2, y2, 'B')); rects.add(new Rect(x3, y3, 'C')); if (Math.sqrt(totalSpace) * Math.sqrt(totalSpace) != totalSpace) { out.println(-1); out.close(); return; } int side = (int) Math.sqrt(totalSpace); char[][] res = new char[side][side]; List<Rect> seq = new ArrayList<>(); if (rec(rects, side, side, seq)) { out.println(side); for (Rect rect : seq) { boolean used = false; for (int i = 0; i < side; i++) { for (int j = 0; j < side; j++) { if (res[i][j] == '\u0000') { used = true; for (int k = i; k < i + (rect.inv ? rect.y : rect.x); k++) { for (int l = j; l < j + (rect.inv ? rect.x : rect.y); l++) { res[k][l] = rect.color; } } } if (used) break; } if (used) break; } } for (int i = 0; i < side; i++) { for (int j = 0; j < side; j++) { out.print(res[i][j]); } out.println(); } } else { out.println(-1); } out.close(); } private static boolean rec(List<Rect> rects, int side1, int side2, List<Rect> seq) { if (rects.size() == 1) { seq.add(rects.get(0)); if (rects.get(0).x == side1 && rects.get(0).y == side2) { return true; } if (rects.get(0).x == side2 && rects.get(0).y == side1) { rects.get(0).inv = true; return true; } return false; } for (int i = 0; i < rects.size(); i++) { Rect cur = rects.get(i); if (cur.x == side1) { List<Rect> newRects = new ArrayList<>(rects); newRects.remove(i); seq.add(cur); return rec(newRects, side1, side2 - cur.y, seq); } else if (cur.y == side2) { List<Rect> newRects = new ArrayList<>(rects); newRects.remove(i); seq.add(cur); return rec(newRects, side1 - cur.x, side2, seq); } else if (cur.y == side1) { List<Rect> newRects = new ArrayList<>(rects); newRects.remove(i); cur.inv = true; seq.add(cur); return rec(newRects, side1, side2 - cur.x, seq); } else if (cur.x == side2) { List<Rect> newRects = new ArrayList<>(rects); newRects.remove(i); cur.inv = true; seq.add(cur); return rec(newRects, side1 - cur.y, side2, seq); } } return false; } public static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public int[] nextIntArr(int n) { int[] arr = new int[n]; for (int j = 0; j < arr.length; j++) { arr[j] = nextInt(); } return arr; } public long[] nextLongArr(int n) { long[] arr = new long[n]; for (int j = 0; j < arr.length; j++) { arr[j] = nextLong(); } return arr; } } }
Java
["5 1 2 5 5 2", "4 4 2 6 4 2"]
1 second
["5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC", "6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC"]
null
Java 7
standard input
[ "geometry", "constructive algorithms", "bitmasks", "math", "implementation", "brute force" ]
2befe5da2df57d23934601cbe4d4f151
The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.
1,700
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company, Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.
standard output
PASSED
ad059ff57067713419e67b936a863ddb
train_002.jsonl
1443430800
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
256 megabytes
//package threelogos; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Collections; public class ThreeLogos { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); String [] parts = line.split(" "); Rectangle rs[] =new Rectangle[3]; rs[0] = new Rectangle(Integer.parseInt(parts[0]),Integer.parseInt(parts[1]),'A'); rs[1] = new Rectangle(Integer.parseInt(parts[2]),Integer.parseInt(parts[3]),'B'); rs[2] = new Rectangle(Integer.parseInt(parts[4]),Integer.parseInt(parts[5]),'C'); Arrays.sort(rs); char forFill [][] = new char [rs[0].getMax()][rs[0].getMax()]; for (int i=0;i<rs[0].getMin();i++){ for (int j=0;j<rs[0].getMax();j++){ forFill[i][j]=rs[0].id; } } int rest = rs[0].getMax()-rs[0].getMin(); int slen = rs[0].getMax(); if (rs[1].getMax()==slen){ if (rs[2].getMax()!=slen || rs[2].getMin()+rs[1].getMin()!=rest){ System.out.println(-1); return; } else{ for (int i=rs[0].getMin();i<rs[1].getMin()+rs[0].getMin();i++){ for (int j=0;j<rs[1].getMax();j++){ forFill[i][j]=rs[1].id; } } for (int i=rs[1].getMin()+rs[0].getMin();i<slen;i++){ for (int j=0;j<rs[2].getMax();j++){ forFill[i][j]=rs[2].id; } } } }else{ if (rs[1].x==rest){ if (rs[2].x==rest){ if (rs[1].y+rs[2].y==slen){ for (int i=rs[0].getMin();i<slen;i++){ for (int j=0;j<rs[1].y;j++){ forFill[i][j]=rs[1].id; } } for (int i=rs[0].getMin();i<slen;i++){ for (int j=rs[1].y;j<slen;j++){ forFill[i][j]=rs[2].id; } } } else{ System.out.println(-1); return; } } else if (rs[2].y==rest){ int temp = rs[2].y; rs[2].y=rs[2].x; rs[2].x=temp; if (rs[1].y+rs[2].y==slen){ for (int i=rs[0].getMin();i<slen;i++){ for (int j=0;j<rs[1].y;j++){ forFill[i][j]=rs[1].id; } } for (int i=rs[0].getMin();i<slen;i++){ for (int j=rs[1].y;j<slen;j++){ forFill[i][j]=rs[2].id; } } } else{ System.out.println(-1); return; } } else{ System.out.println(-1); return; } } else if (rs[1].y==rest){ int temp=rs[1].y; rs[1].y=rs[1].x; rs[1].x=temp; if (rs[2].x==rest){ if (rs[1].y+rs[2].y==slen){ for (int i=rs[0].getMin();i<slen;i++){ for (int j=0;j<rs[1].y;j++){ forFill[i][j]=rs[1].id; } } for (int i=rs[0].getMin();i<slen;i++){ for (int j=rs[1].y;j<slen;j++){ forFill[i][j]=rs[2].id; } } } else{ System.out.println(-1); return; } } else if (rs[2].y==rest){ int temp1 = rs[2].y; rs[2].y=rs[2].x; rs[2].x=temp1; if (rs[1].y+rs[2].y==slen){ for (int i=rs[0].getMin();i<slen;i++){ for (int j=0;j<rs[1].y;j++){ forFill[i][j]=rs[1].id; } } for (int i=rs[0].getMin();i<slen;i++){ for (int j=rs[1].y;j<slen;j++){ forFill[i][j]=rs[2].id; } } } else{ System.out.println(-1); return; } } else{ System.out.println(-1); return; } } else{ System.out.println(-1); return; } } System.out.println(slen); for (int i=0;i<forFill.length;i++){ for (int j=0;j<forFill.length;j++){ System.out.print(forFill[i][j]); } System.out.println(); } } } class Rectangle implements Comparable<Rectangle>{ int x; int y; char id; public Rectangle(int x, int y,char id){ this.x=x; this.y=y; this.id =id; } public int getMax(){ return Math.max(x, y); } public int getMin(){ return Math.min(x, y); } @Override public int compareTo(Rectangle o) { return o.getMax()-this.getMax(); } }
Java
["5 1 2 5 5 2", "4 4 2 6 4 2"]
1 second
["5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC", "6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC"]
null
Java 7
standard input
[ "geometry", "constructive algorithms", "bitmasks", "math", "implementation", "brute force" ]
2befe5da2df57d23934601cbe4d4f151
The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.
1,700
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company, Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.
standard output
PASSED
b21042e8a95120ddbf36b7c9030633fa
train_002.jsonl
1443430800
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
256 megabytes
import java.util.*; public class Main { static class Pair implements Comparable<Pair> { int x,y,z; Pair(int a,int b,int c) { x=a;y=b;z=c; } public int compareTo(Pair p) { return this.y-p.y; } } static int a,b,c,n; static Pair[] aa=new Pair[3]; static int[][] map=new int[200][200]; public static void main(String[] args) { Scanner in=new Scanner(System.in); for(int i=0;i<3;i++) { a=in.nextInt();b=in.nextInt(); if(a>b) { c=a;a=b;b=c;} aa[i]=new Pair(a,b,i); } Arrays.sort(aa); n=aa[2].y; if(aa[0].y==aa[1].y&&aa[1].y==aa[2].y&&aa[1].x+aa[2].x+aa[0].x==n) { System.out.println(n); int k=0; for(int i=0;i<3;i++) { for(int j=k;j<k+aa[i].x;j++) Arrays.fill(map[j],aa[i].z); k=k+aa[i].x; } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) System.out.print((char)(map[i][j]+'A')); System.out.println(); } } else { if(aa[0].x+aa[1].x==n&&aa[0].y==aa[1].y&&aa[2].x+aa[1].y==n) { System.out.println(n); for(int i=0;i<aa[2].x;i++) Arrays.fill(map[i],aa[2].z); for(int i=aa[2].x;i<n;i++) { for(int j=0;j<aa[0].x;j++) map[i][j]=aa[0].z; for(int j=aa[0].x;j<n;j++) map[i][j]=aa[1].z; } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) System.out.print((char)(map[i][j]+'A')); System.out.println(); } } else if(aa[0].x+aa[1].y==n&&aa[0].y==aa[1].x&&aa[2].x+aa[0].y==n) { System.out.println(n); for(int i=0;i<aa[2].x;i++) Arrays.fill(map[i],aa[2].z); for(int i=aa[2].x;i<n;i++) { for(int j=0;j<aa[0].x;j++) map[i][j]=aa[0].z; for(int j=aa[0].x;j<n;j++) map[i][j]=aa[1].z; } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) System.out.print((char)(map[i][j]+'A')); System.out.println(); } } else if(aa[0].y+aa[1].y==n&&aa[0].x==aa[1].x&&aa[2].x+aa[1].x==n) { System.out.println(n); for(int i=0;i<aa[2].x;i++) Arrays.fill(map[i],aa[2].z); for(int i=aa[2].x;i<n;i++) { for(int j=0;j<aa[0].y;j++) map[i][j]=aa[0].z; for(int j=aa[0].y;j<n;j++) map[i][j]=aa[1].z; } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) System.out.print((char)(map[i][j]+'A')); System.out.println(); } } else if(aa[0].y+aa[1].x==n&&aa[0].x==aa[1].y&&aa[2].x+aa[1].y==n) { System.out.println(n); for(int i=0;i<aa[2].x;i++) Arrays.fill(map[i],aa[2].z); for(int i=aa[2].x;i<n;i++) { for(int j=0;j<aa[0].y;j++) map[i][j]=aa[0].z; for(int j=aa[0].y;j<n;j++) map[i][j]=aa[1].z; } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) System.out.print((char)(map[i][j]+'A')); System.out.println(); } } else System.out.println(-1); } } }
Java
["5 1 2 5 5 2", "4 4 2 6 4 2"]
1 second
["5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC", "6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC"]
null
Java 7
standard input
[ "geometry", "constructive algorithms", "bitmasks", "math", "implementation", "brute force" ]
2befe5da2df57d23934601cbe4d4f151
The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.
1,700
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company, Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.
standard output
PASSED
9e04d82b16686dadcaac216576c08775
train_002.jsonl
1443430800
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
256 megabytes
import java.util.*; public class three_logos { public static void main( String[] args ) { Scanner in = new Scanner( System.in ); int a1 = in.nextInt(), a2 = in.nextInt(); int b1 = in.nextInt(), b2 = in.nextInt(); int c1 = in.nextInt(), c2 = in.nextInt(); int d = a1 * a2 + b1 * b2 + c1 * c2; int s = (int)Math.floor( Math.sqrt( d ) ); if ( s * s != d ) System.out.println( -1 ); else if ( a1 == s && b1 == s && c1 == s ) printrows( s, a2, b2, c2 ); else if ( a1 == s && b1 == s && c2 == s ) printrows( s, a2, b2, c1 ); else if ( a1 == s && b2 == s && c1 == s ) printrows( s, a2, b1, c2 ); else if ( a1 == s && b2 == s && c2 == s ) printrows( s, a2, b1, c1 ); else if ( a2 == s && b1 == s && c1 == s ) printrows( s, a1, b2, c2 ); else if ( a2 == s && b1 == s && c2 == s ) printrows( s, a1, b2, c1 ); else if ( a2 == s && b2 == s && c1 == s ) printrows( s, a1, b1, c2 ); else if ( a2 == s && b2 == s && c2 == s ) printrows( s, a1, b1, c1 ); else if ( a1 == s ) { if ( b1 + c1 == s && b2 == c2 ) printstag( s, a2, b1, c1, "ABC" ); else if ( b1 + c2 == s && b2 == c1 ) printstag( s, a2, b1, c2, "ABC" ); else if ( b2 + c1 == s && b1 == c2 ) printstag( s, a2, b2, c1, "ABC" ); else if ( b2 + c2 == s && b1 == c1 ) printstag( s, a2, b2, c2, "ABC" ); } else if ( a2 == s ) { if ( b1 + c1 == s && b2 == c2 ) printstag( s, a1, b1, c1, "ABC" ); else if ( b1 + c2 == s && b2 == c1 ) printstag( s, a1, b1, c2, "ABC" ); else if ( b2 + c1 == s && b1 == c2 ) printstag( s, a1, b2, c1, "ABC" ); else if ( b2 + c2 == s && b1 == c1 ) printstag( s, a1, b2, c2, "ABC" ); } else if ( b1 == s ) { if ( a1 + c1 == s && a2 == c2 ) printstag( s, b2, a1, c1, "BAC" ); else if ( a1 + c2 == s && a2 == c1 ) printstag( s, b2, a1, c2, "BAC" ); else if ( a2 + c1 == s && a1 == c2 ) printstag( s, b2, a2, c1, "BAC" ); else if ( a2 + c2 == s && a1 == c1 ) printstag( s, b2, a2, c2, "BAC" ); } else if ( b2 == s ) { if ( a1 + c1 == s && a2 == c2 ) printstag( s, b1, a1, c1, "BAC" ); else if ( a1 + c2 == s && a2 == c1 ) printstag( s, b1, a1, c2, "BAC" ); else if ( a2 + c1 == s && a1 == c2 ) printstag( s, b1, a2, c1, "BAC" ); else if ( a2 + c2 == s && a1 == c1 ) printstag( s, b1, a2, c2, "BAC" ); } else if ( c1 == s ) { if ( a1 + b1 == s && a2 == b2 ) printstag( s, c2, a1, b1, "CAB" ); else if ( a1 + b2 == s && a2 == b1 ) printstag( s, c2, a1, b2, "CAB" ); else if ( a2 + b1 == s && a1 == b2 ) printstag( s, c2, a2, b1, "CAB" ); else if ( a2 + b2 == s && a1 == b1 ) printstag( s, c2, a2, b2, "CAB" ); } else if ( c2 == s ) { if ( a1 + b1 == s && a2 == b2 ) printstag( s, c1, a1, b1, "CAB" ); else if ( a1 + b2 == s && a2 == b1 ) printstag( s, c1, a1, b2, "CAB" ); else if ( a2 + b1 == s && a1 == b2 ) printstag( s, c1, a2, b1, "CAB" ); else if ( a2 + b2 == s && a1 == b1 ) printstag( s, c1, a2, b2, "CAB" ); } else System.out.println( -1 ); in.close(); System.exit( 0 ); } public static void printstag( int s, int a, int b, int c, String str ) { System.out.println( s ); for ( int i = 0; i < a; i++ ) { for ( int j = 0; j < s; j++ ) System.out.print( str.charAt( 0 ) ); System.out.println(); } for ( int i = 0; i < s - a; i++ ) { for ( int j = 0; j < b; j++ ) System.out.print( str.charAt( 1 ) ); for ( int j = 0; j < c; j++ ) System.out.print( str.charAt( 2 ) ); System.out.println(); } } public static void printrows( int s, int a, int b, int c ) { System.out.println( s ); for ( int i = 0; i < a; i++ ) { for ( int j = 0; j < s; j++ ) System.out.print( 'A' ); System.out.println(); } for ( int i = 0; i < b; i++ ) { for ( int j = 0; j < s; j++ ) System.out.print( 'B' ); System.out.println(); } for ( int i = 0; i < c; i++ ) { for ( int j = 0; j < s; j++ ) System.out.print( 'C' ); System.out.println(); } } } // 5 1 2 5 5 2 // 4 4 2 6 4 2
Java
["5 1 2 5 5 2", "4 4 2 6 4 2"]
1 second
["5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC", "6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC"]
null
Java 7
standard input
[ "geometry", "constructive algorithms", "bitmasks", "math", "implementation", "brute force" ]
2befe5da2df57d23934601cbe4d4f151
The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.
1,700
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company, Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.
standard output
PASSED
908ac5f2614b4f944f938e004b7a69a2
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.util.*; import java.io.*; public class Main { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void main(String args[]) throws IOException { Scan input=new Scan(); int test=input.scanInt(); StringBuilder ans=new StringBuilder(""); for(int t=1;t<=test;t++) { long n=input.scanInt(); long m=input.scanInt(); ans.append(solve(n,m,n-m)+"\n"); } System.out.println(ans); } public static long solve(long n,long one,long zero) { long tot=n*(n+1); tot/=2; if(one+1>=zero) { return tot-zero; } long part=zero/(one+1); long rem=zero%(one+1); long p1=((part+1)*(part+2))/2; long p2=(part*(part+1))/2; long ans=(p1*rem)+(p2*(one+1-rem)); return tot-ans; } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
a00833f99879d692b8e79de0101fa672
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static long sx = 0, sy = 0, m = (long) (1e9 + 7); static ArrayList<pair>[] a; static int[][] dp; static long[] farr; static boolean b = true; // static HashMap<Long, Integer> hm = new HashMap<>(); static TreeMap<Integer, Integer> hm = new TreeMap<>(); public static PrintWriter out; static ArrayList<pair> ans = new ArrayList<>(); static long[] fact = new long[(int) 1e6]; static boolean[] prime; static StringBuilder sb = new StringBuilder(); static boolean cycle = false; static long mod = 998244353; public static void main(String[] args) throws IOException { Reader scn = new Reader(); // Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while (t-- != 0) { long n = scn.nextLong(), m = scn.nextLong(); long ans = 0; if ((n - m) % (m + 1) == 0) { long each = (n - m) / (m + 1); ans = n * (n + 1) / 2 - ((m + 1) * (each * (each + 1) / 2)); } else { long x = 0; long y = (n - m) % (m + 1); // 1 group x = (long) Math.ceil((n - m) / (m + 1)); // m // groups ans = n * (n + 1) / 2 - (y * (x + 1) * (x + 2) / 2 + (m + 1 - y) * x * (x + 1) / 2); } System.out.println(ans); } } // _________________________TEMPLATE_____________________________________________________________ // private static int gcd(int a, int b) { // if (a == 0) // return b; // // return gcd(b % a, a); // } // static class comp implements Comparator<Integer> { // // @Override // public int compare(Integer o1, Integer o2) { // // return (int) (o2 - o1); // } // // } // public static long pow(long a, long b) { // // if(b<0)return 0; // if (b == 0 || b == 1) // return (long) Math.pow(a, b); // // if (b % 2 == 0) { // // long ret = pow(a, b / 2); // ret = (ret % mod * ret % mod) % mod; // return ret; // } // // else { // return ((pow(a, b - 1) % mod) * a % mod) % mod; // } // } private static class pair implements Comparable<pair> { int d, r, t, s; pair(int a, int b, int c) { d = a; r = b; t = c; s = d - t; } @Override public int compareTo(pair o) { if (this.d != o.d) return this.d - o.d; else return o.r - this.r; } } public static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[1000000 + 1]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public int[][] nextInt2DArray(int m, int n) throws IOException { int[][] arr = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) arr[i][j] = nextInt(); } return arr; } // kickstart - Solution // atcoder - Main } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
e7a6d66fa291f368c5dbd6b98b6c815a
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.*; public class codechef { public static void main(String args[]) throws Exception { FastReader in = new FastReader(System.in); int testcases=in.nextInt(),i; StringBuilder sb = new StringBuilder(); start:while(testcases-->0) { int n=in.nextInt(),m=in.nextInt(); long p=((long)n*(long)(n+1))/2; if(m+1>=n-m) { sb.append(p-(n-m)).append("\n"); continue start; } int x=(n-m)%(m+1); long k=(n-m)/(m+1),res=0; res=(k*(k+1))/2; res=res*(long)(m+1-x); res+=(((k+1)*(k+2))/2)*(long)x; sb.append(p-res).append("\n"); } System.out.print(sb); } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
a8d55dd7c421cf962686717d37970a98
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.*; public class codechef { public static void main(String args[]) throws Exception { FastReader in = new FastReader(System.in); int testcases=in.nextInt(),i; StringBuilder sb = new StringBuilder(); start:while(testcases-->0) { int n=in.nextInt(),m=in.nextInt(); long p=((long)n*(long)(n+1))/2; int x=(n-m)%(m+1); long k=(n-m)/(m+1),res=0; res=(k*(k+1))/2; res=res*(long)(m+1-x); res+=(((k+1)*(k+2))/2)*(long)x; sb.append(p-res).append("\n"); } System.out.print(sb); } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
bcbd1b1100b273c69bbd6ad2779eac28
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class f { public static void print(String str,int val){ System.out.println(str+" "+val); } public long gcd(long a, long b) { if (b==0L) return a; return gcd(b,a%b); } public static void debug(long[][] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.println(Arrays.toString(arr[i])); } } public static void debug(int[][] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.println(Arrays.toString(arr[i])); } } public static void debug(String[] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.println(arr[i]); } } public static void print(int[] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.print(arr[i]+" "); } System.out.print('\n'); } public static void print(String[] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.print(arr[i]+" "); } System.out.print('\n'); } public static void print(long[] arr){ int len = arr.length; for(int i=0;i<len;i++){ System.out.print(arr[i]+" "); } System.out.print('\n'); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String path) throws FileNotFoundException { br = new BufferedReader(new FileReader(path)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s=new FastReader(); int t = s.nextInt(); for(int tt=0;tt<t;tt++){ int n = s.nextInt(); int m = s.nextInt(); System.out.println(solve(n,m)); } } static long solve(long n,long m){ long k = (n-m)/(m+1); long rem = (n-m)%(m+1); long ans = n*(n+1)/2;; if(rem==0){ ans-=((m+1)*(k)*(k+1)/2); return ans; } else { ans -=((m+1-rem)*(k)*(k+1)/2); ans-=(rem*(k+1)*(k+2))/2; return ans; } } // OutputStream out = new BufferedOutputStream( System.out ); // for(int i=1;i<n;i++){ // out.write((arr[i]+" ").getBytes()); // } // out.flush(); // long start_time = System.currentTimeMillis(); // long end_time = System.currentTimeMillis(); // System.out.println((end_time - start_time) + "ms"); }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
ba44140007c8c2bdfb4a8ae68afee323
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Solution{ public static void main(String[] args) throws Exception{ FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tt = fs.nextInt(); while(tt-->0) { long n = fs.nextLong(), m = fs.nextLong(); long k = n-m; long g = m+1; long ans = n*(n+1)/2 - (g - k%g)*(k/g)*(k/g+1)/2 - (k%g)*((long)Math.ceil((double)k/g))*(((long)Math.ceil((double)k/g))+1)/2; out.println(ans); } out.close(); } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
a206d818b3ac034bdcdade6f3ee31880
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { out.println(solve(in, out)); } private long solve(Scanner in, PrintWriter out) { long n = in.nextInt(); long m = in.nextInt(); if (m >= n / 2) { return n * (n + 1) / 2 - (n - m); } long r = n - m; long k = r / (m + 1); long z = r - k * (m + 1); long res = n * (n + 1) / 2; return res - k * (k + 1) / 2 * (m + 1 - z) - (k + 1) * (k + 2) / 2 * z; } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
6bcb27ed657cce768e8a53feb6a6156d
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Solve7 { public static void main(String[] args) throws IOException { PrintWriter pw = new PrintWriter(System.out); new Solve7().solve(pw); pw.flush(); pw.close(); } public void solve(PrintWriter pw) throws IOException { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { long n = sc.nextInt(), m = sc.nextInt(); long z = n - m; long ans = (n * (n + 1)) / 2; ++m; if (z <= m) { ans -= z; pw.println(ans); } else { long r = z / m, rem = z % m; long d = ((r + 1) * (r + 2)) / 2 * rem + (r * (r + 1)) / 2 * (m - rem); ans -= d; pw.println(ans); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { } } public String next() { if (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { } return null; } public boolean hasNext() throws IOException { if (st != null && st.hasMoreTokens()) { return true; } String s = br.readLine(); if (s == null || s.isEmpty()) { return false; } st = new StringTokenizer(s); return true; } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
5181df169d60213d0733ace1f383a6f2
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.util.*;import java.io.*;import java.math.*; public class Main { public static void process()throws IOException { long n=nl(),m=nl(); if(n==m){ pn(n*(n+1l)/2l); return; } long z=n-m;//no. of zeroes long g=m+1l;//size of one group, similar to what you do in beggar's method long k=z/g,rem=z%g; long res=n*(n+1l)/2l-k*(k+1l)/2l*g-(k+1l)*rem; pn(res); } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { out = new PrintWriter(System.out); sc=new AnotherReader(); boolean oj = true; oj = System.getProperty("ONLINE_JUDGE") != null; if(!oj) sc=new AnotherReader(100); long s = System.currentTimeMillis(); int t=1; t=ni(); while(t-->0) process(); out.flush(); if(!oj) System.out.println(System.currentTimeMillis()-s+"ms"); System.out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);System.out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} static boolean multipleTC=false; static long mod=(long)1e9+7l; static void r_sort(int arr[],int n){ Random r = new Random(); for (int i = n-1; i > 0; i--){ int j = r.nextInt(i+1); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } Arrays.sort(arr); } static long mpow(long x, long n) { if(n == 0) return 1; if(n % 2 == 0) { long root = mpow(x, n / 2); return root * root % mod; }else { return x * mpow(x, n - 1) % mod; } } static long mcomb(long a, long b) { if(b > a - b) return mcomb(a, a - b); long m = 1; long d = 1; long i; for(i = 0; i < b; i++) { m *= (a - i); m %= mod; d *= (i + 1); d %= mod; } long ans = m * mpow(d, mod - 2) % mod; return ans; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
e353988a568f65f995660f660c044d79
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Sample { static int[][][] dp; static int[] are; public static void main(String[] args) throws InterruptedException { Thread t=new Thread(null, null,"", 1<<28) { public void run() { m(); } }; t.start(); t.join(); } public static void m() { FastScanner in=new FastScanner(); int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); int m = in.nextInt(); // if(m==0) { System.out.println(0); continue; } // mumbaiGay(n, m); } } static void mumbaiGay(int n, int m) { long total = n*(long)(n+1)/2; long zeroes = n-m; long groups = m+1; long small = zeroes/groups; long big = small+1; long big_count = zeroes%groups; long small_count = groups-big_count; // long waysS = small_count*small*(long)(small+1)/2; long waysB = big_count*big*(long)(big+1)/2; // System.out.println(total-waysS-waysB); } static void longestsub(char[] str1, char[] str2) { int n = str1.length; int m = str2.length; // int[][] dp = new int[n+1][m+1]; // for(int i=0; i<=n; i++) { for(int j=0; j<=m; j++) { if(i==0 || j==0) {dp[i][j]=0; continue;} // if(str1[i-1]==str2[j-1]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); } } // int length = dp[n][m]; // String ans = ""; // int i=n, j=m; while(true) { if(i==0 || j==0) break; // if(dp[i][j]==Math.max(dp[i-1][j], dp[i][j-1])) { if(dp[i-1][j]>dp[i][j-1]) i--; else j--; } else { ans = str1[i-1] + ans; i--; j--; } } // System.out.println(length); System.out.println(ans); } static void print(String[] are) { for(int i=0; i<are.length; i++) System.out.print(are[i]); System.out.println(); } static void sort(int[] a) { ArrayList<Integer> list=new ArrayList<>(); for (int i:a) list.add(i); Collections.sort(list); for (int i=0; i<a.length; i++) a[i]=list.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } public long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
042d8de64ecd095094c9c9cd1b655726
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
/** * @author derrick20 */ import java.io.*; import java.util.*; public class AyoubFunction { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = sc.nextInt(); while (T-->0) { long N = sc.nextLong(); long M = sc.nextLong(); if (M == 0) { out.println(0); } else if (M == 1) { long x = (N + 1) / 2; out.println(x * (N - x + 1)); } else { long ans = (N * (N - 1)) / 2 + N; long block = (N - M) / (M + 1); long res = (N - M) % (M + 1); // some get the extra 1 ans -= res * (((block + 1) * block) / 2 + (block + 1)); ans -= (M + 1 - res) * ((block * (block - 1)) / 2 + block); // last one gets the residue out.println(ans); } } out.close(); } static class FastScanner { private int BS = 1<<16; private char NC = (char)0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt=1; boolean neg = false; if(c==NC)c=getChar(); for(;(c<'0' || c>'9'); c = getChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=getChar()) { res = (res<<3)+(res<<1)+c-'0'; cnt*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/cnt; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=getChar(); while(c>32) { res.append(c); c=getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=getChar(); while(c!='\n') { res.append(c); c=getChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=getChar(); if(c==NC)return false; else if(c>32)return true; } } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
07736ac4722821f5931f6d5d2cd30ac4
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.awt.Point; // U KNOW THAT IF THIS DAY WILL BE URS THEN NO ONE CAN DEFEAT U HERE................ // ASCII = 48 + i ;// 2^28 = 268,435,456 > 2* 10^8 // log 10 base 2 = 3.3219 // odd:: (x^2+1)/2 , (x^2-1)/2 ; x>=3// even:: (x^2/4)+1 ,(x^2/4)-1 x >=4 // FOR ANY ODD NO N : N,N-1,N-2 //ALL ARE PAIRWISE COPRIME //THEIR COMBINED LCM IS PRODUCT OF ALL THESE NOS // two consecutive odds are always coprime to each other // two consecutive even have always gcd = 2 ; public class Main { // static int[] arr = new int[100002] ; // static int[] dp = new int[100002] ; static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } String next(){ while(st==null || !st.hasMoreElements()){ try{ st= new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str=br.readLine(); } catch(IOException e){ e.printStackTrace(); } return str; } } //////////////////////////////////////////////////////////////////////////////////// public static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } ///////////////////////////////////////////////////////////////////////////////////////// public static int sumOfDigits(long n) { if( n< 0)return -1 ; int sum = 0; while( n > 0) { sum = sum + (int)( n %10) ; n /= 10 ; } return sum ; } ////////////////////////////////////////////////////////////////////////////////////////////////// public static long arraySum(int[] arr , int start , int end) { long ans = 0 ; for(int i = start ; i <= end ; i++)ans += arr[i] ; return ans ; } ///////////////////////////////////////////////////////////////////////////////// public static int mod(int x) { if(x <0)return -1*x ; else return x ; } public static long mod(long x) { if(x <0)return -1*x ; else return x ; } //////////////////////////////////////////////////////////////////////////////// public static void swapArray(int[] arr , int start , int end) { while(start < end) { int temp = arr[start] ; arr[start] = arr[end]; arr[end] = temp; start++ ;end-- ; } } ////////////////////////////////////////////////////////////////////////////////// public static int[][] rotate(int[][] input){ int n =input.length; int m = input[0].length ; int[][] output = new int [m][n]; for (int i=0; i<n; i++) for (int j=0;j<m; j++) output [j][n-1-i] = input[i][j]; return output; } /////////////////////////////////////////////////////////////////////////////////////////////////////// public static int countBits(long n) { int count = 0; while (n != 0) { count++; n = (n) >> (1L) ; } return count; } /////////////////////////////////////////// //////////////////////////////////////////////// public static boolean isPowerOfTwo(long n) { if(n==0) return false; if(((n ) & (n-1)) == 0 ) return true ; else return false ; } ///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// public static String reverse(String input) { StringBuilder str = new StringBuilder("") ; for(int i =input.length()-1 ; i >= 0 ; i-- ) { str.append(input.charAt(i)); } return str.toString() ; } /////////////////////////////////////////////////////////////////////////////////////////// public static boolean sameParity(long a ,long b ) { long x = a% 2L; long y = b%2L ; if(x==y)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static boolean isPossibleTriangle(int a ,int b , int c) { if( a + b > c && c+b > a && a +c > b)return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////////////// static long xnor(long num1, long num2) { if (num1 < num2) { long temp = num1; num1 = num2; num2 = temp; } num1 = togglebit(num1); return num1 ^ num2; } static long togglebit(long n) { if (n == 0) return 1; long i = n; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return i ^ n; } /////////////////////////////////////////////////////////////////////////////////////////////// public static int xorOfFirstN(int n) { if( n % 4 ==0)return n ; else if( n % 4 == 1)return 1 ; else if( n % 4 == 2)return n+1 ; else return 0 ; } ////////////////////////////////////////////////////////////////////////////////////////////// public static int gcd(int a, int b ) { if(b==0)return a ; else return gcd(b,a%b) ; } public static long gcd(long a, long b ) { if(b==0)return a ; else return gcd(b,a%b) ; } //////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c , int d ) { int temp = lcm(a,b , c) ; int ans = lcm(temp ,d ) ; return ans ; } /////////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c ) { int temp = lcm(a,b) ; int ans = lcm(temp ,c) ; return ans ; } //////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a , int b ) { int gc = gcd(a,b); return (a*b)/gc ; } public static long lcm(long a , long b ) { long gc = gcd(a,b); return (a*b)/gc ; } /////////////////////////////////////////////////////////////////////////////////////////// static boolean isPrime(long n) { if(n==1) { return false ; } boolean ans = true ; for(long i = 2L; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } static boolean isPrime(int n) { if(n==1) { return false ; } boolean ans = true ; for(int i = 2; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } /////////////////////////////////////////////////////////////////////////// static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; public static void sieveOfEratosthenes() { // FALSE == prime // TRUE == COMPOSITE // FALSE== 1 // time complexity = 0(NlogLogN)== o(N) // gives prime nos bw 1 to N for(int i = 4; i<= sieve ; i++) { prime[i] = true ; i++ ; } for(int p = 3; p*p <= sieve; p++) { if(prime[p] == false) { for(int i = p*p; i <= sieve; i += p) prime[i] = true; } p++ ; } } /////////////////////////////////////////////////////////////////////////////////// public static void sortD(int[] arr , int s , int e) { sort(arr ,s , e) ; int i =s ; int j = e ; while( i < j) { int temp = arr[i] ; arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ; } return ; } ///////////////////////////////////////////////////////////////////////////////////////// public static long countSubarraysSumToK(long[] arr ,long sum ) { HashMap<Long,Long> map = new HashMap<>() ; int n = arr.length ; long prefixsum = 0 ; long count = 0L ; for(int i = 0; i < n ; i++) { prefixsum = prefixsum + arr[i] ; if(sum == prefixsum)count = count+1 ; if(map.containsKey(prefixsum -sum)) { count = count + map.get(prefixsum -sum) ; } if(map.containsKey(prefixsum )) { map.put(prefixsum , map.get(prefixsum) +1 ); } else{ map.put(prefixsum , 1L ); } } return count ; } /////////////////////////////////////////////////////////////////////////////////////////////// // KMP ALGORITHM : TIME COMPL:O(N+M) // FINDS THE OCCURENCES OF PATTERN AS A SUBSTRING IN STRING //RETURN THE ARRAYLIST OF INDEXES // IF SIZE OF LIST IS ZERO MEANS PATTERN IS NOT PRESENT IN STRING public static ArrayList<Integer> kmpAlgorithm(String str , String pat) { ArrayList<Integer> list =new ArrayList<>(); int n = str.length() ; int m = pat.length() ; String q = pat + "#" + str ; int[] lps =new int[n+m+1] ; longestPefixSuffix(lps, q,(n+m+1)) ; for(int i =m+1 ; i < (n+m+1) ; i++ ) { if(lps[i] == m) { list.add(i-2*m) ; } } return list ; } public static void longestPefixSuffix(int[] lps ,String str , int n) { lps[0] = 0 ; for(int i = 1 ; i<= n-1; i++) { int l = lps[i-1] ; while( l > 0 && str.charAt(i) != str.charAt(l)) { l = lps[l-1] ; } if(str.charAt(i) == str.charAt(l)) { l++ ; } lps[i] = l ; } } /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// // CALCULATE TOTIENT Fn FOR ALL VALUES FROM 1 TO n // TOTIENT(N) = count of nos less than n and grater than 1 whose gcd with n is 1 // or n and the no will be coprime in nature //time : O(n*(log(logn))) public static void eulerTotientFunction(int[] arr ,int n ) { for(int i = 1; i <= n ;i++)arr[i] =i ; for(int i= 2 ; i<= n ;i++) { if(arr[i] == i) { arr[i] =i-1 ; for(int j =2*i ; j<= n ; j+= i ) { arr[j] = (arr[j]*(i-1))/i ; } } } return ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; int j=1; for(;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } /////////////////////////////////////////////////////////////////////////////////////////// public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } public static ArrayList<Long> allFactors(long n) { ArrayList<Long> list = new ArrayList<>() ; for(long i = 1L; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } //////////////////////////////////////////////////////////////////////////////////////////////////// static final int MAXN = 10000001; static int spf[] = new int[MAXN]; static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) spf[i] = i; for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } // The above code works well for n upto the order of 10^7. // Beyond this we will face memory issues. // Time Complexity: The precomputation for smallest prime factor is done in O(n log log n) // using sieve. // Where as in the calculation step we are dividing the number every time by // the smallest prime number till it becomes 1. // So, let’s consider a worst case in which every time the SPF is 2 . // Therefore will have log n division steps. // Hence, We can say that our Time Complexity will be O(log n) in worst case. static Vector<Integer> getFactorization(int x) { Vector<Integer> ret = new Vector<>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } ///////////////////////////////////////////////////////////////////////////////////////// public static long knapsack(int[] weight,long value[],int maxWeight){ int n= value.length ; //dp[i] stores the profit with KnapSack capacity "i" long []dp = new long[maxWeight+1]; //initially profit with 0 to W KnapSack capacity is 0 Arrays.fill(dp, 0); // iterate through all items for(int i=0; i < n; i++) //traverse dp array from right to left for(int j = maxWeight; j >= weight[i]; j--) dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]); /*above line finds out maximum of dp[j](excluding ith element value) and val[i] + dp[j-wt[i]] (including ith element value and the profit with "KnapSack capacity - ith element weight") */ return dp[maxWeight]; } /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// // to return max sum of any subarray in given array public static long kadanesAlgorithm(long[] arr) { if(arr.length == 0)return 0 ; long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long kadanesAlgorithm(int[] arr) { if(arr.length == 0)return 0 ; long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } /////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// public static long binarySerachGreater(int[] arr , int start , int end , int val) { // fing total no of elements strictly grater than val in sorted array arr if(start > end)return 0 ; //Base case int mid = (start + end)/2 ; if(arr[mid] <=val) { return binarySerachGreater(arr,mid+1, end ,val) ; } else{ return binarySerachGreater(arr,start , mid -1,val) + end-mid+1 ; } } ////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //TO GENERATE ALL(DUPLICATE ALSO EXIST) PERMUTATIONS OF A STRING // JUST CALL generatePermutation( str, start, end) start :inclusive ,end : exclusive //Function for swapping the characters at position I with character at position j public static String swapString(String a, int i, int j) { char[] b =a.toCharArray(); char ch; ch = b[i]; b[i] = b[j]; b[j] = ch; return String.valueOf(b); } //Function for generating different permutations of the string public static void generatePermutation(String str, int start, int end) { //Prints the permutations if (start == end-1) System.out.println(str); else { for (int i = start; i < end; i++) { //Swapping the string by fixing a character str = swapString(str,start,i); //Recursively calling function generatePermutation() for rest of the characters generatePermutation(str,start+1,end); //Backtracking and swapping the characters again. str = swapString(str,start,i); } } } //////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public static long factMod(long n, long mod) { if (n <= 1) return 1; long ans = 1; for (int i = 1; i <= n; i++) { ans = (ans * i) % mod; } return ans; } ///////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// public static long power(int a ,int b) { //time comp : o(logn) long x = (long)(a) ; long n = (long)(b) ; if(n==0)return 1 ; if(n==1)return x; long ans =1L ; while(n>0) { if(n % 2 ==1) { ans = ans *x ; } n = n/2L ; x = x*x ; } return ans ; } //////////////////////////////////////////////////////////////////////////////////////////////////// public static long powerMod(long x, long n, long mod) { //time comp : o(logn) if(n==0)return 1L ; if(n==1)return x; long ans = 1; while (n > 0) { if (n % 2 == 1) ans = (ans * x) % mod; x = (x * x) % mod; n /= 2; } return ans; } ////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// /* lowerBound - finds largest element equal or less than value paased upperBound - finds smallest element equal or more than value passed if not present return -1; */ public static long lowerBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static int lowerBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=k) { ans=arr[mid]; start=mid+1; } else { end=mid-1; } } return ans; } public static long upperBound(long[] arr,long k) { long ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } public static int upperBound(int[] arr,int k) { int ans=-1; int start=0; int end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]>=k) { ans=arr[mid]; end=mid-1; } else { start=mid+1; } } return ans; } ////////////////////////////////////////////////////////////////////////////////////////// public static void printArray(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printArrayln(int[] arr , int si ,int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printLArray(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } } public static void printLArrayln(long[] arr , int si , int ei) { for(int i = si ; i <= ei ; i++) { out.print(arr[i] +" ") ; } out.println() ; } public static void printtwodArray(int[][] ans) { for(int i = 0; i< ans.length ; i++) { for(int j = 0 ; j < ans[0].length ; j++)out.print(ans[i][j] +" "); out.println() ; } out.println() ; } static long modPow(long a, long x, long p) { //calculates a^x mod p in logarithmic time. long res = 1; while(x > 0) { if( x % 2 != 0) { res = (res * a) % p; } a = (a * a) % p; x /= 2; } return res; } static long modInverse(long a, long p) { //calculates the modular multiplicative of a mod p. //(assuming p is prime). return modPow(a, p-2, p); } static long modBinomial(long n, long k, long p) { // calculates C(n,k) mod p (assuming p is prime). long numerator = 1; // n * (n-1) * ... * (n-k+1) for (int i=0; i<k; i++) { numerator = (numerator * (n-i) ) % p; } long denominator = 1; // k! for (int i=1; i<=k; i++) { denominator = (denominator * i) % p; } // numerator / denominator mod p. return ( numerator* modInverse(denominator,p) ) % p; } static void update(int val , long[] bit ,int n) { for( ; val <= n ; val += (val &(-val)) ) { bit[val]++ ; } } static long query(int val , long[] bit , int n) { long ans = 0L; for( ; val >=1 ; val-=(val&(-val)) )ans += bit[val]; return ans ; } static int countSetBits(long n) { int count = 0; while (n > 0) { n = (n) & (n - 1L); count++; } return count; } ///////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// // static ArrayList<Integer>[] tree ; // static long[] child; // static int mod= 1000000007 ; // static int[][] pre = new int[3001][3001]; // static int[][] suf = new int[3001][3001] ; //program to calculate noof nodes in subtree for every vertex including itself // static void dfs(int sv) // { // child[sv] = 1L; // for(Integer x : tree[sv]) // { // if(child[x] == 0) // { // dfs(x) ; // child[sv] += child[x] ; // } // } // } static long factorial(long a) { if(a== 0L || a==1L)return 1L ; return a*factorial(a-1L) ; } ////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// public static void solve() { FastReader scn = new FastReader() ; //Scanner scn = new Scanner(System.in); //int[] store = {2 ,3, 5 , 7 ,11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 } ; // product of first 11 prime nos is greater than 10 ^ 12; //ArrayList<Integer> arr[] = new ArrayList[n] ; ArrayList<Integer> list = new ArrayList<>() ; ArrayList<Long> lista = new ArrayList<>() ; ArrayList<Long> listb = new ArrayList<>() ; // ArrayList<Integer> lista = new ArrayList<>() ; // ArrayList<Integer> listb = new ArrayList<>() ; //ArrayList<String> lists = new ArrayList<>() ; HashMap<Integer,Integer> map = new HashMap<>() ; //HashMap<Long,Long> map = new HashMap<>() ; HashMap<Integer,Integer> map1 = new HashMap<>() ; HashMap<Integer,Integer> map2 = new HashMap<>() ; //HashMap<String,Integer> maps = new HashMap<>() ; //HashMap<Integer,Boolean> mapb = new HashMap<>() ; //HashMap<Point,Integer> point = new HashMap<>() ; Set<Integer> set = new HashSet<>() ; Set<Integer> setx = new HashSet<>() ; Set<Integer> sety = new HashSet<>() ; StringBuilder sb =new StringBuilder("") ; //Collections.sort(list); //if(map.containsKey(arr[i]))map.put(arr[i] , map.get(arr[i]) +1 ) ; //else map.put(arr[i],1) ; // if(map.containsKey(temp))map.put(temp , map.get(temp) +1 ) ; // else map.put(temp,1) ; //int bit =Integer.bitCount(n); // gives total no of set bits in n; // Arrays.sort(arr, new Comparator<Pair>() { // @Override // public int compare(Pair a, Pair b) { // if (a.first != b.first) { // return a.first - b.first; // for increasing order of first // } // return a.second - b.second ; //if first is same then sort on second basis // } // }); int testcase = 1; testcase = scn.nextInt() ; for(int testcases =1 ; testcases <= testcase ;testcases++) { //if(map.containsKey(arr[i]))map.put(arr[i],map.get(arr[i])+1) ;else map.put(arr[i],1) ; //if(map.containsKey(temp))map.put(temp,map.get(temp)+1) ;else map.put(temp,1) ; // tree = new ArrayList[n] ; // child = new long[n] ; // for(int i = 0; i< n; i++) // { // tree[i] = new ArrayList<Integer>(); // } long n = scn.nextLong() ; long one = scn.nextLong() ; long zero = n -one ; if( zero <= one) { long ans = (n*(n+1)/2) - ( zero) ; out.println(ans) ; } else{ long nofgrp = one+1 ; long grpsize = zero/nofgrp ; long ans = n*(n+1)/2 ; ans = ans - nofgrp*( (grpsize*(grpsize+1))/2 ) ; ans = ans - (zero % nofgrp)*(grpsize+1); out.println(ans) ; } set.clear() ; sb.delete(0 , sb.length()) ; list.clear() ;lista.clear() ;listb.clear() ; map.clear() ; map1.clear() ; map2.clear() ; setx.clear() ;sety.clear() ; } // test case end loop out.flush() ; } // solve fn ends public static void main (String[] args) throws java.lang.Exception { solve() ; } } class Pair { int first ; int second ; @Override public String toString() { String ans = "" ; ans += this.first ; ans += " "; ans += this.second ; return ans ; } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
117ffd09661d5a1f0d0e4ee4c78c0a61
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.*; import java.util.*; public class CF1301C extends PrintWriter { CF1301C() { super(System.out); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1301C o = new CF1301C(); o.main(); o.flush(); } void main() { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int q = (n - m) / (m + 1); int r = (n - m) % (m + 1); long ans = (long) n * (n + 1) / 2; long a0 = (long) q * (q + 1) / 2; long a1 = (long) (q + 1) * (q + 2) / 2; ans -= (m + 1 - r) * a0 + r * a1; println(ans); } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
16981aaaac88eb0dc423c2219e9eb652
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.*; import java.util.*; import java.util.Map.Entry; public class gym{ static long getSum(long i) { return (i*(i+1))/2; } public static void main(String[] args) throws Exception{ MScanner sc=new MScanner(System.in); PrintWriter pw=new PrintWriter(System.out); int tc=sc.nextInt(); while(tc-->0) { long n=sc.nextLong(),m=sc.nextLong(); long zeros=n-m; long all=getSum(n); long groups=zeros/(m+1); long remove=(m+1)*getSum(groups); long remain=zeros%(m+1); remove+=remain*(groups+1); pw.println(all-remove); } pw.flush(); } static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] takearr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] takearrl(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public Integer[] takearrobj(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] takearrlobj(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
d773066eafe9fdb44b573e2e2b648e9f
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; public class Solution { /* static int n; static long max; public static void recurse(int pos,String s,int rem){ if(pos==n){ calc(s); return; } if(n-pos==rem){ recurse(pos+1,s+"1",rem-1); } else if(rem>0 && n-pos>rem){ recurse(pos+1,s+"1",rem-1); recurse(pos+1,s+"0",rem); } else recurse(pos+1,s+"0",rem); } public static void calc(String s){ long ans=0,pre=0; for(int i=0;i<n;i++){ if(s.charAt(i)=='1'){ ans=ans+(i-pre+1)*(n-i); pre=i+1; } } if(ans>max){ max=ans; //System.out.println(s); //System.out.println(ans); } }*/ public static void main(String args[]) throws Exception { FastReader in = new FastReader(System.in); StringBuilder sb = new StringBuilder(); int i, j; int t=in.nextInt(); while(t-->0){ long n=in.nextInt()+1; long m=in.nextInt()+1; long mod=n%m; long ceil,floor; if(mod>0) ceil=n/m+1; else ceil=n/m; floor=n/m; long ans=(n*n-mod*ceil*ceil-(m-mod)*(floor)*floor)/2; sb.append(ans).append("\n"); } System.out.println(sb); } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
77396af39fc56b99481e0fdcb941203e
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.util.Scanner; public class ayoubsFunction { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); StringBuilder finAns = new StringBuilder(); while (--T >= 0) { long n = sc.nextLong(), m = sc.nextLong(); long z = n - m; long g = m + 1; long k = z / g; long ans = ((n * (n + 1)) / 2) - (((k * (k + 1)) / 2) * g) - ((k + 1) * (z % g)); finAns.append(ans + "\n"); } System.out.println(finAns); sc.close(); } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
7871bcd43b363d696d568b5c2893b58b
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.*; import java.util.*; public class C { static long sum(long x) { return x * (x + 1) / 2; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int tc = sc.nextInt(); while (tc-- > 0) { int n = sc.nextInt(), m = sc.nextInt(),z=n-m,g=m+1; int k=z/g; out.println(sum(n)-sum(k)*g-(k+1)*1L*(z%g)); } out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } boolean ready() throws IOException { return br.ready(); } } static void sort(int[] a) { shuffle(a); Arrays.sort(a); } static void shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
8feefe5f9e8e2c679441ce369bc4027d
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1301c { public static void main(String[] args) throws IOException { int t = ri(); while(t --> 0) { long n = rnl(), m = nl(), r = n - m; if(m == 0) { prln(0); continue; } ++m; long ans = n * (n + 1) / 2, x = r / m; ans -= (r % m) * ((x + 1) * (x + 2) / 2) + (m - r % m) * (x * (x + 1) / 2); prln(ans); } close(); } static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __rand = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e10 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // math util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int fli(double d) {return (int)d;} static int cei(double d) {return (int)ceil(d);} static long fll(double d) {return (long)d;} static long cel(double d) {return (long)ceil(d);} static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);} static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);} static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;} static long hash(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);} // array util static void reverse(int[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(long[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(char[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static int[] copy(int[] a) {int[] ans = new int[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static long[] copy(long[] a) {long[] ans = new long[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static char[] copy(char[] a) {char[] ans = new char[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static int[] sorted(int[] a) {int[] ans = copy(a); sort(ans); return ans;} static long[] sorted(long[] a) {long[] ans = copy(a); sort(ans); return ans;} static int[] rsorted(int[] a) {int[] ans = copy(a); rsort(ans); return ans;} static long[] rsorted(long[] a) {long[] ans = copy(a); rsort(ans); return ans;} // graph util static List<List<Integer>> graph(int n) {List<List<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;} static List<List<Integer>> graph(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connect(g, rni() - 1, ni() - 1); return g;} static List<List<Integer>> graph(int n, int m) throws IOException {return graph(graph(n), m);} static List<List<Integer>> dgraph(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connecto(g, rni() - 1, ni() - 1); return g;} static List<List<Integer>> dgraph(List<List<Integer>> g, int n, int m) throws IOException {return dgraph(graph(n), m);} static List<Set<Integer>> sgraph(int n) {List<Set<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;} static List<Set<Integer>> sgraph(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connect(g, rni() - 1, ni() - 1); return g;} static List<Set<Integer>> sgraph(int n, int m) throws IOException {return sgraph(sgraph(n), m);} static List<Set<Integer>> dsgraph(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connecto(g, rni() - 1, ni() - 1); return g;} static List<Set<Integer>> dsgraph(List<Set<Integer>> g, int n, int m) throws IOException {return dsgraph(sgraph(n), m);} static void connect(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);} static void connecto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);} static void dconnect(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);} static void dconnecto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);} // input static void r() throws IOException {input = new StringTokenizer(__in.readLine());} static int ri() throws IOException {return Integer.parseInt(__in.readLine());} static long rl() throws IOException {return Long.parseLong(__in.readLine());} static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;} static int[] riam1(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()) - 1; return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;} static char[] rcha() throws IOException {return __in.readLine().toCharArray();} static String rline() throws IOException {return __in.readLine();} static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());} static int ni() {return Integer.parseInt(input.nextToken());} static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());} static long nl() {return Long.parseLong(input.nextToken());} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {__out.println("yes");} static void pry() {__out.println("Yes");} static void prY() {__out.println("YES");} static void prno() {__out.println("no");} static void prn() {__out.println("No");} static void prN() {__out.println("NO");} static void pryesno(boolean b) {__out.println(b ? "yes" : "no");}; static void pryn(boolean b) {__out.println(b ? "Yes" : "No");} static void prYN(boolean b) {__out.println(b ? "YES" : "NO");} static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next()); else __out.println();} static void h() {__out.println("hlfd");} static void flush() {__out.flush();} static void close() {__out.close();} }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
07af58e8ab1e242c236d55cdd7d2f966
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main{ static int p=1000000007; public static void main (String args[])throws IOException{ Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n = sc.nextInt(), m=sc.nextInt(); int a = (n-m)/(m+1), b=(n-m)%(m+1); //out.println(a+" "+b); long count=(long)n*(n+1)/2; count -= ((long)a*(a+1)/2*(m+1-b)); count -= ((long)(a+2)*(a+1)/2*b); out.println(count); } out.flush(); } static void print(int[] arr) { for(int i=0; i<arr.length; i++) System.out.print(arr[i]+" "); System.out.println(); } public static int abs(int x) {return ((x > 0) ? x : -x);} public static int max(int a, int b) {return Math.max(a, b);} public static int min(int a, int b) {return Math.min(a, b);} static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int modInverse(int a, int m) { int g = gcd(a, m); if (g != 1) return -1; else return power(a, m - 2, m); } // To compute x^y under modulo m static int power(int x, int y, int m) { if (y == 0) return 1; int p = power(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } static int[] primeGenerator(int num) { int length=0, arr[]=new int[num], a=num, factor=1; if(num%2==0) { while(num%2==0) { num/=2; factor*=2; } arr[length++]=factor; } for(int i=3; i*i<=a; i++) { factor=1; if(num%i==0) { while(num%i==0) { num/=i; factor*=i; } arr[length++]=factor; } } if(num>1) arr[length++]=num; return Arrays.copyOfRange(arr, 0, length); } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } boolean ready() throws IOException { return br.ready(); } } static void sort(int[] a) { shuffle(a); Arrays.sort(a); } static void shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } } } class Pair{ int x; int y; Pair(int a, int b){ x=a; y=b; } void print() { System.out.println(this.x+" "+this.y); } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
88d2f0b45ac8b8da0a0ef4e566e7d028
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Rextester{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuffer sb = new StringBuffer(); int t = Integer.parseInt(br.readLine()); while(t-->0){ StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); if(k==0){ sb.append("0\n"); continue; } long max = (long)n*(n+1); max/=2; if(n%2==0){ if(k>=n/2){ sb.append(max-n+k).append("\n"); } else{ long left = n-k; long w = left/(k+1); long z = left%(k+1); long more = z*(((w+1)*(w+2))/2) + (k+1-z)*(w*(w+1))/2; sb.append(max-more).append("\n"); } } else{ if(k>=(n-1)/2){ sb.append(max-n+k).append("\n"); } else{ long left = n-k; long w = left/(k+1); long z = left%(k+1); long more = z*(((w+1)*(w+2))/2) + (k+1-z)*(w*(w+1))/2; sb.append(max-more).append("\n"); } } } br.close(); System.out.println(sb); } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
347535625add76b55ef8fc45870a3bd9
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.util.*; import java.io.*; public class Function { public static void main(String[] args) { MyScanner s = new MyScanner(); int cases = s.nextInt(); for (int i = 0; i < cases; i++) { long n = s.nextLong(); long m = s.nextLong(); long total = n * (n+1) / 2L; long x = (n-m) / (m+1L); long y = (n-m) - (m+1L) * x; long p1 = (m - y + 1) * (x + 1) * x / 2L; long p2 = y * (x + 2) * (x + 1) / 2L; System.out.println(total - (p1 + p2)); } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
7c3ac2f8100c7703d80117e1965f5392
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.*; import java.util.*; public class Main implements Runnable{ private void solve()throws IOException{ int n=nextInt(); int m=nextInt(); int x=(n-m)/(m+1); int y=(n-m)%(m+1); long ans=1l*n*(n+1)/2; ans-=1l*y*(x+1)*(x+2)/2; ans-=1l*(m-y+1)*x*(x+1)/2; out.println(ans); } /////////////////////////////////////////////////////////// final long mod=(long)(1e9+7); final int inf=(int)(1e9+1); final int maxn=(int)(1e6); final long lim=(long)(1e18); public void run(){ try{ br=new BufferedReader(new InputStreamReader(System.in)); st=null; out=new PrintWriter(System.out); // solve(); int t=nextInt(); for(int i=1;i<=t;i++){ // out.print("Case #"+i+": "); solve(); } br.close(); out.close(); }catch(Exception e){ e.printStackTrace(); System.exit(1); } } public static void main(String args[])throws IOException{ new Main().run(); } int max(int ... a){ int ret=a[0]; for(int i=1;i<a.length;i++) ret=Math.max(ret,a[i]); return ret; } int min(int ... a){ int ret=a[0]; for(int i=1;i<a.length;i++) ret=Math.min(ret,a[i]); return ret; } void debug(Object ... a){ System.out.print("> "); for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } void debug(int a[]){debuga(Arrays.stream(a).boxed().toArray());} void debug(long a[]){debuga(Arrays.stream(a).boxed().toArray());} void debuga(Object a[]){ System.out.print("> "); for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } BufferedReader br; StringTokenizer st; PrintWriter out; String nextToken()throws IOException{ while(st==null || !st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine()throws IOException{ return br.readLine(); } int nextInt()throws IOException{ return Integer.parseInt(nextToken()); } long nextLong()throws IOException{ return Long.parseLong(nextToken()); } double nextDouble()throws IOException{ return Double.parseDouble(nextToken()); } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
d70e816913e625e0b914ff4c239ab42b
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.*; import java.util.*; public class C { void solve(FastIO io) { long n = io.nextInt(); long m = io.nextInt(); long zero = n - m; long len = zero / (m+1); long left = zero % (m+1); long ans = left * ((((len + 2) * (len + 1)) / 2)); ans += (m+1 - left) * (((len * (len+1)) / 2)); long total = (n * (n+1)) / 2; io.println(total - ans); } public static void main(String[] args) { FastIO io = new FastIO(); int t = io.nextInt(); for (int i = 0; i < t; i++) new C().solve(io); io.close(); } static class FastIO extends PrintWriter { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); FastIO() { super(System.out); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(r.readLine()); } catch (Exception e) { //TODO: handle exception } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
55044a8a187cf5cc8f4820ab0ea33e62
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.util.*; import java.io.*; public class C619 { public static void main(String [] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t > 0) { long n = sc.nextInt(); long m = sc.nextInt(); long total = ((n + 1) * n) / 2; long size = (n-m) / (m + 1); long rem = (n-m)%(m+1); long small = m + 1 - rem; long large = rem; long bad = small * (size + 1) * size / 2 + large * (size + 2) * (size + 1) / 2; out.println(total - bad); t--; } out.close(); } //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
4f85920ef63ec693339c01b234a86584
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.util.*; import java.io.*; public class AyoubFunction_div2_619 { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); StringTokenizer st=new StringTokenizer(br.readLine()); int t=Integer.parseInt(st.nextToken()); while(t-->0) { st=new StringTokenizer(br.readLine()); long n=Integer.parseInt(st.nextToken()); long m=Integer.parseInt(st.nextToken()); long g=m+1; long z=n-m; long pkka=z/g; long addon=z%g; long val=((n*(n+1))/2)-(((pkka*(pkka+1))/2)*g)-((pkka+1)*(addon)); out.println(val); } out.close(); } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
d2c991da2ae6005125faac9e9f5252eb
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class A { final static FastReader scan = new FastReader(); public static void main(String[] args) { int t = scan.nextInt(); while (t-->0){ long n = scan.nextLong(); long m = scan.nextLong(); long ans = (long) n * (n+1)/2; long g = m+1; long z = m-n; n-=m; m++; long t1 = n/m,t2=n%m; long len = n / m; long large = n % m; long small = m - large; ans -= large * (len + 1) * (len + 2) / 2; ans -= small * (len + 1) * (len) / 2; System.out.println(ans); } } static int[] IntArrayIp(int n){ int[] a = new int[n]; for (int i=0;i<n;i++) { a[i] = scan.nextInt(); } return a; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
ee52db3822d8d24e46d4c7d7a8ef2247
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Jenish */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CAyoubsFunction solver = new CAyoubsFunction(); solver.solve(1, in, out); out.close(); } static class CAyoubsFunction { long find(long n, long m) { if (n == 0) return 0; if (m >= n) return (n * (n + 1)) / 2; long ans = 0; if (n % 2 == 1) { ans = Math.max(ans, Math.max(find(n / 2, m - 1), find(n / 2, m - 1)) + ((n / 2) + 1) * ((n / 2) + 1)); long r = (m - 1); ans = Math.max(ans, find(n / 2, r / 2) + find(n / 2, r / 2 + r % 2) + ((n / 2) + 1) * ((n / 2) + 1)); } else { long r = (m - 1); long a = n / 2; long b = a - 1; ans = Math.max(ans, Math.max(find(a, m - 1), find(b, m - 1)) + ((n / 2)) * ((n / 2))); ans = Math.max(ans, (find(a, r / 2) + find(b, r / 2 + r % 2)) + ((n / 2)) * ((n / 2))); ans = Math.max(ans, (find(a, r / 2 + r % 2) + find(b, r / 2)) + ((n / 2)) * ((n / 2))); } return ans; } public void solve(int testNumber, ScanReader in, PrintWriter out) { int t = in.scanInt(); while (t-- > 0) { find(1, 1); long n = in.scanInt() + 1; long m = in.scanInt() + 1; out.println(OEISformula(n, m)); } } long OEISformula(long n, long k) { long c = ceil(n, k); long f = floor(n, k); return (long) ((n * n) - ((n % k) * c * c) - ((k - (n % k)) * f * f)) / 2; } long ceil(long a, long b) { if (a % b == 0) return a / b; else return a / b + 1; } long floor(long a, long b) { if (a % b == 0) return a / b; else return a / b; } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
d93c7d86861402d7a4d0d69585230509
train_002.jsonl
1581604500
Ayoub thinks that he is a very smart person, so he created a function $$$f(s)$$$, where $$$s$$$ is a binary string (a string which contains only symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of substrings in the string $$$s$$$ that contains at least one symbol, that is equal to "1".More formally, $$$f(s)$$$ is equal to the number of pairs of integers $$$(l, r)$$$, such that $$$1 \leq l \leq r \leq |s|$$$ (where $$$|s|$$$ is equal to the length of string $$$s$$$), such that at least one of the symbols $$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1". For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$.Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $$$n$$$ and $$$m$$$ and asked him this problem. For all binary strings $$$s$$$ of length $$$n$$$ which contains exactly $$$m$$$ symbols equal to "1", find the maximum value of $$$f(s)$$$.Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
256 megabytes
import java.util.*; import java.math.*; import java.io.*; import java.text.*; public class practice { // heloo world nudniobv udivbo // buyfhsfnoisdfnoi public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l + r) / 2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static int n; public static int m; public static int dis; public static ArrayList<Integer> a = new ArrayList<>(); public static void hold_round() { for (int i = 0; i < n; i++) { int x = a.get(i); if (--(x) == 0) dis--; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long t = Long.parseLong(br.readLine()); for (int xa = 0; xa < t; xa++) { StringTokenizer st = new StringTokenizer(br.readLine()); long n = Long.parseLong(st.nextToken()); long m = Long.parseLong(st.nextToken()); long ans = (long)n * (long)(n + 1) / 2l; long z = n - m; long k = z / (m + 1); ans -= (long)(m +1) * (long)k * (long)(k + 1) / 2l; ans -= (long)(z % (m + 1)) * (long)(k + 1); System.out.println(ans); } } }
Java
["5\n3 1\n3 2\n3 3\n4 0\n5 2"]
1 second
["4\n5\n6\n0\n12"]
NoteIn the first test case, there exists only $$$3$$$ strings of length $$$3$$$, which has exactly $$$1$$$ symbol, equal to "1". These strings are: $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so the maximum value is $$$4$$$ and the answer is $$$4$$$.In the second test case, the string $$$s$$$ with the maximum value is "101".In the third test case, the string $$$s$$$ with the maximum value is "111".In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.In the fifth test case, the string $$$s$$$ with the maximum value is "01010" and it is described as an example in the problem statement.
Java 11
standard input
[ "greedy", "combinatorics", "math", "binary search", "strings" ]
7458f44802c134de6fed7b4de84ea68c
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The only line for each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 10^{9}$$$, $$$0 \leq m \leq n$$$) — the length of the string and the number of symbols equal to "1" in it.
1,700
For every test case print one integer number — the maximum value of $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly $$$m$$$ symbols, equal to "1".
standard output
PASSED
7872e1bb705ac23532af84fccffd154f
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.Scanner; public class Kino1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int coin1 = 0, coin2 = 0; for (int i = 0; i < n; i++) { int correct = scanner.nextInt(); if (correct == 25) coin1++; else if (correct == 50) { if (coin1 > 0) { coin1--; coin2++; } else { System.out.println("NO"); return; } } else if (correct == 100) { if (coin1 > 0 && coin2 > 0) { coin1--; coin2--; } else if (coin1 >= 3) { coin1 -= 3; } else { System.out.println("NO"); return; } } } System.out.println("YES"); scanner.close(); } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
ebb930a59b9da56a3d9f123a33d80301
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.Scanner; public class elzhan { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int b25 = 0, b50 = 0; for (int i = 0; i < n; i++) { int next = sc.nextInt(); if (next == 25) b25++; else if (next == 50) { if (b25 > 0) { b25--; b50++; } else { System.out.println("NO"); return; } } else if (next == 100) { if (b25 > 0 && b50 > 0) { b25--; b50--; } else if (b25 >= 3) { b25 -= 3; } else { System.out.println("NO"); return; } } } System.out.println("YES"); sc.close(); } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
59260f65a8d4dd214f67c860bfe3dbd8
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.Scanner; public class Kino { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int coin1 = 0, coin2 = 0; for (int i = 0; i < n; i++) { int correct = scanner.nextInt(); if (correct == 25) coin1++; else if (correct == 50) { if (coin1 > 0) { coin1--; coin2++; } else { System.out.println("NO"); return; } } else if (correct == 100) { if (coin1 > 0 && coin2 > 0) { coin1--; coin2--; } else if (coin1 >= 3) { coin1 -= 3; } else { System.out.println("NO"); return; } } } System.out.println("YES"); scanner.close(); } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
1329967ebd58e07466c2cc2ca129a3e7
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.*; import java.io.PrintWriter; public class A349 implements Runnable { private Scanner in = new Scanner(System.in); private PrintWriter out = new PrintWriter(System.out); private int n; private int a[]; private String ans = "NO"; public static void main(String[] args) { new Thread(new A349()).start(); } private void read() { n = in.nextInt(); a = new int[n]; int fee25 = 0; int fee50 = 0; int fee100 = 0; boolean bool = true; for(int i = 0; i < n; i++) { a[i] = in.nextInt(); if(bool) { if(a[i] == 25) { fee25+=25; } else if(a[i] == 50) { fee50 += 50; if(fee25 - 25 < 0) { bool = false; } else { fee25 -= 25; } } else if(a[i] == 100){ fee100 += 100; if(fee50 - 50 >= 0) { if(fee25 - 25 < 0){ bool = false; } else { fee50 -= 50; fee25 -= 25; } } else if(fee25 >= 75) { fee25 -= 75; } else { bool = false; } } } } if(bool){ ans = "YES"; } } private void solve() { } private void write() { out.println(ans); } public void run() { read(); solve(); write(); out.close(); } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
ba37ff1ae72ed96dcb1068b751217d0d
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.Scanner; /** * * @author Temirlan */ public class t31 { public static void main(String[] args){ Scanner sc=new Scanner(System.in); String gl=sc.nextLine(); String vt=sc.nextLine(); String[] vt1=vt.split(" "); int o1=Integer.parseInt(gl); int[] o2=new int[o1]; int k=0; int k1=0; int k2=0; int y=0; for (int i = 0; i < o1; i++) { o2[i]=Integer.parseInt(vt1[i]); } for (int i = 0; i < o1; i++) { if(o2[i]==25){ k+=1; } else if(o2[i]==50){ if(k==0){ System.out.println("NO"); y+=1; break; }else{ k-=1; k1+=1; } } else if(o2[i]==100){ if(k==0){ System.out.println("NO"); y+=1; break; } else if(k<3){ if(k1==0){ System.out.println("NO"); y+=1; break; }else{ k2+=1; k1-=1; k-=1; } } else if(k>=3){ if(k1>=1){ k1-=1; k-=1; k2+=1; }else if(k1==0){ k-=3; k2+=1; } } } } if(y==0){ System.out.println("YES"); } } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
e3dbf01c31bc1a6838945574c5a8fc4a
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * * @author Muhammad Bahaa */ public class ACinemaLine { public static void main(String[] args) { int n, temp; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); Scanner in = new Scanner(System.in); map.put(25, 0); map.put(50, 0); n = in.nextInt(); // if (n == 40012 || n == 68656 || n == 80000) { // System.out.println("YES"); // return; // } for (int i = 0; i < n; i++) { temp = in.nextInt(); if (temp == 25) { map.put(25, map.get(25) + 1); } else if (temp == 50) { if (map.get(25) > 0) { map.put(25, map.get(25) - 1); map.put(50, map.get(50) + 1); } else { System.out.println("NO"); return; } } else if (temp == 100) { if (map.get(25) > 0 && map.get(50) > 0) { map.put(25, map.get(25) - 1); map.put(50, map.get(50) - 1); } else if (map.get(25) >= 3) { map.put(25, map.get(25) - 3); } else { System.out.println("NO"); return; } } } System.out.println("YES"); } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
22d97107aa44421395c14d4ab8c3473c
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.Scanner; /** * * @author Dell */ public class NewMain24 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner s = new Scanner(System.in); int x = s.nextInt(); int count25 = 0, count50 = 0, count100 = 0; int cash = 0; boolean done = true; for (int i = 0; i < x; i++) { int tic = s.nextInt(); if (tic == 25) { count25++; } else if (tic == 50) { if (count25 >= 1) { count50++; count25--; } else { done = false; break; } } else if (tic == 100) { if (count50 >= 1 && count25 >= 1) { count50--; count25 --; count100++; } else if (count25 >= 3) { count25 -= 3; count100++; } else { done = false; break; } } } if(done){ System.out.println("YES"); }else{ System.out.println("NO"); } } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
7f8bd4fda56c0b151363858305fa10ab
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); long n = in.nextInt(); long i,current; long b25=0,b50=0,b100=0; boolean flag=true; for(i=0;i<n;i++) { current = in.nextInt(); if(current==25) b25++; else if(current==50) { if(b25 <1) { flag=false; break; } else { b25--; b50++; } } else if(current==100) { if(b25 >=1 && b50>=1) { b25--; b50--; b100++; } else if(b25>=3) { b25=b25-3; b100++; } else { flag=false; break; } } } if(flag==true) System.out.println("YES"); else System.out.println("NO"); } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
f82bec53a3b9ddf92cccd03f921e2ab2
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.*; import java.util.Scanner; public class JavaApplication3 { public static void main(String[] args){ Scanner in = new Scanner(System.in); int numberofpeople = in.nextInt(); int dvacatpyat = 0; int pyadesyat = 0; int count = 1; for (int i=0;i<numberofpeople;i++){ int t = in.nextInt(); if(t==25){ dvacatpyat = dvacatpyat +1; } else if(t==50){ if(dvacatpyat==0){ System.out.print("NO"); count=0; break; } else{ dvacatpyat = dvacatpyat-1; pyadesyat = pyadesyat +1; } } else{ if(dvacatpyat>0 && pyadesyat>0){ dvacatpyat = dvacatpyat -1; pyadesyat = pyadesyat -1; } else if(dvacatpyat>2){ dvacatpyat = dvacatpyat -3; } else{ System.out.println("NO"); count=0; break; } } } if (count==1){ System.out.println("YES"); } } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
e6a20e3578dd94976c2d45d134b152fa
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.io.*; import java.math.BigInteger; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { static final int INF = Integer.MAX_VALUE; static void mergeSort(int[] a, int p, int r) { if (p < r) { int q = (p + r) / 2; mergeSort(a, p, q); mergeSort(a, q + 1, r); merge(a, p, q, r); } } static void merge(int[] a, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; int[] L = new int[n1 + 1], R = new int[n2 + 1]; // int[] L1 = new int[n1 + 1], R1 = new int[n2 + 1]; for (int i = 0; i < n1; i++) { L[i] = a[p + i]; // L1[i] = b[p + i]; } for (int i = 0; i < n2; i++) { R[i] = a[q + 1 + i]; // R1[i] = b[q + 1 + i]; } L[n1] = R[n2] = INF; // L1[n1] = R1[n2] = INF; for (int k = p, i = 0, j = 0; k <= r; k++) { if (L[i] <= R[j]) { a[k] = L[i++]; // b[k] = L1[i++]; } else { a[k] = R[j++]; // b[k] = R1[j++]; } } } static void mergeSort(int[] a, int[] b, int[] c, int p, int r) { if (p < r) { int q = (p + r) / 2; mergeSort(a, b, c, p, q); mergeSort(a, b, c, q + 1, r); merge(a, b, c, p, q, r); } } static void merge(int[] a, int[] b, int[] c, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; int[] L = new int[n1 + 1], R = new int[n2 + 1]; int[] L1 = new int[n1 + 1], R1 = new int[n2 + 1]; int[] L2 = new int[n1 + 1], R2 = new int[n2 + 1]; // int[] L2 = new int[n1 + 1], R2 = new int[n2 + 1]; for (int i = 0; i < n1; i++) { L[i] = a[p + i]; L1[i] = b[p + i]; L2[i] = c[p + i]; // L2[i] = a[p + i]; } for (int i = 0; i < n2; i++) { R[i] = a[q + 1 + i]; R1[i] = b[q + 1 + i]; R2[i] = c[q + 1 + i]; // R2[i] = a[q + 1 + i]; } L[n1] = R[n2] = INF; L1[n1] = R1[n2] = INF; L2[n1] = R2[n2] = INF; int j = 0, k = 0; for (int i = p; i <= r; i++) { if (L[j] < R[k]) { a[i] = L[j]; b[i] = L1[j]; c[i] = L2[j++]; //j++; } else if ((L[j] > R[k])) { a[i] = R[k]; b[i] = R1[k]; c[i] = R2[k++]; //k++; } else { //des if (L1[j] < R1[k]) { a[i] = L[j]; b[i] = L1[j]; c[i] = L2[j++]; //j++; } else { a[i] = R[k]; b[i] = R1[k]; c[i] = R2[k++]; } } } } public static int[] sieve(int n) { int a[] = new int[n + 1]; for (int i = 2; i <= n; i++) { a[i] = 1; } for (int i = 2; i <= Math.sqrt(n); i++) { if (a[i] == 1) { for (int k = 2; i * k <= n; k++) { a[i * k] = 0; } } } return a; } public static int sum(int a) { int su = 0; while (a > 0) { su += a % 10; a /= 10; } return su; } public static boolean prime(int n) { if (n == 1) { return true; } for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } public static boolean isvowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y') { return true; } else { return false; } } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static List<Integer> gd(int a, int b) { int m = (int) gcd(a, b); List<Integer> q = new ArrayList(); for (int i = 1; i * i <= m; ++i) { if (m % i == 0) { q.add(i); if (i != m / i) { q.add(m / i); } } } return q; } static long sum(long n) { return n * (n + 1) / 2; } static int divisors(int n, int m) { if (n - (m + (m * m)) >= 0) { return m; } for (int i = (int) Math.sqrt(m); i >= 1; i--) { if (m % i == 0 && n - (m + (m * i)) >= 0) { return i; } } return 1; } static boolean cal(int val, int k, int t) { int res = val, p = 1; while (val / Math.pow(k, p) > 0) { res += val / Math.pow(k, p); p++; } return res >= t; } static int Last_Ocuu(int[] a, int n, long v) { int start = 0, end = n - 1, mid, ans = -1; while (start <= end) { mid = (start + end) >> 1; if (a[mid] < v) { ans = mid; end = mid - 1; } else if (a[mid] >= v) { //ans=mid; start = mid + 1; } } return ans; } static int Last_Ocuu(List<Long> a, int n, int i, long v) { int sta = 0, end = n - 1, ans = -1; while (sta <= end) { int mid = (sta + end) >> 1; if (a.get(mid) + a.get(i) >= v && mid != i) { ans = mid; end = mid - 1; } else { sta = mid + 1; } } return ans; } static long summtion(int[] a) { long s = 0; for (int i = 0; i < a.length; i++) { s += (long) a[i]; } return s; } static boolean vv(int i, int j, int n, int m) { if (i >= 0 && i < n && j < m && j >= 0) { return true; } return false; } public static void main(String[] args) throws IOException, ParseException { Scannerr in = new Scannerr(System.in); PrintWriter or = new PrintWriter(System.out); // BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); int n=in.nextInt(); int []a= new int[n]; for (int i = 0; i < n; i++) { a[i]=in.nextInt(); //a[i]=25; } int fi=0; int d25=0; for (int i = 0; i < n; i++) { int y=a[i]; if (y==25) { d25++; } else if(y==50){ if (d25>=1) { d25--; fi++; } else { System.out.println("NO");return; } } else { if (fi>=1&&d25>=1) { d25--;fi--; } else if (fi<1&&d25>=3) { d25-=3; } else { System.out.println("NO");return; } } } System.out.println("YES"); or.flush(); } static class Scannerr { StringTokenizer st; BufferedReader br; public Scannerr(FileReader fileReader) throws FileNotFoundException { br = new BufferedReader(fileReader); } public Scannerr(InputStream s) throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) { if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) { f *= 10; } } } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public boolean ready() throws IOException { return br.ready(); } } } class Pair { int ind; int val; //int min; public Pair(int n, int p) { ind = n; val = p; // min = m; } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
f4b1e15b162e131cdd986d7737d0358d
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.Scanner; public class test { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int x; int c25=0; int c50=0; boolean s=true; while(n--!=0){ x=in.nextInt(); if(s){ if(x==25) c25++; else if(x==50){ c25--; c50++; } else { if(c50>0){ c50--; c25--; } else c25-=3; } if(c25<0)s=false; } } if(s==true)System.out.println("YES"); else System.out.println("NO"); } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
f18cd887cf22d345887da39164813a95
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.*; public class Cin { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; int f=0; int tf=0,fi=0; for(int i=0;i<n;i++) a[i]=sc.nextInt(); if(a[0]==50 || a[0]==100) System.out.println("NO"); else{ for(int i=0;i<n;i++) { if(a[i]==25) tf++; else if(a[i]==50) { if(tf<1) { f=1; break; } else { tf--; fi++; } } else if(a[i]==100) { if(tf>0 && fi>0) { tf--; fi--; } else if(tf>=3) tf=tf-3; else { f=1; break; } } } if(f==0) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
a4aed7dbbbfc82c177997ddbea2401e9
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.Arrays; /** * Test */ public class Test { public static void main(String[] args) throws IOException { int change = 0,n,bill,b25=0,b50=0; boolean possible = true; Scanner in = new Scanner(System.in); n = in.nextInt(); for( int i=0 ; i<n ; i++ ){ bill = in.nextInt(); if(possible){ if(bill == 100){ if(b50>0){ b50--; b25--; }else{ b25-=3; } } else if(bill == 50){ b50++; b25--; } else if(bill == 25) b25++; if(b25<0) possible = false; } } if(possible) System.out.println("YES"); else System.out.println("NO"); } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
044bc90a1382458b1f1946eaaa2146c8
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.*; public class Cinema { public static void main(String[]flkbjlb) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int arr[]=new int[n]; int count1=0,count2=0,count3=0; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); if(arr[i]==25) { count1++; } if(arr[i]==50) { count2++; if(count1>0) { count1--; } else { System.out.println("NO"); return; } } if(arr[i]==100) { count3++; if(count1>0&&count2>0) { count1--; count2--; } else if(count1>2) { count1-=3; } else { System.out.println("NO"); return; } } } System.out.println("YES"); } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
9369252e39bfcbea3d5ea0e95015de60
train_002.jsonl
1380295800
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
256 megabytes
import java.util.Scanner; public class CInemaLine { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCase = sc.nextInt(); int countTwentyFive = 0; int countFifty = 0; boolean flag = true; for(int i = 0 ; i < testCase ; i++){ int num = sc.nextInt(); if(i == 0 && num > 25){ flag = false; break; }else if(num == 25){ countTwentyFive++; }else if(num == 50 && countTwentyFive > 0){ countTwentyFive--; countFifty++; }else if(num == 100 && countFifty > 0 && countTwentyFive > 0){ countFifty--; countTwentyFive--; }else if(num == 100 && countTwentyFive > 2){ countTwentyFive -= 3; }else{ flag = false; break; } } if(flag) System.out.println("YES"); else System.out.println("NO"); } }
Java
["4\n25 25 50 50", "2\n25 100", "4\n50 50 25 25"]
2 seconds
["YES", "NO", "NO"]
null
Java 8
standard input
[ "implementation", "greedy" ]
63b20ab2993fddf2cc469c4c4e8027df
The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
1,100
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
standard output
PASSED
425bc16eaa98260a63b9b158ab42ae0c
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; public class Solution { private static Scanner sc = new Scanner(System.in); private static int[] a; private static char[] s; private static int n; private static void readInt() { a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } } private static void readString() { s = sc.next().toCharArray(); } public static void main(String[] args) { n = sc.nextInt(); int k = sc.nextInt(); int[] map = new int[k]; readString(); for (char c : s) { map[c - 'A']++; } int min = n; for (int value : map) min = Math.min(min, value); System.out.println(min * k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
20b2c508398c85450933141387b6d47b
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; public class Equality { public static void main(String [] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(), k = input.nextInt(); String str = input.next(); Set<Character> set = new HashSet<>(); for (int i = 0; i < str.length(); i++) set.add(str.charAt(i)); Object[] arr = set.toArray(); Map<Object, Integer> map = new HashMap<>(); for (Object o : arr) map.put(o, 0); for( int i = 0; i < str.length(); i++ ) if( map.containsKey( str.charAt(i) ) ) map.merge( str.charAt(i), 1, Integer::sum ); if( map.size() < k ) System.out.println(0); else { int min = Collections.min( map.values() ); System.out.println(min * k); } } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
82ea07e850ca86574e12599236e9e0ab
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; public class equality { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=in.nextInt(); int k=in.nextInt(); String s=in.next(); Set<Character> set = new HashSet<>(); for(int i=0;i<n;i++) { set.add(s.charAt(i)); } if(set.size()!=k) { System.out.println("0"); System.exit(0); } char arr[]=s.toCharArray(); Arrays.parallelSort(arr); int max_count = Integer.MAX_VALUE,curr_count = 1; for (int i = 1; i < n; i++) { if (arr[i] == arr[i - 1]) curr_count++; else { if (curr_count < max_count) { max_count = curr_count; } curr_count = 1; } } if (curr_count < max_count) { max_count = curr_count; } System.out.println(max_count*k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
434c1c08c008cd902ed1ba6228c9e417
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.Scanner; public class Equality_1038A { public static void main(String[] args) { // System.out.println("Hello world"); int[] alpabate = new int[26]; int n, k; String s; Scanner input = new Scanner(System.in); n = input.nextInt(); k = input.nextInt(); input.nextLine(); s = input.nextLine(); for (int i = 0; i < n; i++) { int l = s.charAt(i) - 'A'; alpabate[l]++; // System.out.println(alpabate[l]); } int m = n; for (int i = 0; i < k; i++) { m = Math.min(m, alpabate[i]); // System.out.println(m + " " + alpabate[i]); } System.out.println(m * k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
9a5098190bd7f5349a51c130cebe5f6b
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
//package hello; import java.util.*; public class capitalization { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int l,cnt; l = scan.nextInt(); cnt = scan.nextInt(); String pattern =""; while(pattern.length()==0) pattern = scan.nextLine(); int[] frequency = new int[cnt]; int mi = Integer.MAX_VALUE; for(int i=0;i<pattern.length();i++) { frequency[pattern.charAt(i)-'A']++; } for(int i=0;i<cnt;i++)mi = Math.min(mi, frequency[i]); System.out.println(mi*cnt); scan.close(); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
cbe38201e6e5bfb6a448a1ff727f3925
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; import java.math.*; public class Submit { public static void main(String[] args) { Scanner kb=new Scanner(System.in); String line=kb.nextLine(); String[] sp=line.split(" "); int n=Integer.parseInt(sp[0]); int k=Integer.parseInt(sp[1]); String s=kb.nextLine(); int[] letterAppearances=new int[k]; for (int i=0;i<n;i++) { letterAppearances[(int)s.charAt(i)-65]++; } Arrays.sort(letterAppearances); System.out.println(letterAppearances[0]*k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
709882efcfdf9d66b7150fc7f033aeba
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); int[] x=new int[b]; String s=sc.next(); for(int i=0;i<a;i++) { for(int j=0;j<b;j++) { if(s.charAt(i)==j+'A') x[j]++; } } Arrays.sort(x); System.out.print(x[0]*b); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
0a3eefdfcae35775e28b5f89734ddfce
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; // SHIVAM GUPTA : //NSIT //decoder_1671 //BEING PERFECTIONIST IS NOT AN OPTION // ASCII = 48 + i ; // SHIVAM GUPTA : /* Name of the class has to be "Main" only if the class is public. */ public class Main { static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out); } String next(){ while(st==null || !st.hasMoreElements()){ try{ st= new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str = ""; try{ str=br.readLine(); } catch(IOException e){ e.printStackTrace(); } return str; } } ///////////////////////////////////////////////////////////////////////////////////// public static int min(int a ,int b , int c, int d) { int[] arr = new int[4] ; arr[0] = a; arr[1] = b ; arr[2] = c; arr[3] = d; Arrays.sort(arr) ; return arr[0]; } ///////////////////////////////////////////////////////////////////////////// public static int max(int a ,int b , int c, int d) { int[] arr = new int[4] ; arr[0] = a; arr[1] = b ; arr[2] = c; arr[3] = d; Arrays.sort(arr) ; return arr[3]; } /////////////////////////////////////////////////////////////////////////////////// static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; static int[] dp = new int[10000001] ; public static void sieveOfEratosthenes() { // FALSE == prime // TRUE == COMPOSITE // FALSE== 1 for(int i=0;i< sieve + 1;i++) prime[i] = false; for(int p = 2; p*p <= sieve; p++) { if(prime[p] == false) { for(int i = p*p; i <= sieve; i += p) prime[i] = true; } } } /////////////////////////////////////////////////////////////////////////////////// public static String reverse(String input) { String op = "" ; for(int i = 0; i < input.length() ; i++ ) { op = input.charAt(i)+ op ; } return op ; } /////////////////////////////////////////////////////////////////////////////////////// public static void sortD(int[] arr) { sort(arr ,0 , arr.length-1) ; int i =0 ; int j = arr.length -1 ; while( i < j) { int temp = arr[i] ; arr[i] =arr[j] ; arr[j] = temp ; i++ ; j-- ; } return ; } /////////////////////////////////////////////////////////////////////////////////////// public static boolean sameParity(long a ,long b ) { if(a%2 == b %2) { return true ; } else{ return false ; } } /////////////////////////////////////////////////////////////////////////////////////// public static boolean sameParity(int a ,int b ) { if(a%2 == b %2) { return true ; } else{ return false ; } } //////////////////////////////////////////////////////////////////////////////////// public static boolean isPossibleTriangle(int a ,int b , int c) { if( a + b > c && c+b > a && a +c > b) { return true ; } return false ; } ///////////////////////////////////////////////////////////////////// public static int gcd(int a, int b ) { if(b==0)return a ; else return gcd(b,a%b) ; } //////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c , int d ) { int temp = lcm(a,b , c) ; int ans = lcm(temp ,d ) ; return ans ; } ////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a, int b ,int c ) { int temp = lcm(a,b) ; int ans = lcm(temp ,c) ; return ans ; } //////////////////////////////////////////////////////////////////////////////////////// public static int lcm(int a , int b ) { int gc = gcd(a,b); return (a*b)/gc ; } ///////////////////////////////////////////////////////////////////////////////////////////// static boolean isPrime(int n) { if(n==1) { return false ; } boolean ans = true ; for(int i = 2; i*i <= n ;i++) { if(n% i ==0) { ans = false ;break ; } } return ans ; } ////////////////////////////////////////////////////////////////////////////////////////// public static long nCr(int n,int k) { long ans=1; k=k>n-k?n-k:k; int j=1; for(;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } /////////////////////////////////////////////////////////////////////////////////////////// public static ArrayList<Integer> allFactors(int n) { ArrayList<Integer> list = new ArrayList<>() ; for(int i = 1; i*i <= n ;i++) { if( n % i == 0) { if(i*i == n) { list.add(i) ; } else{ list.add(i) ; list.add(n/i) ; } } } return list ; } ///////////////////////////////////////////////////////////////////////////// public static boolean isPowerOfTwo(int n) { if(n==0) return false; if(((n ) & (n-1)) == 0 ) return true ; else return false ; } //////////////////////////////////////////////////////////////////////////////////// public static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } ///////////////////////////////////////////////////////////////////////////////////////// static final int MAXN = 100001; static int spf[] = new int[MAXN]; static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) spf[i] = i; for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } // The above code works well for n upto the order of 10^7. // Beyond this we will face memory issues. // Time Complexity: The precomputation for smallest prime factor is done in O(n log log n) // using sieve. // Where as in the calculation step we are dividing the number every time by // the smallest prime number till it becomes 1. // So, let’s consider a worst case in which every time the SPF is 2 . // Therefore will have log n division steps. // Hence, We can say that our Time Complexity will be O(log n) in worst case. static Vector<Integer> getFactorization(int x) { Vector<Integer> ret = new Vector<>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } ///////////////////////////////////////////////////////////////////////////////////////// public static int knapsack(int[] weight,int value[],int maxWeight){ int n= value.length ; //dp[i] stores the profit with KnapSack capacity "i" int []dp = new int[maxWeight+1]; //initially profit with 0 to W KnapSack capacity is 0 Arrays.fill(dp, 0); // iterate through all items for(int i=0; i < n; i++) //traverse dp array from right to left for(int j = maxWeight; j >= weight[i]; j--) dp[j] = Math.max(dp[j] , value[i] + dp[j - weight[i]]); /*above line finds out maximum of dp[j](excluding ith element value) and val[i] + dp[j-wt[i]] (including ith element value and the profit with "KnapSack capacity - ith element weight") */ return dp[maxWeight]; } /////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// // to return max sum of any subarray in given array public static long kadanesAlgorithm(long[] arr) { long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } ///////////////////////////////////////////////////////////////////////////////////////////// public static long kadanesAlgorithm(int[] arr) { long[] dp = new long[arr.length] ; dp[0] = arr[0] ; long max = dp[0] ; for(int i = 1; i < arr.length ; i++) { if(dp[i-1] > 0) { dp[i] = dp[i-1] + arr[i] ; } else{ dp[i] = arr[i] ; } if(dp[i] > max)max = dp[i] ; } return max ; } /////////////////////////////////////////////////////////////////////////////////////// // Arrays.sort(arr, new Comparator<Pair>() { // @Override // public int compare(Pair first, Pa second) { // if (first.getAge() != second.getAge()) { // return first.getAge() - second.getAge(); // } // return first.getName().compareTo(second.getName()); // } // }); ///////////////////////////////////////////////////////////////////////////////////////// public static long binarySerachGreater(int[] arr , int start , int end , int val) { // fing total no of elements strictly grater than val in sorted array arr if(start > end)return 0 ; //Base case int mid = (start + end)/2 ; if(arr[mid] <=val) { return binarySerachGreater(arr,mid+1, end ,val) ; } else{ return binarySerachGreater(arr,start , mid -1,val) + end-mid+1 ; } } public static void main (String[] args) throws java.lang.Exception { FastReader scn = new FastReader() ; int n = scn.nextInt() ;int k = scn.nextInt() ; String str = scn.next() ; int[] arr = new int[k] ; for(int i = 0 ; i < str.length() ; i++ ) { char ch = str.charAt(i) ; int a = ch-65 ; arr[a]++ ; } int min = arr[0] ; for(int i = 1; i < k; i++) { if(arr[i] < min)min = arr[i] ; } out.println(min*k) ; out.flush() ; } } class Pair { int first ; int second ; @Override public String toString() { String ans = "" ; ans += this.first ; ans += " "; ans += this.second ; return ans ; } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
399e728855c5d5c2531ce7b6ac270347
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import static java.lang.Integer.min; public class Helloworld { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String test=br.readLine(); int n= Integer.parseInt(test.substring(0,test.indexOf(' '))); int p= Integer.parseInt(test.substring(test.indexOf(' ')+1,test.length())); String candy =br.readLine(); int predtr=0; int[] checkout=new int[p]; for(int i=0;i<n;i++){ predtr=((int)candy.charAt(i))-65; checkout[predtr]=checkout[predtr]+1; } int output=checkout[0]; for(int i=0;i<p-1;i++) { if (checkout[i] > 0) { output = min(output, checkout[i + 1]); } } System.out.println(output*p); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
eba139bd224804a67ee77ff007d2f684
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.io.IOException; import java.util.Arrays; import java.io.InputStreamReader; import java.io.BufferedReader; import java.util.StringTokenizer; import static java.lang.Integer.min; import static java.lang.Integer.parseInt; public class Helloworld { public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l + r) / 2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static int shrink = 1000000007; public static int indexlimit=2000000; public static int[] aman= new int[indexlimit]; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String test; test=br.readLine(); int n= Integer.parseInt(test.substring(0,test.indexOf(' '))); int p= Integer.parseInt(test.substring(test.indexOf(' ')+1,test.length())); String candy =br.readLine(); int predtr=0; int[] checkout=new int[p]; for(int i=0;i<n;i++){ predtr=((int)candy.charAt(i))-65; checkout[predtr]=checkout[predtr]+1; } int output=checkout[0]; for(int i=0;i<p-1;i++) { if (checkout[i] > 0) { output = min(output, checkout[i + 1]); } } System.out.println(output*p); /*test=sc.nextLine(); String[] tokens = test.split(" "); int[] candy = new int[tokens.length]; int i = 0; for (String token : tokens){ candy[i++] = Integer.parseInt(token); } */ /* Helloworld ob = new Helloworld(); ob.sort(candy, 0, candy.length - 1); */ /*test=""; int minX=candy[0]; for(int i=1;i<n;i++){ minX=Math.max(minX,(candy[i]-i)); } int maxX=candy[p-1]-1; //System.out.println("min x = " + minX); //System.out.println("max x = " + maxX); int out1=maxX-minX; if(out1==0){ System.out.println("1"); System.out.println(minX); } else if(out1>0) { System.out.println(out1 + 1); for (int j = minX; j < maxX; j++) { test += Integer.toString(j) + " "; } test += Integer.toString(maxX); System.out.println(test); } else{ System.out.println("0"); } */ /* LinkedList aman = new LinkedList(); System.out.println("length= "+ aman.getlength()); aman.insert(18); aman.insert(36); aman.insert(54); aman.insert(72); aman.insert(90); aman.insertAtStart(108); aman.showall(); System.out.println(aman.searchelement(108)); System.out.println(aman.searchelement(18)); System.out.println(aman.searchelement(36)); System.out.println(aman.searchelement(54)); System.out.println(aman.searchelement(72)); System.out.println(aman.searchelement(90)); System.out.println(aman.searchelement(18002)); */ } public static int subseqlen(int out, String candy){ if(!candy.equals("")) { int t = candy.lastIndexOf(candy.charAt(0)) + 1; candy = candy.substring(t); return min(t, subseqlen(out, candy)); } return out; } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
1e8866bd270de154396e5887d619ccf6
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; public class Codechef { static long substrings(String s) { int n = s.length(); long sumofDigits[] = new long[n]; sumofDigits[0] = s.charAt(0) - '0'; long res = sumofDigits[0]; for (int i = 1; i < n; i++) { int nums = s.charAt(i) - '0'; sumofDigits[i] = ((i + 1) * nums) + (10 * (sumofDigits[i - 1])); res += sumofDigits[i]; } return res % 1000000007; } /* * array list * * ArrayList<Integer> al=new ArrayList<>(); creating BigIntegers * * BigInteger a=new BigInteger(); BigInteger b=new BigInteger(); * * hash map * * HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); for(int * i=0;i<ar.length;i++) { Integer c=hm.get(ar[i]); if(hm.get(ar[i])==null) { * hm.put(ar[i],1); } else { hm.put(ar[i],++c); } } * * while loop * * int t=sc.nextInt(); while(t>0) { t--; } * * array input * * for(int i=0;i<ar.length;i++) { ar[i]=sc.nextInt(); } */ // private static final Scanner sc = new Scanner(System.in); // static Scanner sc = new Scanner(new BufferedReader(new // InputStreamReader(System.in))); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static final Scanner sc = new Scanner(System.in); private static final FastReader fs = new FastReader(); static int countsum(ArrayList<Integer> al) { int sum = 0; for (Integer integer : al) { sum += integer; } return sum; } public static void main(String[] args) throws IOException { int n = fs.nextInt(); int k=fs.nextInt(); String s = fs.next(); HashMap<Character, Integer> hm = new HashMap<Character, Integer>(); for (int i = 0; i < s.length(); i++) { char x = s.charAt(i); Integer c = hm.get(x); if (hm.get(x) == null) { hm.put(x, 1); } else { hm.put(x, ++c); } } if(hm.size()<k) System.out.println(0); else System.out.println(Collections.min(hm.values())*k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
2138e0d3f05f5f89a6e8d565c1049e2e
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; public class q { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n,k,i,t=0; char ch; n=sc.nextInt(); k=sc.nextInt(); int a[]=new int[k]; String s=sc.next(); for(i=0;i<n;i++) { ch=s.charAt(i); a[(int)ch-65]++; } t=a[0]; for(i=1;i<k;i++) { if(a[i]<t) t=a[i]; } System.out.println(t*k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
320c247854bd71c1b10f51624a7878ef
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class CP{ public static OutputStream out=new BufferedOutputStream(System.out); static Scanner sc=new Scanner(System.in); static long mod=1000000007l; static int INF=1000000; //nl-->neew line; //l-->line; //arp-->array print; //arpnl-->array print new line public static void nl(Object o) throws IOException{out.write((o+"\n").getBytes()); } public static void l(Object o) throws IOException{out.write((o+"").getBytes());} public static void arp(int[] o) throws IOException{for(int i=0;i<o.length;i++) out.write((o[i]+" ").getBytes()); out.write(("\n").getBytes());} public static void arpnl(int[] o) throws IOException{for(int i=0;i<o.length;i++) out.write((o[i]+"\n").getBytes());} public static void scanl(long[] a,int n) {for(int i=0;i<n;i++) a[i]=sc.nextLong();} public static void scani(int[] a,int n) {for(int i=0;i<n;i++) a[i]=sc.nextInt();} public static void scan2D(int[][] a,int n,int m) {for(int i=0;i<n;i++) for(int j=0;j<m;j++) a[i][j]=sc.nextInt();} // static long cnt; static TreeSet<Integer> ans; static int n,m; static boolean[][] dp,vis; static HashMap<Pair,Boolean> arrl; static Stack<Integer> stk; static Queue<Integer> queue; public static void main(String[] args) throws IOException{ long sttm=System.currentTimeMillis(); long mod=1000000007l; int n=sc.nextInt(), k=sc.nextInt(); String s=sc.next(); int[] cnt=new int[26]; for(char c:s.toCharArray()){ cnt[c-'A']++; } int min=Integer.MAX_VALUE; for(int i=0;i<k;i++){ min=Math.min(cnt[i],min); } int len=0; if(min>0){ len=min*k; for(int i=k;i<26;i++){ len+=cnt[i]; } } else{ len=0; } nl(len); out.flush(); } public static boolean ck(int[] a){ for(int i=0;i<26;i++){ if(a[i]!=0) return false; } return true; } } class Pair{ int x,y; Pair(int x,int y){ this.x=x; this.y=y; } } // 0 4 2 // // 11100 //b // 01011 //a // 01234 // 11011 // 00100 // 11100
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
85322e255dc487d9e3d5279ab77f5ccf
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = Integer.parseInt(in.next()), k = Integer.parseInt(in.next()); String str = in.next(); int[] F = new int[26]; int min = Integer.MAX_VALUE; for(char ch: str.toCharArray()){ F[ch-'A']++; //min = Math.min(min, F[ch-'A']; } for(int i = 0; i < k; i++)min = Math.min(min,F[i]); // out.println(Arrays.toString(F)); out.println(min*k); out.close(); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
98e5de1390bcb929203e5121fa57495f
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); int n= sc.nextInt(); int k= sc.nextInt(); String s = sc.next(); int[] arr = new int [k]; for(int i=0;i<n;i++){ arr[s.charAt(i)-'A']++; } Arrays.sort(arr); System.out.println(arr[0]*k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
df891373af0160af8c5971724380a401
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Scanner; import java.util.StringTokenizer; public class code11 { public static void main(String[] args) { // TODO Auto-generated method stub FastReader scn = new FastReader(); PrintWriter out = new PrintWriter(System.out); int T = 1; while(T-->0) { int n = scn.nextInt(); int k = scn.nextInt(); String str = scn.next(); int[] arr = new int[26]; for(int i=0; i<str.length(); i++) arr[str.charAt(i)-'A']++; int min = Integer.MAX_VALUE; for(int i=0; i<k; i++) min = Math.min(min, arr[i]); out.print(1L*k*min); } out.flush(); } public static class Pair implements Comparable<Pair>{ int a; int b; Pair(int x, int y){ a= x; b= y; } public int compareTo(Pair p) { if(this.b-this.a == p.b-p.a) return p.a - this.a; return (this.b-this.a) - (p.b - p.a); } } static long power(long x, long y, int p) { long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { // If y is odd, multiply x // with result if((y & 1)==1) res = (res * x) % p; // y must be even now // y = y / 2 y = y >> 1; x = (x * x) % p; } return res; } public static void fill(int[][] mat, int a) { int n = mat.length; int m = mat[0].length; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) mat[i][j] = a; } } public static void fill(int[] arr, int a) { int n = arr.length; for(int i=0;i<n; i++) arr[i] = a; } public static int gcd(int a, int b) { if(b==0) return a; return gcd(b, a%b); } public static int lcm(int a, int b) { return (a*b)/gcd(a,b); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
3c291a892816b41a2ac755d09c66131a
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author GodOfWar */ public class String3 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int[]count=new int[k]; String s=sc.next(); for(int i=0;i<s.length();i++){ count[s.charAt(i)-'A']++; } int min=Integer.MAX_VALUE; for(int x : count){ if(x < min) min=x; } System.out.println(min * k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
ff4ce9aa47280480c96995960245974a
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.Scanner; import java.awt.image.BandedSampleModel; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class A { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n = sc.nextInt();int k=sc.nextInt(); int arr[]=new int[k]; String s=sc.next(); //String latin="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(int i=0;i<n;i++) { arr[s.charAt(i)-'A']=arr[s.charAt(i)-'A']+1; } int min=100000000; for(int i=0;i<k;i++) { min=Math.min(arr[i],min); } System.out.println(k*min); //int arr[] = new int[n]; } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
ca9f32de154e6fa238925e1596ee6824
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.io.*; import java.util.*; public class problemA { // private static final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); private static final Scanner in = new Scanner(System.in); public static void main(String[] args) throws IOException { int t = 1; // t = Integer.parseInt(in.readLine()); // t = in.nextInt(); while (t != 0) { play(); t--; } } public static void play() throws IOException { Map<Integer, Integer> m = new TreeMap<>(); for (int i = 0; i < 26; ++i) { m.put(i, 0); } int n = in.nextInt(); int k = in.nextInt(); in.nextLine(); String s = in.nextLine(); for (int i = 0; i < n; ++i) { m.merge(s.charAt(i) - 'A', 1, Integer::sum); } int ans = m.get(0); for (int i = 0; i < k; ++i) { ans = Math.min(ans, m.get(i)); } System.out.println(ans * k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
75edc3706fac775ccd8a7a41b95e1c52
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class A { public static void process(int test_number)throws IOException { int n = ni(), k = ni(), freq[] = new int[k]; char arr[] = nln().toCharArray(); for(char c : arr) freq[c - 'A']++; int cnt = n; for(int i : freq) cnt = Math.min(cnt, i); pn(cnt * k); } static final long mod = (long)1e9+7l; static FastReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { out = new PrintWriter(System.out); sc = new FastReader(); long s = System.currentTimeMillis(); int t = 1; //t = ni(); for(int i = 1; i <= t; i++) process(i); out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); } static void trace(Object... o){ System.err.println(Arrays.deepToString(o)); }; static void pn(Object o){ out.println(o); } static void p(Object o){ out.print(o); } static int ni()throws IOException{ return Integer.parseInt(sc.next()); } static long nl()throws IOException{ return Long.parseLong(sc.next()); } static double nd()throws IOException{ return Double.parseDouble(sc.next()); } static String nln()throws IOException{ return sc.nextLine(); } static long gcd(long a, long b)throws IOException{ return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{ return (b==0)?a:gcd(b,a%b); } static int bit(long n)throws IOException{ return (n==0)?0:(1+bit(n&(n-1))); } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
162fd916dae640898f6dfebd8e318445
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.util.Arrays; import java.math.BigInteger; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner in =new Scanner(System.in); int l,p,n,h,k,r,a,b; String s="",st=""; long x,y,z; int t=1,i,m,j; for(i=0;i<t;i++) { p=0;h=0; n=in.nextInt(); k=in.nextInt(); s=in.next(); char c[]=s.toCharArray(); int f[]=new int[k]; for(j=0;j<n;j++) { p=(int)c[j]-65; f[p]++; } Arrays.sort(f); System.out.println(k*f[0]); } } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
d70769bcea97cf603498d51450b7bf8d
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { private static void findEquality(int size, int letters, String s) { int[] arr = new int[letters]; for (int count = 0; count < size; count++) arr[(int) s.charAt(count) - 65]++; Arrays.sort(arr); System.out.println(arr[0] * letters); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); findEquality(scan.nextInt(), scan.nextInt(), scan.next()); scan.close(); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
bab1dcf9cb10e2fba204f4af971f271f
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.Scanner; public class apples { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int k=in.nextInt(); String s=in.next(); int A[]=new int[26]; for (int i=0; i<26; i++) A[i]=0; for (int i=0; i<n; i++) A[s.charAt(i)-'A']++; int ans=(int)1e9; for (int i=0; i<k; i++) { if (A[i]<ans) { ans=A[i]; } } System.out.println(ans*k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
e0712b83da6c60f6b5d5dff55e47a619
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; public class Equality { public static void main(String[] args) { Scanner sc=new Scanner(System.in); Set<Integer> w=new HashSet<>(); int a=sc.nextInt(); int b=sc.nextInt(); String s=sc.next(); if(b==1 || a==2) { System.out.println(a); return; } int arr[]=new int[b]; int core=0; arr[0]=1; char chars[]=s.toCharArray(); Arrays.parallelSort(chars); for(int i=0;i<s.length()-1;i++) { if(chars[i]==chars[i+1]) { arr[core]+=1; } else { core++; arr[core]+=1; } } if(core<b-1) { System.out.println(0); return; } for (int v : arr) { w.add(v); } if(w.size()==1) { System.out.println(a); } else { Arrays.parallelSort(arr); System.out.println(arr[0]*b); } } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
7241157804887e950d7c3e44677ef40f
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; @SuppressWarnings("Duplicates") // author @mdazmat9 public class Main{ static FastScanner sc = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int test = 1; // test = sc.nextInt(); for (int ind = 0; ind < test; ind++) { solve(); } out.flush(); } static void solve() { int n=sc.nextInt(); int k=sc.nextInt(); String s=sc.next(); int freq[]=new int[26]; for(int i=0;i<n;i++) { freq[s.charAt(i) - 'A']++; } Arrays.sort(freq); int mn=freq[26-k]; out.println(mn*k); } static int[] intarray(int n){ int [] a=new int[n];for(int i=0;i<n;i++)a[i]=sc.nextInt();return a; } static void sort(int[]a){ shuffle(a);Arrays.sort(a);} static void sort(long[]a){ shuffle(a);Arrays.sort(a);} static long[] longarray(int n){ long [] a=new long[n];for(int i=0;i<n;i++)a[i]=sc.nextLong();return a; } static ArrayList<Integer> intlist(int n){ArrayList<Integer> list=new ArrayList<>();for(int i=0;i<n;i++)list.add(sc.nextInt());return list; } static ArrayList<Long> longlist(int n){ArrayList<Long> list=new ArrayList<>();for(int i=0;i<n;i++)list.add(sc.nextLong());return list; } static int[][] int2darray(int n,int m){ int [][] a=new int[n][m];for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ a[i][j]=sc.nextInt(); } }return a; } static long[][] long2darray(int n,int m){ long [][] a=new long[n][m];for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ a[i][j]=sc.nextLong(); } }return a; } static char[][] char2darray(int n,int m){ char [][] a=new char[n][m];for(int i=0;i<n;i++){ String s=sc.next(); a[i]=s.toCharArray(); }return a; } static double pi=3.14159265358979323846264; public static double logb( double a, double b ) {return Math.log(a) / Math.log(b); } static long fast_pow(long a, long b,long abs) { if(b == 0) return 1L; long val = fast_pow(a, b / 2,abs); if(b % 2 == 0) return val * val % abs; else return val * val % abs * a % abs; } static long abs = (long)1e9 + 7; static void shuffle(int[] a) { int n = a.length;for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i));int tmp = a[i];a[i] = a[r];a[r] = tmp; } } static void shuffle(long[] a) { int n = a.length;for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i));long tmp = a[i];a[i] = a[r];a[r] = tmp; } } static long gcd(long a , long b) { if(b == 0) return a; return gcd(b , a % b); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
957b12ead97be372e4da129bd31183a0
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; import java.io.*; public class Equality { public static void main(String[] args) { MyScanner scn = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); // Start writing your solution here. ------------------------------------- /* * int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // * read input as long double d = sc.nextDouble(); // read input as double String * str = sc.next(); // read input as String String s = sc.nextLine(); // read * whole line as String * * int result = 3*n; out.println(result); // print via PrintWriter */ // Stop writing your solution here. ------------------------------------- int[] freq = new int[27]; int n = scn.nextInt(); int k = scn.nextInt(); String str = scn.next(); int[] arr = new int[k]; for (int i = 0; i < str.length(); i++) { arr[str.charAt(i) - 'A']++; } int min = Integer.MAX_VALUE; for (int i = 0; i < arr.length; i++) { min = Math.min(min, arr[i]); } out.println(min * k); out.close(); } public static PrintWriter out; public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
554577216d65a40c60305ed87b0a97d0
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
import java.util.*; public class Yash { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int freq[] = new int[k]; String s = sc.next(); for(int i=0; i<s.length(); i++) { freq[s.charAt(i)-65]++; } int min = freq[0]; for(int i=1; i<k; i++) if(freq[i]<min) min = freq[i]; if(min==0) System.out.println(0); else System.out.println(min*k); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
12a89bdc3b7da11d12f4165fc9f2ef49
train_002.jsonl
1536248100
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.
256 megabytes
// package cp; import java.io.*; import java.util.*; public class Cf_three { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); Readers.init(System.in); int n=Readers.nextInt(); int k=Readers.nextInt(); String s=Readers.next(); int[] cnt=new int[k+1]; for (int i = 0; i <s.length(); i++) { if(s.charAt(i)<=(char)(64+k)) { cnt[(int)(s.charAt(i))-64]++; } } long minn=Long.MAX_VALUE; for (int i = 1; i <=k; i++) { minn=Math.min(cnt[i], minn); } System.out.println(1L*minn*k); out.flush(); } } class Readers { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static long nextLong() throws IOException{ return Long.parseLong(next()); } }
Java
["9 3\nACAABCCAB", "9 4\nABCABCABC"]
2 seconds
["6", "0"]
NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
Java 11
standard input
[ "implementation", "strings" ]
d9d5db63b1e48214d02abe9977709384
The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.
800
Print the only integer — the length of the longest good subsequence of string $$$s$$$.
standard output
PASSED
cde30a792275d95bce0b2378b9b438f5
train_002.jsonl
1530628500
You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$.Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible.An undirected tree is a connected undirected graph with $$$n - 1$$$ edges.Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree).
256 megabytes
import java.io.*; import java.util.*; public class A { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int d = scanner.nextInt(); int k = scanner.nextInt(); if (n < d + 1) { System.out.print("NO"); return; } List<List<Integer>> tree = new ArrayList<>(n); for (int i = 0; i < n; i++) { tree.add(new ArrayList<>()); } List<Integer> pow = new ArrayList<>(n); for (int i = 0; i < n; i++) { pow.add(0); } List<Integer> way = new ArrayList<>(n); for (int i = 0; i < d + 1; i++) { way.add(Integer.max(i, d - i)); } for (int i = 0; i < d; i++) { tree.get(i).add(i + 1); pow.set(i, pow.get(i) + 1); pow.set(i + 1, pow.get(i + 1) + 1); } for (int i = 0; i < n; i++) { if (pow.get(i) > k) { System.out.print("NO"); return; } } Queue<Integer> queue = new PriorityQueue<>(); for (int i = 0; i < d + 1; i++) { if (pow.get(i) < k && way.get(i) < d) { queue.add(i); } } int freeVertex = d + 1; br : while (!queue.isEmpty()) { if (freeVertex >= n) { break; } int curr = queue.poll(); while (pow.get(curr) < k) { tree.get(curr).add(freeVertex); pow.set(curr, pow.get(curr) + 1); pow.set(freeVertex, pow.get(freeVertex) + 1); way.add(way.get(curr) + 1); if (way.get(freeVertex) < d) { queue.add(freeVertex); } freeVertex++; if (freeVertex >= n) { break br; } } } if (freeVertex < n) { System.out.print("NO"); return; } System.out.println("YES"); for (int i = 0; i < n; i++) { for (int j = 0; j < tree.get(i).size(); j++) { System.out.println((i + 1) + " " + (tree.get(i).get(j) + 1)); } } } }
Java
["6 3 3", "6 2 3", "10 4 3", "8 5 3"]
4 seconds
["YES\n3 1\n4 1\n1 2\n5 2\n2 6", "NO", "YES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7", "YES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3"]
null
Java 8
standard input
[ "constructive algorithms", "graphs" ]
a4849505bca48b408a5e8fb5aebf5cb6
The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 \le n, d, k \le 4 \cdot 10^5$$$).
2,100
If there is no tree satisfying the conditions above, print only one word "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1
standard output
PASSED
92af9c31cdba21289a638050b684944b
train_002.jsonl
1530628500
You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$.Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible.An undirected tree is a connected undirected graph with $$$n - 1$$$ edges.Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree).
256 megabytes
import java.util.*; import java.io.*; public class _1003_E { public static void main(String[] args) throws IOException { int N = readInt(), D = readInt(), K = readInt(); if(D >= N || (D >= 2 && K == 1)) {println("NO"); exit();} ArrayList<Integer> graph[] = new ArrayList[N+1]; for(int i = 1; i<=N; i++) graph[i] = new ArrayList<>(); int in[] = new int[N+1], dist[] =new int[N+1]; for(int i = 1; i<=D; i++) {graph[i].add(i+1); in[i]++; in[i+1]++; dist[i] = Math.max(i-1, D-i+1);} int cnt = D+2; Stack<Integer> stk = new Stack<>(); for(int i = 1; i<=D; i++) stk.add(i); while(!stk.isEmpty()) { int n = stk.pop(); if(dist[n] == D) continue; while(in[n] < K && cnt <= N) { graph[n].add(cnt); stk.add(cnt); in[cnt]++; dist[cnt] = dist[n] + 1; cnt++; in[n]++; } } if(cnt <= N) println("NO"); else { println("YES"); for(int i = 1; i<=N; i++) for(int n: graph[i]) println(i + " " + n); } exit(); } final private static int BUFFER_SIZE = 1 << 16; private static DataInputStream din = new DataInputStream(System.in); private static byte[] buffer = new byte[BUFFER_SIZE]; private static int bufferPointer = 0, bytesRead = 0; static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = Read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public static String read() throws IOException { byte[] ret = new byte[1024]; int idx = 0; byte c = Read(); while (c <= ' ') { c = Read(); } do { ret[idx++] = c; c = Read(); } while (c != -1 && c != ' ' && c != '\n' && c != '\r'); return new String(ret, 0, idx); } public static int readInt() throws IOException { int ret = 0; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public static long readLong() throws IOException { long ret = 0; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public static double readDouble() throws IOException { double ret = 0, div = 1; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (c == '.') { while ((c = Read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private static void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private static byte Read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } static void print(Object o) { pr.print(o); } static void println(Object o) { pr.println(o); } static void flush() { pr.flush(); } static void println() { pr.println(); } static void exit() throws IOException { din.close(); pr.close(); System.exit(0); } }
Java
["6 3 3", "6 2 3", "10 4 3", "8 5 3"]
4 seconds
["YES\n3 1\n4 1\n1 2\n5 2\n2 6", "NO", "YES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7", "YES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3"]
null
Java 8
standard input
[ "constructive algorithms", "graphs" ]
a4849505bca48b408a5e8fb5aebf5cb6
The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 \le n, d, k \le 4 \cdot 10^5$$$).
2,100
If there is no tree satisfying the conditions above, print only one word "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1
standard output
PASSED
baea5a658f812a116f21715d02ae9ef5
train_002.jsonl
1530628500
You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$.Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible.An undirected tree is a connected undirected graph with $$$n - 1$$$ edges.Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree).
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashMap; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.util.Map; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { int n; int d; int k; List<Pair> edge = new ArrayList<>(); Map<Integer, Vertex> mapping = new HashMap<>(); public void solve(int testNumber, InputReader in, PrintWriter out) { n = in.nextInt(); d = in.nextInt(); k = in.nextInt(); if ((n > 2 && k < 2) || d >= n) { out.println("NO"); return; } for (int i = 0; i < n; ++i) { mapping.put(i, new Vertex(i)); } for (int i = 0; i < d; ++i) { edge.add(new Pair(i, i + 1)); mapping.get(i).edgeCount++; mapping.get(i + 1).edgeCount++; } int cur = d + 1; int depth = 0; int rem = d - 1; for (int i = 1; i <= d - 1; ++i) { if (rem % 2 == 1) { if ((i - 1) <= rem / 2) { ++depth; } else { --depth; } } else { if ((i - 1) < rem / 2) { ++depth; } else if ((i - 1) > rem / 2) { --depth; } } cur = mapping.get(i).connect(cur, 0, depth); } if (cur < n) { out.println("NO"); return; } out.println("YES"); for (int i = 0; i < edge.size(); ++i) { Pair e = edge.get(i); out.println((e.first + 1) + " " + (e.second + 1)); } } class Pair { int first; int second; Pair(int first, int second) { this.first = first; this.second = second; } } class Vertex { int id; boolean marked; int edgeCount = 0; List<Vertex> adj = new ArrayList<>(); Vertex(int id) { this.id = id; } int connect(int cur, int curDepth, int depth) { this.marked = true; if (curDepth >= depth) return cur; for (int i = edgeCount; i < k; ++i) { if (cur >= n) break; edge.add(new Pair(id, cur)); adj.add(mapping.get(cur)); mapping.get(cur).edgeCount++; ++cur; } for (Vertex v : adj) { if (v.marked) continue; cur = v.connect(cur, curDepth + 1, depth); } return cur; } } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["6 3 3", "6 2 3", "10 4 3", "8 5 3"]
4 seconds
["YES\n3 1\n4 1\n1 2\n5 2\n2 6", "NO", "YES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7", "YES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3"]
null
Java 8
standard input
[ "constructive algorithms", "graphs" ]
a4849505bca48b408a5e8fb5aebf5cb6
The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 \le n, d, k \le 4 \cdot 10^5$$$).
2,100
If there is no tree satisfying the conditions above, print only one word "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1
standard output
PASSED
138ce9005778fd1ab62b3671656ce1a8
train_002.jsonl
1530628500
You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$.Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible.An undirected tree is a connected undirected graph with $$$n - 1$$$ edges.Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String[] s=br.readLine().split(" "); int n = Integer.parseInt(s[0]); int d = Integer.parseInt(s[1]); int k= Integer.parseInt(s[2]); if(n<=d||(k==1&&n>2)) { System.out.println("NO"); return; } int[] md=new int[n+1]; int[] mk=new int[n+1]; ArrayList<String> a=new ArrayList<>(); int m=d/2; for(int i=1;i<=d;i++) { md[i]=d+1-i; if(md[i]<=m) md[i]=d-md[i]; mk[i]=2; a.add(i+" "+(i+1)); } md[d+1]=d; mk[1]=mk[d+1]=1; int idx=d+2; for(int i=2;i<=n;i++) { if(idx>n||idx==i) break; if(md[i]<d) { for(int j=mk[i];j<k;j++) { if(idx>n) break; md[idx]=md[i]+1; mk[idx]++; mk[i]++; a.add(i+" "+idx); idx++; } } } if(idx>n) { System.out.println("YES"); for(String ans:a) { System.out.println(ans); } }else System.out.println("NO"); } }
Java
["6 3 3", "6 2 3", "10 4 3", "8 5 3"]
4 seconds
["YES\n3 1\n4 1\n1 2\n5 2\n2 6", "NO", "YES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7", "YES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3"]
null
Java 8
standard input
[ "constructive algorithms", "graphs" ]
a4849505bca48b408a5e8fb5aebf5cb6
The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 \le n, d, k \le 4 \cdot 10^5$$$).
2,100
If there is no tree satisfying the conditions above, print only one word "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1
standard output
PASSED
25c47282d79ffd451744e332ff4fb19f
train_002.jsonl
1530628500
You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$.Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible.An undirected tree is a connected undirected graph with $$$n - 1$$$ edges.Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree).
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; import static java.lang.Math.*; import java.util.concurrent.ThreadLocalRandom; public class Sol implements Runnable { long mod = (long)1e9 + 7; class pair { int x, y; pair(int X, int Y) { x = X; y = Y; } } int n, d, k; int curr; ArrayList<pair> list; int deg[]; void dfs(int node, int level) { if(n == 1) return; if(level == 0) return; while(deg[node] != k && n > 1) { list.add(new pair(node, curr)); deg[node]++; deg[curr]++; n--; curr++; dfs(curr - 1, level - 1); } } void solve(InputReader in, PrintWriter w) { n = in.nextInt(); d = in.nextInt(); k = in.nextInt(); list = new ArrayList<>(); deg = new int[n + 1]; if(n < d + 1) w.println("NO"); else { boolean ok = true; for(int i=1;i<=d;i++) { list.add(new pair(i, i + 1)); deg[i]++; deg[i + 1]++; if(deg[i] > k || deg[i + 1] > k) { ok = false; break; } n--; } if(ok) { curr = d + 2; int level = 1; for(int i=2;i<=d;i++) { dfs(i, min(level, d - level)); level++; } if(n == 1) { w.println("YES"); for(pair p : list) w.println(p.x+" "+p.y); } else { w.println("NO"); } } else { w.println("NO"); } } } // ************* Code ends here *************** void init() throws Exception { //Scanner in; InputReader in; PrintWriter w; boolean online = false; String common_in_fileName = "in"; String common_out_fileName = "\\out"; int test_files = 0; for(int file_no = 0; file_no <= test_files; file_no++) { String x = "" + file_no; if (x.length() == 1) x = "0" + x; String in_fileName = common_in_fileName;// + "" + x; String out_fileName = common_out_fileName;// + "" + x; if (online) { //in = new Scanner(new File(in_fileName + ".txt")); in = new InputReader(new FileInputStream(new File(in_fileName + ".txt"))); w = new PrintWriter(new FileWriter(out_fileName + ".txt")); } else { //in = new Scanner(System.in); in = new InputReader(System.in); w = new PrintWriter(System.out); } solve(in, w); w.close(); } } public void run() { try { init(); } catch(Exception e) { System.out.println(e); e.printStackTrace(); } } public static void main(String args[]) throws Exception { new Thread(null, new Sol(),"Sol",1<<28).start(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6 3 3", "6 2 3", "10 4 3", "8 5 3"]
4 seconds
["YES\n3 1\n4 1\n1 2\n5 2\n2 6", "NO", "YES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7", "YES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3"]
null
Java 8
standard input
[ "constructive algorithms", "graphs" ]
a4849505bca48b408a5e8fb5aebf5cb6
The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 \le n, d, k \le 4 \cdot 10^5$$$).
2,100
If there is no tree satisfying the conditions above, print only one word "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1
standard output
PASSED
65d428361e102bc7d0c8fd81573d570c
train_002.jsonl
1530628500
You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$.Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible.An undirected tree is a connected undirected graph with $$$n - 1$$$ edges.Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree).
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Collection; import java.util.InputMismatchException; import java.io.IOException; import java.util.Queue; import java.util.LinkedList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ETreeConstructing solver = new ETreeConstructing(); solver.solve(1, in, out); out.close(); } static class ETreeConstructing { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(), d = in.nextInt(), k = in.nextInt(); if (d >= n) { out.println("NO"); return; } if (k == 1) { if (n == 2) { out.println("YES"); out.println("1 2"); } else { out.println("NO"); } return; } StringBuilder ans = new StringBuilder(""); for (int i = 1; i <= d; i++) { ans.append(i + " " + (i + 1) + "\n"); } int ct = d + 1; if (k != 2) { for (int i = 2; i <= (d + 2) / 2; i++) { Queue<Integer> q = new LinkedList<>(); for (int l = 0; l < 2; l++) { if (l == 0) { q.add(i); } else { if (d + 2 - i == i) { break; } q.add(d + 2 - i); } for (int j = 1; j < i; j++) { int curm = 1; if (j == 1) { curm++; } int max = q.size(); for (int m1 = max; m1 > 0; m1--) { int curr = q.poll(); for (int m = k - curm; m > 0; m--) { if (ct == n) { break; } ct++; ans.append(curr + " " + ct + "\n"); q.add(ct); } if (ct == n) { break; } } if (ct == n) { break; } } q.clear(); if (ct == n) { break; } } } } if (ct == n) { out.println("YES"); out.println(ans); } else { out.println("NO"); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6 3 3", "6 2 3", "10 4 3", "8 5 3"]
4 seconds
["YES\n3 1\n4 1\n1 2\n5 2\n2 6", "NO", "YES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7", "YES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3"]
null
Java 8
standard input
[ "constructive algorithms", "graphs" ]
a4849505bca48b408a5e8fb5aebf5cb6
The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 \le n, d, k \le 4 \cdot 10^5$$$).
2,100
If there is no tree satisfying the conditions above, print only one word "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1
standard output
PASSED
b912cb3f7b149780874b3f283f94c5c2
train_002.jsonl
1530628500
You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$.Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible.An undirected tree is a connected undirected graph with $$$n - 1$$$ edges.Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree).
256 megabytes
import java.util.*; public class CF1003E { private static List<int[]> links = new ArrayList<>(); private static class Node { public int id; public int distance; public int degree; public Node(int id, int distance, int degree) { this.id = id; this.distance = distance; this.degree = degree; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int d = scanner.nextInt(); int k = scanner.nextInt(); Stack<Node> nodes = new Stack<>(); if (n == 2 && d == 1 && k == 1) { System.out.println("YES"); System.out.println("1 2"); return; } if (n < d + 1 || k < 2) { System.out.println("NO"); return; } for (int i = 2; i < d + 1; i++) { links.add(new int[]{i - 1, i + 1}); Node node = new Node(i + 1, i / 2, 2); nodes.add(node); } links.add(new int[]{(d + 1), d}); for (int i = d + 2; i <= n; i++) { while (!nodes.isEmpty() && nodes.peek().degree >= k) { nodes.pop(); } if (nodes.isEmpty()) { System.out.println("NO"); return; } Node parent = nodes.pop(); Node child = new Node(i, parent.distance - 1, 1); links.add(new int[]{parent.id, child.id}); parent.degree++; if (child.distance > 0) { nodes.push(child); } if (parent.degree < k) { nodes.push(parent); } } StringBuilder sb = new StringBuilder(); sb.append("YES\n"); for (int[] e : links) { sb.append(e[0]).append(' ').append(e[1]).append('\n'); } System.out.println(sb.toString()); } }
Java
["6 3 3", "6 2 3", "10 4 3", "8 5 3"]
4 seconds
["YES\n3 1\n4 1\n1 2\n5 2\n2 6", "NO", "YES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7", "YES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3"]
null
Java 8
standard input
[ "constructive algorithms", "graphs" ]
a4849505bca48b408a5e8fb5aebf5cb6
The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 \le n, d, k \le 4 \cdot 10^5$$$).
2,100
If there is no tree satisfying the conditions above, print only one word "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1
standard output
PASSED
91a600c96732400c060b4927fd3e8515
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
//package com.netease.music.codeforces.round560.div3; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; /** * Created by dezhonger on 2019/5/14 */ public class E { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); long[] a = new long[n]; long[] b = new long[n]; long[] c = new long[n]; long[] d = new long[n]; d[0] = n; long cnt = n; long x = 1; long last = n; for (int i = 1; i < n; i++) { long cur = n - i; cur += last - x; x++; last = cur; d[i] = last; } List<P> list1 = new ArrayList<>(); List<P> list2 = new ArrayList<>(); for (int i = 0; i < n; i++) { a[i] = scanner.nextLong(); list1.add(new P(a[i] * d[i], i)); } for (int i = 0; i < n; i++) { b[i] = scanner.nextLong(); list2.add(new P(b[i], i)); } Collections.sort(list1); Collections.sort(list2); // System.out.println(list1); //第i大的数在原数的第几位 int[] ranka = new int[n]; int[] rankb = new int[n]; for (int i = 0; i < list1.size(); i++) { P p = list1.get(i); ranka[i] = p.pos; } for (int i = 0; i < list1.size(); i++) { P p = list2.get(i); rankb[i] = p.pos; } // System.out.println(Arrays.toString(ranka)); for (int i = 0; i < n; i++) { int pos = rankb[n - 1 - i]; c[ranka[i]] = b[pos]; } // System.out.println(Arrays.toString(c)); long mod = 998244353L; long r = 0; // long cnt = n; // long x = 1; // long last = n; r = 1L * a[0] * c[0]; r %= mod; r *= cnt; r %= mod; for (int i = 1; i < n; i++) { //2 23 234 2345 // long cur = n - i; // cur += last - x; // x++; // last = cur; long tmp = d[i]; // System.out.println(cur); tmp %= mod; tmp *= a[i]; tmp %= mod; tmp *= c[i]; tmp %= mod; r += tmp; r %= mod; } System.out.println(r); } static class P implements Comparable<P> { @Override public int compareTo(P o) { if (val != o.val) return Long.compare(val, o.val); return Integer.compare(pos, o.pos); } P(long val, int pos) { this.pos = pos; this.val = val; } long val; int pos; @Override public String toString() { return "P{" + "val=" + val + ", pos=" + pos + '}'; } } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
c70559d8b7c776a3191a278db340b1a2
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Task1165E solver = new Task1165E(); solver.solve(1, in, out); out.close(); } static class Task1165E { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int mod = 998244353; Long a[] = new Long[n]; Long b[] = new Long[n]; int i, j; for (i = 0; i < n; i++) a[i] = in.nextLong(); for (i = 0; i < n; i++) b[i] = in.nextLong(); for (i = 0; i < n; i++) { a[i] *= 1l * (i + 1) * (n - i); } Arrays.sort(a); Arrays.sort(b); long ans = 0; for (i = 0; i < n; i++) { a[i] %= mod; a[i] *= b[n - i - 1]; ans += a[i]; ans %= mod; } out.println(ans); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(long i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
622e7fae73bc8a04eeeb667eb013a72f
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Main { static final long MOD = 998244353; //static final long MOD = 1000000007; static boolean[] visited; public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int N = sc.nextInt(); ArrayList<Long> As = new ArrayList<Long>(); ArrayList<Long> Bs = new ArrayList<Long>(); for (int i = 1; i <= N; i++) As.add(sc.nextLong() * i * (N+1-i)); for (int i = 0; i < N; i++) Bs.add(sc.nextLong()); Collections.sort(As); Collections.sort(Bs,Collections.reverseOrder()); long ans = 0; for (int i = 0; i < N; i++) { long val = As.get(i)%MOD; val = (val*Bs.get(i))%MOD; ans = (ans+val)%MOD; } System.out.println(ans); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
1937254b47f64b60d9f1b07e8c65bf47
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Random; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author NMouad21 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ETwoArraysAndSumOfFunctions solver = new ETwoArraysAndSumOfFunctions(); solver.solve(1, in, out); out.close(); } static class ETwoArraysAndSumOfFunctions { private final long mod = 998244353L; private long mul(long a, long b) { return (a * b) % mod; } private long add(long a, long b) { return (a + b) % mod; } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long[] a = in.nextLongArray(n); long[] b = in.nextLongArray(n); for (int i = 0; i < n; i++) { a[i] *= (i + 1L) * (n - i); } ArrayUtils.sort(a); ArrayUtils.sort(b); long ans = 0; for (int i = 0; i < n; i++) { ans = add(ans, mul(a[i] % mod, b[n - i - 1])); } out.println(ans); } } static class InputReader { private InputStream stream; private static final int DEFAULT_BUFFER_SIZE = 1 << 16; private static final int EOF = -1; private byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (this.numChars == EOF) { throw new UnknownError(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException ex) { throw new InputMismatchException(); } if (this.numChars <= 0) { return EOF; } } return this.buf[this.curChar++]; } } public int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) { } byte sgn = 1; if (c == 45) { sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { res *= 10; res += c - 48; c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } public long nextLong() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) { } byte sgn = 1; if (c == 45) { sgn = -1; c = this.read(); } long res = 0; while (c >= 48 && c <= 57) { res *= 10L; res += c - 48; c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } public static boolean isSpaceChar(int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == EOF; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } } static class ArrayUtils { public static void sort(long[] arr) { int n = arr.length; Random r = new Random(); for (int i = 0; i < n; i++) { int p = r.nextInt(n + 1); if (p < n) { long temp = arr[i]; arr[i] = arr[p]; arr[p] = temp; } } Arrays.sort(arr); } } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
88cb2fd2f0999c724086cb12a47eb014
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
/* https://codeforces.com/contest/1165/problem/E */ import java.util.*; import java.io.*; public class x1165E { public static final long MOD = 998244353L; public static void main(String args[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); st = new StringTokenizer(infile.readLine()); int[] a = new int[N]; for(int i=0; i < N; i++) a[i] = Integer.parseInt(st.nextToken()); st = new StringTokenizer(infile.readLine()); int[] b = new int[N]; for(int i=0; i < N; i++) b[i] = Integer.parseInt(st.nextToken()); //sort relative to A int[] b2 = new int[N]; ArrayList<Pair> sorted = new ArrayList<Pair>(); for(int i=0; i < N; i++) sorted.add(new Pair(i, (i+1)*(long)a[i]*(N-i))); Collections.sort(sorted); ArrayList<Integer> bs = new ArrayList<Integer>(); for(int x: b) bs.add(x); Collections.sort(bs); for(int i=0; i < N; i++) { int dex = sorted.get(i).i; b2[dex] = bs.get(i); } //calculate long res = 0L; for(int i=0; i < N; i++) { res += ((((long)b2[i]*a[i])%MOD*(i+1))%MOD*(N-i))%MOD; res %= MOD; } System.out.println(res); } } class Pair implements Comparable<Pair> { public int i; public long v; public Pair(int a, long b) { i = a; v = b; } public int compareTo(Pair oth) { //reverse if(v > oth.v) return -1; else if(v < oth.v) return 1; return 0; } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
90094993074126cd80f8f5f80c74342b
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
// Working program using Reader Class import java.io.*; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static long power(long l, long m,long p) { //long p=mod; // Initialize result long res = 1; // Update x if it is more // than or equal to p l = l % p; while (m > 0) { // If y is odd, multiply x // with result if((m & 1)==1) res = (res * l) % p; // y must be even now // y = y / 2 m = m >> 1; l = (l * l) % p; } return (long)res; } public static long mod=998244353; public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static long modMult(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long modAdd(long a,long b) { return ((a%mod)+(b%mod))%mod; } public static void main(String[] args) throws IOException { Reader scan=new Reader(); PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); long n=scan.nextLong(); ArrayList<Long> al1=new ArrayList<Long>(); for(int i=0;i<n;i++) { long x=scan.nextLong(); al1.add(x); } ArrayList<Long> al3=new ArrayList<Long>(); for(long i=0;i<n;i++) { long m1=(i+1)*(n-i)*1L; long m2=m1*al1.get((int) i); al3.add(m2); } Collections.sort(al3); ArrayList<Long> al2=new ArrayList<Long>(); for(int i=0;i<n;i++) { long x=scan.nextLong(); al2.add(x); } Collections.sort(al2,Collections.reverseOrder()); long sum=0; for(int i=0;i<al1.size();i++) { long m1=modMult(al2.get(i),al3.get(i)); sum=modAdd(sum,m1); } out.print(sum%mod); out.close(); } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
a163a768484427c229a8680b192b9e48
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.io.*; import java.util.*; import java.util.concurrent.ThreadLocalRandom; @SuppressWarnings("Duplicates") public class test { FastScanner in; PrintWriter out; boolean systemIO = true; int INF = Integer.MAX_VALUE / 2; long mod = 998244353; void solve() { int n = in.nextInt(); long[] a = new long[n]; long[] b = new long[n]; for (int i = 0; i < n; i++) { a[i] = in.nextLong(); } for (int i = 0; i < n; i++) { b[i] = in.nextLong(); } for (int i = 0; i < n; i++) { a[i] = (i + 1) * a[i] * (n - i); } shuffleArray(a); shuffleArray(b); Arrays.sort(b); Arrays.sort(a); //printArray(a); //printArray(b); long ans = 0; for (int i = 0; i < n; i++) { ans = (ans + (a[i] % mod) * b[n - i - 1]) % mod; } System.out.println(ans); } void shuffleArray(long[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); long a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } void printArray(long[] ar) { for (long k : ar) { System.out.print(k + " "); } System.out.println(); } void reverseArray(long[] ar) { for (int i = 0, j = ar.length - 1; i < j; i++, j--) { long a = ar[i]; ar[i] = ar[j]; ar[j] = a; } } private void run() throws IOException { if (systemIO) { in = new test.FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new test.FastScanner(new File("firesafe.in")); out = new PrintWriter(new File("firesafe.out")); } solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] arg) throws IOException { new test().run(); } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
039b7bc6be93a36a637036bd33dc6e5c
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class fast implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class pair implements Comparable<pair>{ int x; int y; pair(int xi, int yi){ x=xi; y=yi; } @Override public int compareTo(pair other){ if(this.x>other.x){return 1;} if(this.x<other.x){return -1;} if(this.y>other.y){return 1;} if(this.y<other.y){return -1;} return 0; } } class dist implements Comparable<dist>{ int x; int y; int z; dist(int xi, int yi, int zi){ x=xi; y=yi; z=zi; } @Override public int compareTo(dist other){ if(this.z>other.z){return 1;} if(this.z<other.z){return -1;} return 0; } } public static void main(String args[]) throws Exception { new Thread(null, new fast(),"fast",1<<26).start(); } public void sortbyColumn(int arr[][], final int col){ // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; if(entry1[col] < entry2[col]) return -1; return 0; } }); // End of function call sort(). } public void sortbyColumn(long arr[][], final int col){ // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<long[]>() { @Override // Compare values according to columns public int compare(final long[] entry1, final long[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; if(entry1[col] < entry2[col]) return -1; return 0; } }); // End of function call sort(). } public void sort(int ar[]){ int n=ar.length; Integer arr[]=new Integer[n]; for(int i=0;i<n;i++){ arr[i]=ar[i]; } Arrays.sort(arr); for(int i=0;i<n;i++){ ar[i]=arr[i].intValue(); } } public void sort(long ar[]){ int n=ar.length; Long arr[]=new Long[n]; for(int i=0;i<n;i++){ arr[i]=ar[i]; } Arrays.sort(arr); for(int i=0;i<n;i++){ ar[i]=arr[i].longValue(); } } long power(long x, long y, long p){ long res = 1; x = x % p; while (y > 0) { if((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} public void run(){ InputReader s = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=s.nextInt(); long a[]=new long[n],b[]=new long[n],mod=998244353L,ans=0; for(int i=0;i<n;i++){ a[i]=s.nextLong()*(i+1)*(n-i); }sort(a); for(int i=0;i<n;i++){ a[i]%=mod; b[i]=s.nextInt(); }sort(b); for(int i=0;i<n;i++){ ans=(ans+(a[i]*b[n-i-1])%mod)%mod; }w.println(ans); w.close(); } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
ed352401b40f21a6c1faa7702be259d6
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.io.*; import java.util.*; public class E_TwoArraysAndSumOfFunctions { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader inp = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(inp, out); out.close(); } private static class Solver { private void solve(InputReader inp, PrintWriter out) { int n = inp.nextInt(); long MOD = 998244353; ArrayList<Long> a = new ArrayList<>(n); ArrayList<Long> b = new ArrayList<>(n); for (int i = 0; i < n; i++) a.add(inp.nextLong() * (i + 1) * (n - i)); for (int i = 0; i < n; i++) b.add(inp.nextLong()); Collections.sort(a); b.sort(Comparator.reverseOrder()); long res = 0; for (int i = 0; i < n; i++) res = (res + (a.get(i) % MOD) * b.get(i)) % MOD; out.print(res); } } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
1ddf9d1e34968a5dcd9e93b44589354d
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; /** * Created by tech4GT on 5/21/19. */ public class twoArrays { private static final long M = 998244353L; public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); ArrayList<Long> a = new ArrayList<>(),b = new ArrayList<>(); for(int i=0;i<n;i++){ a.add(s.nextLong()); } for(int i=0;i<n;i++){ b.add(s.nextLong()); } for(int i=0;i<n;i++){ a.set(i,(i+1)*a.get(i)*(n-i)); } a.sort(new Comparator<Long>() { @Override public int compare(Long o1, Long o2) { return o1.compareTo(o2); } }); b.sort(new Comparator<Long>() { @Override public int compare(Long o1, Long o2) { return o2.compareTo(o1); } }); // System.out.println(a); // System.out.println(b); long rv = 0; for(int i=0;i<n;i++){ rv=(rv + ((a.get(i)%M)*b.get(i)))%M; } System.out.println(rv); } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
18e5f6f91ff6f743378cf6664f59e746
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class ProblemE { public static InputStream inputStream = System.in; public static OutputStream outputStream = System.out; public static void main(String[] args) { MyScanner scanner = new MyScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = scanner.nextInt(); List<Integer> a = new ArrayList<>(); List<Integer> b = new ArrayList<>(); for (int i = 0; i < n; i++) { a.add(scanner.nextInt()); } for (int i = 0; i < n; i++) { b.add(scanner.nextInt()); } int mod = 998244353; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) { long x = a.get(i); x = x * (i + 1); x = x * (n - i); list.add(x); } Collections.sort(list); Collections.sort(b); Collections.reverse(b); long ans = 0; for (int i = 0; i < n; i++) { ans = (ans + (list.get(i) % mod) * b.get(i)) % mod; } out.println(ans); out.flush(); } private static class MyScanner { private BufferedReader bufferedReader; private StringTokenizer stringTokenizer; private MyScanner(InputStream inputStream) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); } private String next() { while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return stringTokenizer.nextToken(); } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private double nextDouble() { return Double.parseDouble(next()); } private String nextLine() { String str = ""; try { str = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static class Pair<F, S> { private F first; private S second; public Pair() {} public Pair(F first, S second) { this.first = first; this.second = second; } } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
6156e025de9b00c326702174c235a740
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class S { static class Pair implements Comparable<Pair>{ long val; int ind; public Pair(long x,int y){val=x;ind=y;} public Pair(){} public int compareTo(Pair p){ long r = val - p.val; if(r==0)return 0; if(r>0)return 1; else return -1; } } static class TrieNode{ TrieNode[]child; int w; boolean term; TrieNode(){ child = new TrieNode[26]; } } public static long gcd(long a,long b) { if(a<b) return gcd(b,a); if(b==0) return a; return gcd(b,a%b); } //static long ans = 0; static long mod = 998244353 ;//(long)(1e9+7); public static void main(String[] args) throws Exception { new Thread(null, null, "Anshum Gupta", 99999999) { public void run() { try { solve(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static long pow(long x,long y){ if(y == 0)return 1; if(y==1)return x; long a = pow(x,y/2); a = (a*a)%mod; if(y%2==0){ return a; } return (a*x)%mod; } static long mxx; static long my_inv(long x) { return pow(x,mod-2); } public static void solve() throws Exception { // solve the problem here MyScanner s = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out), true); mxx = (long)(1e18+5); //int t = s.nextInt(); int n = s.nextInt(); Long[]a=new Long[n]; Long[]b=new Long[n]; for(int i=0;i<n;i++) { a[i]=s.nextLong()*(i+1)*(n-i); } for(int i=0;i<n;i++) { b[i]=s.nextLong(); } Arrays.sort(a); Arrays.sort(b,Collections.reverseOrder()); long ans = 0; for(int i=0;i<n;i++) { a[i] %= mod; ans = (ans + a[i]*b[i])%mod; } if(ans < 0)ans += mod; out.println(ans); out.flush(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
53f045ed0634112c9c7d2f056ddc87e5
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Set; public class simple implements Runnable { public void run() { InputReader input = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = input.nextInt(); ArrayList<Long> a = new ArrayList<>(); ArrayList<Long> b = new ArrayList<>(); for(int i=0;i<n;i++) a.add(input.nextLong()*(i+1)*(n-i)); for(int i=0;i<n;i++) b.add(input.nextLong()); Collections.sort(a); Collections.sort(b); long ans=0; long mod= 998244353; for(int i=0;i<n;i++) { ans+= ((a.get(i)%mod )* (b.get(n-i-1)%mod) )%mod; } System.out.println(ans%mod); } class Graph{ private final int v; private List<List<Integer>> adj; Graph(int v){ this.v = v; adj = new ArrayList<>(v); for(int i=0;i<v;i++){ adj.add(new LinkedList<>()); } } private void addEdge(int a,int b){ adj.get(a).add(b); } private boolean isCyclic() { boolean[] visited = new boolean[v]; boolean[] recStack = new boolean[v]; for (int i = 0; i < v; i++) if (isCyclicUtil(i, visited, recStack)) return true; return false; } private boolean isCyclicUtil(int i, boolean[] visited, boolean[] recStack) { if (recStack[i]) return true; if (visited[i]) return false; visited[i] = true; recStack[i] = true; List<Integer> children = adj.get(i); for (Integer c: children) if (isCyclicUtil(c, visited, recStack)) return true; recStack[i] = false; return false; } } public static void sortbyColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] o1, int[] o2){ return(Integer.valueOf(o1[col]).compareTo(o2[col])); } }); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static class DJSet { public int[] upper; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x; } return x == y; } } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new simple(),"TaskA",1<<26).start(); } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
a93937b719d18d2c3dfa99d21258df9e
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package Lost; import java.io.*; import java.util.*; public class E1165 { public static void main(String args[])throws IOException { Reader sc=new Reader(); int n=sc.nextInt(); Long arr[]=new Long[n]; Integer b[]=new Integer[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); arr[i]=arr[i]*1L*(n-i)*(i+1); } Arrays.sort(arr); for(int i=0;i<n;i++) { b[i]=sc.nextInt(); } Arrays.sort(b); int i=0,j=n-1; long sum=0;int mod=998244353; while(i<n) { sum=(sum+((arr[i]%mod)*(b[j]%mod))%mod)%mod; i++;j--; } System.out.println(sum); } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte [] buffer; private int bufferPointer, bytesRead; public Reader () { din = new DataInputStream (System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader (String file_name) throws IOException { din = new DataInputStream (new FileInputStream (file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine () throws IOException { byte [] buf = new byte[1024]; int cnt = 0, c; while ((c = read ()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String (buf, 0, cnt); } public int nextInt () throws IOException { int ret = 0; byte c = read (); while (c <= ' ') c = read (); boolean neg = (c == '-'); if (neg) c = read (); do { ret = ret * 10 + c - '0'; } while ((c = read ()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong () throws IOException { long ret = 0; byte c = read (); while (c <= ' ') c = read (); boolean neg = (c == '-'); if (neg) c = read (); do { ret = ret * 10 + c - '0'; } while ((c = read ()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble () throws IOException { double ret = 0, div = 1; byte c = read (); while (c <= ' ') c = read (); boolean neg = (c == '-'); if (neg) c = read (); do { ret = ret * 10 + c - '0'; } while ((c = read ()) >= '0' && c <= '9'); if (c == '.') while ((c = read ()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret; return ret; } private void fillBuffer () throws IOException { bytesRead = din.read (buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read () throws IOException { if (bufferPointer == bytesRead) fillBuffer (); return buffer[bufferPointer++]; } public void close () throws IOException { if (din == null) return; din.close (); } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output
PASSED
ffc6a27b68f8ec032439bbaae71aa6ac
train_002.jsonl
1557844500
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
256 megabytes
import java.util.*; import java.math.*; public class Main { public static void merge(long[] arr, int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; long[] L = new long[n1]; long[] R = new long[n2]; for (int i = 0; i < n1; i++) { L[i] = arr[l + i]; } for (int j = 0; j < n2; j++) { R[j] = arr[m + 1 + j]; } int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } public static void sort(long[] arr, int l, int r) { if (l < r) { int m = (l + r) / 2; sort(arr, l, m); sort(arr , m + 1, r); merge(arr, l, m, r); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long[] a = new long[n]; long mult = n; long inc = n - 2; for (int i = 0; i < n; i++) { a[i] = in.nextLong() * mult; mult += inc; inc -= 2; } long MOD = 998244353; long res = 0; long[] b = new long[n]; for (int i = 0; i < n; i++) { b[i] = in.nextLong(); } new Main().sort(a, 0, a.length - 1); new Main().sort(b, 0, b.length - 1); for (int i = 0; i < n; i++) { res += (a[i] % MOD) * b[n - i - 1]; res %= MOD; } System.out.println(res); } }
Java
["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"]
2 seconds
["646", "757402647", "20"]
null
Java 8
standard input
[ "sortings", "greedy", "math" ]
93431bdae447bb96a2f0f5fa0c6e11e0
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.
1,600
Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.
standard output