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
|
aca2f863f4d90dcf7f13ef4626e82f88
|
train_001.jsonl
|
1398612600
|
As usual, Sereja has array a, its elements are integers: a[1],βa[2],β...,βa[n]. Let's introduce notation:A swap operation is the following sequence of actions: choose two indexes i,βj (iββ βj); perform assignments tmpβ=βa[i],βa[i]β=βa[j],βa[j]β=βtmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations?
|
256 megabytes
|
import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
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.HashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Contest {
private static PrintWriter out = new PrintWriter(System.out);
private static final Random random = new Random();
private static int n, m, k, tc, x;
private static int[] a;
private static final int OO = (int) 1e9;
private static final int mod = (int) 1e9 + 7;
private static final int MAX = (int) (1e5 + 10);
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
a = new int[n];
for(int i = 0;i < n;i++)
a[i] = sc.nextInt();
int ans = Integer.MIN_VALUE;
for(int i = 0;i < n;i++) {
ArrayList<Integer> sub = new ArrayList<Integer>();
for(int j = i;j < n;j++) {
// System.out.println(i + " " + j);
sub.add(a[j]);
ans = Math.max(ans, solve(sub, i, j));
// System.out.println(ans);
}
}
out.println(ans);
out.flush();
}
private static int solve(ArrayList<Integer> sub, int i, int j) {
Collections.sort(sub);
ArrayList<Integer> not_sub = new ArrayList<>();
for(int i1 = 0;i1 < i;i1++)
not_sub.add(a[i1]);
for(int i1 = j + 1;i1 < n;i1++)
not_sub.add(a[i1]);
Collections.sort(not_sub);
//System.out.println(not_sub);
int cur_sum = sum(sub);
int swaps = 0;
int j1 = not_sub.size() - 1;
for(int i1 = 0;i1 < sub.size() && swaps < k;i1++) {
int el = sub.get(i1);
for(;j1 >= 0 && swaps < k;) {
if(not_sub.get(j1) > el) {
swaps++;
cur_sum = cur_sum - el + not_sub.get(j1);
j1--;
}
break;
}
}
return cur_sum;
}
private static int sum(ArrayList<Integer> a) {
int sum = 0;
for(int X : a)
sum += X;
return sum;
}
private static void ruffleSort(int[] a) {
int n = a.length;// shuffle, then sosrt
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
java.util.Arrays.sort(a);
}
private static class Scanner {
public BufferedReader reader;
public StringTokenizer st;
public Scanner(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
} catch (Exception e) {
throw (new RuntimeException());
}
}
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
|
["10 2\n10 -1 2 2 2 2 2 2 -1 10", "5 10\n-1 -1 -1 -1 -1"]
|
1 second
|
["32", "-1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"sortings",
"brute force"
] |
ff69d22bc683e5d1d83438585224c774
|
The first line contains two integers n and k (1ββ€βnββ€β200;Β 1ββ€βkββ€β10). The next line contains n integers a[1], a[2], ..., a[n] (β-β1000ββ€βa[i]ββ€β1000).
| 1,500 |
In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations.
|
standard output
| |
PASSED
|
bd89ec4446bce55f5b29f128405144e1
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.util.Scanner;
import java.util.Arrays;
public class Balanced {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
Integer t= sc.nextInt();
Integer[] l=new Integer[t];
for( Integer i=0; i< l.length;i++){
l[i]=sc.nextInt();
}
Arrays.sort(l);
if(l[0]==l[l.length-1])
System.out.println(l.length);
else {
Integer max=1,x=0;
for (Integer i=0; i<l.length; i++) {
while(l[i]-l[x]>5)
x++;
max=Math.max(max,i-x+1);
}
System.out.println(max);
}
sc.close();
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
9a76a04522c6dbfe43fbf5b03e4554e9
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.lang.*;
public class GFG {
public static void main (String[] args) {
PrintWriter pw=new PrintWriter(System.out);
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Integer[] a=new Integer[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Arrays.sort(a);
int max=1;
for(int i=0,j=0;i<n-1;i++){
while(j<n&&a[j]-a[i]<=5)
{
j++;
max = Math.max(max, j - i);
}
}
pw.print(max);
pw.close();
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
01bd861bd76a42b4d7a031154acb081a
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
//package CodeforcesProject;
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Input.input = new Input();
Output.output = new Output();
int quantity = Input.input.ReadInt();
int[] base = Input.input.ReadArrayInt(" ");
FastSort.SortWithoutReturn(base, quantity, 2);
int answer = 0, l, r;
for (int i = 0; i < quantity; i++) {
if (answer == 0) {
if (i + 1 < quantity && base[i + 1] - base[i] <= 5) {
answer++;
}
}
if (i + 1 < quantity) {
if (i + answer < quantity && (base[i + answer] - base[i]) <= 5) {
l = i + 1;
r = quantity;
while (l + 1 != r) {
int number = (l + r) / 2;
if (check(base, number, i)) {
l = number;
continue;
}
r = number;
}
if (base[l] - base[i] <= 5) {
answer = l - i;
}
}
}
}
System.out.print(answer + 1);
}
private static boolean check(int[] base, int number, int index) {
return base[number] - base[index] <= 5;
}
}
class method {
protected static int gcd(int a, int b) { // NOD
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static int lcm(int a, int b) { // NOK
return a / gcd(a, b) * b;
}
protected static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
protected static void exit() throws Exception {
Output.output.write.flush();
Input.input.read.close();
Output.output.write.close();
}
}
class graph {
private static int[][] base;
private static boolean[] used;
protected static int quantity = 0;
private static Integer[] pred;
protected static void start(int length) {
used = new boolean[length];
pred = new Integer[length];
}
protected static void RibMatrixToDefault(int length) throws Exception {
start(length);
base = new int[length][];
int FirstSize, SecondSize;
int[] rib, FirstArray, SecondArray, NewFirstArray, NewSecondArray;
for (int i = 0; i < length; i++) {
rib = Arrays.stream(Input.input.ReadArrayInt(" ")).map(element -> element - 1).toArray();
FirstArray = base[rib[0]];
SecondArray = base[rib[1]];
if (FirstArray == null) {
FirstSize = 0;
} else {
FirstSize = FirstArray.length;
}
if (SecondArray == null) {
SecondSize = 0;
} else {
SecondSize = SecondArray.length;
}
NewFirstArray = new int[FirstSize + 2];
NewSecondArray = new int[SecondSize + 2];
for (int index = 0; index < Math.max(FirstSize, SecondSize); index++) {
if (index < FirstSize) {
NewFirstArray[index] = FirstArray[index];
}
if (index < SecondSize) {
NewSecondArray[index] = SecondArray[index];
}
}
NewFirstArray[FirstSize] = rib[1];
NewSecondArray[SecondSize] = rib[0];
NewFirstArray[FirstSize + 1] = 1;
NewSecondArray[SecondSize + 1] = 1;
base[rib[0]] = NewFirstArray;
base[rib[1]] = NewSecondArray;
}
}
protected static void AdjacencyMatrixToDefault(int length, int dont) throws Exception {
start(length);
base = new int[length][];
List<Integer> buffer = new ArrayList<>();
int[] InputArray;
for (int i = 0; i < length; i++) {
InputArray = Input.input.ReadArrayInt(" ");
for (int index = 0; index < length; index++) {
if (i != index && InputArray[index] != dont) {
buffer.add(index);
buffer.add(InputArray[index]);
}
}
base[i] = buffer.stream().mapToInt(element -> element).toArray();
buffer.clear();
}
}
protected static void dfs(int position) throws Exception {
used[position] = true;
quantity++;
int next;
for (int index = 0; index < base[position].length; index += 2) {
next = base[position][index];
if (!used[next]) {
pred[next] = position;
dfs(next);
} else {
if (next != pred[position]) { // if cycle
throw new Exception();
}
}
}
}
protected static int dijkstra(int start, int stop, int size) {
start--;
stop--;
int[] dist = new int[size];
for (int i = 0; i < size; i++) {
if (i != start) {
dist[i] = Integer.MAX_VALUE;
}
pred[i] = start;
}
Queue<int[]> queue = new PriorityQueue<>((int[] first, int[] second) -> Integer.compare(first[1], second[1]));
queue.add(new int[]{start, 0});
int position;
int[] GetQueue;
while (queue.size() != 0) {
GetQueue = queue.poll();
position = GetQueue[0];
if (GetQueue[1] > dist[position]) {
continue;
}
for (int index = 0; index < base[position].length; index += 2) {
if (dist[position] + base[position][index + 1] < dist[base[position][index]] && !used[base[position][index]]) {
dist[base[position][index]] = dist[position] + base[position][index + 1];
pred[base[position][index]] = position;
queue.add(new int[]{base[position][index], dist[base[position][index]]});
}
}
used[position] = true;
}
return dist[stop] == Integer.MAX_VALUE ? -1 : dist[stop];
}
protected static boolean FloydWarshall(int[][] base, int length, int dont) {
for (int k = 0; k < length; k++) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (base[i][k] == dont || base[k][j] == dont) {
continue;
}
int total = base[i][k] + base[k][j];
if (base[i][j] != dont) {
base[i][j] = Math.min(base[i][j], total);
} else {
base[i][j] = total;
}
}
}
}
for (int index = 0; index < length; index++) {
if (base[index][index] != 0) { // if cycle
return false;
}
}
return true;
}
}
class FastSort {
protected static void SortWithoutReturn(int[] array, int length, int ShellHeapMergeMyInsertionSort) {
sort(array, ShellHeapMergeMyInsertionSort, length);
}
protected static int[] SortWithReturn(int[] array, int length, int ShellHeapMergeMyInsertionSort) {
sort(array, ShellHeapMergeMyInsertionSort, length);
return array;
}
private static void sort(int[] array, int ShellHeapMergeMyInsertionSort, int length) {
if (ShellHeapMergeMyInsertionSort < 0 || ShellHeapMergeMyInsertionSort > 4) {
Random random = new Random();
ShellHeapMergeMyInsertionSort = random.nextInt(4);
}
if (ShellHeapMergeMyInsertionSort == 0) {
ShellSort(array);
} else if (ShellHeapMergeMyInsertionSort == 1) {
HeapSort(array);
} else if (ShellHeapMergeMyInsertionSort == 2) {
MergeSort(array, 0, length - 1);
} else if (ShellHeapMergeMyInsertionSort == 3) {
StraightMergeSort(array, length);
} else if (ShellHeapMergeMyInsertionSort == 4) {
insertionSort(array);
}
}
private static void StraightMergeSort(int[] array, int size) {
if (size == 0) {
return;
}
int length = (size / 2) + ((size % 2) == 0 ? 0 : 1);
Integer[][] ZeroBuffer = new Integer[length + length % 2][2];
Integer[][] FirstBuffer = new Integer[0][0];
for (int index = 0; index < length; index++) {
int ArrayIndex = index * 2;
int NextArrayIndex = index * 2 + 1;
if (NextArrayIndex < size) {
if (array[ArrayIndex] > array[NextArrayIndex]) {
ZeroBuffer[index][0] = array[NextArrayIndex];
ZeroBuffer[index][1] = array[ArrayIndex];
} else {
ZeroBuffer[index][0] = array[ArrayIndex];
ZeroBuffer[index][1] = array[NextArrayIndex];
}
} else {
ZeroBuffer[index][0] = array[ArrayIndex];
}
}
boolean position = false;
int pointer0, pointer, pointer1, number = 4, NewPointer, count;
Integer[][] NewBuffer;
Integer[][] OldBuffer;
length = (size / 4) + ((size % 4) == 0 ? 0 : 1);
while (true) {
pointer0 = 0;
count = (number / 2) - 1;
if (!position) {
FirstBuffer = new Integer[length + length % 2][number];
NewBuffer = FirstBuffer;
OldBuffer = ZeroBuffer;
} else {
ZeroBuffer = new Integer[length + length % 2][number];
NewBuffer = ZeroBuffer;
OldBuffer = FirstBuffer;
}
for (int i = 0; i < length; i++) {
pointer = 0;
pointer1 = 0;
NewPointer = pointer0 + 1;
if (length == 1) {
for (int g = 0; g < size; g++) {
if (pointer > count || OldBuffer[pointer0][pointer] == null) {
array[g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) {
if (OldBuffer[pointer0][pointer] == null) {
continue;
}
array[g] = OldBuffer[pointer0][pointer];
pointer++;
} else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) {
array[g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else {
array[g] = OldBuffer[pointer0][pointer];
pointer++;
}
}
return;
}
for (int g = 0; g < number; g++) {
if (pointer > count || OldBuffer[pointer0][pointer] == null) {
if (OldBuffer[NewPointer][pointer1] == null) {
continue;
}
NewBuffer[i][g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) {
if (OldBuffer[pointer0][pointer] == null) {
continue;
}
NewBuffer[i][g] = OldBuffer[pointer0][pointer];
pointer++;
} else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) {
NewBuffer[i][g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else {
NewBuffer[i][g] = OldBuffer[pointer0][pointer];
pointer++;
}
}
pointer0 += 2;
}
position = !position;
length = length / 2 + length % 2;
number *= 2;
}
}
private static void ShellSort(int[] array) {
int j;
for (int gap = array.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < array.length; i++) {
int temp = array[i];
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) {
array[j] = array[j - gap];
}
array[j] = temp;
}
}
}
private static void HeapSort(int[] array) {
for (int i = array.length / 2 - 1; i >= 0; i--)
shiftDown(array, i, array.length);
for (int i = array.length - 1; i > 0; i--) {
swap(array, 0, i);
shiftDown(array, 0, i);
}
}
private static void shiftDown(int[] array, int i, int n) {
int child;
int tmp;
for (tmp = array[i]; leftChild(i) < n; i = child) {
child = leftChild(i);
if (child != n - 1 && (array[child] < array[child + 1]))
child++;
if (tmp < array[child])
array[i] = array[child];
else
break;
}
array[i] = tmp;
}
private static int leftChild(int i) {
return 2 * i + 1;
}
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void MergeSort(int[] array, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
MergeSort(array, low, mid);
MergeSort(array, mid + 1, high);
merge(array, low, mid, high);
}
}
private static void merge(int[] array, int low, int mid, int high) {
int n = high - low + 1;
int[] Temp = new int[n];
int i = low, j = mid + 1;
int k = 0;
while (i <= mid || j <= high) {
if (i > mid)
Temp[k++] = array[j++];
else if (j > high)
Temp[k++] = array[i++];
else if (array[i] < array[j])
Temp[k++] = array[i++];
else
Temp[k++] = array[j++];
}
for (j = 0; j < n; j++)
array[low + j] = Temp[j];
}
private static void insertionSort(int[] elements) {
for (int i = 1; i < elements.length; i++) {
int key = elements[i];
int j = i - 1;
while (j >= 0 && key < elements[j]) {
elements[j + 1] = elements[j];
j--;
}
elements[j + 1] = key;
}
}
}
class Input {
protected BufferedReader read;
protected static boolean FileInput = false;
protected static Input input;
Input() {
try {
read = new BufferedReader(FileInput ? new FileReader("input.txt") : new InputStreamReader(System.in));
} catch (Exception error) {
}
}
protected int ReadInt() throws Exception {
return Integer.parseInt(read.readLine());
}
protected long ReadLong() throws Exception {
return Long.parseLong(read.readLine());
}
protected String ReadString() throws Exception {
return read.readLine();
}
protected int[] ReadArrayInt(String split) throws Exception {
return Arrays.stream(read.readLine().split(split)).mapToInt(Integer::parseInt).toArray();
}
protected long[] ReadArrayLong(String split) throws Exception {
return Arrays.stream(read.readLine().split(split)).mapToLong(Long::parseLong).toArray();
}
protected String[] ReadArrayString(String split) throws Exception {
return read.readLine().split(split);
}
}
class Output {
protected BufferedWriter write;
protected static boolean FileOutput = false;
protected static Output output;
Output() {
try {
write = new BufferedWriter(FileOutput ? new FileWriter("output.txt") : new OutputStreamWriter(System.out));
} catch (Exception error) {
}
}
protected void WriteArray(int[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Integer.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteArray(long[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Long.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
public void WriteArray(String[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(array[index]);
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteArray(boolean[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Boolean.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteInt(int number, String split) {
try {
write.write(Integer.toString(number));
write.write(split);
} catch (Exception error) {
}
}
protected void WriteString(String word, String split) {
try {
write.write(word);
write.write(split);
} catch (Exception error) {
}
}
protected void WriteLong(Long number, String split) {
try {
write.write(Long.toString(number));
write.write(split);
} catch (Exception error) {
}
}
protected void WriteEnter() {
try {
write.newLine();
} catch (Exception e) {
}
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
18cb78da239cb75959d24e2be4fed9bd
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
//package CodeforcesProject;
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Input.input = new Input();
Output.output = new Output();
int quantity = Input.input.ReadInt();
int[] base = Input.input.ReadArrayInt(" ");
FastSort.SortWithoutReturn(base, quantity, 0);
int answer = 0, l, r;
for (int i = 0; i < quantity; i++) {
if (answer == 0) {
if (i + 1 < quantity && base[i + 1] - base[i] <= 5) {
answer++;
}
}
if (i + 1 < quantity) {
if (i + answer < quantity && (base[i + answer] - base[i]) <= 5) {
l = i + 1;
r = quantity;
while (l + 1 != r) {
int number = (l + r) / 2;
if (check(base, number, i)) {
l = number;
continue;
}
r = number;
}
if (base[l] - base[i] <= 5) {
answer = l - i;
}
}
}
}
System.out.print(answer + 1);
}
private static boolean check(int[] base, int number, int index) {
return base[number] - base[index] <= 5;
}
}
class method {
protected static int gcd(int a, int b) { // NOD
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static int lcm(int a, int b) { // NOK
return a / gcd(a, b) * b;
}
protected static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
protected static void exit() throws Exception {
Output.output.write.flush();
Input.input.read.close();
Output.output.write.close();
}
}
class graph {
private static int[][] base;
private static boolean[] used;
protected static int quantity = 0;
private static Integer[] pred;
protected static void start(int length) {
used = new boolean[length];
pred = new Integer[length];
}
protected static void RibMatrixToDefault(int length) throws Exception {
start(length);
base = new int[length][];
int FirstSize, SecondSize;
int[] rib, FirstArray, SecondArray, NewFirstArray, NewSecondArray;
for (int i = 0; i < length; i++) {
rib = Arrays.stream(Input.input.ReadArrayInt(" ")).map(element -> element - 1).toArray();
FirstArray = base[rib[0]];
SecondArray = base[rib[1]];
if (FirstArray == null) {
FirstSize = 0;
} else {
FirstSize = FirstArray.length;
}
if (SecondArray == null) {
SecondSize = 0;
} else {
SecondSize = SecondArray.length;
}
NewFirstArray = new int[FirstSize + 2];
NewSecondArray = new int[SecondSize + 2];
for (int index = 0; index < Math.max(FirstSize, SecondSize); index++) {
if (index < FirstSize) {
NewFirstArray[index] = FirstArray[index];
}
if (index < SecondSize) {
NewSecondArray[index] = SecondArray[index];
}
}
NewFirstArray[FirstSize] = rib[1];
NewSecondArray[SecondSize] = rib[0];
NewFirstArray[FirstSize + 1] = 1;
NewSecondArray[SecondSize + 1] = 1;
base[rib[0]] = NewFirstArray;
base[rib[1]] = NewSecondArray;
}
}
protected static void AdjacencyMatrixToDefault(int length, int dont) throws Exception {
start(length);
base = new int[length][];
List<Integer> buffer = new ArrayList<>();
int[] InputArray;
for (int i = 0; i < length; i++) {
InputArray = Input.input.ReadArrayInt(" ");
for (int index = 0; index < length; index++) {
if (i != index && InputArray[index] != dont) {
buffer.add(index);
buffer.add(InputArray[index]);
}
}
base[i] = buffer.stream().mapToInt(element -> element).toArray();
buffer.clear();
}
}
protected static void dfs(int position) throws Exception {
used[position] = true;
quantity++;
int next;
for (int index = 0; index < base[position].length; index += 2) {
next = base[position][index];
if (!used[next]) {
pred[next] = position;
dfs(next);
} else {
if (next != pred[position]) { // if cycle
throw new Exception();
}
}
}
}
protected static int dijkstra(int start, int stop, int size) {
start--;
stop--;
int[] dist = new int[size];
for (int i = 0; i < size; i++) {
if (i != start) {
dist[i] = Integer.MAX_VALUE;
}
pred[i] = start;
}
Queue<int[]> queue = new PriorityQueue<>((int[] first, int[] second) -> Integer.compare(first[1], second[1]));
queue.add(new int[]{start, 0});
int position;
int[] GetQueue;
while (queue.size() != 0) {
GetQueue = queue.poll();
position = GetQueue[0];
if (GetQueue[1] > dist[position]) {
continue;
}
for (int index = 0; index < base[position].length; index += 2) {
if (dist[position] + base[position][index + 1] < dist[base[position][index]] && !used[base[position][index]]) {
dist[base[position][index]] = dist[position] + base[position][index + 1];
pred[base[position][index]] = position;
queue.add(new int[]{base[position][index], dist[base[position][index]]});
}
}
used[position] = true;
}
return dist[stop] == Integer.MAX_VALUE ? -1 : dist[stop];
}
protected static boolean FloydWarshall(int[][] base, int length, int dont) {
for (int k = 0; k < length; k++) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (base[i][k] == dont || base[k][j] == dont) {
continue;
}
int total = base[i][k] + base[k][j];
if (base[i][j] != dont) {
base[i][j] = Math.min(base[i][j], total);
} else {
base[i][j] = total;
}
}
}
}
for (int index = 0; index < length; index++) {
if (base[index][index] != 0) { // if cycle
return false;
}
}
return true;
}
}
class FastSort {
protected static void SortWithoutReturn(int[] array, int length, int ShellHeapMergeMyInsertionSort) {
sort(array, ShellHeapMergeMyInsertionSort, length);
}
protected static int[] SortWithReturn(int[] array, int length, int ShellHeapMergeMyInsertionSort) {
sort(array, ShellHeapMergeMyInsertionSort, length);
return array;
}
private static void sort(int[] array, int ShellHeapMergeMyInsertionSort, int length) {
if (ShellHeapMergeMyInsertionSort < 0 || ShellHeapMergeMyInsertionSort > 4) {
Random random = new Random();
ShellHeapMergeMyInsertionSort = random.nextInt(4);
}
if (ShellHeapMergeMyInsertionSort == 0) {
ShellSort(array);
} else if (ShellHeapMergeMyInsertionSort == 1) {
HeapSort(array);
} else if (ShellHeapMergeMyInsertionSort == 2) {
MergeSort(array, 0, length - 1);
} else if (ShellHeapMergeMyInsertionSort == 3) {
StraightMergeSort(array, length);
} else if (ShellHeapMergeMyInsertionSort == 4) {
insertionSort(array);
}
}
private static void StraightMergeSort(int[] array, int size) {
if (size == 0) {
return;
}
int length = (size / 2) + ((size % 2) == 0 ? 0 : 1);
Integer[][] ZeroBuffer = new Integer[length + length % 2][2];
Integer[][] FirstBuffer = new Integer[0][0];
for (int index = 0; index < length; index++) {
int ArrayIndex = index * 2;
int NextArrayIndex = index * 2 + 1;
if (NextArrayIndex < size) {
if (array[ArrayIndex] > array[NextArrayIndex]) {
ZeroBuffer[index][0] = array[NextArrayIndex];
ZeroBuffer[index][1] = array[ArrayIndex];
} else {
ZeroBuffer[index][0] = array[ArrayIndex];
ZeroBuffer[index][1] = array[NextArrayIndex];
}
} else {
ZeroBuffer[index][0] = array[ArrayIndex];
}
}
boolean position = false;
int pointer0, pointer, pointer1, number = 4, NewPointer, count;
Integer[][] NewBuffer;
Integer[][] OldBuffer;
length = (size / 4) + ((size % 4) == 0 ? 0 : 1);
while (true) {
pointer0 = 0;
count = (number / 2) - 1;
if (!position) {
FirstBuffer = new Integer[length + length % 2][number];
NewBuffer = FirstBuffer;
OldBuffer = ZeroBuffer;
} else {
ZeroBuffer = new Integer[length + length % 2][number];
NewBuffer = ZeroBuffer;
OldBuffer = FirstBuffer;
}
for (int i = 0; i < length; i++) {
pointer = 0;
pointer1 = 0;
NewPointer = pointer0 + 1;
if (length == 1) {
for (int g = 0; g < size; g++) {
if (pointer > count || OldBuffer[pointer0][pointer] == null) {
array[g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) {
if (OldBuffer[pointer0][pointer] == null) {
continue;
}
array[g] = OldBuffer[pointer0][pointer];
pointer++;
} else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) {
array[g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else {
array[g] = OldBuffer[pointer0][pointer];
pointer++;
}
}
return;
}
for (int g = 0; g < number; g++) {
if (pointer > count || OldBuffer[pointer0][pointer] == null) {
if (OldBuffer[NewPointer][pointer1] == null) {
continue;
}
NewBuffer[i][g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) {
if (OldBuffer[pointer0][pointer] == null) {
continue;
}
NewBuffer[i][g] = OldBuffer[pointer0][pointer];
pointer++;
} else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) {
NewBuffer[i][g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else {
NewBuffer[i][g] = OldBuffer[pointer0][pointer];
pointer++;
}
}
pointer0 += 2;
}
position = !position;
length = length / 2 + length % 2;
number *= 2;
}
}
private static void ShellSort(int[] array) {
int j;
for (int gap = array.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < array.length; i++) {
int temp = array[i];
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) {
array[j] = array[j - gap];
}
array[j] = temp;
}
}
}
private static void HeapSort(int[] array) {
for (int i = array.length / 2 - 1; i >= 0; i--)
shiftDown(array, i, array.length);
for (int i = array.length - 1; i > 0; i--) {
swap(array, 0, i);
shiftDown(array, 0, i);
}
}
private static void shiftDown(int[] array, int i, int n) {
int child;
int tmp;
for (tmp = array[i]; leftChild(i) < n; i = child) {
child = leftChild(i);
if (child != n - 1 && (array[child] < array[child + 1]))
child++;
if (tmp < array[child])
array[i] = array[child];
else
break;
}
array[i] = tmp;
}
private static int leftChild(int i) {
return 2 * i + 1;
}
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void MergeSort(int[] array, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
MergeSort(array, low, mid);
MergeSort(array, mid + 1, high);
merge(array, low, mid, high);
}
}
private static void merge(int[] array, int low, int mid, int high) {
int n = high - low + 1;
int[] Temp = new int[n];
int i = low, j = mid + 1;
int k = 0;
while (i <= mid || j <= high) {
if (i > mid)
Temp[k++] = array[j++];
else if (j > high)
Temp[k++] = array[i++];
else if (array[i] < array[j])
Temp[k++] = array[i++];
else
Temp[k++] = array[j++];
}
for (j = 0; j < n; j++)
array[low + j] = Temp[j];
}
private static void insertionSort(int[] elements) {
for (int i = 1; i < elements.length; i++) {
int key = elements[i];
int j = i - 1;
while (j >= 0 && key < elements[j]) {
elements[j + 1] = elements[j];
j--;
}
elements[j + 1] = key;
}
}
}
class Input {
protected BufferedReader read;
protected static boolean FileInput = false;
protected static Input input;
Input() {
try {
read = new BufferedReader(FileInput ? new FileReader("input.txt") : new InputStreamReader(System.in));
} catch (Exception error) {
}
}
protected int ReadInt() throws Exception {
return Integer.parseInt(read.readLine());
}
protected long ReadLong() throws Exception {
return Long.parseLong(read.readLine());
}
protected String ReadString() throws Exception {
return read.readLine();
}
protected int[] ReadArrayInt(String split) throws Exception {
return Arrays.stream(read.readLine().split(split)).mapToInt(Integer::parseInt).toArray();
}
protected long[] ReadArrayLong(String split) throws Exception {
return Arrays.stream(read.readLine().split(split)).mapToLong(Long::parseLong).toArray();
}
protected String[] ReadArrayString(String split) throws Exception {
return read.readLine().split(split);
}
}
class Output {
protected BufferedWriter write;
protected static boolean FileOutput = false;
protected static Output output;
Output() {
try {
write = new BufferedWriter(FileOutput ? new FileWriter("output.txt") : new OutputStreamWriter(System.out));
} catch (Exception error) {
}
}
protected void WriteArray(int[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Integer.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteArray(long[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Long.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
public void WriteArray(String[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(array[index]);
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteArray(boolean[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Boolean.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteInt(int number, String split) {
try {
write.write(Integer.toString(number));
write.write(split);
} catch (Exception error) {
}
}
protected void WriteString(String word, String split) {
try {
write.write(word);
write.write(split);
} catch (Exception error) {
}
}
protected void WriteLong(Long number, String split) {
try {
write.write(Long.toString(number));
write.write(split);
} catch (Exception error) {
}
}
protected void WriteEnter() {
try {
write.newLine();
} catch (Exception e) {
}
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
1479f833780f79e5ec20cb72ca1ff808
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
//package CodeforcesProject;
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Input.input = new Input();
Output.output = new Output();
int quantity = Input.input.ReadInt();
int[] base = Input.input.ReadArrayInt(" ");
FastSort.SortWithoutReturn(base, quantity, 3);
int answer = 0, l, r;
for (int i = 0; i < quantity; i++) {
if (answer == 0) {
if (i + 1 < quantity && base[i + 1] - base[i] <= 5) {
answer++;
}
}
if (i + 1 < quantity) {
if (i + answer < quantity && (base[i + answer] - base[i]) <= 5) {
l = i + 1;
r = quantity;
while (l + 1 != r) {
int number = (l + r) / 2;
if (check(base, number, i)) {
l = number;
continue;
}
r = number;
}
if (base[l] - base[i] <= 5) {
answer = l - i;
}
}
}
}
System.out.print(answer + 1);
}
private static boolean check(int[] base, int number, int index) {
return base[number] - base[index] <= 5;
}
}
class method {
protected static int gcd(int a, int b) { // NOD
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static int lcm(int a, int b) { // NOK
return a / gcd(a, b) * b;
}
protected static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
protected static void exit() throws Exception {
Output.output.write.flush();
Input.input.read.close();
Output.output.write.close();
}
}
class graph {
private static int[][] base;
private static boolean[] used;
protected static int quantity = 0;
private static Integer[] pred;
protected static void start(int length) {
used = new boolean[length];
pred = new Integer[length];
}
protected static void RibMatrixToDefault(int length) throws Exception {
start(length);
base = new int[length][];
int FirstSize, SecondSize;
int[] rib, FirstArray, SecondArray, NewFirstArray, NewSecondArray;
for (int i = 0; i < length; i++) {
rib = Arrays.stream(Input.input.ReadArrayInt(" ")).map(element -> element - 1).toArray();
FirstArray = base[rib[0]];
SecondArray = base[rib[1]];
if (FirstArray == null) {
FirstSize = 0;
} else {
FirstSize = FirstArray.length;
}
if (SecondArray == null) {
SecondSize = 0;
} else {
SecondSize = SecondArray.length;
}
NewFirstArray = new int[FirstSize + 2];
NewSecondArray = new int[SecondSize + 2];
for (int index = 0; index < Math.max(FirstSize, SecondSize); index++) {
if (index < FirstSize) {
NewFirstArray[index] = FirstArray[index];
}
if (index < SecondSize) {
NewSecondArray[index] = SecondArray[index];
}
}
NewFirstArray[FirstSize] = rib[1];
NewSecondArray[SecondSize] = rib[0];
NewFirstArray[FirstSize + 1] = 1;
NewSecondArray[SecondSize + 1] = 1;
base[rib[0]] = NewFirstArray;
base[rib[1]] = NewSecondArray;
}
}
protected static void AdjacencyMatrixToDefault(int length, int dont) throws Exception {
start(length);
base = new int[length][];
List<Integer> buffer = new ArrayList<>();
int[] InputArray;
for (int i = 0; i < length; i++) {
InputArray = Input.input.ReadArrayInt(" ");
for (int index = 0; index < length; index++) {
if (i != index && InputArray[index] != dont) {
buffer.add(index);
buffer.add(InputArray[index]);
}
}
base[i] = buffer.stream().mapToInt(element -> element).toArray();
buffer.clear();
}
}
protected static void dfs(int position) throws Exception {
used[position] = true;
quantity++;
int next;
for (int index = 0; index < base[position].length; index += 2) {
next = base[position][index];
if (!used[next]) {
pred[next] = position;
dfs(next);
} else {
if (next != pred[position]) { // if cycle
throw new Exception();
}
}
}
}
protected static int dijkstra(int start, int stop, int size) {
start--;
stop--;
int[] dist = new int[size];
for (int i = 0; i < size; i++) {
if (i != start) {
dist[i] = Integer.MAX_VALUE;
}
pred[i] = start;
}
Queue<int[]> queue = new PriorityQueue<>((int[] first, int[] second) -> Integer.compare(first[1], second[1]));
queue.add(new int[]{start, 0});
int position;
int[] GetQueue;
while (queue.size() != 0) {
GetQueue = queue.poll();
position = GetQueue[0];
if (GetQueue[1] > dist[position]) {
continue;
}
for (int index = 0; index < base[position].length; index += 2) {
if (dist[position] + base[position][index + 1] < dist[base[position][index]] && !used[base[position][index]]) {
dist[base[position][index]] = dist[position] + base[position][index + 1];
pred[base[position][index]] = position;
queue.add(new int[]{base[position][index], dist[base[position][index]]});
}
}
used[position] = true;
}
return dist[stop] == Integer.MAX_VALUE ? -1 : dist[stop];
}
protected static boolean FloydWarshall(int[][] base, int length, int dont) {
for (int k = 0; k < length; k++) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (base[i][k] == dont || base[k][j] == dont) {
continue;
}
int total = base[i][k] + base[k][j];
if (base[i][j] != dont) {
base[i][j] = Math.min(base[i][j], total);
} else {
base[i][j] = total;
}
}
}
}
for (int index = 0; index < length; index++) {
if (base[index][index] != 0) { // if cycle
return false;
}
}
return true;
}
}
class FastSort {
protected static void SortWithoutReturn(int[] array, int length, int ShellHeapMergeMyInsertionSort) {
sort(array, ShellHeapMergeMyInsertionSort, length);
}
protected static int[] SortWithReturn(int[] array, int length, int ShellHeapMergeMyInsertionSort) {
sort(array, ShellHeapMergeMyInsertionSort, length);
return array;
}
private static void sort(int[] array, int ShellHeapMergeMyInsertionSort, int length) {
if (ShellHeapMergeMyInsertionSort < 0 || ShellHeapMergeMyInsertionSort > 4) {
Random random = new Random();
ShellHeapMergeMyInsertionSort = random.nextInt(4);
}
if (ShellHeapMergeMyInsertionSort == 0) {
ShellSort(array);
} else if (ShellHeapMergeMyInsertionSort == 1) {
HeapSort(array);
} else if (ShellHeapMergeMyInsertionSort == 2) {
MergeSort(array, 0, length - 1);
} else if (ShellHeapMergeMyInsertionSort == 3) {
StraightMergeSort(array, length);
} else if (ShellHeapMergeMyInsertionSort == 4) {
insertionSort(array);
}
}
private static void StraightMergeSort(int[] array, int size) {
if (size == 0) {
return;
}
int length = (size / 2) + ((size % 2) == 0 ? 0 : 1);
Integer[][] ZeroBuffer = new Integer[length + length % 2][2];
Integer[][] FirstBuffer = new Integer[0][0];
for (int index = 0; index < length; index++) {
int ArrayIndex = index * 2;
int NextArrayIndex = index * 2 + 1;
if (NextArrayIndex < size) {
if (array[ArrayIndex] > array[NextArrayIndex]) {
ZeroBuffer[index][0] = array[NextArrayIndex];
ZeroBuffer[index][1] = array[ArrayIndex];
} else {
ZeroBuffer[index][0] = array[ArrayIndex];
ZeroBuffer[index][1] = array[NextArrayIndex];
}
} else {
ZeroBuffer[index][0] = array[ArrayIndex];
}
}
boolean position = false;
int pointer0, pointer, pointer1, number = 4, NewPointer, count;
Integer[][] NewBuffer;
Integer[][] OldBuffer;
length = (size / 4) + ((size % 4) == 0 ? 0 : 1);
while (true) {
pointer0 = 0;
count = (number / 2) - 1;
if (!position) {
FirstBuffer = new Integer[length + length % 2][number];
NewBuffer = FirstBuffer;
OldBuffer = ZeroBuffer;
} else {
ZeroBuffer = new Integer[length + length % 2][number];
NewBuffer = ZeroBuffer;
OldBuffer = FirstBuffer;
}
for (int i = 0; i < length; i++) {
pointer = 0;
pointer1 = 0;
NewPointer = pointer0 + 1;
if (length == 1) {
for (int g = 0; g < size; g++) {
if (pointer > count || OldBuffer[pointer0][pointer] == null) {
array[g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) {
if (OldBuffer[pointer0][pointer] == null) {
continue;
}
array[g] = OldBuffer[pointer0][pointer];
pointer++;
} else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) {
array[g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else {
array[g] = OldBuffer[pointer0][pointer];
pointer++;
}
}
return;
}
for (int g = 0; g < number; g++) {
if (pointer > count || OldBuffer[pointer0][pointer] == null) {
if (OldBuffer[NewPointer][pointer1] == null) {
continue;
}
NewBuffer[i][g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) {
if (OldBuffer[pointer0][pointer] == null) {
continue;
}
NewBuffer[i][g] = OldBuffer[pointer0][pointer];
pointer++;
} else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) {
NewBuffer[i][g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else {
NewBuffer[i][g] = OldBuffer[pointer0][pointer];
pointer++;
}
}
pointer0 += 2;
}
position = !position;
length = length / 2 + length % 2;
number *= 2;
}
}
private static void ShellSort(int[] array) {
int j;
for (int gap = array.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < array.length; i++) {
int temp = array[i];
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) {
array[j] = array[j - gap];
}
array[j] = temp;
}
}
}
private static void HeapSort(int[] array) {
for (int i = array.length / 2 - 1; i >= 0; i--)
shiftDown(array, i, array.length);
for (int i = array.length - 1; i > 0; i--) {
swap(array, 0, i);
shiftDown(array, 0, i);
}
}
private static void shiftDown(int[] array, int i, int n) {
int child;
int tmp;
for (tmp = array[i]; leftChild(i) < n; i = child) {
child = leftChild(i);
if (child != n - 1 && (array[child] < array[child + 1]))
child++;
if (tmp < array[child])
array[i] = array[child];
else
break;
}
array[i] = tmp;
}
private static int leftChild(int i) {
return 2 * i + 1;
}
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void MergeSort(int[] array, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
MergeSort(array, low, mid);
MergeSort(array, mid + 1, high);
merge(array, low, mid, high);
}
}
private static void merge(int[] array, int low, int mid, int high) {
int n = high - low + 1;
int[] Temp = new int[n];
int i = low, j = mid + 1;
int k = 0;
while (i <= mid || j <= high) {
if (i > mid)
Temp[k++] = array[j++];
else if (j > high)
Temp[k++] = array[i++];
else if (array[i] < array[j])
Temp[k++] = array[i++];
else
Temp[k++] = array[j++];
}
for (j = 0; j < n; j++)
array[low + j] = Temp[j];
}
private static void insertionSort(int[] elements) {
for (int i = 1; i < elements.length; i++) {
int key = elements[i];
int j = i - 1;
while (j >= 0 && key < elements[j]) {
elements[j + 1] = elements[j];
j--;
}
elements[j + 1] = key;
}
}
}
class Input {
protected BufferedReader read;
protected static boolean FileInput = false;
protected static Input input;
Input() {
try {
read = new BufferedReader(FileInput ? new FileReader("input.txt") : new InputStreamReader(System.in));
} catch (Exception error) {
}
}
protected int ReadInt() throws Exception {
return Integer.parseInt(read.readLine());
}
protected long ReadLong() throws Exception {
return Long.parseLong(read.readLine());
}
protected String ReadString() throws Exception {
return read.readLine();
}
protected int[] ReadArrayInt(String split) throws Exception {
return Arrays.stream(read.readLine().split(split)).mapToInt(Integer::parseInt).toArray();
}
protected long[] ReadArrayLong(String split) throws Exception {
return Arrays.stream(read.readLine().split(split)).mapToLong(Long::parseLong).toArray();
}
protected String[] ReadArrayString(String split) throws Exception {
return read.readLine().split(split);
}
}
class Output {
protected BufferedWriter write;
protected static boolean FileOutput = false;
protected static Output output;
Output() {
try {
write = new BufferedWriter(FileOutput ? new FileWriter("output.txt") : new OutputStreamWriter(System.out));
} catch (Exception error) {
}
}
protected void WriteArray(int[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Integer.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteArray(long[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Long.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
public void WriteArray(String[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(array[index]);
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteArray(boolean[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Boolean.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteInt(int number, String split) {
try {
write.write(Integer.toString(number));
write.write(split);
} catch (Exception error) {
}
}
protected void WriteString(String word, String split) {
try {
write.write(word);
write.write(split);
} catch (Exception error) {
}
}
protected void WriteLong(Long number, String split) {
try {
write.write(Long.toString(number));
write.write(split);
} catch (Exception error) {
}
}
protected void WriteEnter() {
try {
write.newLine();
} catch (Exception e) {
}
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
c1bf934367a0fe041b71534313189a21
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
//package CodeforcesProject;
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Input.input = new Input();
Output.output = new Output();
int quantity = Input.input.ReadInt();
int[] base = Input.input.ReadArrayInt(" ");
FastSort.SortWithoutReturn(base, quantity, 1);
int answer = 0, l, r;
for (int i = 0; i < quantity; i++) {
if (answer == 0) {
if (i + 1 < quantity && base[i + 1] - base[i] <= 5) {
answer++;
}
}
if (i + 1 < quantity) {
if (i + answer < quantity && (base[i + answer] - base[i]) <= 5) {
l = i + 1;
r = quantity;
while (l + 1 != r) {
int number = (l + r) / 2;
if (check(base, number, i)) {
l = number;
continue;
}
r = number;
}
if (base[l] - base[i] <= 5) {
answer = l - i;
}
}
}
}
System.out.print(answer + 1);
}
private static boolean check(int[] base, int number, int index) {
return base[number] - base[index] <= 5;
}
}
class method {
protected static int gcd(int a, int b) { // NOD
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
protected static int lcm(int a, int b) { // NOK
return a / gcd(a, b) * b;
}
protected static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
protected static void exit() throws Exception {
Output.output.write.flush();
Input.input.read.close();
Output.output.write.close();
}
}
class graph {
private static int[][] base;
private static boolean[] used;
protected static int quantity = 0;
private static Integer[] pred;
protected static void start(int length) {
used = new boolean[length];
pred = new Integer[length];
}
protected static void RibMatrixToDefault(int length) throws Exception {
start(length);
base = new int[length][];
int FirstSize, SecondSize;
int[] rib, FirstArray, SecondArray, NewFirstArray, NewSecondArray;
for (int i = 0; i < length; i++) {
rib = Arrays.stream(Input.input.ReadArrayInt(" ")).map(element -> element - 1).toArray();
FirstArray = base[rib[0]];
SecondArray = base[rib[1]];
if (FirstArray == null) {
FirstSize = 0;
} else {
FirstSize = FirstArray.length;
}
if (SecondArray == null) {
SecondSize = 0;
} else {
SecondSize = SecondArray.length;
}
NewFirstArray = new int[FirstSize + 2];
NewSecondArray = new int[SecondSize + 2];
for (int index = 0; index < Math.max(FirstSize, SecondSize); index++) {
if (index < FirstSize) {
NewFirstArray[index] = FirstArray[index];
}
if (index < SecondSize) {
NewSecondArray[index] = SecondArray[index];
}
}
NewFirstArray[FirstSize] = rib[1];
NewSecondArray[SecondSize] = rib[0];
NewFirstArray[FirstSize + 1] = 1;
NewSecondArray[SecondSize + 1] = 1;
base[rib[0]] = NewFirstArray;
base[rib[1]] = NewSecondArray;
}
}
protected static void AdjacencyMatrixToDefault(int length, int dont) throws Exception {
start(length);
base = new int[length][];
List<Integer> buffer = new ArrayList<>();
int[] InputArray;
for (int i = 0; i < length; i++) {
InputArray = Input.input.ReadArrayInt(" ");
for (int index = 0; index < length; index++) {
if (i != index && InputArray[index] != dont) {
buffer.add(index);
buffer.add(InputArray[index]);
}
}
base[i] = buffer.stream().mapToInt(element -> element).toArray();
buffer.clear();
}
}
protected static void dfs(int position) throws Exception {
used[position] = true;
quantity++;
int next;
for (int index = 0; index < base[position].length; index += 2) {
next = base[position][index];
if (!used[next]) {
pred[next] = position;
dfs(next);
} else {
if (next != pred[position]) { // if cycle
throw new Exception();
}
}
}
}
protected static int dijkstra(int start, int stop, int size) {
start--;
stop--;
int[] dist = new int[size];
for (int i = 0; i < size; i++) {
if (i != start) {
dist[i] = Integer.MAX_VALUE;
}
pred[i] = start;
}
Queue<int[]> queue = new PriorityQueue<>((int[] first, int[] second) -> Integer.compare(first[1], second[1]));
queue.add(new int[]{start, 0});
int position;
int[] GetQueue;
while (queue.size() != 0) {
GetQueue = queue.poll();
position = GetQueue[0];
if (GetQueue[1] > dist[position]) {
continue;
}
for (int index = 0; index < base[position].length; index += 2) {
if (dist[position] + base[position][index + 1] < dist[base[position][index]] && !used[base[position][index]]) {
dist[base[position][index]] = dist[position] + base[position][index + 1];
pred[base[position][index]] = position;
queue.add(new int[]{base[position][index], dist[base[position][index]]});
}
}
used[position] = true;
}
return dist[stop] == Integer.MAX_VALUE ? -1 : dist[stop];
}
protected static boolean FloydWarshall(int[][] base, int length, int dont) {
for (int k = 0; k < length; k++) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (base[i][k] == dont || base[k][j] == dont) {
continue;
}
int total = base[i][k] + base[k][j];
if (base[i][j] != dont) {
base[i][j] = Math.min(base[i][j], total);
} else {
base[i][j] = total;
}
}
}
}
for (int index = 0; index < length; index++) {
if (base[index][index] != 0) { // if cycle
return false;
}
}
return true;
}
}
class FastSort {
protected static void SortWithoutReturn(int[] array, int length, int ShellHeapMergeMyInsertionSort) {
sort(array, ShellHeapMergeMyInsertionSort, length);
}
protected static int[] SortWithReturn(int[] array, int length, int ShellHeapMergeMyInsertionSort) {
sort(array, ShellHeapMergeMyInsertionSort, length);
return array;
}
private static void sort(int[] array, int ShellHeapMergeMyInsertionSort, int length) {
if (ShellHeapMergeMyInsertionSort < 0 || ShellHeapMergeMyInsertionSort > 4) {
Random random = new Random();
ShellHeapMergeMyInsertionSort = random.nextInt(4);
}
if (ShellHeapMergeMyInsertionSort == 0) {
ShellSort(array);
} else if (ShellHeapMergeMyInsertionSort == 1) {
HeapSort(array);
} else if (ShellHeapMergeMyInsertionSort == 2) {
MergeSort(array, 0, length - 1);
} else if (ShellHeapMergeMyInsertionSort == 3) {
StraightMergeSort(array, length);
} else if (ShellHeapMergeMyInsertionSort == 4) {
insertionSort(array);
}
}
private static void StraightMergeSort(int[] array, int size) {
if (size == 0) {
return;
}
int length = (size / 2) + ((size % 2) == 0 ? 0 : 1);
Integer[][] ZeroBuffer = new Integer[length + length % 2][2];
Integer[][] FirstBuffer = new Integer[0][0];
for (int index = 0; index < length; index++) {
int ArrayIndex = index * 2;
int NextArrayIndex = index * 2 + 1;
if (NextArrayIndex < size) {
if (array[ArrayIndex] > array[NextArrayIndex]) {
ZeroBuffer[index][0] = array[NextArrayIndex];
ZeroBuffer[index][1] = array[ArrayIndex];
} else {
ZeroBuffer[index][0] = array[ArrayIndex];
ZeroBuffer[index][1] = array[NextArrayIndex];
}
} else {
ZeroBuffer[index][0] = array[ArrayIndex];
}
}
boolean position = false;
int pointer0, pointer, pointer1, number = 4, NewPointer, count;
Integer[][] NewBuffer;
Integer[][] OldBuffer;
length = (size / 4) + ((size % 4) == 0 ? 0 : 1);
while (true) {
pointer0 = 0;
count = (number / 2) - 1;
if (!position) {
FirstBuffer = new Integer[length + length % 2][number];
NewBuffer = FirstBuffer;
OldBuffer = ZeroBuffer;
} else {
ZeroBuffer = new Integer[length + length % 2][number];
NewBuffer = ZeroBuffer;
OldBuffer = FirstBuffer;
}
for (int i = 0; i < length; i++) {
pointer = 0;
pointer1 = 0;
NewPointer = pointer0 + 1;
if (length == 1) {
for (int g = 0; g < size; g++) {
if (pointer > count || OldBuffer[pointer0][pointer] == null) {
array[g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) {
if (OldBuffer[pointer0][pointer] == null) {
continue;
}
array[g] = OldBuffer[pointer0][pointer];
pointer++;
} else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) {
array[g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else {
array[g] = OldBuffer[pointer0][pointer];
pointer++;
}
}
return;
}
for (int g = 0; g < number; g++) {
if (pointer > count || OldBuffer[pointer0][pointer] == null) {
if (OldBuffer[NewPointer][pointer1] == null) {
continue;
}
NewBuffer[i][g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) {
if (OldBuffer[pointer0][pointer] == null) {
continue;
}
NewBuffer[i][g] = OldBuffer[pointer0][pointer];
pointer++;
} else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) {
NewBuffer[i][g] = OldBuffer[NewPointer][pointer1];
pointer1++;
} else {
NewBuffer[i][g] = OldBuffer[pointer0][pointer];
pointer++;
}
}
pointer0 += 2;
}
position = !position;
length = length / 2 + length % 2;
number *= 2;
}
}
private static void ShellSort(int[] array) {
int j;
for (int gap = array.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < array.length; i++) {
int temp = array[i];
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) {
array[j] = array[j - gap];
}
array[j] = temp;
}
}
}
private static void HeapSort(int[] array) {
for (int i = array.length / 2 - 1; i >= 0; i--)
shiftDown(array, i, array.length);
for (int i = array.length - 1; i > 0; i--) {
swap(array, 0, i);
shiftDown(array, 0, i);
}
}
private static void shiftDown(int[] array, int i, int n) {
int child;
int tmp;
for (tmp = array[i]; leftChild(i) < n; i = child) {
child = leftChild(i);
if (child != n - 1 && (array[child] < array[child + 1]))
child++;
if (tmp < array[child])
array[i] = array[child];
else
break;
}
array[i] = tmp;
}
private static int leftChild(int i) {
return 2 * i + 1;
}
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void MergeSort(int[] array, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
MergeSort(array, low, mid);
MergeSort(array, mid + 1, high);
merge(array, low, mid, high);
}
}
private static void merge(int[] array, int low, int mid, int high) {
int n = high - low + 1;
int[] Temp = new int[n];
int i = low, j = mid + 1;
int k = 0;
while (i <= mid || j <= high) {
if (i > mid)
Temp[k++] = array[j++];
else if (j > high)
Temp[k++] = array[i++];
else if (array[i] < array[j])
Temp[k++] = array[i++];
else
Temp[k++] = array[j++];
}
for (j = 0; j < n; j++)
array[low + j] = Temp[j];
}
private static void insertionSort(int[] elements) {
for (int i = 1; i < elements.length; i++) {
int key = elements[i];
int j = i - 1;
while (j >= 0 && key < elements[j]) {
elements[j + 1] = elements[j];
j--;
}
elements[j + 1] = key;
}
}
}
class Input {
protected BufferedReader read;
protected static boolean FileInput = false;
protected static Input input;
Input() {
try {
read = new BufferedReader(FileInput ? new FileReader("input.txt") : new InputStreamReader(System.in));
} catch (Exception error) {
}
}
protected int ReadInt() throws Exception {
return Integer.parseInt(read.readLine());
}
protected long ReadLong() throws Exception {
return Long.parseLong(read.readLine());
}
protected String ReadString() throws Exception {
return read.readLine();
}
protected int[] ReadArrayInt(String split) throws Exception {
return Arrays.stream(read.readLine().split(split)).mapToInt(Integer::parseInt).toArray();
}
protected long[] ReadArrayLong(String split) throws Exception {
return Arrays.stream(read.readLine().split(split)).mapToLong(Long::parseLong).toArray();
}
protected String[] ReadArrayString(String split) throws Exception {
return read.readLine().split(split);
}
}
class Output {
protected BufferedWriter write;
protected static boolean FileOutput = false;
protected static Output output;
Output() {
try {
write = new BufferedWriter(FileOutput ? new FileWriter("output.txt") : new OutputStreamWriter(System.out));
} catch (Exception error) {
}
}
protected void WriteArray(int[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Integer.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteArray(long[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Long.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
public void WriteArray(String[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(array[index]);
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteArray(boolean[] array, String split) {
try {
int length = array.length;
for (int index = 0; index < length; index++) {
write.write(Boolean.toString(array[index]));
if (index + 1 != length) {
write.write(split);
}
}
} catch (Exception error) {
}
}
protected void WriteInt(int number, String split) {
try {
write.write(Integer.toString(number));
write.write(split);
} catch (Exception error) {
}
}
protected void WriteString(String word, String split) {
try {
write.write(word);
write.write(split);
} catch (Exception error) {
}
}
protected void WriteLong(Long number, String split) {
try {
write.write(Long.toString(number));
write.write(split);
} catch (Exception error) {
}
}
protected void WriteEnter() {
try {
write.newLine();
} catch (Exception e) {
}
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
5bf8806f7ed7b6bca13ae5a8a893be74
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class C544 {
public static void main(String[] args) {
FastScanner in = new FastScanner(System.in);
int N = in.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for(int i = 0; i < N; i++)
list.add(in.nextInt());
Collections.sort(list);
int[] arr = new int[N];
for(int i = 0; i < N; i++)
arr[i] = list.get(i);
Arrays.sort(arr);
int max = 1;
int prev = 0;
for(int p1 = 0; p1 < N; p1++) {
int p2 = prev;
while(p2 < N && arr[p2] <= arr[p1] + 5) {
p2++;
}
prev = p2;
max = Math.max(p2 - p1, max);
}
System.out.println(max);
}
/**
* Source: Matt Fontaine
*/
static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int chars;
public FastScanner(InputStream stream) {
this.stream = stream;
}
int read() {
if (chars == -1)
throw new InputMismatchException();
if (curChar >= chars) {
curChar = 0;
try {
chars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (chars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
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 String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
c078d7d63e67dea47c32cfabee7c6ea6
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main
{
static void swap(int arr[], int ind1, int ind2)
{
int temp = arr[ind1];
arr[ind1]= arr[ind2];
arr[ind2] = temp;
}
public static void main(String args[]) throws IOException
{
Scanner scan = new Scanner(System.in);
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
Random rand = new Random();
int n = Integer.parseInt(reader.readLine());
String input[] = reader.readLine().trim().split(" ");
int arr[] = new int[n];
for(int i=0;i<n;i++)
{
arr[i] = Integer.parseInt(input[i]);
}
if(n!=1)
{
for(int i=0;i<n;i++)
{
swap(arr, rand.nextInt(n-1), rand.nextInt(n-1));
}
}
Arrays.sort(arr);
int currSize = 0;
int prev = 0;
int ans =0;
for(int i=0;i<n;i++)
{
while(prev<n && arr[prev]-arr[i]<=5)
{
prev++;
ans = Math.max(ans, prev-i);
}
}
// System.out.println(Arrays.toString(arr));
System.out.println(ans);
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
3ac0b3dfff032f72d937e0d8f7e87d67
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.io.*;
import java.util.Arrays;
import java.math.*;
public class main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
long[] a = Arrays.stream(br.readLine().split(" ")).mapToLong(Long::parseLong).toArray();
heapsort(a);
int ans = 0;
int j = 0;
for (int i = 0; i < n; i++) {
while (j < n && a[j] - a[i] <= 5) {
++j;
ans = Math.max(ans, j - i);
}
}
wr.write(Integer.toString(ans));
br.close();
wr.close();
}
public static void heapsort(long[] a) {
for (int i = a.length / 2 - 1; i >= 0; i--)
// convert the array to a heap
shiftDown(a, i, a.length);
for (int i = a.length - 1; i > 0; i--) {
swap(a, 0, i); /* deleteMax */
shiftDown(a, 0, i);
}
} // end heapSort
private static void shiftDown(long[] a, int i, int n) {
int child;
long tmp;
for (tmp = a[i]; leftChild(i) < n; i = child) {
child = leftChild(i);
if (child != n - 1 && (a[child] < a[child + 1]))
child++;
if (tmp < a[child])
a[i] = a[child];
else
break;
}
a[i] = tmp;
}
private static int leftChild(int i) {
return 2 * i + 1;
}
// swap numbers
public static void swap(long[] numbers, int i, int j) {
long temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
fb6174f5d199abe34963a30903802686
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Main {
static InputReader scan = new InputReader(new BufferedInputStream(System.in));
static ArrayList<Integer> list = new ArrayList<>();
static int loss = 0;
public static void main(String[] args) {
int n = scan.nextInt();
for (int i = 0; i < n; i++) {
list.add(scan.nextInt());
}
Collections.sort(list);
int max = 1;
loss = list.get(0);
for (int i = 0; i < n; i++) {
if (max >= n - i)
break;
if(i>0&&list.get(i-1)==list.get(i)) {
continue;
}
int temp = fun(i);
if (temp > max) {
max = temp;
}
for( ;i+1<n&&Math.abs(list.get(i+1)-loss)>5;i++) {
}
}
System.out.println(max);
}
static int fun(int n) {
int num = list.get(n);
int count = 0;
for (int i = n ; i < list.size(); i++) {
if (Math.abs(list.get(i) - num) <= 5) {
count++;
} else {
// +++
loss = list.get(i);
break;
}
}
return count;
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
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());
}
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
bf494435ce50325b01e88b80b877a49c
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
static InputReader scan = new InputReader(new BufferedInputStream(System.in));
static Map<Integer, Integer> map = new HashMap<>();
public static void main(String[] args) {
int n = scan.nextInt();
for (int i = 0; i < n; i++) {
int temp = scan.nextInt();
if (map.containsKey(temp)) {
map.put(temp, map.get(temp) + 1);
} else {
map.put(temp, 1);
}
}
int max = 0;
for (Integer i : map.keySet()) {
int temp = fun(i);
if (max < temp) {
max = temp;
}
}
System.out.println(max);
}
static int fun(int i) {
int count = 0;
if (map.containsKey(i)) {
count = count + map.get(i);
}
if (map.containsKey(i + 1)) {
count = count + map.get(i + 1);
}
if (map.containsKey(i + 2)) {
count = count + map.get(i + 2);
}
if (map.containsKey(i + 3)) {
count = count + map.get(i + 3);
}
if (map.containsKey(i + 4)) {
count = count + map.get(i + 4);
}
if (map.containsKey(i + 5)) {
count = count + map.get(i + 5);
}
return count;
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
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());
}
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
e98cd73103804f5e3a3c3764831ea97b
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Aaaa
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
ArrayList<Integer> a=new ArrayList<>();
String line=br.readLine();
String str[]=line.trim().split("\\s+");
for(int i=0;i<n;i++)
{
int x=Integer.parseInt(str[i]);
a.add(x);
}
Collections.sort(a);
int j=0,ans=0;
for(int i=0;i<n;i++)
{
while(j<n&&(a.get(j)-a.get(i))<=5)
{
j++;
ans=Math.max(ans,j-i);
}
}
System.out.println(ans);
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
b2cd1d2d2751da1bcaee71c9e78975a9
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class aaaaaaaaaaaaaaaa {
public void run() throws Exception {
FastReader file = new FastReader();
int times = file.nextInt();
ArrayList<Long> n = new ArrayList();
for (int i = 0; i < times; i++) {
n.add(file.nextLong());
}
Collections.sort(n);
int max = 0;
for (int i= 0; i < times; i++) {
long cur = n.get(i);
int lo = i + 1, hi = times - 1;
int ind = 0;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (n.get(mid) <= cur + 5) {
ind = mid;
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
max = Math.max(max, ind - i + 1);
}
System.out.println(max);
}
public static void main(String[] args) throws Exception {
new aaaaaaaaaaaaaaaa().run();
}
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
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
bea8772579102871a8782a0e1ca31691
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
public class b
{ static int c=0;
public static int[] mergesort(int [] a,int[] b){
int i=0;
int j=0;
int h=0;
int k[] = new int[a.length+b.length];
while(i<a.length && j<b.length){
if(a[i]<=b[j]){
k[h]=a[i];
h=h+1;
i=i+1;
}
else{
k[h]=b[j];
c=c+a.length-i;
j=j+1;
h=h+1;
}
}
if(i<a.length){
while(i<a.length){
k[h]=a[i];
h=h+1;
i=i+1;
}
}
else{
while(j<b.length){
k[h]=b[j];
h=h+1;
j=j+1;
}
}
//System.out.println("c "+c);
return(k);
}
public static int[] merge(int low,int high,int[] arr1){
if(low==high ){
int[] x = new int[1];
x[0]=arr1[low];
return(x);
}
else if (high==low+1){
int[] x = new int[2];
if(arr1[low]>arr1[high]){
c=c+1;
x[0]=arr1[high];
x[1]=arr1[low];
}
else{
x[0]=arr1[low];
x[1]=arr1[high];
}
return(x);
}
else{
int mid = (int)(low+high)/2;
mid = mid-1;
int[] p = merge(low,mid,arr1);
int[] q = merge(mid+1,high,arr1);
int[] s = mergesort(p,q);
return(s);
}}
public static void main(String args[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n= Integer.parseInt(br.readLine());
String s1 = br.readLine();
StringTokenizer tok = new StringTokenizer(s1," ");
int a[]= new int[n];
long a1[]= new long[n];
long c=0;
int count=0;
int ans=0;
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(tok.nextToken());
}
int[] h = new int[n];
int[] g = merge(0,a.length-1,a);
int f=0;
int p=0;
int q=0;
while(p<n){
//q=p;
while(q<n && (g[q]-g[p])<=5){
q=q+1;
}
h[p]=q-p;
p=p+1;
}
int max=0;
max = h[0];
for(int l=0;l<n;l++){
if(max<h[l]){
max=h[l];
}
}
System.out.print(max);
}}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
961b5b68abd376b768838b3c8b43a332
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solve6 {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(System.out);
new Solve6().solve(pw);
pw.flush();
pw.close();
}
public void solve(PrintWriter pw) throws IOException {
FastReader sc = new FastReader();
int n = sc.nextInt();
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
int max = 0;
for (int i = 0; i < n; i++) {
max = Math.max(upperBound(a,0,n-1,a[i]+5) - i, max);
}
pw.println(max);
}
public static int upperBound(Integer[] array, int indexSt, int indexEn, int value) {
if (value >= array[indexEn]) {
return indexEn + 1;
}
int low = indexSt;
int high = indexEn;
while (low < high) {
final int mid = (low + high) / 2;
if (value >= array[mid]) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
static class FastReader {
StringTokenizer st;
BufferedReader br;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public boolean hasNext() throws IOException {
String s = br.readLine();
if (s == null || s.isEmpty()) {
return false;
}
st = new StringTokenizer(s);
return true;
}
public String next() throws IOException {
if (st == null || !st.hasMoreTokens()) {
String s = br.readLine();
if (s.isEmpty()) {
return null;
}
st = new StringTokenizer(s);
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
8c9949d5591886ccbc821944a1bb9e33
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Solve6 {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(System.out);
new Solve6().solve(pw);
pw.flush();
pw.close();
}
public void solve(PrintWriter pw) throws IOException {
FastReader sc = new FastReader();
int n = sc.nextInt();
LinkedList<Integer> a = new LinkedList();
HashMap<Integer, Integer> h = new HashMap();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
if (!h.containsKey(x)) {
a.add(x);
h.put(x, 1);
} else {
h.replace(x, h.get(x) + 1);
}
}
int max = 0;
for (Integer x : a) {
int s = 0;
for (int i = 0; i <= 5; i++) {
s += h.getOrDefault(x + i, 0);
}
max = Math.max(max, s);
}
pw.println(max);
}
static class FastReader {
StringTokenizer st;
BufferedReader br;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public boolean hasNext() throws IOException {
String s = br.readLine();
if (s == null || s.isEmpty()) {
return false;
}
st = new StringTokenizer(s);
return true;
}
public String next() throws IOException {
if (st == null || !st.hasMoreTokens()) {
String s = br.readLine();
if (s.isEmpty()) {
return null;
}
st = new StringTokenizer(s);
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
dcdd99941c546ac88d3a2c02cc01443c
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class BalancedTeam {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), max = 0, i_1=0, i_2=0;
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) {
a[i]=in.nextInt();
}
Arrays.sort(a);
while (i_2 < n) {
if (a[i_2]-a[i_1]<=5) {
i_2++;
} else {
max = Math.max(max, i_2-i_1);
i_1++;
}
}
if(a[i_2-1]-a[i_1] <= 5)
max=Math.max(max,i_2-i_1);
System.out.println(max);
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
f2ca233032228a41e94474b01759a32d
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class BalancedTeam {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), m=0, a=0, b=0;
Integer[] l = new Integer[n];
for (int i=0; i<n; i++)
l[i] = in.nextInt();
Arrays.sort(l);
while (b<n)
m = l[b] - l[a] > 5 ? Math.max(m, b-a++) : m+0*b++;
m = l[b-1] - l[a] <= 5 ? Math.max(m, b-a) : m;
System.out.println(m);
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
277ff70f3ad5e7f26cc96d2366a09b53
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class BalancedTeam {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt(), max=0, i_1=0, i_2=0;
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i]=in.nextInt();
Arrays.sort(a);
while (i_2 < n)
max = a[i_2]-a[i_1]<=5?max+i_2++-i_2+1:Math.max(max, i_2-i_1++);
max=a[i_2-1]-a[i_1] <= 5?Math.max(max,i_2-i_1):max;
System.out.println(max);
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
836f77bc4312e4d4478d6d8f512fa7dc
|
train_001.jsonl
|
1551971100
|
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.
|
256 megabytes
|
import java.util.HashMap;
import java.util.Scanner;
public class newwinwae {
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
int ar[]=new int[n];
for(int i=0;i<n;i++)
{
ar[i]=scan.nextInt();
if(map.containsKey(ar[i])) {
map.put(ar[i],map.get(ar[i])+1);
}
else {
map.put(ar[i], 1);
}
}
long real=0;
for(int i=0;i<n;i++)
{
long ans=0;
int now=ar[i];
for(int j=0;j<=(5);j++)
{
//System.out.println(map.containsKey(now+j));
if(map.containsKey(now+j)) {
//System.out.println(map.get(now+j)+"adder");
ans+=map.get(now+j);
}
}
real=Math.max(ans, real);
}
System.out.println(real);
}
}
|
Java
|
["6\n1 10 17 12 15 2", "10\n1337 1337 1337 1337 1337 1337 1337 1337 1337 1337", "6\n1 1000 10000 10 100 1000000000"]
|
2 seconds
|
["3", "10", "1"]
|
NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
|
Java 8
|
standard input
|
[
"two pointers",
"sortings"
] |
8b075d96b3c0172d756109b4801d68de
|
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) β the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.
| 1,200 |
Print one integer β the maximum possible number of students in a balanced team.
|
standard output
| |
PASSED
|
6f0250a0233f611719f8208aaed808df
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C3 {
static FastScanner in = new FastScanner(System.in);
public static void main (String args[]){
int n = in.nextInt();
int m = in.nextInt();
int a[] = new int[n];
int up[]= new int[n];
int down[]= new int[n];
for(int i = 0 ; i < n;i++){
a[i] = in.nextInt();
}
up[n-1]=down[n-1]=n-1;
for(int i = n-2 ; i >=0 ; i--){
if(a[i] <= a[i+1]){
up[i]=up[i+1];
}else{
up[i] = i;
}
if(a[i] >= a[i+1]){
down[i]=down[i+1];
}
else{
down[i]=i;
}
}
for(int i = 0 ; i < m ;i++){
int st = in.nextInt()-1;
int en = in.nextInt()-1;
int tmp = down[up[st]];
if(tmp >=en)
System.out.println("Yes");
else
System.out.println("No");
}
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
75be7914be98be27240625644704d203
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
public class C {
public static BufferedReader in;
public static PrintWriter out;
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
boolean showLineError = true;
if (showLineError) {
solve();
out.close();
} else {
try {
solve();
} catch (Exception e) {
} finally {
out.close();
}
}
}
static void debug(Object... os) {
out.println(Arrays.deepToString(os));
}
private static void solve() throws IOException {
String[] line = in.readLine().split(" ");
int n = Integer.parseInt(line[0]);
int m = Integer.parseInt(line[1]);
long[] arr = new long[n + 1];
line = in.readLine().split(" ");
for (int i = 1; i <= n; i++) {
arr[i] = Long.parseLong(line[i-1]);
}
int[] crec = new int[n + 1];
int[] decrec = new int[n + 1];
crec[1]=1;
decrec[1]=1;
for (int i = 2; i <= n; i++) {
crec[i] = (arr[i]>=arr[i-1]?crec[i-1] : i);
decrec[i] = (arr[i]<=arr[i-1]?decrec[i-1] : i);
}
for(int i =0;i<m;i++){
line = in.readLine().split(" ");
int l = Integer.parseInt(line[0]);
int r = Integer.parseInt(line[1]);
if(l>=crec[decrec[r]]){
out.println("Yes");
}else{
out.println("No");
}
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
4240a480d6bd28d681eface53a2b81c8
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Pr279C {
public static void main(String[] args) throws IOException {
new Pr279C().run();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out, true);
solve();
out.flush();
}
void solve() throws IOException {
int n = nextInt();
int m = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
int[] left = new int[n];
int[] right = new int[n];
for(int i = 1; i < n; i++) {
if(a[i] <= a[i-1]) {
left[i] = left[i-1]+1;
}
}
for(int i = n-2; i > -1; i--) {
if(a[i] <= a[i+1]) {
right[i] = right[i+1]+1;
}
}
for(int i = 0; i < m; i++) {
int la = nextInt() - 1;
int rb = nextInt() - 1;
if(right[la] + left[rb] >= rb - la) {
out.println("Yes");
}else {
out.println("No");
}
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
ba32d47b7721349414f58b64974c2c16
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Ladder {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[] a = new int[n];
st = new StringTokenizer(f.readLine());
for (int i = 0; i < n; i++)
a[i] = Integer.parseInt(st.nextToken());
int[] x = new int[n];
int[] y = new int[n];
for (int i = n-1; i >= 0; i--)
if (i < n-1 && a[i] <= a[i+1])
x[i] = x[i+1];
else
x[i] = i;
for (int i = 0; i < n; i++)
if (i > 0 && a[i] <= a[i-1])
y[i] = y[i-1];
else
y[i] = i;
for (int i = 0; i < m; i++) {
st = new StringTokenizer(f.readLine());
int l = Integer.parseInt(st.nextToken())-1;
int r = Integer.parseInt(st.nextToken())-1;
System.out.println(x[l] >= y[r] ? "Yes" : "No");
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
dffaedad891243afc7a4a1900d354055
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import java.util.StringTokenizer;
/**
* @author Polyarniy Nickolay
*/
public class ProblemC {
private BufferedReader in;
private PrintWriter out;
private StringTokenizer tok;
private final String DELIMETER = " ";
private final boolean ENABLE_MULTITEST = false;
private final boolean DEBUG = true;
private final String FILENAME = null;//if FILENAME = null, then works with console
public void run() throws Exception {
initInputOutput();
do {
init();
solve();
} while (hasMoreTokens() && ENABLE_MULTITEST);
finish();
}
private void init() throws Exception {
}
private void solve() throws Exception {
int n = nextInt();
int m = nextInt();
int[] a = nextArrayInt(n);
int[] prev = new int[n];
int[] next = new int[n];
prev[0] = 0;
for (int i = 1; i < n; i++) {
if (a[i - 1] <= a[i]) {
prev[i] = prev[i - 1];
} else {
prev[i] = i;
}
}
next[n - 1] = n - 1;
for (int i = n - 2; i >= 0; i--) {
if (a[i] >= a[i + 1]) {
next[i] = next[i + 1];
} else {
next[i] = i;
}
}
int pow = 1;
while (pow < n) {
pow *= 2;
}
int[] t = new int[2 * pow];
for (int i = pow; i < pow + n; i++) {
t[i] = i - pow;
}
for (int i = pow - 1; i >= 1; i--) {
if (a[t[i * 2]] > a[t[i * 2 + 1]]) {
t[i] = t[2 * i];
} else {
t[i] = t[2 * i + 1];
}
}
// out.println(Arrays.toString(a));
// out.println(Arrays.toString(t));
// out.println(Arrays.toString(next));
// out.println(Arrays.toString(prev));
for (int i = 0; i < m; i++) {
int l = nextInt() - 1;
int r = nextInt() - 1;
int max = r;
int curL = l + pow;
int curR = r + pow;
while (curL <= curR) {
if (curL % 2 == 1) {
if (a[t[curL]] > a[max]) {
max = t[curL];
}
curL++;
}
if (curR % 2 == 0) {
if (a[t[curR]] > a[max]) {
max = t[curR];
}
curR--;
}
curL /= 2;
curR /= 2;
}
// out.println(l+"-"+r+" "+max);
if (prev[max] <= l && next[max] >= r) {
out.println("Yes");
} else {
out.println("No");
}
}
}
public static void main(String[] args) throws Exception {
ProblemC solution = new ProblemC();
solution.run();
}
private void initInputOutput() throws Exception {
if (FILENAME == null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
private void shuffleArray(Object[] arr) {
Random r = new Random();
for (int i = 0; i < arr.length; ++i) {
Object tmp = arr[i];
int j = r.nextInt(arr.length);
arr[i] = arr[j];
arr[j] = tmp;
}
}
private void shuffleArray(int[] arr) {
Random r = new Random();
for (int i = 0; i < arr.length; ++i) {
int tmp = arr[i];
int j = r.nextInt(arr.length);
arr[i] = arr[j];
arr[j] = tmp;
}
}
private int[] nextArrayInt(int n) throws Exception {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
private String nextWord() throws Exception {
if (updateTokenizer()) {
return null;
} else {
return tok.nextToken();
}
}
private boolean hasMoreTokens() throws Exception {
return !updateTokenizer();
}
private boolean updateTokenizer() throws Exception {
while (tok == null || !tok.hasMoreTokens()) {
String nextLine = in.readLine();
if (nextLine == null || nextLine.isEmpty()) {
return true;
}
tok = new StringTokenizer(nextLine, DELIMETER);
}
return false;
}
private int nextInt() throws Exception {
return Integer.parseInt(nextWord());
}
private long nextLong() throws Exception {
return Long.parseLong(nextWord());
}
private void finish() throws Exception {
in.close();
out.close();
}
private void print(String s) {
if (DEBUG) {
System.out.print(s);
}
}
private void println(String s) {
if (DEBUG) {
System.out.println(s);
}
}
private void println() {
if (DEBUG) {
System.out.println();
}
}
private long[] getFirstSimpleNums(int n) {
boolean[] notPr = new boolean[n];
int res = n;
notPr[0] = true;
res--;
notPr[1] = true;
res--;
for (int i = 2; i < n; ++i) {
if (!notPr[i]) {
for (int j = i + i; j < n; j += i) {
if (!notPr[j]) {
notPr[j] = true;
res--;
}
}
}
}
long[] resA = new long[res];
int next = 0;
for (int i = 0; i < n; i++) {
if (!notPr[i]) {
resA[next] = i;
next++;
}
}
return resA;
}
private static class Pair {
int a;
int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public static final Comparator<Pair> comparator = new Comparator<Pair>() {
@Override
public int compare(Pair pair1, Pair pair2) {
return (pair1.a - pair2.a) != 0 ? (pair1.a - pair2.a) : (pair1.b - pair2.b);
}
};
@Override
public String toString() {
return "{" + a + "|" + b + '}';
}
}
// /**
// * Examples(n: res):
// * 0: 0
// * 1: 0
// * 2: 1
// * 3: 2
// * 4: 2
// * 5: 3
// * 7: 3
// * 8: 3
// * 10:4
// */
// public static int logCeil(int n) {
// int res = 0;
// int pow = 1;
// while (pow < n) {
// pow *= 2;
// res++;
// }
// return res;
// }
//
// private static class Tree<T> {
//
// Object[] t;
// Object[] push;
// int n;
// int capacity;
// TreeType<T> tree;
//
// public Tree(int capacity, T[] a, TreeType<T> tree) {
// this.tree = tree;
// n = 1;
// while (n < capacity) {
// n *= 2;
// }
// t = new Object[2 * n];
// push = new Object[n];
// this.capacity = capacity;
// System.arraycopy(a, 0, t, n, capacity);
// for (int i = n - 1; i >= 1; i--) {
// t[i] = tree.calc((T) t[i * 2], (T) t[i * 2 + 1]);
// }
// }
//
// public T getOnRange(int l, int r) {
// l += n;
// r += n;
// return getOnRange(l, r, 1, n, 2 * n - 1);
// }
//
// public T getOnRange(int l, int r, int cur, int rl, int rr) {
// if (cur >= n) {
// return (T) t[cur];
// }
// push(cur);
// if (l == rl && r == rr) {
// return (T) t[cur];
// }
// int m = (rl + rr) / 2;
// T res = null;
// if (l <= m) {
// res = tree.sum(res, getOnRange(l, Math.min(m, r), 2 * cur, rl, m));
// }
// if (r >= m + 1) {
// res = tree.sum(res, getOnRange(Math.max(m + 1, l), r, 2 * cur + 1, m + 1, rr));
// }
// return res;
// }
//
// public T get(int ind) {
// int cur = 1;
// int rl = n;
// int rr = 2 * n - 1;
// while (cur < n) {
// push(cur);
// int m = (rl + rr) / 2;
// if (ind <= m) {
// cur = 2 * ind;
// rr = m;
// } else {
// cur = 2 * ind + 1;
// rl = m + 1;
// }
// }
// return (T) t[cur];
// }
//
// public void add(T x, int ind) {
// int cur = 1;
// int rl = n;
// int rr = 2 * n - 1;
// while (cur < n) {
// push(cur);
// int m = (rl + rr) / 2;
// if (ind <= m) {
// cur = 2 * ind;
// rr = m;
// } else {
// cur = 2 * ind + 1;
// rl = m + 1;
// }
// }
// t[cur] = tree.calc((T) t[cur], x);
// }
//
// private void push(int cur) {
// if (!tree.isEmpty((T) push[cur])) {
// for (int ch = 0; ch <= 1; ch++) {
// if (2 * cur + ch < n) {
// push[2 * cur + ch] = tree.calc((T)push[2 * cur + ch], (T)push[cur]);
// } else {
// t[2 * cur + ch] = tree.calc((T)t[2 * cur + ch], (T)push[cur]);
// }
// }
// push[cur] = null;
// }
// }
// }
//
// private static interface TreeType<T> {
//
// public T calc(T a, T b);
//
// public boolean isEmpty(T push);
// }
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
4ba35f56c1a53cd09da428965dca8457
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
public class C {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
int n = in.readInt();
int m = in.readInt();
int[] A = new int[n];
for (int i = 0; i < n; i++)
A[i] = in.readInt();
int[] Max = new int[n];
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && A[j] >= A[j - 1])
j++;
while (i < j)
Max[i++] = j;
}
int[] Min = new int[n];
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && A[j] <= A[j - 1])
j++;
while (i < j)
Min[i++] = j;
}
for (int i = 0; i < m; i++) {
int x = in.readInt() - 1;
int y = in.readInt() - 1;
int next = Max[x];
if (y < next)
System.out.println("Yes");
else {
next = Min[next];
if (y < next)
System.out.println("Yes");
else
System.out.println("No");
}
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1000];
private int curChar, numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private 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 readInt() {
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 readLong() {
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 String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
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, readInt());
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, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
b2b81708750e335f9bc7e68a3cad9079
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
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.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class Solver {
public static void main(String[] Args) throws NumberFormatException,
IOException {
new Solver().Run();
}
PrintWriter pw;
StringTokenizer Stok;
BufferedReader br;
public String nextToken() throws IOException {
while (Stok == null || !Stok.hasMoreTokens()) {
Stok = new StringTokenizer(br.readLine());
}
return Stok.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
class Segment implements Comparable<Segment>{
public int nach, kon;
public Segment(int anach, int akon){
nach=anach;
kon=akon;
}
@Override
public int compareTo(Segment arg0) {
// TODO Auto-generated method stub
if (nach!=arg0.nach)
return nach-arg0.nach;
return kon-arg0.kon;
}
}
int n;
int kolRecv;
int[] a;
int nach, kon;
List<Segment> segms;
public void findSegments(){
int nach;
for (int i=0; i<n-1; i++){
if (a[i]>a[i+1])
{
nach=i;
int j=i+1;
while (j<n && a[j]==a[i+1]){
j++;
}
if (j<n){
if (a[j]>a[i+1])
segms.add(new Segment(nach, j));
}
}
}
Collections.sort(segms);
}
public boolean isStaircase(int nach, int kon){
int kolSegms=segms.size();
int min=-1;
int max=kolSegms;
int mid;
while (max-min>1){
mid=(max+min)/2;
if (segms.get(mid).nach<nach)
min=mid;
else
max=mid;
}
if (max>=kolSegms)
return true;
if (segms.get(max).kon<=kon)
return false;
return true;
}
public void Run() throws NumberFormatException, IOException {
//br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter("output.txt");
br=new BufferedReader(new InputStreamReader(System.in)); pw=new PrintWriter(new OutputStreamWriter(System.out));
n=nextInt();
kolRecv=nextInt();
a=new int[n];
segms=new ArrayList<Solver.Segment>();
for (int i=0; i<n; i++){
a[i]=nextInt();
}
findSegments();
for (int i=0; i<kolRecv; i++){
nach=nextInt()-1;
kon=nextInt()-1;
if (isStaircase(nach,kon))
pw.println("Yes");
else
pw.println("No");
}
pw.flush();
pw.close();
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
71e892f1c27da5875a53b2780996fec6
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
public class C {
public void solve() throws Exception {
int n = nextInt(), m = nextInt();
int[] p = nextArr(n);
int[] inc = new int[n];
int[] desc = new int[n];
int idx = 0;
for (int i=0; i<n-1; ++i) {
if (p[i]>p[i+1]) {
for (int j=idx; j<=i; ++j) inc[j] = i;
idx = i+1;
}
}
for (int j=idx; j<n; ++j) inc[j] = n-1;
idx = 0;
for (int i=0; i<n-1; ++i) {
if (p[i]<p[i+1]) {
for (int j=idx; j<=i; ++j) desc[j] = i;
idx = i+1;
}
}
for (int j=idx; j<n; ++j) desc[j] = n-1;
// debug(inc);
// debug(desc);
for (int i=0; i<m; ++i) {
int L = nextInt()-1, R = nextInt()-1;
if (inc[L]>=R || desc[L]>=R || (inc[L]<=R && desc[inc[L]]>=R)) println("Yes");
else println("No");
}
}
////////////////////////////////////////////////////////////////////////////
boolean showDebug = true;
static boolean useFiles = false;
static String inFile = "input.txt";
static String outFile = "output.txt";
double EPS = 1e-7;
int INF = Integer.MAX_VALUE;
long INFL = Long.MAX_VALUE;
double INFD = Double.MAX_VALUE;
int absPos(int num) {
return num<0 ? 0:num;
}
long absPos(long num) {
return num<0 ? 0:num;
}
double absPos(double num) {
return num<0 ? 0:num;
}
int min(int... nums) {
int r = nums[0];
for (int i=1; i<nums.length; ++i)
if (nums[i]<r) r=nums[i];
return r;
}
int max(int... nums) {
int r = nums[0];
for (int i=1; i<nums.length; ++i)
if (nums[i]>r) r=nums[i];
return r;
}
long minL(long... nums) {
long r = nums[0];
for (int i=1; i<nums.length; ++i)
if (nums[i]<r) r=nums[i];
return r;
}
long maxL(long... nums) {
long r = nums[0];
for (int i=1; i<nums.length; ++i)
if (nums[i]>r) r=nums[i];
return r;
}
double minD(double... nums) {
double r = nums[0];
for (int i=1; i<nums.length; ++i)
if (nums[i]<r) r=nums[i];
return r;
}
double maxD(double... nums) {
double r = nums[0];
for (int i=1; i<nums.length; ++i)
if (nums[i]>r) r=nums[i];
return r;
}
long sumArr(int[] arr) {
long res = 0;
for (int i=0; i<arr.length; ++i)
res+=arr[i];
return res;
}
long sumArr(long[] arr) {
long res = 0;
for (int i=0; i<arr.length; ++i)
res+=arr[i];
return res;
}
double sumArr(double[] arr) {
double res = 0;
for (int i=0; i<arr.length; ++i)
res+=arr[i];
return res;
}
long partsFitCnt(long partSize, long wholeSize) {
return (partSize+wholeSize-1)/partSize;
}
boolean odd(long num) {
return (num&1)==1;
}
boolean hasBit(int num, int pos) {
return (num&(1<<pos))>0;
}
boolean isLetter(char c) {
return (c>='a' && c<='z') || (c>='A' && c<='Z');
}
boolean isLowercase(char c) {
return (c>='a' && c<='z');
}
boolean isUppercase(char c) {
return (c>='A' && c<='Z');
}
boolean isDigit(char c) {
return (c>='0' && c<='9');
}
boolean charIn(String chars, String s) {
if (s==null) return false;
if (chars==null || chars.equals("")) return true;
for (int i=0; i<s.length(); ++i)
for (int j=0; j<chars.length(); ++j)
if (chars.charAt(j)==s.charAt(i)) return true;
return false;
}
String stringn(String s, int n) {
if (n<1 || s==null) return "";
StringBuilder sb = new StringBuilder(s.length()*n);
for (int i=0; i<n; ++i) sb.append(s);
return sb.toString();
}
String str(Object o) {
if (o==null) return "";
return o.toString();
}
long timer = System.currentTimeMillis();
void startTimer() {
timer = System.currentTimeMillis();
}
void stopTimer() {
System.err.println("time: "+(System.currentTimeMillis()-timer)/1000.0);
}
static class InputReader {
private byte[] buf;
private int bufPos = 0, bufLim = -1;
private InputStream stream;
public InputReader(InputStream stream, int size) {
buf = new byte[size];
this.stream = stream;
}
private void fillBuf() throws IOException {
bufLim = stream.read(buf);
bufPos = 0;
}
char read() throws IOException {
if (bufPos>=bufLim) fillBuf();
return (char)buf[bufPos++];
}
boolean hasInput() throws IOException {
if (bufPos>=bufLim) fillBuf();
return bufPos<bufLim;
}
}
static InputReader inputReader;
static BufferedWriter outputWriter;
char nextChar() throws IOException {
return inputReader.read();
}
char nextNonWhitespaceChar() throws IOException {
char c = inputReader.read();
while (c<=' ') c=inputReader.read();
return c;
}
String nextWord() throws IOException {
StringBuilder sb = new StringBuilder();
char c = inputReader.read();
while (c<=' ') c=inputReader.read();
while (c>' ') {
sb.append(c);
c = inputReader.read();
}
return new String(sb);
}
String nextLine() throws IOException {
StringBuilder sb = new StringBuilder();
char c = inputReader.read();
while (c<=' ') c=inputReader.read();
while (c!='\n' && c!='\r') {
sb.append(c);
c = inputReader.read();
}
return new String(sb);
}
int nextInt() throws IOException {
int r = 0;
char c = nextNonWhitespaceChar();
boolean neg = false;
if (c=='-') neg=true;
else r=c-48;
c = nextChar();
while (c>='0' && c<='9') {
r*=10;
r+=c-48;
c=nextChar();
}
return neg ? -r:r;
}
long nextLong() throws IOException {
long r = 0;
char c = nextNonWhitespaceChar();
boolean neg = false;
if (c=='-') neg=true;
else r = c-48;
c = nextChar();
while (c>='0' && c<='9') {
r*=10L;
r+=c-48L;
c=nextChar();
}
return neg ? -r:r;
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextWord());
}
int[] nextArr(int size) throws NumberFormatException, IOException {
int[] arr = new int[size];
for (int i=0; i<size; ++i)
arr[i] = nextInt();
return arr;
}
long[] nextArrL(int size) throws NumberFormatException, IOException {
long[] arr = new long[size];
for (int i=0; i<size; ++i)
arr[i] = nextLong();
return arr;
}
double[] nextArrD(int size) throws NumberFormatException, IOException {
double[] arr = new double[size];
for (int i=0; i<size; ++i)
arr[i] = nextDouble();
return arr;
}
String[] nextArrS(int size) throws NumberFormatException, IOException {
String[] arr = new String[size];
for (int i=0; i<size; ++i)
arr[i] = nextWord();
return arr;
}
char[] nextArrCh(int size) throws IOException {
char[] arr = new char[size];
for (int i=0; i<size; ++i)
arr[i] = nextNonWhitespaceChar();
return arr;
}
char[][] nextArrCh(int rows, int columns) throws IOException {
char[][] arr = new char[rows][columns];
for (int i=0; i<rows; ++i)
for (int j=0; j<columns; ++j)
arr[i][j] = nextNonWhitespaceChar();
return arr;
}
char[][] nextArrChBorders(int rows, int columns, char border) throws IOException {
char[][] arr = new char[rows+2][columns+2];
for (int i=1; i<=rows; ++i)
for (int j=1; j<=columns; ++j)
arr[i][j] = nextNonWhitespaceChar();
for (int i=0; i<columns+2; ++i) {
arr[0][i] = border;
arr[rows+1][i] = border;
}
for (int i=0; i<rows+2; ++i) {
arr[i][0] = border;
arr[i][columns+1] = border;
}
return arr;
}
void printf(String format, Object... args) throws IOException {
outputWriter.write(String.format(format, args));
}
void print(Object o) throws IOException {
outputWriter.write(o.toString());
}
void println(Object o) throws IOException {
outputWriter.write(o.toString());
outputWriter.newLine();
}
void print(Object... o) throws IOException {
for (int i=0; i<o.length; ++i) {
if (i!=0) outputWriter.write(' ');
outputWriter.write(o[i].toString());
}
}
void println(Object... o) throws IOException {
print(o);
outputWriter.newLine();
}
void printn(Object o, int n) throws IOException {
String s = o.toString();
for (int i=0; i<n; ++i) {
outputWriter.write(s);
if (i!=n-1) outputWriter.write(' ');
}
}
void printnln(Object o, int n) throws IOException {
printn(o, n);
outputWriter.newLine();
}
void printArr(int[] arr) throws IOException {
for (int i=0; i<arr.length; ++i) {
if (i!=0) outputWriter.write(' ');
outputWriter.write(Integer.toString(arr[i]));
}
}
void printArr(long[] arr) throws IOException {
for (int i=0; i<arr.length; ++i) {
if (i!=0) outputWriter.write(' ');
outputWriter.write(Long.toString(arr[i]));
}
}
void printArr(double[] arr) throws IOException {
for (int i=0; i<arr.length; ++i) {
if (i!=0) outputWriter.write(' ');
outputWriter.write(Double.toString(arr[i]));
}
}
void printArr(String[] arr) throws IOException {
for (int i=0; i<arr.length; ++i) {
if (i!=0) outputWriter.write(' ');
outputWriter.write(arr[i]);
}
}
void printlnArr(int[] arr) throws IOException {
printArr(arr);
outputWriter.newLine();
}
void printlnArr(long[] arr) throws IOException {
printArr(arr);
outputWriter.newLine();
}
void printlnArr(double[] arr) throws IOException {
printArr(arr);
outputWriter.newLine();
}
void printlnArr(String[] arr) throws IOException {
printArr(arr);
outputWriter.newLine();
}
void halt(Object... o) throws IOException {
if (o.length!=0) println(o);
outputWriter.flush(); outputWriter.close();
System.exit(0);
}
void debug(Object... o) {
if (showDebug) System.err.println(Arrays.deepToString(o));
}
public static void main(String[] args) throws Exception {
Locale.setDefault(Locale.US);
if (!useFiles) {
inputReader = new InputReader(System.in, 1<<16);
outputWriter = new BufferedWriter(new OutputStreamWriter(System.out), 1<<16);
} else {
inputReader = new InputReader(new FileInputStream(new File(inFile)), 1<<16);
outputWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outFile))), 1<<16);
}
new C().solve();
outputWriter.flush(); outputWriter.close();
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
9e9693e9daf60f98b31f01fe61c8a594
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class C279 {
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public static void main(String[] args) throws IOException {
new C279().run();
}
public void solve() throws IOException {
int n = nextInt();
int m = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
int[] d = new int[n];
int[] last = new int[n];
int cnt = 0;
int i = 0;
while (i < n - 1) {
while (i < n - 1 && a[i] <= a[i + 1]) {
d[i + 1] = cnt;
if (a[i + 1] == a[i]) {
last[i + 1] = last[i];
} else {
last[i + 1] = i + 1;
}
i++;
}
while (i < n - 1 && a[i] >= a[i + 1]) {
d[i + 1] = cnt;
if (a[i + 1] == a[i]) {
last[i + 1] = last[i];
} else {
last[i + 1] = i + 1;
}
i++;
}
cnt++;
}
int[] newlast = new int[n];
for (i = 0; i < n; i++) {
int k = last[i];
newlast[k] = i;
}
for (i = 1; i < n; i++) {
if (newlast[i] == 0) {
newlast[i] = newlast[i - 1];
}
}
for (i = 0; i < m; i++) {
int x = nextInt() - 1;
int y = nextInt() - 1;
if (d[newlast[x]] == d[y]) {
out.println("Yes");
} else if (newlast[x] < n - 1 && d[newlast[x] + 1] == d[y] && a[newlast[x]] <= a[newlast[x] + 1]) {
out.println("Yes");
} else {
out.println("No");
}
}
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
af22359d8747b1ff66ee51155ca65f58
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
//package codeforces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class C {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
StringTokenizer stringTokenizer;
C() throws IOException {
// reader = new BufferedReader(new FileReader("input.txt"));
// writer = new PrintWriter(new FileWriter("bridges.out"));
}
String next() throws IOException {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
final int MOD = 1000 * 1000 + 3;
int sum(int a, int b) {
a += b;
return a >= MOD ? a - MOD : a;
}
int product(int a, int b) {
return (int) (1l * a * b % MOD);
}
int pow(int x, int k) {
int result = 1;
while (k > 0) {
if (k % 2 == 1) {
result = product(result, x);
}
x = product(x, x);
k /= 2;
}
return result;
}
int inv(int x) {
return pow(x, MOD - 2);
}
void solve() throws IOException {
int n = nextInt(), m = nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = nextInt();
}
int[] goesUpTill = new int[n];
int[] goesDownTill = new int[n];
goesDownTill[n - 1] = n - 1;
goesUpTill[n - 1] = n - 1;
for(int i = n - 2; i >= 0; i--) {
if(a[i] < a[i + 1]) {
goesDownTill[i] = i;
} else {
goesDownTill[i] = goesDownTill[i + 1];
}
if(a[i] > a[i + 1]) {
goesUpTill[i] = i;
} else {
goesUpTill[i] = goesUpTill[i + 1];
}
}
for(int i = 0; i < m; i++) {
int l = nextInt() - 1, r = nextInt() - 1;
int f = goesDownTill[goesUpTill[l]];
writer.println(f >= r ? "Yes" : "No");
}
writer.close();
}
public static void main(String[] args) throws IOException {
new C().solve();
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
76c750de56e3958d603d8bf36b8757e0
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
private void solve() throws IOException {
int n = nextInt();
int m = nextInt();
int a[] = new int[n];
for (int i = 0; i < n; ++i)
a[i] = nextInt();
int l[] = new int[m];
int r[] = new int[m];
for (int i = 0; i < m; ++i) {
l[i] = nextInt() - 1;
r[i] = nextInt() - 1;
}
int h[] = new int[n];
int i = 1;
while (i < n - 1) {
int j = i + 1;
while (j < n - 1 && a[j] == a[i])
++j;
// if (a[i - 1] < a[i] && a[j] < a[i])
// for (int t = i; t < j; ++t)
// h[t] = 1;else
if (a[i - 1] > a[i] && a[j] > a[i])
for (int t = i; t < j; ++t)
h[t] = -1;
i = j;
}
int b[] = new int[n];
for (i = 1; i < n; ++i) {
if (h[i] == -1 && h[i - 1] != -1)
b[i] = b[i - 1] + 1;
else
b[i] = b[i - 1];
}
for (i = 0; i < m; ++i) {
if (b[r[i]] - b[l[i]] == 0
|| (b[r[i]] - b[l[i]] == 1 && h[r[i]] == -1))
println("Yes");
else
println("No");
}
}
public static void main(String[] args) {
new Main().run();
}
public void run() {
try {
if (isFileIO) {
pw = new PrintWriter(new File("output.out"));
br = new BufferedReader(new FileReader("input.in"));
} else {
pw = new PrintWriter(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
}
solve();
pw.close();
br.close();
} catch (IOException e) {
System.err.println("IO Error");
}
}
private void print(Object o) {
pw.print(o);
}
private void println(Object o) {
pw.println(o);
}
private void println() {
pw.println();
}
int[] nextIntArray(int n) throws IOException {
int arr[] = new int[n];
for (int i = 0; i < n; ++i)
arr[i] = Integer.parseInt(nextToken());
return arr;
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(br.readLine());
}
return tokenizer.nextToken();
}
private BufferedReader br;
private StringTokenizer tokenizer;
private PrintWriter pw;
private final boolean isFileIO = false;
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
628cfd7f0083588914fcd006931b0a1a
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Codeforces {
static int arr[];
static Pair dp[];
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int n = Reader.nextInt(), m = Reader.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
arr[i] = Reader.nextInt();
int up[] = new int[n];
int down[] = new int[n];
up[n-1] = down[n-1] = n-1;
for(int i = n-2; i > -1; i--){
up[i] = arr[i+1] >= arr[i] ? up[i+1] : i;
down[i] = arr[i+1] <= arr[i] ? down[i+1] : i;
}
for(int i = 0; i < m; i++){
int x = Reader.nextInt(), y = Reader.nextInt();
x--; y--;
if(down[up[x]] >= y)
System.out.println("Yes");
else
System.out.println("No");
}
}
}
class Pair{
int x;
int y;
Pair(int a, int b){
x = a;
y = b;
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
public static int pars(String x) {
int num = 0;
int i = 0;
if (x.charAt(0) == '-') {
i = 1;
}
for (; i < x.length(); i++) {
num = num * 10 + (x.charAt(i) - '0');
}
if (x.charAt(0) == '-') {
return -num;
}
return num;
}
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static void init(FileReader input) {
reader = new BufferedReader(input);
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return pars(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
class NN implements Comparable<NN>{
String s;
int freq;
NN(int x, String y){
s = y;
freq = x;
}
@Override
public int compareTo(NN t) {
int dif = this.freq - t.freq;
if(dif == 0) // freq are equals
return this.s.compareTo(t.s);
else
return dif;
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
5aabca4667e7e55eef8cb0a05b13d11b
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author sousnake
*/
public class LADDER_DP {
public static void main(String [] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
s = br.readLine().split(" ");
int ar[] = new int [n];
for(int i=0;i<n;i++){
ar[i] = Integer.parseInt(s[i]);
}
int maxinc[]= new int[n];
maxinc[n-1]=n-1;
for(int i=n-2;i>=0;i--){
if(ar[i]<=ar[i+1])
maxinc[i]=maxinc[i+1];
else maxinc[i]=i;
}
int maxdec[]=new int[n];
maxdec[n-1]=n-1;
for(int i=n-2;i>=0;i--){
if(ar[i]>=ar[i+1])
maxdec[i]=maxdec[i+1];
else maxdec[i]=i;
}
//for(int i=0;i<n;i++)
// System.out.println(maxinc[i]+" "+maxdec[i]);
for(int i=0;i<m;i++){
s = br.readLine().split(" ");
int x = Integer.parseInt(s[0])-1;
int y = Integer.parseInt(s[1])-1;
int z = maxdec[maxinc[x]];
if(y<=z)
System.out.println("Yes");
else System.out.println("No");
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
721232f258516f97aa02e41c1a7ce1cd
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) throws Exception {
int arLen = nextInt();
int queryCnt = nextInt();
int[] ar = new int[arLen];
for (int i = 0; i < arLen; i++) {
ar[i] = nextInt();
}
int[] earliestEq = new int[arLen];
earliestEq[0] = 0;
for (int i = 1; i < arLen; i++) {
if (ar[i] == ar[i - 1]) {
earliestEq[i] = earliestEq[i - 1];
} else {
earliestEq[i] = i;
}
}
int[] earliestLadderSt = new int[arLen];
earliestLadderSt[0] = 0;
for (int i = 1; i < arLen; i++) {
if (ar[i] > ar[i - 1]) {
if (ar[i - 1] >= ar[earliestLadderSt[i - 1]]) {
earliestLadderSt[i] = earliestLadderSt[i - 1];
} else {
earliestLadderSt[i] = earliestEq[i - 1];
}
} else if (ar[i] < ar[i - 1]) {
if (ar[i - 1] <= ar[earliestLadderSt[i - 1]]) {
earliestLadderSt[i] = earliestLadderSt[i - 1];
} else {
earliestLadderSt[i] = earliestEq[i - 1];
}
} else {
earliestLadderSt[i] = earliestLadderSt[i - 1];
}
}
PrintWriter out = new PrintWriter(System.out);
for (int i = 0; i < queryCnt; i++) {
int st = nextInt() - 1;
int end = nextInt() - 1;
if (earliestLadderSt[end] <= st
|| (ar[earliestLadderSt[end]] >= ar[end] && earliestLadderSt[earliestLadderSt[end]] <= st)) {
out.println("Yes");
} else {
out.println("No");
}
}
out.flush();
}
static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static int nextInt() throws Exception {
return Integer.parseInt(next());
}
static double nextDouble() throws Exception {
return Double.parseDouble(next());
}
static String next() throws Exception {
while (true) {
if (tokenizer.hasMoreTokens()) {
return tokenizer.nextToken();
}
String s = br.readLine();
if (s == null) {
return null;
}
tokenizer = new StringTokenizer(s);
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
1b084d5d90dea2080547c0fc9b056a8f
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Diego Huerfano ( diego.link )
*/
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);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
final int n=in.readInt(), m=in.readInt();
int a[]=in.readInts( n );
int maxUp[]=new int[n], maxDown[]=new int[n];
maxUp[n-1]=1;
for( int i=n-2; i>=0; i-- ) {
if( a[i]<=a[i+1] ) maxUp[i]=maxUp[i+1]+1;
else maxUp[i]=1;
}
maxDown[0]=1;
for( int i=1; i<n; i++ ) {
if( a[i]<=a[i-1] ) maxDown[i]=maxDown[i-1]+1;
else maxDown[i]=1;
}
for (int i = 0; i < m; i++) {
int li=in.readInt()-1, ri=in.readInt()-1;
int num=maxUp[li] + maxDown[ri]-1;
if( num>=ri-li+1 ) out.printLine( "Yes" );
else out.printLine( "No" );
}
}
}
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 int readInt() {
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 int[] readInts( int n ){
int ans[]=new int[n];
for( int i=0; i<n; i++ ) ans[i]=readInt();
return ans;
}
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);
}
}
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 print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print( objects );
writer.println();
}
public void close() {
writer.close();
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
1016d7b6d11529d41f5cef9e441bb4c3
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.IOException;
import java.util.InputMismatchException;
public class Ladder {
public static void main(String[] args) {
FasterScanner sc = new FasterScanner();
int N = sc.nextInt();
int M = sc.nextInt();
int[] A = sc.nextIntArray(N, 1);
int[] fwd = new int[N + 1];
fwd[N] = N;
for (int i = N - 1; i >= 1; i--) {
if (A[i] <= A[i + 1]) {
fwd[i] = fwd[i + 1];
} else {
fwd[i] = i;
}
}
int[] bwd = new int[N + 1];
bwd[1] = 1;
for (int i = 2; i <= N; i++) {
if (A[i] <= A[i - 1]) {
bwd[i] = bwd[i - 1];
} else {
bwd[i] = i;
}
}
StringBuilder sb = new StringBuilder();
for (int q = 0; q < M; q++) {
int L = sc.nextInt();
int R = sc.nextInt();
int fwdPart = fwd[L];
int bwdPart = bwd[R];
sb.append((fwdPart >= bwdPart) ? "Yes\n" : "No\n");
}
System.out.print(sb.toString());
}
public static class FasterScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
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 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 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 int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
4b2fff9e758a81f5abb4f0e3db3ea7de
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
// external help was required to solve this sum...my bad...
import java.io.*;
import java.util.*;
public final class ladder
{
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
static final String s1="Yes",s2="No";
public static void main(String args[]) throws Exception
{
int n=sc.nextInt();
int q=sc.nextInt();
long[] a=new long[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextLong();
}
int i=0;
int[] rt=new int[n];
int[] lt=new int[n];
while(i<n)
{
int j=i;
while(i<n-1 && a[i+1]>=a[i])
{
i++;
}
for(int k=j;k<=i;k++)
{
rt[k]=i;
}
i++;
}
i=n-1;
while(i>=0)
{
int j=i;
while(i>0 && a[i-1]>=a[i])
{
i--;
}
for(int k=j;k>=i;k--)
{
lt[k]=i;
}
i--;
}
while(q>0)
{
int l=sc.nextInt()-1;
int r=sc.nextInt()-1;
if(rt[l]>=lt[r])
{
out.println(s1);
}
else
{
out.println(s2);
}
q--;
}
out.close();
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
f2f0c4913c9d07d4e9989a4f799d19ae
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
// external help was required to solve this sum...my bad...
import java.io.*;
import java.util.*;
public final class ladder
{
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
static final String s1="Yes",s2="No";
public static void main(String args[]) throws Exception
{
int n=sc.nextInt();
int q=sc.nextInt();
long[] a=new long[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextLong();
}
int i=0;
int[] rt=new int[n];
int[] lt=new int[n];
while(i<n)
{
int j=i;
while(i<n-1 && a[i+1]>=a[i])
{
i++;
}
for(int k=j;k<=i;k++)
{
rt[k]=i;
}
i++;
}
i=n-1;
while(i>=0)
{
int j=i;
while(i>0 && a[i-1]>=a[i])
{
i--;
}
for(int k=j;k>=i;k--)
{
lt[k]=i;
}
i--;
}
while(q>0)
{
int l=sc.nextInt()-1;
int r=sc.nextInt()-1;
if(r-l+1<=2)
{
out.println(s1);
}
else
{
if(rt[l]>=lt[r])
{
out.println(s1);
}
else
{
out.println(s2);
}
}
q--;
}
out.close();
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
d7038631da6b1ade5eeb0f3e8920509a
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
// external help was required to solve this sum...my bad...
import java.io.*;
import java.util.*;
public final class ladder
{
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
static final String s1="Yes",s2="No";
public static void main(String args[]) throws Exception
{
int n=sc.nextInt();
int q=sc.nextInt();
long[] a=new long[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextLong();
}
int i=0;
int[] rt=new int[n];
int[] lt=new int[n];
while(i<n)
{
int j=i;
while(i<n-1 && a[i+1]>=a[i])
{
i++;
}
for(int k=j;k<=i;k++)
{
rt[k]=i;
}
i++;
}
i=n-1;
while(i>=0)
{
int j=i;
while(i>0 && a[i-1]>=a[i])
{
i--;
}
for(int k=j;k>=i;k--)
{
lt[k]=i;
}
i--;
}
while(q>0)
{
int l=sc.nextInt()-1;
int r=sc.nextInt()-1;
if(rt[l]<lt[r])
{
out.println(s2);
}
else
{
out.println(s1);
}
q--;
}
out.close();
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
7652938fa4bf48521b32bbf8b1d20204
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class C {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
void solve() throws IOException {
int n = nextInt();
int q = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
int[] toUp = new int[n];
int[] toDown = new int[n];
toUp[n - 1] = toDown[n - 1] = n - 1;
for (int i = n - 2; i >= 0; i--) {
if (a[i] <= a[i + 1])
toUp[i] = toUp[i + 1];
else
toUp[i] = i;
if (a[i] >= a[i + 1])
toDown[i] = toDown[i + 1];
else
toDown[i] = i;
}
while (q-- > 0) {
int l = nextInt() - 1;
int r = nextInt() - 1;
int end = toDown[toUp[l]];
if (end >= r) {
out.println("Yes");
} else {
out.println("No");
}
}
}
C() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new C();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
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
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
75f2da973ae250bab980b2d659fd97a7
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.util.*;
import static java.lang.System.*;
public class A279 {
Scanner sc = new Scanner(in);
public static Random rand=new Random();
public void run() {
int n=sc.nextInt(),m=sc.nextInt();
int[] a=nextIntArray(n);
int[] l=new int[n];
int[] r=new int[n];
int prev=a[0];
for(int i=1;i<n;i++){
if(prev>=a[i]){
l[i]=l[i-1];
}else{
l[i]=i;
}
prev=a[i];
}
int next=a[n-1];
r[n-1]=n-1;
for(int i=n-2;i>=0;i--){
if(a[i]<=next){
r[i]=r[i+1];
}else{
r[i]=i;
}
next=a[i];
}
for(int q=0;q<m;q++){
int lv=sc.nextInt()-1,rv=sc.nextInt()-1;
int rr=Math.min(rv,r[lv]);
int ll=Math.max(lv,l[rv]);
if(ll<=rr)ln("Yes");
else ln("No");
}
}
public static void main(String[] _) {
new A279().run();
}
public int[] nextIntArray(int n){
int[] res=new int[n];
for(int i=0;i<n;i++){
res[i]=sc.nextInt();
}
return res;
}
public static void pr(Object o) {
out.print(o);
}
public static void ln(Object o) {
out.println(o);
}
public static void ln() {
out.println();
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
127f0c5068109c880f2e781905868b2d
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
// http://codeforces.com/contest/279/problem/C
public class Ladder {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int lastNum = Integer.MIN_VALUE;
int start = 1;
int turn = 1;
boolean up = true;
ArrayList<Integer> left = new ArrayList<Integer>();
ArrayList<Integer> right = new ArrayList<Integer>();
int n = s.nextInt();
int m = s.nextInt();
for (int i = 1; i <= n; i++) {
int a = s.nextInt();
if (a > lastNum) {
if (!up) {
left.add(start);
right.add(i - 1);
start = turn;
}
up = true;
} else if (a < lastNum) {
up = false;
turn = i;
}
lastNum = a;
}
left.add(start);
right.add(n);
for (int i = 0; i < m; i++) {
int l = s.nextInt();
int r = s.nextInt();
int idx = Collections.binarySearch(left, l);
if (idx < 0) {
idx = -idx - 2;
}
if (r <= right.get(idx)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
ea897f620af0900f44a7c6beb9f4e80a
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
// http://codeforces.com/contest/279/problem/C
public class Ladder {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
int[] min = new int[n];
for (int i = 1; i < n; i++) {
if (a[i] <= a[i - 1]) {
min[i] = min[i - 1];
} else {
min[i] = i;
}
}
int[] max = new int[n];
max[n - 1] = n - 1;
for (int i = n - 2; i >= 0; i--) {
if (a[i] <= a[i + 1]) {
max[i] = max[i + 1];
} else {
max[i] = i;
}
}
for (int i = 0; i < m; i++) {
int l = s.nextInt() - 1;
int r = s.nextInt() - 1;
if (min[r] <= max[l]) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
5a47d02138c59d45d4e1896ec2cd4c12
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.util.Scanner;
import java.util.Arrays;
public class C{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int q = input.nextInt();
int num[] = new int[n];
int to[] = new int[n];
int up[] = new int[n];
boolean dec[] = new boolean[n];
int from = 0;
int bef = input.nextInt();
num[0] = bef;
Arrays.fill(to, -1);
for(int i = 1; i < n; i++){
num[i] = input.nextInt();
if(num[i] < bef){
for(int j = from; j < i; j++)
up[j] = i - 1;
from = i;
}
bef = num[i];
}
for(int j = from; j < n; j++){
up[j] = n - 1;
}
int down[] = new int[n];
from = 0;
bef = num[0];
for(int i = 1; i < n; i++){
if(num[i] > bef){
for(int j = from; j < i; j++)
down[j] = i - 1;
from = i;
}
bef = num[i];
}
down[n - 1] = n - 1;
up[n - 1] = n - 1;
for(int j = from; j < n; j++){
down[j] = n - 1;
}
for(int i = 0; i < n - 1; i++){
if(num[i] > num[i + 1])
dec[i] = true;
}
for(int i = 0; i < n; i++){
to[i] = down[up[i]];
}
for(int i = 0; i < q; i++){
int a = input.nextInt() - 1;
int b = input.nextInt() - 1;
if(a == b){
System.out.println("Yes");
continue;
}
if(to[a] < b)
System.out.println("No");
else{
if(a != b)
System.out.println("Yes");
else
System.out.println("No");
}
}
input.close();
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
8e950b0c5be62017ac1db54914d02479
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Ghost
*/
public class C {
public static boolean DEBUG = false;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = new int[n+10];
int set_n = 0;
int[] set_x = new int[n+10];
int[] set_y = new int[n+10];
for(int i = 0; i < n; i++)
a[i] = sc.nextInt();
int k = 0;
while(k < n-1){
if(DEBUG)
System.out.println("start " + k);
boolean des = false;
while(k < n-1 && a[k] >= a[k+1]){
des = true;
k++;
}
if(DEBUG)
System.out.println(des+" "+ k);
if (des && k < n-1){
int i = k;
while(i > 0 && a[i-1] == a[k])
i--;
if (i == 0 && a[0] == a[k]) continue;
set_y[set_n] = k;
set_x[set_n] = i;
set_n++;
if (DEBUG){
System.out.println(set_x[set_n] + " " + set_y[set_n]);
}
}else
while(k < n-1 && a[k] <= a[k+1])
k++;
}
ques[] listQuest = new ques[m];
for(int i = 0; i < m; i++){
int x = sc.nextInt()-1;
int y = sc.nextInt()-1;
listQuest[i] = new ques(x,y,i);
}
Arrays.sort(listQuest);
String[] answer = new String[m];
int j = 0;
for(int i = 0; i < m; i++){
while (j < set_n && listQuest[i].x >= set_x[j]) j++;
if (listQuest[i].x < set_x[j] && listQuest[i].y > set_y[j])
answer[listQuest[i].pos] = "No";
else
answer[listQuest[i].pos] = "Yes";
}
for(int i = 0; i < m; i++){
System.out.println(answer[i]);
}
sc.close();
}
}
class ques implements Comparable<ques>{
public int x;
public int y;
public int pos;
public ques(int x, int y, int p){
this.x = x;
this.y = y;
this.pos = p;
}
@Override
public int compareTo(ques o) {
return this.x - o.x;
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
16072f6e223da9c6e935d42a09a6fd22
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Scanner;
public class Main2 {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int m = input.nextInt();
int[] a = new int[n];
for(int i = 0 ; i < n ; i++){
a[i] = input.nextInt();
}
int[] st = new int[m];
int[] en = new int[m];
for(int i = 0 ; i < m ; i++){
st[i] = input.nextInt();
en[i] = input.nextInt();
}
count(n,m,a,st,en);
}
public static void count(int n,int m,int[] a,int[] st,int[] en){
int[] oknum = new int[a.length];
//boolean[] switcher = new boolean[a.length];
int stdown = -1;
int past = a[0];
int jindex = 0;
for(int i = 1 ; i < a.length ; i++){
if(a[i] < past){
stdown = i;
}else if(a[i] > past){
for(int j = jindex ; j < stdown ; j++){
if(oknum[j] == 0){
oknum[j] = i;
jindex = j;
}
}
}
past = a[i];
}
/*for(int i = 0 ; i < a.length ; i++){
System.out.println(oknum[i]);
}*/
for(int i = 0 ; i < m ; i++){
if(oknum[st[i]-1] < en[i] && oknum[st[i]-1] != 0){
System.out.println("No");
}else{
System.out.println("Yes");
}
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
d289e42f63cb8fe768be11fe856ff695
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class C {
static StreamTokenizer st;
public static void main(String[] args) throws IOException {
st = new StreamTokenizer(new BufferedReader(new InputStreamReader(
System.in)));
int n = nextInt();
int m = nextInt();
int []arr = new int [n+1];
int []inc = new int [n+1];
int []dec = new int [n+1];
for (int i = 1; i <= n; i++) {
arr[i] = nextInt();
}
dec[1] = 1;
inc[1] = 1;
for (int i = 2; i <=n; i++) {
dec[i] = i;
inc[i] = i;
if (arr[i-1] >= arr[i]) dec[i] = dec[i-1];
if (arr[i-1] <= arr[i]) inc[i] = inc[i-1];
}
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
for (int i = 1; i <=m; i++) {
int left = nextInt();
int right = nextInt();
out.println(inc[dec[right]] <= left?"Yes":"No");
}
out.close();
}
private static int nextInt() throws IOException {
st.nextToken();
return (int) st.nval;
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
1d6a9dcdaae05d3942ce27cd2a0d064f
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.math.*;
public class Task
{
public static void main(String[] args) throws IOException
{
new Task().run();
}
StreamTokenizer in;
Scanner ins;
PrintWriter out;
int nextInt() throws IOException
{
in.nextToken();
return (int)in.nval;
}
long nextLong() throws IOException
{
in.nextToken();
return (long)in.nval;
}
char nextChar() throws IOException{
in.nextToken();
return (char)in.ttype;
}
String nextString() throws IOException{
in.nextToken();
return in.sval;
}
long gcdLight(long a, long b){
a = Math.abs(a);
b = Math.abs(b);
while(a != 0 && b != 0){
if(a > b)
a %= b;
else
b %= a;
}
return a + b;
}
ForGCD gcd(int a,int b)
{
ForGCD tmp = new ForGCD();
if(a == 0)
{
tmp.x = 0;
tmp.y = 1;
tmp.d = b;
}
else
{
ForGCD tmp1 = gcd(b%a, a);
tmp.d = tmp1.d;
tmp.y = tmp1.x;
tmp.x = tmp1.y - tmp1.x*(b/a);
}
return tmp;
}
int[] upper, lower;
int A[];
int n,m;
void run() throws IOException
{
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
ins = new Scanner(System.in);
out = new PrintWriter(System.out);
try
{
if(System.getProperty("xDx")!=null)
{
in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
ins = new Scanner(new FileReader("input.txt"));
out = new PrintWriter(new FileWriter("output.txt"));
}
}
catch(Exception e)
{
}
n = nextInt();
m = nextInt();
A = new int[n];
upper = new int[n];
lower = new int[n];
for(int i = 0; i < n; i++){
A[i] = nextInt();
}
calcUp();
calcLow();
for(int i = 0; i < m; i++){
int l = nextInt() - 1;
int r = nextInt() - 1;
int k = upper[l];
if(k >= r){
out.println("Yes");
}else if(lower[k] >= r){
out.println("Yes");
}else{
out.println("No");
}
}
out.close();
}
private long pow(long x, long p) {
if(p == 0){
return 1;
}
long res = pow(x, p/2);
res *= res;
if(p%2 == 1){
res *= x;
}
return res;
}
private void calcUp() {
int e = 0;
int s = 0;
while(e < n){
for(; e < n - 1 && A[e] <= A[e + 1]; e++);
for(; s <= e; s++){
upper[s] = e;
}
s = ++e;
}
}
private void calcLow() {
int e = 0;
int s = 0;
while(e < n){
for(; e < n - 1 && A[e] >= A[e + 1]; e++);
for(; s <= e; s++){
lower[s] = e;
}
s = ++e;
}
}
class ForGCD
{
int x,y,d;
}
class Boxes implements Comparable
{
public long k,a;
public Boxes(long k, long a){
this.k = k;
this.a = a;
}
public int compareTo(Object obj)
{
Boxes b = (Boxes) obj;
if(k < b.k)
return -1;
else
if(k == b.k)
return 0;
else
return 1;
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
affb75d54c09cf211855d3900598d91b
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author George Marcus
*/
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);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int N = in.nextInt();
int M = in.nextInt();
int[] A = new int[N];
for(int i = 0; i < N; i++)
A[i] = in.nextInt();
int[] inc = new int[N];
int[] dec = new int[N];
inc[N - 1] = dec[N - 1] = 1;
for(int i = N - 2; i >= 0; i--) {
if(A[i] == A[i + 1]) {
inc[i] = inc[i + 1] + 1;
dec[i] = dec[i + 1] + 1;
}
else if(A[i] < A[i + 1]) {
dec[i] = 1;
inc[i] = inc[i + 1] + 1;
}
else if(A[i] > A[i + 1]) {
dec[i] = dec[i + 1] + 1;
inc[i] = 1;
}
}
for(int i = 0; i < M; i++) {
int left = in.nextInt() - 1;
int right = in.nextInt() - 1;
int to = left + inc[left] - 1;
to = to + dec[to] - 1;
if(to >= right)
out.println("Yes");
else
out.println("No");
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
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() {
return Integer.parseInt(nextString());
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
f61ba4485113722ac6f61b9fe8700449
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
new Solution().run();
}
}
final class Solution implements Runnable {
final static class Nim {
public int index;
public long g;
public int cnt;
public int lineType;
public Nim(int index, int cnt, long g, int lineType) {
this.index = index;
this.cnt = cnt;
this.g = g;
this.lineType = lineType;
}
public long getGrungy() {
if (cnt % 2 == 0)
return 0;
else
return g;
}
}
final static class Event implements Comparable<Event> {
public int pos;
public int delta;
public Event(int pos, int delta) {
this.pos = pos;
this.delta = delta;
}
@Override
public int compareTo(Event o) {
return pos - o.pos;
}
}
double INF = 1e10;
int OFFSET = 5000;
final static class Point {
final static Point UP = new Point(0, 1);
final static Point DOWN = new Point(0, -1);
final static Point RIGHT = new Point(1, 0);
final static Point LEFT = new Point(-1, 0);
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point add (Point o) {
return new Point(x + o.x, y + o.y);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point o = (Point)obj;
return x == o.x && y == o.y;
}
else
return false;
}
}
class Query {
public int left;
public int index;
public Query(int left, int index) {
this.left = left;
this.index = index;
}
}
int solve() throws Throwable {
int n = in.nextInt();
int m = in.nextInt();
boolean[] answers = new boolean[m];
ArrayList<Query>[] queries = new ArrayList[n];
for (int i = 0; i < queries.length; i++)
queries[i] = new ArrayList<>();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = in.nextInt();
for (int i = 0; i < m; i++) {
int l = in.nextInt() - 1;
int r = in.nextInt() - 1;
queries[r].add(new Query(l, i));
}
int biggestLadderLen = 1;
boolean afterPoint = false;
for (int i = 0; i < queries[0].size(); i++) {
answers[queries[0].get(i).index] = true;
}
int sameCount = 1;
for (int i = 1; i < n; i++) {
if (a[i] >= a[i - 1] && !afterPoint) {
biggestLadderLen++;
}
else if (a[i] <= a[i - 1] && afterPoint) {
biggestLadderLen++;
}
else if (a[i] < a[i - 1] && !afterPoint) {
afterPoint = true;
biggestLadderLen++;
}
else if (a[i] > a[i - 1] && afterPoint) {
biggestLadderLen = 2 + sameCount - 1;
afterPoint = false;
}
for (int j = 0; j < queries[i].size(); j++) {
Query cur = queries[i].get(j);
if (cur.left > i - biggestLadderLen)
answers[cur.index] = true;
else
answers[cur.index] = false;
}
if (a[i] == a[i - 1])
sameCount++;
else
sameCount = 1;
}
for (int i = 0; i < m; i++) {
out.println(answers[i] ? "Yes" : "No");
}
/*
int k = in.nextInt();
HashMap<Integer, ArrayList<Event>>[] events = new HashMap[2];
for (int i = 0; i < events.length; i++)
events[i] = new HashMap<>();
for (int i = 0; i < k; i++) {
int xb = in.nextInt();
int yb = in.nextInt();
int xe = in.nextInt();
int ye = in.nextInt();
if (xb == xe) {
if (events[0].get(xb) == null)
events[0].put(xb, new ArrayList<>());
if (yb > ye) {
int tmp = yb;
yb = ye;
ye = tmp;
}
ArrayList<Event> curX = events[0].get(xb);
curX.add(new Event(yb, 1));
curX.add(new Event(ye, -1));
} else {
if (events[1].get(yb) == null)
events[1].put(yb, new ArrayList<>());
if (xb > xe) {
int tmp = xb;
xb = xe;
xe = tmp;
}
ArrayList<Event> curY = events[1].get(yb);
curY.add(new Event(xb, 1));
curY.add(new Event(xe, -1));
}
}
HashMap<Integer, Integer>[] batchSizes = new HashMap[2];
for (int i = 0; i < batchSizes.length; i++)
batchSizes[i] = new HashMap<>();
for (int eventType = 0; eventType < events.length; eventType++) {
HashMap<Integer, ArrayList<Event>> curMap = events[eventType];
for (Map.Entry<Integer, ArrayList<Event>> kv : curMap.entrySet()) {
ArrayList<Event> curEvents = kv.getValue();
Collections.sort(curEvents);
int totalLen = eventType == 0 ? m : n;
int openedCount = 0;
Event prev = null;
for (int i = 0; i < curEvents.size(); i++) {
Event cur = curEvents.get(i);
if (openedCount != 0) {
totalLen -= (cur.pos - prev.pos);
}
openedCount += cur.delta;
prev = cur;
}
batchSizes[eventType].put(kv.getKey(), totalLen);
}
}
ArrayList<Nim> nims = new ArrayList<>();
nims.add(new Nim(-1, n - batchSizes[0].size() - 1, m, 0));
nims.add(new Nim(-1, m - batchSizes[1].size() - 1, n, 1));
for (int lineType = 0; lineType < batchSizes.length; lineType++) {
for (Map.Entry<Integer, Integer> entry : batchSizes[lineType].entrySet())
nims.add(new Nim(entry.getKey(), 1, entry.getValue(), lineType));
}
long grundy = 0;
for (int i = 0; i < nims.size(); i++)
grundy ^= nims.get(i).getGrungy();
if (grundy == 0)
out.print("SECOND");
else {
out.print("FIRST");
for (int i = 0; i < nims.size(); i++)
}
*/
return 0;
}
FastScanner in;
PrintWriter out;
@Override
public void run() {
try {
boolean ONLINE_JUDGE = false;
try {
Locale.setDefault(Locale.US);
ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
} catch (SecurityException e) {
System.err.println("Operation forbidden");
}
long time = System.currentTimeMillis();
if (ONLINE_JUDGE) {
in = new FastScanner(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new FastScanner(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
int ret = solve();
out.close();
in.close();
long delta = System.currentTimeMillis() - time;
long freeMem = Runtime.getRuntime().freeMemory();
long totalMem = Runtime.getRuntime().totalMemory();
System.err.printf("Time: %.3f s\n", delta / 1000.0f);
System.err.printf("Memory: %.3f MB\n", (float)(totalMem - freeMem) / (1 << 20));
if (ret != 0)
System.exit(ret);
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
}
@SuppressWarnings("unused")
final class FastScanner {
BufferedReader bufferedReader;
StringTokenizer stringTokenizer;
public FastScanner(Reader r) {
bufferedReader = new BufferedReader(r);
}
public String nextToken() throws IOException, NullPointerException {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens())
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
return stringTokenizer.nextToken();
}
public String nextLine() throws IOException {
return bufferedReader.readLine();
}
public int nextInt() throws IOException, NullPointerException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(nextToken());
}
public float nextFloat() throws IOException {
return Float.parseFloat(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public int read() throws IOException {
return bufferedReader.read();
}
public void close() throws IOException {
bufferedReader.close();
}
public int[] readIntArray(int n) throws Throwable {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
close();
}
}
final class DisjointSetUnion {
int[] parent;
int[] rank;
public DisjointSetUnion(int size) {
parent = new int[size];
rank = new int[size];
for (int i = 0; i < size; i++)
parent[i] = i;
}
public int getLeader(int element) {
if (parent[element] == element)
return element;
else
return parent[element] = getLeader(parent[element]);
}
public boolean union(int x, int y) {
x = getLeader(x);
y = getLeader(y);
if (x == y)
return true;
if (rank[x] < rank[y]) {
int temp = x;
x = y;
y = temp;
}
parent[y] = x;
if (rank[x] == rank[y])
++rank[x];
return false;
}
}
@SuppressWarnings("unused")
final class Utils {
private Utils() {}
final static int INF_I = Integer.MAX_VALUE;
final static double INF_D = Double.POSITIVE_INFINITY;
final static long INF_L = Long.MAX_VALUE;
public static int[] solveAssignmentProblem(double[][] a) {
int n = a.length - 1;
int m = a[0].length - 1;
double[] u = new double[n + 1], v = new double[m + 1], minv = new double[m + 1];
boolean[] used = new boolean[m + 1];
int[] p = new int[m + 1], prev = new int[m + 1];
for (int i = 1; i <= n; i++) {
Arrays.fill(used, false);
Arrays.fill(minv, INF_D);
int j0 = 0;
p[j0] = i;
do {
int i0 = p[j0];
used[j0] = true;
double delta = INF_D;
int j1 = 0;
for (int j = 1; j <= m; j++) {
if (!used[j]) {
double cur = a[i0][j] + u[i0] + v[j];
if (cur < minv[j]) {
minv[j] = cur;
prev[j] = j0;
}
if (minv[j] < delta) {
delta = minv[j];
j1 = j;
}
}
}
for (int j = 0; j <= m; j++) {
if (used[j]) {
u[p[j]] -= delta;
v[j] += delta;
} else
minv[j] -= delta;
}
j0 = j1;
} while (p[j0] != 0);
do {
int j1 = prev[j0];
p[j0] = p[j1];
j0 = j1;
} while (j0 != 0);
}
return p;
}
//a - 1 indexed
public static int[] hungarian(int[][] a) {
int n = a.length - 1;
int m = a[0].length - 1;
int[] prev = new int[m + 1];
int[] u = new int[n + 1];
int[] v = new int[m + 1];
boolean[] used = new boolean[m + 1];
int[] p = new int[m + 1];
int[] minv = new int[m + 1];
for (int i = 1; i <= n; i++) {
Arrays.fill(used, false);
Arrays.fill(minv, INF_I);
int j0 = 0;
p[j0] = i;
do {
used[j0] = true;
int i0 = p[j0];
int delta = INF_I;
int j1 = 0;
for (int j = 1; j <= m; j++) {
if (!used[j]) {
int cur = a[i0][j] + u[i0] + v[j];
if (cur < minv[j]) {
minv[j] = cur;
prev[j] = j0;
}
if (minv[j] < delta) {
j1 = j;
delta = minv[j];
}
}
}
for (int j = 0; j <= m; j++) {
if (used[j]) {
u[p[j]] -= delta;
v[j] += delta;
} else
minv[j] -= delta;
}
j0 = j1;
} while (p[j0] != 0);
do {
int j1 = prev[j0];
p[j0] = p[j1];
j0 = j1;
} while (j0 != 0);
}
return p;
}
//a - 1 indexed
public static int[] hungarian(long[][] a) {
int n = a.length - 1;
int m = a[0].length - 1;
int[] prev = new int[m + 1];
long[] u = new long[n + 1];
long[] v = new long[m + 1];
boolean[] used = new boolean[m + 1];
int[] p = new int[m + 1];
long[] minv = new long[m + 1];
for (int i = 1; i <= n; i++) {
Arrays.fill(used, false);
Arrays.fill(minv, INF_L);
int j0 = 0;
p[j0] = i;
do {
used[j0] = true;
int i0 = p[j0];
long delta = INF_L;
int j1 = 0;
for (int j = 1; j <= m; j++) {
if (!used[j]) {
long cur = a[i0][j] + u[i0] + v[j];
if (cur < minv[j]) {
minv[j] = cur;
prev[j] = j0;
}
if (minv[j] < delta) {
j1 = j;
delta = minv[j];
}
}
}
for (int j = 0; j <= m; j++) {
if (used[j]) {
u[p[j]] -= delta;
v[j] += delta;
} else
minv[j] -= delta;
}
j0 = j1;
} while (p[j0] != 0);
do {
int j1 = prev[j0];
p[j0] = p[j1];
j0 = j1;
} while (j0 != 0);
}
return p;
}
public static long sqr(long x) {
return x * x;
}
public static int gcd(int x, int y) {
while (y > 0) {
x %= y;
int temp = x;
x = y;
y = temp;
}
return x;
}
public static int lcm(int x, int y) {
return x / gcd(x, y) * y;
}
public static long gcd(long x, long y) {
while (y > 0) {
x %= y;
long temp = x;
x = y;
y = temp;
}
return x;
}
public static long lcm(long x, long y) {
return x / gcd(x, y) * y;
}
public static long modInverse(long x, long mod) {
return BigInteger.valueOf(x).modInverse(BigInteger.valueOf(mod)).longValue();
}
static Random r = new Random(System.nanoTime());
public static<T> void shuffle(T[] array) {
int n = array.length;
for (int i = 0; i < n; i++) {
int j = r.nextInt(n);
T t = array[i];
array[i] = array[j];
array[j] = t;
}
}
public static void shuffle(int[] array) {
int n = array.length;
for (int i = 0; i < n; i++) {
int j = r.nextInt(n);
int t = array[i];
array[i] = array[j];
array[j] = t;
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
8759f23bf1793de5e535ccf789c0ec28
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;
public class C {
int INF = 1 << 28;
//long INF = 1L << 62;
double EPS = 1e-10;
void run() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
int[] ar = new int[n+2]; ar[n+1] = (int)1e9 + 1;
for(int i=1;i<=n;i++) ar[i] = sc.nextInt();
int[] u = new int[n+2], d = new int[n+2];
for(int i=1;i<=n;i++) if(ar[i-1] >= ar[i]) d[i] = d[i-1] + 1; else d[i] = 1;
for(int i=n;i>=1;i--) if(ar[i+1] >= ar[i]) u[i] = u[i+1] + 1; else u[i] = 1;
for(int i=0;i<m;i++) {
int l = sc.nextInt(), r = sc.nextInt();
if(d[r] + u[l] > r - l) System.out.println("Yes");
else System.out.println("No");
}
}
void debug(Object... os) {
System.err.println(Arrays.deepToString(os));
}
public static void main(String[] args) {
new C().run();
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
cd54f301abcbffc156f23de7ee59a1e1
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
/* Codeforces Template */
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.fill;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.sort;
public class Main {
static long initTime;
static final Random rnd = new Random(7777L);
static boolean writeLog = false;
public static void main(String[] args) throws IOException {
initTime = System.currentTimeMillis();
try {
writeLog = "true".equals(System.getProperty("LOCAL_RUN_7777"));
} catch (SecurityException e) {}
new Thread(null, new Runnable() {
public void run() {
try {
try {
if (new File("input.txt").exists())
System.setIn(new FileInputStream("input.txt"));
} catch (SecurityException e) {}
long prevTime = System.currentTimeMillis();
new Main().run();
log("Total time: " + (System.currentTimeMillis() - prevTime) + " ms");
log("Memory status: " + memoryStatus());
} catch (IOException e) {
e.printStackTrace();
}
}
}, "1", 1L << 24).start();
}
void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
in.close();
}
/***************************************************************
* Solution
**************************************************************/
void solve() throws IOException {
int n = nextInt();
int m = nextInt();
int[] a = nextIntArray(n);
RMQ rmq = new RMQ(a);
int[] begin = new int [n];
int[] end = new int [n];
begin[0] = 0;
for (int i = 1; i < n; i++)
begin[i] = a[i] >= a[i - 1] ? begin[i - 1] : i;
end[n - 1] = n - 1;
for (int i = n - 2; i >= 0; i--)
end[i] = a[i] >= a[i + 1] ? end[i + 1] : i;
for (int q = 0; q < m; q++) {
int l = nextInt() - 1;
int r = nextInt() - 1;
int maxIdx = rmq.getMaxIdx(l, r);
out.println(begin[maxIdx] <= l && r <= end[maxIdx] ? "Yes" : "No");
}
}
class RMQ {
int[] val;
int[] ind;
int n;
RMQ(int[] a) {
this.n = a.length;
val = new int [2 * n + 1];
ind = new int [2 * n + 1];
for (int i = 0; i < n; i++) {
val[n + i] = a[i];
ind[n + i] = i;
}
for (int v = n - 1; v > 0; v--) {
int l = v << 1;
int r = l + 1;
if (val[l] > val[r]) {
val[v] = val[l];
ind[v] = ind[l];
} else {
val[v] = val[r];
ind[v] = ind[r];
}
}
}
int getMaxIdx(int l, int r) {
l += n;
r += n;
int max = Integer.MIN_VALUE;
int ret = -1;
while (l <= r) {
if ((l & 1) == 1)
if (max < val[l]) {
max = val[l];
ret = ind[l];
}
if ((r & 1) == 0)
if (max < val[r]) {
max = val[r];
ret = ind[r];
}
l = (l + 1) >> 1;
r = (r - 1) >> 1;
}
return ret;
}
}
/***************************************************************
* Input
**************************************************************/
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
int[] nextIntArray(int size) throws IOException {
int[] ret = new int [size];
for (int i = 0; i < size; i++)
ret[i] = nextInt();
return ret;
}
long[] nextLongArray(int size) throws IOException {
long[] ret = new long [size];
for (int i = 0; i < size; i++)
ret[i] = nextLong();
return ret;
}
double[] nextDoubleArray(int size) throws IOException {
double[] ret = new double [size];
for (int i = 0; i < size; i++)
ret[i] = nextDouble();
return ret;
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
/***************************************************************
* Output
**************************************************************/
void printRepeat(String s, int count) {
for (int i = 0; i < count; i++)
out.print(s);
}
void printArray(int[] array) {
if (array == null || array.length == 0)
return;
for (int i = 0; i < array.length; i++) {
if (i > 0) out.print(' ');
out.print(array[i]);
}
out.println();
}
void printArray(long[] array) {
if (array == null || array.length == 0)
return;
for (int i = 0; i < array.length; i++) {
if (i > 0) out.print(' ');
out.print(array[i]);
}
out.println();
}
void printArray(double[] array) {
if (array == null || array.length == 0)
return;
for (int i = 0; i < array.length; i++) {
if (i > 0) out.print(' ');
out.print(array[i]);
}
out.println();
}
void printArray(double[] array, String spec) {
if (array == null || array.length == 0)
return;
for (int i = 0; i < array.length; i++) {
if (i > 0) out.print(' ');
out.printf(Locale.US, spec, array[i]);
}
out.println();
}
void printArray(Object[] array) {
if (array == null || array.length == 0)
return;
boolean blank = false;
for (Object x : array) {
if (blank) out.print(' '); else blank = true;
out.print(x);
}
out.println();
}
@SuppressWarnings("rawtypes")
void printCollection(Collection collection) {
if (collection == null || collection.isEmpty())
return;
boolean blank = false;
for (Object x : collection) {
if (blank) out.print(' '); else blank = true;
out.print(x);
}
out.println();
}
/***************************************************************
* Utility
**************************************************************/
static String memoryStatus() {
return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() >> 20) + "/" + (Runtime.getRuntime().totalMemory() >> 20) + " MB";
}
static void checkMemory() {
System.err.println(memoryStatus());
}
static long prevTimeStamp = Long.MIN_VALUE;
static void updateTimer() {
prevTimeStamp = System.currentTimeMillis();
}
static long elapsedTime() {
return (System.currentTimeMillis() - prevTimeStamp);
}
static void checkTimer() {
System.err.println(elapsedTime() + " ms");
}
static void chk(boolean f) {
if (!f) throw new RuntimeException("Assert failed");
}
static void chk(boolean f, String format, Object ... args) {
if (!f) throw new RuntimeException(String.format(format, args));
}
static void log(String format, Object ... args) {
if (writeLog) System.err.println(String.format(Locale.US, format, args));
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
d9e3760869499b3e1471d7c30f4d60fc
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
public class Solution {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
AC solver = new AC();
solver.solve(in, out);
out.close();
}
}
class AC {
int a[] = new int[100010];
int l[] = new int[100010];
int r[] = new int[100010];
public void solve(InputReader in, PrintWriter out) {
int n , m;
n = in.nextInt() ; m = in.nextInt();
for(int i = 1; i <= n; i++) {
a[i] = in.nextInt();
l[i] = r[i] = i;
}
for(int i = n-1; i >= 1; i -- ) if(a[i] <= a[i+1]) l[i] = l[i+1];
for(int j = 2; j <= n; j++) if(a[j] <= a[j-1]) r[j] = r[j-1];
while(m-- > 0) {
int x,y;
x = in.nextInt();
y = in.nextInt();
if(l[x] >= r[y]) {
out.println("Yes");
} else out.println("No");
}
}
}
class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
if (!hasNext())
throw new RuntimeException();
return tokenizer.nextToken();
}
boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens())
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e) {
return false;
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
11787023d287c055ba145cd926d67ae2
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class C implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
public static void main(String[] args) {
new Thread(null, new C(), "", 64*1024*1024).start();
}
public void run() {
try {
long t1 = 0, t2 = 0, m1 = 0, m2 = 0;
if (LOCAL) {
t1 = System.currentTimeMillis();
m1 = Runtime.getRuntime().freeMemory();
}
Locale.setDefault(Locale.US);
if (LOCAL) {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
tok = new StringTokenizer("");
solve();
in.close();
out.close();
if (LOCAL) {
t2 = System.currentTimeMillis();
m2 = Runtime.getRuntime().freeMemory();
System.err.println("Time = " + (t2 - t1) + " ms.");
System.err.println("Memory = " + ((m1 - m2) / 1024) + " KB.");
}
} catch (Throwable e) {
e.printStackTrace(System.err);
throw new RuntimeException();
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
String line = in.readLine();
if (line == null) return null;
tok = new StringTokenizer(line);
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
static class Mergesort {
private Mergesort() {}
public static void sort(int[] a) {
mergesort(a, 0, a.length - 1);
}
public static void sort(long[] a) {
mergesort(a, 0, a.length - 1);
}
public static void sort(double[] a) {
mergesort(a, 0, a.length - 1);
}
private static final int MAGIC_VALUE = 42;
private static void mergesort(int[] a, int leftIndex, int rightIndex) {
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergesort(a, leftIndex, middleIndex);
mergesort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void mergesort(long[] a, int leftIndex, int rightIndex) {
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergesort(a, leftIndex, middleIndex);
mergesort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void mergesort(double[] a, int leftIndex, int rightIndex) {
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergesort(a, leftIndex, middleIndex);
mergesort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
int[] leftArray = new int[length1];
int[] rightArray = new int[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
}
}
}
private static void merge(long[] a, int leftIndex, int middleIndex, int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
long[] leftArray = new long[length1];
long[] rightArray = new long[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
}
}
}
private static void merge(double[] a, int leftIndex, int middleIndex, int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
double[] leftArray = new double[length1];
double[] rightArray = new double[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
}
}
}
private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
int current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
private static void insertionSort(long[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
long current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
private static void insertionSort(double[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
double current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
}
void debug(Object... o) {
if (LOCAL) {
System.err.println(Arrays.deepToString(o));
}
}
final static boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null;
//------------------------------------------------------------------------------
void solve() throws IOException {
int n = readInt();
int m = readInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = readInt();
}
int[] right = new int[n];
int[] left = new int[n];
for (int i = 0; i < n; ) {
if (i == n-1 || a[i] > a[i+1]) {
right[i] = i;
i++;
} else {
int j = i;
while (j+1 < n && a[j] <= a[j+1]) {
j++;
}
for (int k = i; k <= j; k++) {
right[k] = j;
}
i = j+1;
}
}
for (int i = n-1; i >= 0; ) {
if (i == 0 || a[i-1] < a[i]) {
left[i] = i;
i--;
} else {
int j = i;
while (j > 0 && a[j-1] >= a[j]) {
j--;
}
for (int k = j; k <= i; k++) {
left[k] = j;
}
i = j-1;
}
}
debug(a);
debug(left);
debug(right);
for (int i = 0; i < m; i++) {
int L = readInt() - 1;
int R = readInt() - 1;
if (right[L] >= left[R]) {
out.println("Yes");
} else {
out.println("No");
}
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
2dd2c953d4cd4517200b6f58b619187f
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
//package com.whimsycwd.Round171;
import java.util.Arrays;
import java.util.Scanner;
public class TaskC {
static boolean[][] L;
static boolean[][] R;
static int n;
static int m;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = in.nextInt();
}
L = new boolean[n][20];
R = new boolean[n][20];
for (int i = 0; i < n; ++i) {
L[i][0] = true;
R[i][0] = true;
}
for (int k = 1; k < 20; ++k) {
for (int i = 0; i < n; ++i)
if (i + (1 << k) <= n) {
L[i][k] = (a[i + (1 << (k - 1)) - 1] <= a[i
+ (1 << (k - 1))])
&& L[i][k - 1] && L[i + (1 << (k - 1))][k - 1];
}
for (int i = n - 1; i >= 0; --i)
if (i - (1 << k)+1 >= 0) {
R[i][k] = (a[i - (1 << (k - 1))] >= a[i - (1 << (k - 1))
+ 1])
&& R[i][k - 1] && R[i - (1 << (k - 1))][k - 1];
}
}
/* for (int i = 0;i<n;++i){
for (int j = 0;j<20;++j)
System.out.print(L[i][j]+" ");
System.out.println();
}
*/
for (int i = 0;i<m;++i){
int l = in.nextInt()-1;
int r = in.nextInt()-1;
int ll = Right(l,r);
int rr = Left(l,r);
// System.out.println(ll + " " + rr);
if (ll>=rr) System.out.println("Yes");
else System.out.println("No");
// break;
}
}
private static int Left(int l,int r) {
int ret = r;
int last = 19;
while (last>0){
// System.out.println(last + " " + ret);
if (ret-(1<<last)+1<l){
--last;
continue;
}
if (R[ret][last] == true){
ret = ret - (1<<last)+1;
continue;
}
--last;
}
return ret;
}
private static int Right(int l,int r) {
int ret = l;
int last = 19;
while (last>0){
// System.out.println(last + " " + ret);
if (ret+(1<<last)-1>r){
--last;
continue;
}
if (L[ret][last] == true){
ret = ret + (1<<last)-1;
continue;
}
--last;
}
return ret;
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
712ca16b78a816d9b02726abf1f6ca91
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.util.List;
public class Main {
private static StringTokenizer st;
private static BufferedReader br;
public static void print(Object x) {
System.out.println(x + "");
}
public static String join(List<?> x, String space) {
StringBuilder sb = new StringBuilder();
for (Object elt : x) {
sb.append(elt);
sb.append(space);
}
return sb.toString();
}
public static String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine().trim());
}
return st.nextToken();
}
public static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public static List<Double> nextDoubles(int n) throws IOException{
List<Double> lst = new ArrayList<Double>();
for (int i = 0; i < n; i++) {
lst.add(nextDouble());
}
return lst;
}
public static List<Integer> nextInts(int n) throws IOException{
List<Integer> lst = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
lst.add(nextInt());
}
return lst;
}
public static class Fenwick {
List<Integer> treeValues;
int length;
public Fenwick(int length) {
this.length = length;
treeValues = new ArrayList<Integer>();
for (int i = 0; i <= length; i++) {
treeValues.add(0);
}
}
public int get(int i) {
return getCumulative(i) - getCumulative(i - 1);
}
public int getCumulative(int i) {
//print("Getting cumulative " + i);
int total = 0;
while (i > 0) {
total += treeValues.get(i);
i -= (i & -i);
}
return total;
}
public void increment(int i, int val) {
//print("Incrementing " + i);
while (i <= length) {
treeValues.set(i, treeValues.get(i) + val);
i += (i & -i);
}
}
public void set(int i, int val) {
increment(i, val - get(i));
}
}
public static String solve(long s, long k) {
return "";
}
public static int rightmost_inc(int l, int r, Fenwick increasing) {
//In the range l...r inclusive.
//print(l + ", " + r);
if (increasing.getCumulative(l - 1) == increasing.getCumulative(r)) return l - 1;
if (r == l) return r;
int mid = (l + r) / 2;
if (increasing.getCumulative(mid) < increasing.getCumulative(r)) {
return rightmost_inc(mid + 1, r, increasing);
} else {
return rightmost_inc(l, mid, increasing);
}
}
public static int leftmost_dec(int l, int r, Fenwick decreasing) {
//print(l + ", " + r);
if (decreasing.getCumulative(l - 1) == decreasing.getCumulative(r)) return r + 1;
if (r == l) return l;
int mid = (l + r) / 2;
if (decreasing.getCumulative(l - 1) < decreasing.getCumulative(mid)) {
return leftmost_dec(l, mid, decreasing);
} else {
return leftmost_dec(mid + 1, r, decreasing);
}
}
public static void main(String[] args) throws Exception {
//cough drops!
boolean debug = false;
InputStream in = System.in;
if (debug) {
in = new FileInputStream("input.in");
}
br = new BufferedReader(new InputStreamReader(in));
int n = nextInt();
int m = nextInt();
int last = nextInt();
Fenwick increasing = new Fenwick(n - 1);
Fenwick decreasing = new Fenwick(n - 1);
for (int i = 0; i < n - 1; i++) {
int cur = nextInt();
if (cur > last) {
increasing.set(i + 1, 1);
} else if (cur < last) {
decreasing.set(i + 1, 1);
}
last = cur;
}
//Find rightmost increasing, then leftmost decreasing
//if #1 left of #2, then done.
List<String> answers = new ArrayList<String>();
for (int q = 0; q < m; q++) {
int l = nextInt();
int r = nextInt();
if (l == r) {
answers.add("Yes");
continue;
}
r -= 1;
//print("RI " + rightmost_inc(l, r, increasing) + ", LD " + leftmost_dec(l, r, decreasing));
if (rightmost_inc(l, r, increasing) < leftmost_dec(l, r, decreasing)) {
answers.add("Yes");
} else {
answers.add("No");
}
//1 2 1 3 3 5 2 1
// 1 0 1 0 1 0 0
//break;
}
print(join(answers, "\n"));
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
225243b1812e314049fb8ae8127fdc6a
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
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.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int q = in.nextInt();
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = in.nextInt();
}
ArrayList<Integer> left = new ArrayList<Integer>();
ArrayList<Integer> right = new ArrayList<Integer>();
for (int i = 1; i < n - 1; i++) {
if (ar[i] < ar[i - 1]) {
if (ar[i] < ar[i + 1]) {
left.add(i - 1);
right.add(i + 1);
} else if (ar[i] == ar[i + 1]) {
int j = i;
while (j < n - 1 && ar[i] == ar[j]) {
j++;
}
if (ar[i] < ar[j]) {
left.add(i - 1);
right.add(j);
}
i = j-1;
}
}
}
for (int i = 0; i < q; i++) {
int l = in.nextInt()-1;
int r = in.nextInt()-1;
int ind = -(Collections.binarySearch(left, l,
new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
if (a < b) {
return -1;
}
return 1;
}
}) + 1);
if (ind == left.size()) {
out.println("Yes");
} else {
if (right.get(ind) > r) {
out.println("Yes");
} else {
out.println("No");
}
}
}
out.close();
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 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 double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
4e4ffe21442520f0280ad2490a8f599d
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main
{
/********************************************** a list of common variables **********************************************/
private MyScanner scan = new MyScanner();
private PrintWriter out = new PrintWriter(System.out);
private final int SIZEN = (int)(1e5);
private final int MOD = (int)(1e9 + 7);
private final int[] DX = {0, 1, 0, -1}, DY = {-1, 0, 1, 0};
private ArrayList<Integer>[] edge;
public void foo()
{
int n = scan.nextInt();
int m = scan.nextInt();
int[] a = new int[n];
for(int i = 0;i < n;++i)
{
a[i] = scan.nextInt();
}
int[] left = new int[n];
int[] right = new int[n];
for(int i = 1;i < n;++i)
{
left[i] = a[i] > a[i - 1] ? i : left[i - 1];
}
right[n - 1] = n - 1;
for(int i = n - 2;i >= 0;--i)
{
right[i] = a[i] > a[i + 1] ? i : right[i + 1];
}
while(m-- > 0)
{
int x = scan.nextInt() - 1;
int y = scan.nextInt() - 1;
if(right[x] >= left[y])
{
out.println("Yes");
}
else
{
out.println("No");
}
}
}
public static void main(String[] args)
{
Main m = new Main();
m.foo();
m.out.close();
}
/********************************************** a list of common algorithms **********************************************/
/**
* 1---Get greatest common divisor
* @param a : first number
* @param b : second number
* @return greatest common divisor
*/
public long gcd(long a, long b)
{
return 0 == b ? a : gcd(b, a % b);
}
/**
* 2---Get the distance from a point to a line
* @param x1 the x coordinate of one endpoint of the line
* @param y1 the y coordinate of one endpoint of the line
* @param x2 the x coordinate of the other endpoint of the line
* @param y2 the y coordinate of the other endpoint of the line
* @param x the x coordinate of the point
* @param y the x coordinate of the point
* @return the distance from a point to a line
*/
public double getDist(long x1, long y1, long x2, long y2, long x, long y)
{
long a = y2 - y1;
long b = x1 - x2;
long c = y1 * (x2 - x1) - x1 * (y2 - y1);
return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
}
/**
* 3---Get the distance from one point to a segment (not a line)
* @param x1 the x coordinate of one endpoint of the segment
* @param y1 the y coordinate of one endpoint of the segment
* @param x2 the x coordinate of the other endpoint of the segment
* @param y2 the y coordinate of the other endpoint of the segment
* @param x the x coordinate of the point
* @param y the y coordinate of the point
* @return the distance from one point to a segment (not a line)
*/
public double ptToSeg(long x1, long y1, long x2, long y2, long x, long y)
{
double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
if(cross <= 0)
{
return (x - x1) * (x - x1) + (y - y1) * (y - y1);
}
double d = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
if(cross >= d)
{
return (x - x2) * (x - x2) + (y - y2) * (y - y2);
}
double r = cross / d;
double px = x1 + (x2 - x1) * r;
double py = y1 + (y2 - y1) * r;
return (x - px) * (x - px) + (y - py) * (y - py);
}
/**
* 4---KMP match, i.e. kmpMatch("abcd", "bcd") = 1, kmpMatch("abcd", "bfcd") = -1.
* @param s: String to match.
* @param t: String to be matched.
* @return if can match, first index; otherwise -1.
*/
public int[] kmpMatch(char[] s, char[] t)
{
int n = s.length;
int m = t.length;
int[] next = new int[m + 1];
next[0] = -1;
int j = -1;
for(int i = 1;i < m;++i)
{
while(j >= 0 && t[i] != t[j + 1])
{
j = next[j];
}
if(t[i] == t[j + 1])
{
++j;
}
next[i] = j;
}
int[] left = new int[n + 1];
j = -1;
for(int i = 0;i < n;++i)
{
while(j >= 0 && s[i] != t[j + 1])
{
j = next[j];
}
if(s[i] == t[j + 1])
{
++j;
}
if(j == m - 1)
{
left[i + 1] = i - m + 2;
j = next[j];
}
}
for(int i = 1;i <= n;++i)
{
if(0 == left[i])
{
left[i] = left[i - 1];
}
}
return left;
}
class MyScanner
{
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
BufferedInputStream bis = new BufferedInputStream(System.in);
public int read()
{
if (-1 == numChars)
{
throw new InputMismatchException();
}
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = bis.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 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 & 15;
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 & 15) * m;
c = read();
}
}
return res * sgn;
}
public String next()
{
int c = read();
while (isSpaceChar(c))
{
c = read();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c)
{
return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c;
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
d1e9a58bcb6a89b254adaecf149a77e1
|
train_001.jsonl
|
1362411000
|
You've got an array, consisting of n integers a1,βa2,β...,βan. Also, you've got m queries, the i-th query is described by two integers li,βri. Numbers li,βri define a subsegment of the original array, that is, the sequence of numbers ali,βaliβ+β1,βaliβ+β2,β...,βari. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers b1,βb2,β...,βbk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1ββ€βxββ€βk), that the following inequation fulfills: b1ββ€βb2ββ€β...ββ€βbxββ₯βbxβ+β1ββ₯βbxβ+β2...ββ₯βbk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
|
256 megabytes
|
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main
{
/********************************************** a list of common variables **********************************************/
private MyScanner scan = new MyScanner();
private PrintWriter out = new PrintWriter(System.out);
private final int SIZEN = (int)(1e5);
private final int MOD = (int)(1e9 + 7);
private final int[] DX = {0, 1, 0, -1}, DY = {-1, 0, 1, 0};
private ArrayList<Integer>[] edge;
public void foo()
{
int n = scan.nextInt();
int m = scan.nextInt();
int[] a = new int[n];
for(int i = 0;i < n;++i)
{
a[i] = scan.nextInt();
}
int[] left = new int[n];
int[] right = new int[n];
for(int i = 1;i < n;++i)
{
left[i] = a[i] > a[i - 1] ? i : left[i - 1];
}
right[n - 1] = n - 1;
for(int i = n - 2;i >= 0;--i)
{
right[i] = a[i] > a[i + 1] ? i : right[i + 1];
}
while(m-- > 0)
{
int x = scan.nextInt() - 1;
int y = scan.nextInt() - 1;
if(left[y] <= x || right[x] >= y || right[x] >= left[y])
{
out.println("Yes");
}
else
{
out.println("No");
}
}
}
public static void main(String[] args)
{
Main m = new Main();
m.foo();
m.out.close();
}
/********************************************** a list of common algorithms **********************************************/
/**
* 1---Get greatest common divisor
* @param a : first number
* @param b : second number
* @return greatest common divisor
*/
public long gcd(long a, long b)
{
return 0 == b ? a : gcd(b, a % b);
}
/**
* 2---Get the distance from a point to a line
* @param x1 the x coordinate of one endpoint of the line
* @param y1 the y coordinate of one endpoint of the line
* @param x2 the x coordinate of the other endpoint of the line
* @param y2 the y coordinate of the other endpoint of the line
* @param x the x coordinate of the point
* @param y the x coordinate of the point
* @return the distance from a point to a line
*/
public double getDist(long x1, long y1, long x2, long y2, long x, long y)
{
long a = y2 - y1;
long b = x1 - x2;
long c = y1 * (x2 - x1) - x1 * (y2 - y1);
return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
}
/**
* 3---Get the distance from one point to a segment (not a line)
* @param x1 the x coordinate of one endpoint of the segment
* @param y1 the y coordinate of one endpoint of the segment
* @param x2 the x coordinate of the other endpoint of the segment
* @param y2 the y coordinate of the other endpoint of the segment
* @param x the x coordinate of the point
* @param y the y coordinate of the point
* @return the distance from one point to a segment (not a line)
*/
public double ptToSeg(long x1, long y1, long x2, long y2, long x, long y)
{
double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
if(cross <= 0)
{
return (x - x1) * (x - x1) + (y - y1) * (y - y1);
}
double d = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
if(cross >= d)
{
return (x - x2) * (x - x2) + (y - y2) * (y - y2);
}
double r = cross / d;
double px = x1 + (x2 - x1) * r;
double py = y1 + (y2 - y1) * r;
return (x - px) * (x - px) + (y - py) * (y - py);
}
/**
* 4---KMP match, i.e. kmpMatch("abcd", "bcd") = 1, kmpMatch("abcd", "bfcd") = -1.
* @param s: String to match.
* @param t: String to be matched.
* @return if can match, first index; otherwise -1.
*/
public int[] kmpMatch(char[] s, char[] t)
{
int n = s.length;
int m = t.length;
int[] next = new int[m + 1];
next[0] = -1;
int j = -1;
for(int i = 1;i < m;++i)
{
while(j >= 0 && t[i] != t[j + 1])
{
j = next[j];
}
if(t[i] == t[j + 1])
{
++j;
}
next[i] = j;
}
int[] left = new int[n + 1];
j = -1;
for(int i = 0;i < n;++i)
{
while(j >= 0 && s[i] != t[j + 1])
{
j = next[j];
}
if(s[i] == t[j + 1])
{
++j;
}
if(j == m - 1)
{
left[i + 1] = i - m + 2;
j = next[j];
}
}
for(int i = 1;i <= n;++i)
{
if(0 == left[i])
{
left[i] = left[i - 1];
}
}
return left;
}
class MyScanner
{
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
BufferedInputStream bis = new BufferedInputStream(System.in);
public int read()
{
if (-1 == numChars)
{
throw new InputMismatchException();
}
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = bis.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 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 & 15;
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 & 15) * m;
c = read();
}
}
return res * sgn;
}
public String next()
{
int c = read();
while (isSpaceChar(c))
{
c = read();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c)
{
return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c;
}
}
}
|
Java
|
["8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8"]
|
2 seconds
|
["Yes\nYes\nNo\nYes\nNo\nYes"]
| null |
Java 7
|
standard input
|
[
"dp",
"two pointers",
"implementation"
] |
c39db222c42d8e85dee5686088dc3dac
|
The first line contains two integers n and m (1ββ€βn,βmββ€β105) β the number of array elements and the number of queries. The second line contains the sequence of integers a1,βa2,β...,βan (1ββ€βaiββ€β109), where number ai stands for the i-th array element. The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1ββ€βliββ€βriββ€βn) β the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
| 1,700 |
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
|
standard output
| |
PASSED
|
8553a8f1a05ad91b1c419969762448db
|
train_001.jsonl
|
1589286900
|
For the multiset of positive integers $$$s=\{s_1,s_2,\dots,s_k\}$$$, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of $$$s$$$ as follow: $$$\gcd(s)$$$ is the maximum positive integer $$$x$$$, such that all integers in $$$s$$$ are divisible on $$$x$$$. $$$\textrm{lcm}(s)$$$ is the minimum positive integer $$$x$$$, that divisible on all integers from $$$s$$$.For example, $$$\gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6$$$ and $$$\textrm{lcm}(\{4,6\})=12$$$. Note that for any positive integer $$$x$$$, $$$\gcd(\{x\})=\textrm{lcm}(\{x\})=x$$$.Orac has a sequence $$$a$$$ with length $$$n$$$. He come up with the multiset $$$t=\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\}$$$, and asked you to find the value of $$$\gcd(t)$$$ for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
|
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;
}
}
static int primes[];
public static void main(String args[]) throws IOException {
Scan input=new Scan();
sieve(200001);
int n=input.scanInt();
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=input.scanInt();
}
// fact(n,arr);
System.out.println(solve(n,arr));
}
public static long solve(int n,int arr[]) {
HashMap<Integer,Integer> map=new HashMap<>();
int fact[][]=new int[30][primes.length];
for(int i=0;i<primes.length;i++) {
fact[0][i]=n;
map.put(primes[i], i);
}
for(int i=0;i<n;i++) {
if(arr[i]==1) {
continue;
}
if(arr[i]!=1 && !sieve[arr[i]]) {
fact[1][map.get(arr[i])]++;
fact[0][map.get(arr[i])]--;
continue;
}
for(int j=0;j<primes.length;j++) {
int cnt=0;
while(arr[i]%primes[j]==0) {
arr[i]/=primes[j];
cnt++;
}
fact[cnt][j]++;
fact[0][j]--;
if(arr[i]==1) {
break;
}
}
}
long ans=1;
for(int i=0;i<primes.length;i++) {
int sum=0;
for(int j=0;j<30;j++) {
sum+=fact[j][i];
if(sum>=2) {
ans*=(int)Math.pow(primes[i],j);
break;
}
}
}
return ans;
}
static boolean sieve[];
//false for prime number and true for composite number
public static void sieve(int n) {
sieve=new boolean[n];
primes=new int[17984];
int indx=0;
for(int i=2;i<n;i++) {
if(!sieve[i]) {
primes[indx]=i;
indx++;
for(int j=2*i;j<n;j=j+i) {
sieve[j]=true;
}
}
}
// System.out.println(indx);
}
}
|
Java
|
["2\n1 1", "4\n10 24 40 80", "10\n540 648 810 648 720 540 594 864 972 648"]
|
3 seconds
|
["1", "40", "54"]
|
NoteFor the first example, $$$t=\{\textrm{lcm}(\{1,1\})\}=\{1\}$$$, so $$$\gcd(t)=1$$$.For the second example, $$$t=\{120,40,80,120,240,80\}$$$, and it's not hard to see that $$$\gcd(t)=40$$$.
|
Java 11
|
standard input
|
[
"number theory",
"math"
] |
3634a3367a1f05d1b3e8e4369e8427fb
|
The first line contains one integer $$$n\ (2\le n\le 100\,000)$$$. The second line contains $$$n$$$ integers, $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 200\,000$$$).
| 1,600 |
Print one integer: $$$\gcd(\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\})$$$.
|
standard output
| |
PASSED
|
ae24cd4660b6c4629be9e87f23bec125
|
train_001.jsonl
|
1589286900
|
For the multiset of positive integers $$$s=\{s_1,s_2,\dots,s_k\}$$$, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of $$$s$$$ as follow: $$$\gcd(s)$$$ is the maximum positive integer $$$x$$$, such that all integers in $$$s$$$ are divisible on $$$x$$$. $$$\textrm{lcm}(s)$$$ is the minimum positive integer $$$x$$$, that divisible on all integers from $$$s$$$.For example, $$$\gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6$$$ and $$$\textrm{lcm}(\{4,6\})=12$$$. Note that for any positive integer $$$x$$$, $$$\gcd(\{x\})=\textrm{lcm}(\{x\})=x$$$.Orac has a sequence $$$a$$$ with length $$$n$$$. He come up with the multiset $$$t=\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\}$$$, and asked you to find the value of $$$\gcd(t)$$$ for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class C
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int n=Integer.parseInt(bu.readLine());
String s[]=bu.readLine().split(" ");
int a[]=new int[n],i;
for(i=0;i<n;i++)
a[i]=Integer.parseInt(s[i]);
long pre[]=new long[n],suf[]=new long[n],ans=0;
pre[0]=a[0]; suf[n-1]=a[n-1];
for(i=1;i<n;i++)
pre[i]=gcd(pre[i-1],a[i]);
for(i=n-2;i>=0;i--)
suf[i]=gcd(suf[i+1],a[i]);
ans=suf[1];
for(i=1;i<n-1;i++)
{
long g=gcd(pre[i-1],suf[i+1]);
ans=ans*g/gcd(g,ans);
}
ans=ans*pre[n-2]/gcd(pre[n-2],ans);
System.out.print(ans);
}
static long gcd(long a,long b)
{
if(b>a) a=a^b^(b=a);
while(b!=0)
{
long t=b;
b=a%b;
a=t;
}
return a;
}
}
|
Java
|
["2\n1 1", "4\n10 24 40 80", "10\n540 648 810 648 720 540 594 864 972 648"]
|
3 seconds
|
["1", "40", "54"]
|
NoteFor the first example, $$$t=\{\textrm{lcm}(\{1,1\})\}=\{1\}$$$, so $$$\gcd(t)=1$$$.For the second example, $$$t=\{120,40,80,120,240,80\}$$$, and it's not hard to see that $$$\gcd(t)=40$$$.
|
Java 11
|
standard input
|
[
"number theory",
"math"
] |
3634a3367a1f05d1b3e8e4369e8427fb
|
The first line contains one integer $$$n\ (2\le n\le 100\,000)$$$. The second line contains $$$n$$$ integers, $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 200\,000$$$).
| 1,600 |
Print one integer: $$$\gcd(\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\})$$$.
|
standard output
| |
PASSED
|
bbb36570698aaab8b53479421d6765c8
|
train_001.jsonl
|
1589286900
|
For the multiset of positive integers $$$s=\{s_1,s_2,\dots,s_k\}$$$, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of $$$s$$$ as follow: $$$\gcd(s)$$$ is the maximum positive integer $$$x$$$, such that all integers in $$$s$$$ are divisible on $$$x$$$. $$$\textrm{lcm}(s)$$$ is the minimum positive integer $$$x$$$, that divisible on all integers from $$$s$$$.For example, $$$\gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6$$$ and $$$\textrm{lcm}(\{4,6\})=12$$$. Note that for any positive integer $$$x$$$, $$$\gcd(\{x\})=\textrm{lcm}(\{x\})=x$$$.Orac has a sequence $$$a$$$ with length $$$n$$$. He come up with the multiset $$$t=\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\}$$$, and asked you to find the value of $$$\gcd(t)$$$ for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
|
256 megabytes
|
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
public class C {
//author: Nagabhushan S Baddi
private static int n;
private static int[] a;
private static int d;
private static String s;
private static HashMap<Integer, ArrayList<Integer>> g;
public static void main(String[] args) {
// pd(Math.pow(3, 34)%3==0);
n = ini();
a = ina(n);
if (n==1) {
pd(a[0]);
return;
}
// out:
// for(int i=) {
// for(int j=0; j<n; j++) {
// if (i!=a[j] && a[j]%i==0) {
// pd(i);
// break out;
// }
// }
// }
int MX = 2*(int)1e5+100;
boolean[] isPrime = new boolean[MX];
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
int[] count = new int[MX];
HashMap<Integer, Integer> set = new HashMap<>();
for(int x: a) {
set.put(x, set.getOrDefault(x, 0)+1);
}
ArrayList<Integer> primes = new ArrayList<>();
for(int i=2; i<MX; i++) {
if (isPrime[i]) {
for(int j=i; j<MX; j+=i) {
if (j!=i) isPrime[j] = false;
if (set.containsKey(j)) {
count[i]+=set.get(j);
}
}
}
}
long ans = 1;
// pd(MX);
for(int i=2; i<MX; i++) {
if (isPrime[i]) {
// pd(i+" "+count[i]);
if (count[i]>=n-1) {
// ans *= i;
primes.add(i);
}
}
}
for(int p: primes) {
// System.out.println(p);
int min1 = (int)1e9;
int min2 = (int)1e9;
for(int j=0; j<n; j++) {
int counter = 0;
while(a[j]%p==0) {
a[j]/=p;
counter++;
}
if (counter<min1) {
min2 = min1;
min1 = counter;
} else if (counter<min2) {
min2 = counter;
}
}
long power = 1;
for(int z=1; z<=min2; z++) power*=p;
ans *= power;
}
pd(ans);
out.flush();
out.close();
}
//CONSTANTS
private static final int MOD = (int) 1e9 + 7;
//NONPROBLEM CODE
private static InputReader in = new InputReader(System.in);
private static PrintWriter out = new PrintWriter(System.out);
private static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 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 {
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 {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
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 String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
//SORT SHORTCUTS
private static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++) {
b[i] = a[i];
}
Arrays.sort(b);
for (int i = 0; i < n; i++) {
a[i] = b[i];
}
}
private static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++) {
b[i] = a[i];
}
Arrays.sort(b);
for (int i = 0; i < n; i++) {
a[i] = b[i];
}
}
//INPUT SHORTCUTS
private static int[] ina(int n) {
int[] temp = new int[n];
for (int i = 0; i < n; i++) {
temp[i] = in.nextInt();
}
return temp;
}
private static int ini() {
return in.nextInt();
}
private static long inl() {
return in.nextLong();
}
private static String ins() {
return in.readString();
}
//PRINT SHORTCUTS
private static void println(Object... o) {
for (Object x : o) {
out.write(x + "");
}
out.write("\n");
}
private static void pd(Object... o) {
for (Object x : o) {
out.write(x + "");
}
out.flush();
out.write("\n");
}
private static void print(Object... o) {
for (Object x : o) {
out.write(x + "");
}
}
//GRAPH SHORTCUTS
private static HashMap<Integer, ArrayList<Integer>> intree(int n) {
HashMap<Integer, ArrayList<Integer>> g = new HashMap<>();
for (int i = 0; i < n; i++) {
g.put(i, new ArrayList<>());
}
for (int i = 0; i < n - 1; i++) {
int u = ini() - 1;
int v = ini() - 1;
g.get(u).add(v);
g.get(v).add(u);
}
return g;
}
private static HashMap<Integer, ArrayList<Integer>> ingraph(int n, int m) {
HashMap<Integer, ArrayList<Integer>> g = new HashMap<>();
for (int i = 0; i < n; i++) {
g.put(i, new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int u = ini() - 1;
int v = ini() - 1;
g.get(u).add(v);
g.get(v).add(u);
}
return g;
}
private static HashMap<Integer, ArrayList<Edge>> inweightedgraph(int n, int m) {
HashMap<Integer, ArrayList<Edge>> g = new HashMap<>();
for (int i = 0; i < n; i++) {
g.put(i, new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int u = ini() - 1;
int v = ini() - 1;
int w = ini();
Edge edge = new Edge(u, v, w);
g.get(u).add(edge);
g.get(v).add(edge);
}
return g;
}
private static class Edge implements Comparable<Edge> {
private int u, v;
private long w;
public Edge(int a, int b, long c) {
u = a;
v = b;
w = c;
}
public int other(int x) {
return (x == u ? v : u);
}
public int compareTo(Edge edge) {
return Long.compare(w, edge.w);
}
}
private static class Pair {
private int u, v;
public Pair(int a, int b) {
u = a;
v = b;
}
public int hashCode() {
return u + v + u * v;
}
public boolean equals(Object object) {
Pair pair = (Pair) object;
return u == pair.u && v == pair.v;
}
}
private static class Node implements Comparable<Node> {
private int u;
private long dist;
public Node(int a, long b) {
u = a;
dist = b;
}
public int compareTo(Node node) {
return Long.compare(dist, node.dist);
}
}
//MATHS AND NUMBER THEORY SHORTCUTS
private static int gcd(int a, int b) {
//O(log(min(a,b)))
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long modExp(long a, long b) {
if (b == 0)
return 1;
a %= MOD;
long exp = modExp(a, b / 2);
if (b % 2 == 0) {
return (exp * exp) % MOD;
} else {
return (a * ((exp * exp) % MOD)) % MOD;
}
}
}
|
Java
|
["2\n1 1", "4\n10 24 40 80", "10\n540 648 810 648 720 540 594 864 972 648"]
|
3 seconds
|
["1", "40", "54"]
|
NoteFor the first example, $$$t=\{\textrm{lcm}(\{1,1\})\}=\{1\}$$$, so $$$\gcd(t)=1$$$.For the second example, $$$t=\{120,40,80,120,240,80\}$$$, and it's not hard to see that $$$\gcd(t)=40$$$.
|
Java 11
|
standard input
|
[
"number theory",
"math"
] |
3634a3367a1f05d1b3e8e4369e8427fb
|
The first line contains one integer $$$n\ (2\le n\le 100\,000)$$$. The second line contains $$$n$$$ integers, $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 200\,000$$$).
| 1,600 |
Print one integer: $$$\gcd(\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\})$$$.
|
standard output
| |
PASSED
|
bd4b00943853831dd5b748b962ee9e00
|
train_001.jsonl
|
1589286900
|
For the multiset of positive integers $$$s=\{s_1,s_2,\dots,s_k\}$$$, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of $$$s$$$ as follow: $$$\gcd(s)$$$ is the maximum positive integer $$$x$$$, such that all integers in $$$s$$$ are divisible on $$$x$$$. $$$\textrm{lcm}(s)$$$ is the minimum positive integer $$$x$$$, that divisible on all integers from $$$s$$$.For example, $$$\gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6$$$ and $$$\textrm{lcm}(\{4,6\})=12$$$. Note that for any positive integer $$$x$$$, $$$\gcd(\{x\})=\textrm{lcm}(\{x\})=x$$$.Orac has a sequence $$$a$$$ with length $$$n$$$. He come up with the multiset $$$t=\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\}$$$, and asked you to find the value of $$$\gcd(t)$$$ for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
|
256 megabytes
|
import java.util.*;
public class Main {
static long GCD(long a, long b) {
while(a > 0 && b > 0) {
if (a > b)
a %= b;
else
b %= a;
}
return a + b;
}
static long LCM(long a, long b) {
return a / GCD(a, b) * b;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long a = sc.nextInt();
long b, res = 0, lcm;
for(int i = 1; i < n; i++) {
b = sc.nextInt();
lcm = LCM(a, b);
res = GCD(res, lcm);
a = GCD(a, b);
}
System.out.println(res);
sc.close();
}
}
|
Java
|
["2\n1 1", "4\n10 24 40 80", "10\n540 648 810 648 720 540 594 864 972 648"]
|
3 seconds
|
["1", "40", "54"]
|
NoteFor the first example, $$$t=\{\textrm{lcm}(\{1,1\})\}=\{1\}$$$, so $$$\gcd(t)=1$$$.For the second example, $$$t=\{120,40,80,120,240,80\}$$$, and it's not hard to see that $$$\gcd(t)=40$$$.
|
Java 11
|
standard input
|
[
"number theory",
"math"
] |
3634a3367a1f05d1b3e8e4369e8427fb
|
The first line contains one integer $$$n\ (2\le n\le 100\,000)$$$. The second line contains $$$n$$$ integers, $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 200\,000$$$).
| 1,600 |
Print one integer: $$$\gcd(\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\})$$$.
|
standard output
| |
PASSED
|
5e83a02153950a9aba086236ef1cc100
|
train_001.jsonl
|
1589286900
|
For the multiset of positive integers $$$s=\{s_1,s_2,\dots,s_k\}$$$, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of $$$s$$$ as follow: $$$\gcd(s)$$$ is the maximum positive integer $$$x$$$, such that all integers in $$$s$$$ are divisible on $$$x$$$. $$$\textrm{lcm}(s)$$$ is the minimum positive integer $$$x$$$, that divisible on all integers from $$$s$$$.For example, $$$\gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6$$$ and $$$\textrm{lcm}(\{4,6\})=12$$$. Note that for any positive integer $$$x$$$, $$$\gcd(\{x\})=\textrm{lcm}(\{x\})=x$$$.Orac has a sequence $$$a$$$ with length $$$n$$$. He come up with the multiset $$$t=\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\}$$$, and asked you to find the value of $$$\gcd(t)$$$ for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
|
256 megabytes
|
/**
* ******* Created on 12/5/20 5:22 PM*******
*/
import java.io.*;
import java.util.*;
public class C1350 implements Runnable {
private static final int MAX = 2*(int) (1E5 + 5);
private static final int MOD = (int) (1E9 + 7);
private static final long Inf = (long) (1E14 + 10);
private static final double eps = (double) (1E-9);
private void solve() throws IOException {
int t = 1;
//t = reader.nextInt();
while (t-- > 0) {
int n = reader.nextInt();
int[] cnt = new int[MAX];
for(int i=0;i<n;i++){
int a = reader.nextInt();
for(int j=2;j*j<=a;j++){
int pos =j;
while (a%j ==0){
cnt[pos]++;
pos*=j;
a/=j;
}
}
if(a>1)cnt[a]++;
}
long ans =1;
for(int i=2;i<MAX;i++){
if(cnt[i] < n-1)continue;
int j=i;
for(;j<MAX;j*=i){
if(cnt[j] >= n-1)
ans*=i;
else
break;
cnt[j]=0;
}
}
writer.println(ans);
}
}
public static void main(String[] args) throws IOException {
try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
new C1350().run();
}
}
StandardInput reader;
PrintWriter writer;
@Override
public void run() {
try {
reader = new StandardInput();
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
interface Input extends Closeable {
String next() throws IOException;
String nextLine() throws IOException;
default int nextInt() throws IOException {
return Integer.parseInt(next());
}
default long nextLong() throws IOException {
return Long.parseLong(next());
}
default double nextDouble() throws IOException {
return Double.parseDouble(next());
}
default int[] readIntArray() throws IOException {
return readIntArray(nextInt());
}
default int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextInt();
}
return array;
}
default long[] readLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextLong();
}
return array;
}
}
private static class StandardInput implements Input {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer stringTokenizer;
@Override
public void close() throws IOException {
reader.close();
}
@Override
public String next() throws IOException {
if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
@Override
public String nextLine() throws IOException {
return reader.readLine();
}
}
}
|
Java
|
["2\n1 1", "4\n10 24 40 80", "10\n540 648 810 648 720 540 594 864 972 648"]
|
3 seconds
|
["1", "40", "54"]
|
NoteFor the first example, $$$t=\{\textrm{lcm}(\{1,1\})\}=\{1\}$$$, so $$$\gcd(t)=1$$$.For the second example, $$$t=\{120,40,80,120,240,80\}$$$, and it's not hard to see that $$$\gcd(t)=40$$$.
|
Java 11
|
standard input
|
[
"number theory",
"math"
] |
3634a3367a1f05d1b3e8e4369e8427fb
|
The first line contains one integer $$$n\ (2\le n\le 100\,000)$$$. The second line contains $$$n$$$ integers, $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 200\,000$$$).
| 1,600 |
Print one integer: $$$\gcd(\{\textrm{lcm}(\{a_i,a_j\})\ |\ i<j\})$$$.
|
standard output
| |
PASSED
|
7681a702a8e2d95b367944faeeeb70a3
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter printWriter = new PrintWriter(System.out);
int n = Integer.parseInt(in.readLine());
int t = (int)(Math.log(n) / Math.log(2));
String sNumbers[] = in.readLine().split(" ");
int [] numbers = new int[n];
for (int i = 0; i < n; i++) {
numbers[i] = Integer.parseInt(sNumbers[i]);
}
int result = 0;
int k =0;
for (int i = 1; i < n; i++) {
while (true) {
k = i + (int) Math.pow(2, t);
if (k<=n) {
break;
}
else {
t--;
}
}
numbers[k-1] = numbers[k-1] + numbers[i-1];
result = result + numbers[i-1];
System.out.println(result);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 8
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
db19e98360b583996a5c1aeea93c8594
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter printWriter = new PrintWriter(System.out);
int n = Integer.parseInt(in.readLine());
int t = (int) (Math.log(n) / Math.log(2));
String sNumbers[] = in.readLine().split(" ");
int[] numbers = new int[n];
for (int i = 0; i < n; i++) {
numbers[i] = Integer.parseInt(sNumbers[i]);
}
int result = 0;
int k = 0;
for (int i = 1; i < n; i++) {
if (numbers[i - 1] != 0) {
if (t != 0) {
while (true) {
k = i + (int) Math.pow(2, t);
if (k <= n) {
break;
} else {
t--;
}
}
} else {
k = i;
}
numbers[k - 1] = numbers[k - 1] + numbers[i - 1];
result = result + numbers[i - 1];
}
System.out.println(result);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 8
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
b9f417879d6525041bfca36e7c202795
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class A178 {
public static void main(String[] args) throws IOException
{
FS input = new FS(System.in);
int n = input.nextInt();
int[] as = new int[n];
long[] res = new long[n-1];
for(int i = 0; i<n-1; i++)
{
as[i] += input.nextInt();
//if(as[i] == 0) continue;
int max = i+1;
for(int j = 0; j<20; j++)
{
if(i+(1<<j) < n) max = Math.max(max, i+(1<<j));
}
res[i] = i == 0 ? 0 : res[i-1];
res[i] += as[i];
as[max] += as[i];
//if(max < n-1)res[max]+=as[i];
}
//for(int i= 1; i<res.length; i++) res[i] += res[i-1];
PrintWriter out = new PrintWriter(System.out);
for(int i = 0; i<n-1; i++) out.println(res[i]);
out.close();
}
static class FS
{
BufferedReader br;
StringTokenizer st;
public FS(InputStream input)
{
br = new BufferedReader(new InputStreamReader(input));
st = new StringTokenizer("");
}
public FS(File file) throws IOException
{
br = new BufferedReader(new FileReader(file));
st = new StringTokenizer("");
}
public String next() throws IOException
{
if(st.hasMoreTokens())
{
return st.nextToken();
}
else
{
st = new StringTokenizer(br.readLine());
return next();
}
}
public int nextInt() throws IOException
{
return Integer.parseInt(next());
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 8
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
cfd0f42374708a3742461a0776c2a540
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CodeForces
{
public static void main(String[] args)
{
Scanner input = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int n = input.nextInt();
int[] array = new int[n];
for (int i = 0; i < array.length; i++)
{
array[i] = input.nextInt();
}
StringBuilder str = new StringBuilder();
int count = 0;
for (int i = 0; i < array.length - 1; i++)
{
while (array[i] != 0)
{
int z = array.length - 1 - i;
int pos = (int) (Math.log(z) / Math.log(2));
while (pos >= array.length || array[i + (int) Math.pow(2, pos)] == 0)
{
pos--;
}
array[i + (int) Math.pow(2, pos)] += array[i];
count += array[i];
array[i] = 0;
}
str.append(count);
str.append("\n");
}
System.out.println(str.toString());
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 8
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
c846d3b6a0aa5da864167d0a2a9fd63c
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.util.Scanner;
public class A178 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
long[] A = new long[N];
for (int n=0; n<N; n++) {
A[n] = in.nextInt();
}
StringBuilder output = new StringBuilder();
long answer = 0;
for (int n=0; n<N-1; n++) {
if (A[n] > 0) {
int tPow = 1;
while (n+tPow+tPow < N) {
tPow <<= 1;
}
A[n+tPow] += A[n];
answer += A[n];
A[n] = 0;
}
output.append(answer).append('\n');
}
System.out.print(output);
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 8
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
9144a49bca6ab86f5f032769b191547d
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
public class A {
final int MOD = 1000000007;
final double eps = 1e-12;
public A () throws IOException {
int N = sc.nextInt();
long [] A = sc.nextLongs();
start();
long res = 0;
for (int k = 0; k < N-1; ++k) {
int t = 0;
while ((k+1) + (1 << t) <= N) ++t;
--t;
A[k + (1 << t)] += A[k];
res += A[k];
print(res);
}
}
////////////////////////////////////////////////////////////////////////////////////
static MyScanner sc;
static PrintWriter pw = new PrintWriter(System.out);
static void print (Object... a) {
StringBuffer b = new StringBuffer();
for (Object o : a)
b.append(" ").append(o);
pw.println(b.toString().trim());
}
static void exit (Object... a) {
print(a);
exit();
}
static void exit () {
pw.flush();
System.err.println("------------------");
System.err.println("Time: " + (millis() - t) / 1000.0);
System.exit(0);
}
static class MyScanner {
String next() throws IOException {
newLine();
return line[index++];
}
char [] nextChars() throws IOException {
return next().toCharArray();
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
String nextLine() throws IOException {
line = null;
return r.readLine();
}
String [] nextStrings() throws IOException {
line = null;
return r.readLine().split(" ");
}
int [] nextInts() throws IOException {
String [] L = nextStrings();
int [] res = new int [L.length];
for (int i = 0; i < L.length; ++i)
res[i] = Integer.parseInt(L[i]);
return res;
}
long [] nextLongs() throws IOException {
String [] L = nextStrings();
long [] res = new long [L.length];
for (int i = 0; i < L.length; ++i)
res[i] = Long.parseLong(L[i]);
return res;
}
boolean eol() {
return index == line.length;
}
//////////////////////////////////////////////
private final BufferedReader r;
MyScanner () throws IOException {
this(new BufferedReader(new InputStreamReader(System.in)));
}
MyScanner(BufferedReader r) throws IOException {
this.r = r;
}
private String [] line;
private int index;
private void newLine() throws IOException {
if (line == null || eol()) {
line = r.readLine().split(" ");
index = 0;
}
}
}
////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) throws IOException {
run();
exit();
}
static void start() {
t = millis();
}
static void run () throws IOException {
sc = new MyScanner ();
new A();
}
static long t;
static long millis() {
return System.currentTimeMillis();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
6f5d056c4e0ba6d97e27efc3089c2618
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.util.Scanner;
public class A
{
static int[] mas2 = new int[30];
static
{
int x = 1;
mas2[0] = x;
for (int i = 1; i < 30; i++)
{
x = x * 2;
mas2[i] = x;
}
}
public static int getLast(int n)
{
int i = 0;
while (n >= mas2[i])
{
i++;
}
return mas2[i - 1];
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// Scanner in = new Scanner(new FileReader(new File("test.txt")));
int n = in.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++)
{
a[i] = in.nextInt();
}
for (int i = 0; i < n - 1; i++)
{
int last = i + getLast(n - i - 1);
a[last] += a[i];
}
long prev = a[0];
for (int i = 0; i < n - 1; i++)
{
System.out.println(prev);
prev += a[i + 1];
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
ed9625eb95cd53ce74c9b280ec0def76
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class A implements Runnable {
private MyScanner in;
private PrintWriter out;
private void solve() {
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = in.nextInt();
}
long actions = 0;
for (int i = 0; i < n - 1; ++i) {
actions += a[i];
for (int j = 2;; j <<= 1) {
if (i + j >= n) {
a[i + (j >> 1)] += a[i];
break;
}
}
out.println(actions);
}
}
@Override
public void run() {
in = new MyScanner();
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
public static void main(String[] args) {
new A().run();
}
class MyScanner {
private BufferedReader br;
private StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean hasNext() {
while (st == null || !st.hasMoreTokens()) {
try {
String s = br.readLine();
if (s == null) {
return false;
}
st = new StringTokenizer(s);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return st != null && st.hasMoreTokens();
}
private String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String s = br.readLine();
if (s == null) {
return null;
}
st = new StringTokenizer(s);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return st.nextToken();
}
public String nextLine() {
try {
st = null;
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
ff6e4de34a0157d523dd6dfc456e6657
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class Main {
Scanner in;
PrintWriter out;
void setIn(String fI) {
try {
in = new Scanner(new FileInputStream(new File(fI)));
} catch (FileNotFoundException e) {
throw new Error(e);
}
}
void setIn() {
in = new Scanner(System.in);
}
void setOut(String fO) {
try {
out = new PrintWriter(new FileWriter(fO));
} catch (IOException e) {
throw new Error(e);
}
}
void setOut() {
out = new PrintWriter(System.out);
}
class Scanner {
StringTokenizer st;
BufferedReader in;
String del;
Scanner(InputStream is) {
in = new BufferedReader(new InputStreamReader(is));
st = null;
del = " \t\n\r\f";
}
void setDelimiters(String del) {
this.del = del;
}
String next() {
if (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine(), del);
}
return st.nextToken();
}
String nextLine() {
try {
st = null;
return in.readLine();
} catch (IOException e) {
throw new Error(e);
}
}
boolean hasNext() {
try {
return in.ready() || (st != null && st.hasMoreTokens());
} catch (IOException e) {
throw new Error(e);
}
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
BigInteger nextBigInteger() {
return new BigInteger(next());
}
BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}
int[] nextIntArray(int len) {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = nextInt();
}
return a;
}
}
public static void main(String[] args) {
new Main().run();
}
void run() {
setIn();
setOut();
try {
solve();
} finally {
out.close();
}
}
class Point {
int x;
int y;
Point(int X, int Y) {
x = X;
y = Y;
}
@Override
public boolean equals(Object o) {
Point p = (Point) o;
return p.x == x && y == p.y;
}
@Override
public int hashCode() {
return 31 * x + y;
}
}
int getPos(int i, int n) {
int pow = 1;
while (i + pow * 2 <= n)
pow *= 2;
return pow + i;
}
void solve() {
int n = in.nextInt();
int[] a = in.nextIntArray(n);
int curAns = 0;
for (int i = 0; i < n - 1; i++) {
int pos = getPos(i + 1, n) - 1;
int buf = a[i];
curAns += a[i];
a[pos] += a[i];
out.println(curAns);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
405abe0acf85c00952c954daf8e8a948
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solver {
StringTokenizer st;
BufferedReader in;
PrintWriter out;
public static void main(String[] args) throws NumberFormatException, IOException {
Solver solver = new Solver();
solver.open();
solver.solve();
solver.close();
}
public void open() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
public String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
int[] pows = new int[1000];
public void solve() throws NumberFormatException, IOException {
int n = nextInt();
int[] ar = new int[n];
for(int i=0;i<n;i++){
ar[i] = nextInt();
}
int start = 1;
int length = 0;
while (start<=n){
pows[length++] = start;
start*=2;
}
long result = 0;
for(int i=0;i<n-1;i++){
int dif = n-i-1;
int j = length-1;
while(dif<pows[j]){j--;}
result += ar[i];
ar[i+pows[j]] += ar[i];
out.println(result);
}
}
public void close() {
out.flush();
out.close();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
985c21e54623d11c09cc840a8caaaf57
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class A1 {
public void solve() throws IOException {
int n = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
for (int i = 1; i < n; i++) a[i] += a[i - 1];
int ans = 0;
for (int i = 0; i < n - 1; i++) {
if (a[i] == 0) {
writer.println(ans);
continue;
}
int j = 1;
while (i + j < n) {
int min = 1000000000;
for (int k = i; k < i + j; k++) min = Math.min(min, a[k]);
if (min == 0) break;
j <<= 1;
}
j >>= 1;
int add = a[i];
for (int k = i; k < i + j; k++) a[k] -= add;
ans += add;
writer.println(ans);
}
}
public static void main(String[] args) throws FileNotFoundException {
new A1().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
long tbegin = System.currentTimeMillis();
reader = new BufferedReader(new InputStreamReader(System.in));
//reader = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
tokenizer = null;
writer = new PrintWriter(System.out);
//writer = new PrintWriter(new FileOutputStream("output.txt"));
solve();
//reader.close();
//System.out.println(System.currentTimeMillis() - tbegin + "ms");
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
1b8ed008ff0e2e33a92c8b9f6fb3ed41
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.Queue;
import com.sun.org.apache.bcel.internal.generic.LLOAD;
/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
*/
public class Main {
private static BufferedReader br;
private static StringTokenizer st;
private static PrintWriter pw;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//int qq = 1;
int qq = Integer.MAX_VALUE;
//int qq = readInt();
for(int casenum = 1; casenum <= qq; casenum++) {
int n = readInt();
int[] list = new int[n];
for (int i = 0; i < list.length; i++) {
list[i] = readInt();
}
long ret = 0;
int shift = 20;
for (int i = 0; i < list.length-1; i++) {
while(i + (1<<shift) >= list.length) {
shift--;
}
ret += list[i];
list[i + (1<<shift)] += list[i];
list[i] = 0;
pw.println(ret);
}
}
pw.close();
}
private static long readLong() throws IOException {
return Long.parseLong(nextToken());
}
private static double readDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private static int readInt() throws IOException {
return Integer.parseInt(nextToken());
}
private static String nextToken() throws IOException {
while(st == null || !st.hasMoreTokens()) {
if(!br.ready()) {
pw.close();
System.exit(0);
}
st = new StringTokenizer(br.readLine().trim());
}
return st.nextToken();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
76a9a73602c66ae8c4a83f9278ab34a1
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class A {
static StreamTokenizer st;
public static void main(String[] args) throws IOException{
st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = nextInt();
int[]a = new int[n+1];
for (int i = 1; i <= n; i++) {
a[i] = nextInt();
}
long cnt = 0;
for (int i = 1; i < n; i++) {
pw.println((cnt+a[i])+" ");
cnt += a[i];
int t = 1;
while (i+2*t <= n)
t *= 2;
a[i+t] += a[i];
}
pw.close();
}
private static int nextInt() throws IOException{
st.nextToken();
return (int) st.nval;
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
4aa2c9eeca7ecdcbb56a6c4978202af6
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Erik Odenman
*/
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);
EducationalGame solver = new EducationalGame();
solver.solve(1, in, out);
out.close();
}
}
class EducationalGame {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.getInt();
long cnt = 0;
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.getInt();
}
int pow2 = 1;
while (2*pow2 < n) pow2 *= 2;
for (int i = 0; i < n - 1; i++) {
cnt += a[i];
while (i+pow2 >= n) pow2 /= 2;
a[i+pow2] += a[i];
a[i] = 0;
out.println(cnt);
}
}
}
class InputReader {
private BufferedReader in;
private StringTokenizer st;
String token;
public InputReader(InputStream inStream) {
in = new BufferedReader(new InputStreamReader(inStream));
}
public int getInt() {
return Integer.parseInt(next());
}
public String next() {
String res = peekToken();
token = null;
return res;
}
public String peekToken() {
if (token != null) return token;
while (st == null || !st.hasMoreTokens()) {
String line = null;
try {
line = in.readLine();
} catch (IOException e) {
throw new RuntimeException("No more tokens was found");
}
if (line == null) return null;
st = new StringTokenizer(line);
}
return token = st.nextToken();
}
}
class OutputWriter {
PrintWriter out;
public OutputWriter(OutputStream outStream) {
out = new PrintWriter(outStream);
}
public OutputWriter(Writer writer) {
out = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) out.print(' ');
out.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
out.println();
}
public void close() {
out.close();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
148ce866afbeee7cf31b8ede530f7ad4
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
import java.util.StringTokenizer;
public class A {
BufferedReader in;
StringTokenizer str;
PrintWriter out;
String SK;
String next() throws IOException {
while ((str == null) || (!str.hasMoreTokens())) {
SK = in.readLine();
if (SK == null)
return null;
str = new StringTokenizer(SK);
}
return str.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void solve() throws IOException {
int n = nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
long res = 0;
for (int i = 0; i < n - 1; i++) {
int j = 1;
while (i + j < n) {
j = j * 2;
}
j /= 2;
res =res+(long) a[i];
a[i + j] += a[i];
out.println(res);
}
}
void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new A().run();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
0994e91253f127e3793910f802f95ab4
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Solution {
private StringTokenizer st;
private BufferedReader in;
private PrintWriter out;
int[] a;
int[] d;
long dfs(int i, int last, int dd) {
if (dd >= d[i]) {
return 0;
}
long ret = (long)(d[i] - dd) * a[i];
d[i] = dd;
for (int j = 1; j <= i && j < last; j *= 2) {
ret += dfs(i - j, j, dd + 1);
}
return ret;
}
public void solve() throws IOException {
int n = nextInt();
a = new int[n];
d = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = nextInt();
}
Arrays.fill(d, 1000000000);
d[n - 1] = 0;
long[] ans = new long[n];
for (int i = n - 1; i >= 0; --i) {
for (int j = 1; j <= i; j *= 2) {
d[i - j] = Math.min(d[i - j], d[i] + 1);
}
ans[n - 1] += (long)a[i] * d[i];
}
for (int t = n - 2; t > 0; --t) {
ans[t] = ans[t + 1] - dfs(t, t + 1, 0);
}
for (int i = 1; i < n; ++i) {
out.println(ans[i]);
}
}
public void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
eat("");
solve();
out.close();
}
void eat(String s) {
st = new StringTokenizer(s);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
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());
}
static boolean failed = false;
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
new Solution().run();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
09a05a4b244181dc3955212673ed9e3f
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
BufferedReader br;
int n;
public int findPos(int idx) {
int ret;
for (int i = 1;; i <<= 1) {
ret = idx + i;
if (ret >= n) {
i >>= 1;
ret = idx + i;
break;
}
}
return ret;
}
public void go() throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
long[] a = new long[n];
String[] ss = br.readLine().split(" ");
for (int i = 0; i < n; i++)
a[i] = Long.parseLong(ss[i]);
long ans = 0l;
for (int i = 0; i < n - 1; i++) {
int pos = findPos(i);
ans += a[i];
a[pos] += a[i];
System.out.println(ans);
}
}
public static void main(String[] args) throws Exception {
new Main().go();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
4593f64ee2c7cb18b945b056051817e9
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class A {
final String filename = new String("A").toLowerCase();
void solve() throws Exception {
int n = nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
long curAns = 0;
for (int i = 0; i < n - 1; i++) {
int last = 1;
while (i + last * 2 < n) {
last = last * 2;
}
curAns += a[i];
a[i + last] += a[i];
a[i] = 0;
out.println(curAns);
}
}
void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// in = new BufferedReader(new FileReader("input.txt"));
// out = new PrintWriter("output.txt");
solve();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
BufferedReader in;
StringTokenizer st;
PrintWriter out;
String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
public static void main(String[] args) {
new A().run();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
01939135947b4509de8d846266be9138
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.util.Scanner;
public class AAbbyyHard {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextLong();
}
long m = 0;
for (int i = 0; i < n-1; i++) {
if(a[i] > 0) {
int d = n-i-1;
int t = 0;
int v = 0;
while(d > 0) {
if(d%2 == 1)
v = t;
++t;
d/=2;
}
a[i + (1<<v) ] += a[i];
m += a[i];
a[i] = 0;
}
System.out.println(m);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
595b2a8e0c3f9fc7119a0a83004f47fb
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
long []data = new long[n];
for(int i = 0; i < n ; i++){
data[i] = in.nextInt();
}
long start = 0;
int add = 1;
while(add*2 < n){
add *=2;
}
// out.println("Add" + add)
for(int i = 0; i < n - 1; i++){
if(add + i >= n){
add/=2;
}
start += data[i];
data[i + add] += data[i];
out.println(start);
}
out.close();
}
static class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
static class FT {
int[] data;
FT(int n) {
data = new int[n];
}
void update(int index, int val) {
// System.out.println("UPDATE INDEX " + index);
while (index < data.length) {
data[index] += val;
index += index & (-index);
// System.out.println("NEXT " +index);
}
}
int get(int index) {
// System.out.println("GET INDEX " + index);
int result = 0;
while (index > 0) {
result += data[index];
index -= index & (-index);
// System.out.println("BACK " + index);
}
return result;
}
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
static int pow(int a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
int val = pow(a, b / 2);
if (b % 2 == 0) {
return val * val;
} else {
return val * val * a;
}
}
// static Point intersect(Point a, Point b, Point c) {
// double D = cross(a, b);
// if (D != 0) {
// return new Point(cross(c, b) / D, cross(a, c) / D);
// }
// return null;
// }
//
// static Point convert(Point a, double angle) {
// double x = a.x * cos(angle) - a.y * sin(angle);
// double y = a.x * sin(angle) + a.y * cos(angle);
// return new Point(x, y);
// }
// static Point minus(Point a, Point b) {
// return new Point(a.x - b.x, a.y - b.y);
// }
//
// static Point add(Point a, Point b) {
// return new Point(a.x + b.x, a.y + b.y);
// }
//
// static double cross(Point a, Point b) {
// return a.x * b.y - a.y * b.x;
//
//
// }
//
// static class Point {
//
// int x, y;
//
// Point(int x, int y) {
// this.x = x;
// this.y = y;
// }
//
// @Override
// public String toString() {
// return "Point: " + x + " " + y;
// }
// }
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
//System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new FileReader(new File("input.txt")));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
f0db03fc05cbc6c8359a11f1f8bfb998
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
import java.util.LinkedList;
import java.util.Scanner;
public class Main1 {
static int n;
static int[] nums;
public static void ReadConsole() {
Scanner in = new Scanner(System.in);
n = Integer.parseInt(in.nextLine());
String str = in.nextLine();
String[] strs = str.split(" ");
nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = Integer.parseInt(strs[i]);
}
}
public static void ReadFile() {
try {
FileInputStream fstream = new FileInputStream("in.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
n = Integer.parseInt(br.readLine());
String str = br.readLine();
in.close();
String[] strs = str.split(" ");
nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = Integer.parseInt(strs[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public LinkedList<Integer> GetPossibleMove(int forNum) {
LinkedList<Integer> res = new LinkedList<Integer>();
res.add(forNum + 1);
int t2 = 1;
while (true) {
t2 *= 2;
if (t2 < n)
res.add(forNum + t2);
else
break;
}
return res;
}
public static int maxPossibleStep(int forNum) {
return (int) (Math.log(n - forNum - 1) / Math.log(2));
}
public static void main(String[] args) {
ReadConsole();
//ReadFile();
for (int k = 1; k < n; k++) {
int sum = 0;
int[] cnums = nums.clone();
for (int i = 0; i < k; i++) {
sum += cnums[i];
int prev = i;
int mps = i+(int)Math.pow(2,maxPossibleStep(i));
while (true)
{
if (mps >= k)
{
break;
}
else
{
cnums[mps] += cnums[i];
break;
//prev = mps;
//mps = mps+(int)Math.pow(2,maxPossibleStep(mps));
}
}
}
System.out.println(sum);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
2e32a7dff3c63904a27aaeac3766ab67
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.util.*;
/*
*
* A.java
* Written by Andy Huang: 8:19:51 AM Apr 28, 2012
*/
public class A {
static final int twoe[] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};
static boolean toend[];
static void solve() {
int n = in.nInt();
long a[] = new long[n];
toend = new boolean[n];
for (int i = 0; i < n; i++){
a[i] = in.nLong();
for (int x : twoe){
if (i + x == n-1)
toend[i] = true;
}
}
long dp[] = new long[n - 1];
for (int i = 0; i < n - 1; i++){
if (a[i] == 0)
continue;
if (toend[i]){
dp[i] = a[i];
a[i] = 0;
continue;
}
for (int j = 16; j >= 0; j--){
if (i + twoe[j] < n){
dp[i] = a[i];
a[i + twoe[j]] += a[i];
a[i] = 0;
break;
}
}
}
long sum = 0;
for (long ans : dp){
sum += ans;
out.appendln(sum);
}
}
public static void main(String[] args) {
in = new Input();
out = new Output();
solve();
out.print();
}
static Output out;
static Input in;
static void pln(Object o) {
System.out.println(o);
}
static void pf(Object o) {
System.out.print(o);
}
}
final class Input {
private java.io.BufferedReader reader;
private java.util.StringTokenizer tokenizer;
public Input() {
reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new java.util.StringTokenizer(reader.readLine());
}
catch (java.io.IOException e) {
throw new RuntimeException("Input Error");
}
}
return tokenizer.nextToken();
}
public String nLine() {
try {
return reader.readLine();
}
catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
public long nLong() {
return Long.parseLong(next());
}
public int nInt() {
return Integer.parseInt(next());
}
public double nDouble() {
return Double.parseDouble(next());
}
}
final class Output {
public StringBuilder buffer;
Output() {
buffer = new StringBuilder();
}
Output(int size) {
buffer = new StringBuilder(size);
}
void print() {
System.out.print(buffer.toString());
}
void flush() {
System.out.flush();
}
<T> void append(T obj) {
buffer.append(obj);
}
<T> void appendln(T obj) {
append(obj);
append('\n');
}
void delete(int index) {
buffer.deleteCharAt(index);
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
6f0a5d3cca258155cdf0531dea739ce5
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
//package abbyy2.hard;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
public class A {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int[] t = new int[20];
t[0] = 1;
for(int i = 1;i <= 19;i++)t[i] = t[i-1] * 2;
int n = ni();
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
long ct = 0;
for(int k = 0;k < n-1;k++){
for(int q = 19;q >= 0;q--){
if(k+t[q] < n){
a[k+t[q]] += a[k];
ct+=a[k];
a[k] = 0;
break;
}
}
out.println(ct);
}
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception
{
new A().run();
}
public int ni()
{
try {
int num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public long nl()
{
try {
long num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public String ns()
{
try{
int b = 0;
StringBuilder sb = new StringBuilder();
while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' '));
if(b == -1)return "";
sb.append((char)b);
while(true){
b = is.read();
if(b == -1)return sb.toString();
if(b == '\r' || b == '\n' || b == ' ')return sb.toString();
sb.append((char)b);
}
} catch (IOException e) {
}
return "";
}
public char[] ns(int n)
{
char[] buf = new char[n];
try{
int b = 0, p = 0;
while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n'));
if(b == -1)return null;
buf[p++] = (char)b;
while(p < n){
b = is.read();
if(b == -1 || b == ' ' || b == '\r' || b == '\n')break;
buf[p++] = (char)b;
}
return Arrays.copyOf(buf, p);
} catch (IOException e) {
}
return null;
}
double nd() { return Double.parseDouble(ns()); }
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
11bfd1609ae7bd703441f44eca5ef253
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.util.Scanner;
public class Education{
public static int power(int i, int n){
int sum = 1;
while( sum*2 <= n-i ){
sum *= 2;
}
return sum + i;
}
public static int result(int k, int n, int[] a){
int count = 0;
for(int i = 1; i <= k; i++){
a[power(i, n)-1] += a[i-1];
count += a[i-1];
a[i-1] = 0;
}
return count;
}
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
int n = stdIn.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = stdIn.nextInt();
}
int count = 0;
for(int i = 1; i <= n-1; i++){
count = count + result(i, n, a);
System.out.println(count);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
26d83e9f0acce684c79edc37944748fe
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class A
{
String line;
StringTokenizer inputParser;
BufferedReader is;
FileInputStream fstream;
DataInputStream in;
String FInput="";
void openInput(String file)
{
if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin
else
{
try{
fstream = new FileInputStream(file);
in = new DataInputStream(fstream);
is = new BufferedReader(new InputStreamReader(in));
}catch(Exception e)
{
System.err.println(e);
}
}
}
void readNextLine()
{
try {
line = is.readLine();
inputParser = new StringTokenizer(line, " ");
//System.err.println("Input: " + line);
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
catch (NullPointerException e)
{
line=null;
}
}
int NextInt()
{
String n = inputParser.nextToken();
int val = Integer.parseInt(n);
//System.out.println("I read this number: " + val);
return val;
}
long NextLong()
{
String n = inputParser.nextToken();
long val = Long.parseLong(n);
//System.out.println("I read this number: " + val);
return val;
}
String NextString()
{
String n = inputParser.nextToken();
return n;
}
void closeInput()
{
try {
is.close();
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
}
public static void main(String [] argv)
{
String filePath=null;
if(argv.length>0)filePath=argv[0];
new A(filePath);
}
public void readFInput()
{
for(;;)
{
try
{
readNextLine();
FInput+=line+" ";
}
catch(Exception e)
{
break;
}
}
inputParser = new StringTokenizer(FInput, " ");
}
public A(String inputFile)
{
openInput(inputFile);
readNextLine();
int N=NextInt();
long ret=0;
int [] p2 = new int [N+1];
int [] [] p3 = new int [N][N];
Arrays.fill(p2, Integer.MAX_VALUE);
p2[0]=0;
for(int i=0; i<=N; i++)
{
if(p2[i]==Integer.MAX_VALUE)p2[i]=p2[i-1]+1;
for(int j=1; j+i<=N; j*=2)
{
p2[i+j]=Math.min(p2[i+j], p2[i]+1);
}
}
for(int i=0; i<N; i++)
{
int min=p2[i];
for(int j=i; j<N; j++)
{
min=Math.min(min, p2[j]);
p3[i][j]=min;
}
}
readNextLine();
int [] a = new int[N];
for(int i=0; i<N; i++)
{
a[i]=NextInt();
}
for(int i=1; i<N; i++)
{
ret=0;
for(int j=0; j<i; j++)
{
ret+=(long)a[j]*(long)p3[i-j][N-j-1];
}
System.out.println(ret);
}
closeInput();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
09bcead156a3f1772ac67cd70e324499
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
public class a {
static int Div2(int n){
int count = 0;
while (n !=1) {count++; n/=2;}
return count;
}
public static void main(String args[]){
Scanner in = new Scanner (System.in);
int n = in.nextInt();
int two = Div2(n);
int t = (int)Math.pow(2, two);
int d[]=new int[n+1];
int a[]=new int[n+1];
for (int i = 1; i<=n; i++) a[i] = in.nextInt();
d[1] = a[1];
for (int i = 1; i<=n-1; i++){
if (i + t > n){t/=2;}
a[i+t] += a[i];
if (i > 1)d[i] += d[i-1] + a[i];
System.out.println(d[i]);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
403acf1489cf0a01130931ac2b62e3ee
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class A implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Thread(null, new A(), "", 256 * (1L << 20)).start();
}
public void run() {
try {
long t1 = System.currentTimeMillis();
if (System.getProperty("ONLINE_JUDGE") != null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("src/input.txt"));
out = new PrintWriter(System.out);
}
Locale.setDefault(Locale.US);
solve();
in.close();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
// solution
void solve() throws IOException {
int n = readInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = readLong();
}
long res = 0;
for (int i = 0; i < n - 1; i++) {
int j = 1;
while (i + j * 2 < a.length) {
j *= 2;
}
res += a[i];
a[i + j] += a[i];
out.println(res);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
f5d9c4a972eb0fc0f7037e43090555b8
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
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.Collections;
import java.util.Locale;
import java.util.StringTokenizer;
public class A {
private void solve() throws IOException {
int n = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
long res = 0;
for (int i = 0; i < n - 1; i++) {
int d = (n - 1) - i;
int p = 1;
while (p <= d) {
p *= 2;
}
p /= 2;
res += a[i];
a[i + p] += a[i];
a[i] = 0;
println(res);
}
}
private String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
private int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
private double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private void print(Object o) {
writer.print(o);
}
private void println(Object o) {
writer.println(o);
}
private void printf(String format, Object... o) {
writer.printf(format, o);
}
public static void main(String[] args) {
long time = System.currentTimeMillis();
Locale.setDefault(Locale.US);
new A().run();
System.err.printf("%.3f\n", 1e-3 * (System.currentTimeMillis() - time));
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
private void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(13);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
14f360abcb2c7d7a8bf40ec331f712fb
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter writer;
static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
static String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static void main(String[] args) throws IOException {
// reader = new BufferedReader(new FileReader(new File("code.txt")));
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
tokenizer = null;
// File file=new File("code.txt");
// writer = new PrintWriter(file);
solve();
// File("new.odt")));
reader.close();
writer.close();
}
public static void solve() throws IOException {
int n=nextInt();
long s=0;
long[] a=new long[n+5];
for(int i=0; i<n-1; i++)
{
long k=nextLong();
int t=1;
while(i+(t<<1)<n)
t<<=1;
a[i+t]+=k+a[i];
s+=k+a[i];
writer.println(s);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
aef6256ead31c69057c10213dea2e791
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
public class A2{
public static void main(String ar[]){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(in.readLine());
String s=in.readLine();
int a[]=new int[n+1];
int i=1;
for(int j=0;j<s.length();j++){
char c=s.charAt(j);
if(c==' ')i++;
else a[i]=a[i]*10+(c-'0');
}
//for(int j=1;j<n+1;j++)System.out.println(a[j]);
int r[]=new int[n];
for(int j=1;j<n;j++){
int aj=a[j];
if(aj==0){r[j]=r[j-1];continue;}
int z=n-j;
int b=0;
while(z!=1){z>>=1;b++;}
r[j]=aj+r[j-1];
a[j+(1<<b)]+=aj;
}
//System.out.println("--");
//for(int j=1;j<n+1;j++)System.out.println(a[j]);
//System.out.println("--");
for(int j=1;j<n;j++)System.out.println(r[j]);
}catch(Exception e){}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
46bc82a0db96ba92c19b34908b99e6d6
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class taskA {
PrintWriter out;
BufferedReader br;
StringTokenizer st;
String nextToken() throws IOException {
while ((st == null) || (!st.hasMoreTokens()))
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public void solve() throws IOException {
int n = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
long ans = 0;
for (int i = 0; i < n - 1; i++) {
int x = 1;
while (i + x * 2 < n)
x *= 2;
a[i + x] += a[i];
ans += a[i];
out.println(ans);
}
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
//br = new BufferedReader(new FileReader("taskA.in"));
//out = new PrintWriter("taskA.out");
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new taskA().run();
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
b292b1c7aa34e677139c04df5a67c93e
|
train_001.jsonl
|
1335614400
|
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1,βa2,β...,βak (i.e. some prefix of the sequence) equal to zero for some fixed k (kβ<βn), and this should be done in the smallest possible number of moves.One move is choosing an integer i (1ββ€βiββ€βn) such that aiβ>β0 and an integer t (tββ₯β0) such that iβ+β2tββ€βn. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of aiβ+β2t is increased by 1. For example, let nβ=β4 and aβ=β(1,β0,β1,β2), then it is possible to make move iβ=β3, tβ=β0 and get aβ=β(1,β0,β0,β3) or to make move iβ=β1, tβ=β1 and get aβ=β(0,β0,β2,β2) (the only possible other move is iβ=β1, tβ=β0).You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1ββ€βkβ<βn).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class CF
{
public static void main(String [] args) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(stdin.readLine());
String [] l = stdin.readLine().split(" ");
int [] a = new int[n+1];
int [] d = new int[n+1];
for (int i = 0; i < n; i++)
a[i+1] = Integer.parseInt(l[i]);
for (int k = 1; k < n; k++)
{
for (int j = k + 1; j <= n; j++)
d[j] = 0;
for (int i = k; i >= 0; i--)
{
int D = 1 << 28;
for (int j = 1; i + j <= n; j <<= 1)
D = Math.min(D,1+d[i+j]);
d[i] = D;
}
long ans = 0;
for (int i = 1; i <= k; i++)
ans = ans + a[i] * d[i];
System.out.println(ans);
}
}
}
|
Java
|
["4\n1 0 1 2", "8\n1 2 3 4 5 6 7 8"]
|
2 seconds
|
["1\n1\n3", "1\n3\n6\n10\n16\n24\n40"]
| null |
Java 6
|
standard input
|
[
"greedy"
] |
c7e49c643dd8738f273c0d24e56c505f
|
The first input line contains a single integer n. The second line contains n integers ai (0ββ€βaiββ€β104), separated by single spaces. The input limitations for getting 20 points are: 1ββ€βnββ€β300 The input limitations for getting 50 points are: 1ββ€βnββ€β2000 The input limitations for getting 100 points are: 1ββ€βnββ€β105
| 1,000 |
Print exactly nβ-β1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams, or the %I64d specifier.
|
standard output
| |
PASSED
|
22718b7cb931b0ef8b40075cfc48d2e0
|
train_001.jsonl
|
1390231800
|
Iahub got lost in a very big desert. The desert can be represented as a nβΓβn square matrix, where each cell is a zone of the desert. The cell (i,βj) represents the cell at row i and column j (1ββ€βi,βjββ€βn). Iahub can go from one cell (i,βj) only down or right, that is to cells (iβ+β1,βj) or (i,βjβ+β1). Also, there are m cells that are occupied by volcanoes, which Iahub cannot enter. Iahub is initially at cell (1,β1) and he needs to travel to cell (n,βn). Knowing that Iahub needs 1 second to travel from one cell to another, find the minimum time in which he can arrive in cell (n,βn).
|
256 megabytes
|
import java.awt.Point;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.LinkedList;
public class D {
public static void merge(Point[] B, int cnt1, Point[] N, int cnt2) {
for (int i = 0; i < cnt2; i++) {
int lo = 0;
int hi = cnt1 - 1;
int ans = -1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (B[mid].y >= N[i].x) {
ans = mid;
hi = mid - 1;
} else
lo = mid + 1;
}
if (ans == -1 || N[i].y < B[ans].x)
N[i].x = -1;
else {
int inter = Math.max(N[i].x, B[ans].x);
N[i].x = inter;
}
}
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
int n = in.readInt();
int m = in.readInt();
LinkedList<Point> V = new LinkedList<Point>();
for (int i = 0; i < m; i++)
V.add(new Point(in.readInt(), in.readInt()));
Collections.sort(V, new Comparator<Point>() {
public int compare(Point o1, Point o2) {
if (o1.x != o2.x)
return o1.x - o2.x;
return o1.y - o2.y;
}
});
Point[] B = new Point[m + 1];
Point[] N = new Point[m + 1];
B[0] = new Point(1, n);
int cnt = 1;
while (!V.isEmpty()) {
int row = V.getFirst().x;
int index = 0;
int st = 1;
while (!V.isEmpty() && V.getFirst().x == row) {
int col = V.remove().y;
if (col != st)
N[index++] = new Point(st, col - 1);
st = col + 1;
}
if (st <= n)
N[index++] = new Point(st, n);
if (row == 1)
index = 1;
else
merge(B, cnt, N, index);
cnt = 0;
for (int i = 0; i < index; i++)
if (N[i].x != -1)
B[cnt++] = N[i];
if (cnt == 0) {
B[cnt++] = new Point(-1, -1);
break;
}
if ((V.isEmpty() && row != n)
|| (!V.isEmpty() && V.getFirst().x != row + 1)) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < cnt; i++)
min = Math.min(B[i].x, min);
cnt = 0;
B[cnt++] = new Point(min, n);
}
}
boolean can = false;
for (int i = 0; i < cnt; i++)
can |= B[i].x <= n && B[i].y >= n;
if (can)
System.out.println(2 * n - 2);
else
System.out.println(-1);
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1000];
private int curChar, numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private 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 readInt() {
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 readLong() {
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 String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
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, readInt());
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, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
}
|
Java
|
["4 2\n1 3\n1 4", "7 8\n1 6\n2 6\n3 5\n3 6\n4 3\n5 1\n5 2\n5 3", "2 2\n1 2\n2 1"]
|
1 second
|
["6", "12", "-1"]
|
NoteConsider the first sample. A possible road is: (1,β1) βββ (1,β2) βββ (2,β2) βββ (2,β3) βββ (3,β3) βββ (3,β4) βββ (4,β4).
|
Java 6
|
standard input
|
[
"implementation"
] |
50a3dce28a479d140781a0db4eac363e
|
The first line contains two integers n (1ββ€βnββ€β109) and m (1ββ€βmββ€β105). Each of the next m lines contains a pair of integers, x and y (1ββ€βx,βyββ€βn), representing the coordinates of the volcanoes. Consider matrix rows are numbered from 1 to n from top to bottom, and matrix columns are numbered from 1 to n from left to right. There is no volcano in cell (1,β1). No two volcanoes occupy the same location.
| 2,500 |
Print one integer, the minimum time in which Iahub can arrive at cell (n,βn). If no solution exists (there is no path to the final cell), print -1.
|
standard output
| |
PASSED
|
741d4098d9206abf1fc15d0979ccba31
|
train_001.jsonl
|
1418833800
|
Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers.To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets.Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options?
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
SolverB.InputReader in = new SolverB.InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
SolverB solver = new SolverB();
int testCount = 1;
for (int i = 1; i <= testCount; i++) {
solver.solve(in, out);
}
out.close();
}
}
class SolverB {
public void solve(InputReader in, PrintWriter out) {
int n = in.nextInt();
int sp = 0;
int sg = 0;
int sump[] = new int[n+1];
int sumg[] = new int[n+1];
Map<Integer, Integer> mpp = new HashMap<>();
Map<Integer, Integer> mpg = new HashMap<>();
for (int i=1; i<=n; i++) {
int num = in.nextInt();
sump[i] = sump[i-1];
sumg[i] = sumg[i-1];
if (num == 1) {
sp++;
sump[i]++;
mpp.put(sump[i], i);
} else {
sg++;
sumg[i]++;
mpg.put(sumg[i], i);
}
}
int dif = Math.abs(sp - sg);
if (dif == 0) {
out.println(0);
return;
}
int max = Math.max(sp, sg);
List<Pair> sol = new ArrayList<>();
for (int t=1; t<=max; t++) {
int servesP = 0;
int servesG = 0;
int matchP = 0;
int matchG = 0;
int winner = 0;
while (servesP + servesG <= n) {
Integer pp = mpp.get(servesP + t);
Integer pg = mpg.get(servesG + t);
if (pp!=null && pg!=null) {
if (pp < pg) {
servesP = servesP + t;
servesG = sumg[pp];
matchP++;
winner = 1;
} else {
servesG += t;
servesP = sump[pg];
matchG++;
winner = 2;
}
} else if (pp != null) {
servesP = servesP + t;
servesG = sumg[pp];
matchP++;
winner = 1;
} else if (pg != null) {
servesG += t;
servesP = sump[pg];
matchG++;
winner = 2;
} else {
break;
}
}
if (servesP + servesG == n && ((winner == 1 && matchP > matchG) || (winner == 2 && matchG > matchP))) {
sol.add(new Pair(Math.max(matchP, matchG), t));
}
}
Comparator<Pair> comp = new Comparator<Pair>() {
@Override public int compare(final Pair o1, final Pair o2) {
if(o1.s == o2.s) {
return o1.t - o2.t;
}
return o1.s - o2.s;
}
};
Collections.sort(sol, comp);
out.println(sol.size());
for (Pair p : sol) {
out.println(p.s + " " + p.t);
}
}
static class Pair {
int s, t;
Pair(int s, int t) {
this.s = s;
this.t = t;
}
}
static class InputReader {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
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());
}
}
}
|
Java
|
["5\n1 2 1 2 1", "4\n1 1 1 1", "4\n1 2 1 2", "8\n2 1 2 1 1 1 1 1"]
|
2 seconds
|
["2\n1 3\n3 1", "3\n1 4\n2 2\n4 1", "0", "3\n1 6\n2 3\n6 1"]
| null |
Java 7
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
eefea51c77b411640a3b92b9f2dd2cf1
|
The first line contains a single integer nΒ β the length of the sequence of games (1ββ€βnββ€β105). The second line contains n space-separated integers ai. If aiβ=β1, then the i-th serve was won by Petya, if aiβ=β2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record.
| 1,900 |
In the first line print a single number kΒ β the number of options for numbers s and t. In each of the following k lines print two integers si and tiΒ β the option for numbers s and t. Print the options in the order of increasing si, and for equal siΒ β in the order of increasing ti.
|
standard output
| |
PASSED
|
d759b0174464173bb3f4f9b4d1a6f877
|
train_001.jsonl
|
1418833800
|
Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers.To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets.Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options?
|
256 megabytes
|
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class tennis {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
boolean[] serve = new boolean[n];
int[] scoreIdx = new int[n + 1];
int[] oppScoreIdx = new int[n + 1];
Arrays.fill(scoreIdx, -1);
Arrays.fill(oppScoreIdx, -1);
int cnt = 0;
for (int i = 0; i < n; i++) {
int k = scanner.nextInt();
serve[i] = (k == 1);
if (serve[i]) {
cnt++;
scoreIdx[cnt] = i;
} else {
oppScoreIdx[i + 1 - cnt] = i;
}
}
ArrayList<result> results = new ArrayList<>();
for (int t = 1; t <= n; t++) {
// number of sets
// see who win the next set
int prevScore = 0, oppPrevScore = 0;
int sets = 0, oppSets = 0;
int idx = 0;
boolean won = false;
while (idx < n) {
int i1 = -1, i2 = -1;
if (prevScore + t <= n) {
i1 = scoreIdx[prevScore + t];
}
if (oppPrevScore + t <= n) {
i2 = oppScoreIdx[oppPrevScore + t];
}
if ( (i1 < i2 || i2 < 0) && i1 >= 0) {
// player one won
sets++;
idx = i1;
prevScore += t;
oppPrevScore = i1 + 1 - prevScore;
won = true;
} else if (i2 >= 0) {
oppSets++;
oppPrevScore += t;
prevScore = i2 + 1 - oppPrevScore;
idx = i2;
won = false;
} else {
// no more winner
break;
}
}
if (idx == n - 1) {
// match ends
int s = -1;
if (sets > oppSets && won) {
// good match
s = sets;
} else if ( sets < oppSets && ! won) {
s = oppSets;
}
if ( s > 0 ) {
results.add(new result(s, t));
}
}
}
Collections.sort(results);
System.out.println(results.size());
for (result r : results) {
System.out.format("%d %d\n", r.s, r.t);
}
}
private static class result implements Comparable<result> {
final int s, t;
result(int s1, int t1) {
s = s1;
t = t1;
}
@Override
public int compareTo(result o) {
int diff = s - o.s;
if ( diff == 0 ) {
diff = t - o.t;
}
return diff;
}
}
}
|
Java
|
["5\n1 2 1 2 1", "4\n1 1 1 1", "4\n1 2 1 2", "8\n2 1 2 1 1 1 1 1"]
|
2 seconds
|
["2\n1 3\n3 1", "3\n1 4\n2 2\n4 1", "0", "3\n1 6\n2 3\n6 1"]
| null |
Java 7
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
eefea51c77b411640a3b92b9f2dd2cf1
|
The first line contains a single integer nΒ β the length of the sequence of games (1ββ€βnββ€β105). The second line contains n space-separated integers ai. If aiβ=β1, then the i-th serve was won by Petya, if aiβ=β2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record.
| 1,900 |
In the first line print a single number kΒ β the number of options for numbers s and t. In each of the following k lines print two integers si and tiΒ β the option for numbers s and t. Print the options in the order of increasing si, and for equal siΒ β in the order of increasing ti.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.