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
|
9c38ce483243ddf0ac33a874d08e6f3d
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author el-Bishoy
*/
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);
Div2B378_Semifinals solver = new Div2B378_Semifinals();
solver.solve(1, in, out);
out.close();
}
static class Div2B378_Semifinals {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
ArrayList<Pair> both = new ArrayList<>();
int[] fist = new int[n];
int[] second = new int[n];
int[] results1 = new int[n];
int[] results2 = new int[n];
int N = n / 2;
for (int i = 0; i < n; i++) {
int a = in.nextInt(), b = in.nextInt();
fist[i] = a;
second[i] = b;
both.add(new Pair(i, a, true));
both.add(new Pair(i, b, false));
}
Collections.sort(both);
for (int i = 0; i < n; i++) {
Pair p = both.get(i);
if (p.first) {
results1[p.idx] = 1;
} else {
results2[p.idx] = 1;
}
}
for (int i = 0; i < N; i++) {
results1[i] = 1;
results2[i] = 1;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(results1[i]);
}
sb.append("\n");
for (int i = 0; i < n; i++) {
sb.append(results2[i]);
}
out.println(sb);
}
class Pair implements Comparable<Pair> {
int idx;
int score;
boolean first;
public Pair(int idx, int score, boolean first) {
this.idx = idx;
this.score = score;
this.first = first;
}
public int compareTo(Div2B378_Semifinals.Pair that) {
return Integer.compare(this.score, that.score);
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static interface NotNull {
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
221f6f143a422d156542155c594d4d4c
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author el-Bishoy
*/
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);
Div2B378_Semifinals solver = new Div2B378_Semifinals();
solver.solve(1, in, out);
out.close();
}
static class Div2B378_Semifinals {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int[] fist = new int[n];
int[] second = new int[n];
int[] results1 = new int[n];
int[] results2 = new int[n];
int N = n / 2;
for (int i = 0; i < n; i++) {
int a = in.nextInt(), b = in.nextInt();
fist[i] = a;
second[i] = b;
if (i < N) {
results1[i] = 1;
results2[i] = 1;
}
}
int ptr1 = 0, ptr2 = 0;
int idx = 0;
while (ptr1 < n && ptr2 < n && idx < n) {
if (fist[ptr1] < second[ptr2]) {
results1[ptr1] = 1;
ptr1++;
} else {
results2[ptr2] = 1;
ptr2++;
}
idx++;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(results1[i]);
}
sb.append("\n");
for (int i = 0; i < n; i++) {
sb.append(results2[i]);
}
out.println(sb);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
7bf9ae64de732432d988a8f7c790f7cd
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.util.*;
public class Bai1_extra_Semifinals {
public static void main(String[] args) {
int nPart;
Scanner sc = new Scanner(System.in);
nPart = sc.nextInt();
int[][] partResult = new int[nPart][];
// set n participants result
for (int i = 0; i < nPart; i++) {
partResult[i] = new int[2];
partResult[i][0] = sc.nextInt();
partResult[i][1] = sc.nextInt();
}
int k = nPart / 2;
// ArrayList<Integer> rs1 = new ArrayList<>();
// ArrayList<Integer> rs2 = new ArrayList<>();
StringBuilder strRs1 = new StringBuilder();
StringBuilder strRs2 = new StringBuilder();
for (int i = 0; i < k; i++) {
strRs1.append(1);
strRs2.append(1);
// rs1.add(1);
// rs2.add(1);
}
if (k > 0)
for (int i = k; i < nPart; i++) {
if (partResult[i][0] < partResult[nPart - (i + 1)][1])
strRs1.append(1);
else strRs1.append(0);
if (partResult[i][1] < partResult[nPart - (i + 1)][0])
strRs2.append(1);
else strRs2.append(0);
}
else {
if (partResult[0][0] < partResult[0][1]) {
strRs1.append(1);
strRs2.append(0);
} else {
strRs1.append(0);
strRs2.append(1);
}
}
System.out.println(strRs1.toString());
System.out.println(strRs2.toString());
/* for (int i = 0; i < nPart; i++) {
System.out.print(rs1.get(i));
}
System.out.println();
for (int i = 0; i < nPart; i++) {
System.out.print(rs2.get(i));
}*/
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
0024a744d3589a752abe98d84ba3b19b
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.util.*;
public class Bai1_extra_Semifinals {
public static void main(String[] args) {
/*http://codeforces.com/problemset/problem/378/B*/
int nPart;
Scanner sc = new Scanner(System.in);
nPart = sc.nextInt();
// int[][] partResult = new int[nPart][];
int[] team1 = new int[nPart];
int[] team2 = new int[nPart];
// set n participants result
for (int i = 0; i < nPart; i++) {
team1[i] = sc.nextInt();
team2[i] = sc.nextInt();
}
int k = nPart / 2;
StringBuilder strRs1 = new StringBuilder();
StringBuilder strRs2 = new StringBuilder();
for (int i = 0; i < k; i++) {
strRs1.append(1);
strRs2.append(1);
}
// if (k > 0)
for (int i = k; i < nPart; i++) {
// if participation at i, i have to better than the n - i of the other round
if (team1[i] < team2[nPart - (i + 1)])
strRs1.append(1);
else strRs1.append(0);
if (team2[i] < team1[nPart - (i + 1)])
strRs2.append(1);
else strRs2.append(0);
}
/*else {
if (team1[0] < team2[0]) {
strRs1.append(1);
strRs2.append(0);
} else {
strRs1.append(0);
strRs2.append(1);
}
}*/
System.out.println(strRs1.toString());
System.out.println(strRs2.toString());
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
55c74ef2209390a684434f9c4f12f3cd
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
player[][] players = new player[2][n];
player[] allPlayers = new player[2 * n];
for (int i = 0; i < n; i++) {
player p1 = new player(i, in.nextInt());
player p2 = new player(i, in.nextInt());
players[0][i] = p1;
players[1][i] = p2;
allPlayers[2 * i] = p1;
allPlayers[2 * i + 1] = p2;
}
Arrays.sort(players[0], (player p1, player p2) -> p1.score - p2.score);
Arrays.sort(players[1], (player p1, player p2) -> p1.score - p2.score);
Arrays.sort(allPlayers, (player p1, player p2) -> p1.score - p2.score);
for (int i = 0; i < n / 2; i++) {
players[0][i].posWin = true;
players[1][i].posWin = true;
}
for (int i = 0; i < n; i++) {
allPlayers[i].posWin = true;
}
Arrays.sort(players[0], (player p1, player p2) -> p1.numb - p2.numb);
Arrays.sort(players[1], (player p1, player p2) -> p1.numb - p2.numb);
for (int i = 0; i < n; i++) {
out.print(players[0][i].posWin ? '1' : '0');
}
out.println();
for (int i = 0; i < n; i++) {
out.print(players[1][i].posWin ? '1' : '0');
}
}
}
static class player {
int numb;
int score;
boolean posWin;
player(int n, int a) {
numb = n;
score = a;
posWin = false;
}
}
static class InputReader {
private StringTokenizer tokenizer;
private BufferedReader reader;
public InputReader(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
private void fillTokenizer() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public String next() {
fillTokenizer();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
b02153bb17d5490fd0381316d9bf515d
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
//Semifinals
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static int checka[] = new int[(int)1e5 + 1];
static int checkb[] = new int[(int)1e5 + 1];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n,k,i,j,count,x,y;
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> b = new ArrayList<>();
n = sc.nextInt();
for (i=0;i<n;i++) {
x = sc.nextInt();
y = sc.nextInt();
a.add(x);
b.add(y);
}
count = 0;
i = 0;
j = 0;
while (count < n) {
if (a.get(i) < b.get(j)) {
checka[i] = 1;
i++;
}
else {
checkb[j] = 1;
j++;
}
count++;
}
k = 0;
while (2*k <= n) {
if (k - 1 >= 0) {
checka[k - 1] = 1;
checkb[k - 1] = 1;
}
k++;
}
for (i=0;i<n;i++)
System.out.print(checka[i]);
System.out.print("\n");
for (i=0;i<n;i++)
System.out.print(checkb[i]);
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
400527bc6f7a16108483ab4a2a607f5b
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.util.*;
public class RookBishopKing {
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n+1];
int b[] = new int[n+1];
char resA[] = new char[n+1];
char resB[] = new char[n+1];
Arrays.fill(resA,'0');
Arrays.fill(resB,'0');
for (int i=1;i<=n;++i){
a[i] = sc.nextInt();
b[i] = sc.nextInt();
if (i<=n/2){
resA[i] = '1';
resB[i] = '1';
}
}
int posA[] = new int[n+1];
int posB[] = new int[n+1];
int curA = 1, curB=1;
int k = 2*n;
for (int i=1;i<=k;++i){
if (curA>n){
posB[curB] = i;
curB++;
continue;
}
if (curB>n){
posA[curA] = i;
curA++;
continue;
}
if (a[curA]<b[curB]) {
posA[curA] = i;
if (i<=n) resA[curA]='1';
curA++;
}
else if (a[curA]>b[curB]){
posB[curB] = i;
// System.out.println(b[curB] +" " + posB[curB]);
if (i<=n) resB[curB]='1';
curB++;
}
}
// for (int i=1;i<=n;++i) System.out.println(posA[i] + " " + posB[i]);
for (int i=1; i<=n;++i){
System.out.print(resA[i]);
}
System.out.println();
for (int i=1; i<=n;++i){
System.out.print(resB[i]);
}
sc.close();
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
b6c7a7c59e9f34a9aca7ff8c3fde44b1
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.util.Scanner;
/**
*
* @author SONY
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner (System.in);
int N = sc.nextInt();
int [] team1 = new int [N];
int [] team2 = new int [N];
for (int i = 0; i < N; i++){
team1[i] = sc.nextInt();
team2[i] = sc.nextInt();
}
for (int i = 0; i < N; i++){
if (i < N/2 || team1[i] < team2[N-i-1])
System.out.print(1);
else System.out.print(0);
}
System.out.println();
for (int i = 0; i < N; i++){
if (i < N/2 || team2[i] < team1[N-i-1])
System.out.print(1);
else System.out.print(0);
}
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
c2c259e4b64f748d30690554b3bb17c8
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class semifinals {
public static String test(int[] s1, int[] s2, int n) {
int k = n/2;
int count = 0;
int i = 0;
int j = 0;
int[] str1 = new int[n];
int[] str2 = new int[n];
int countForS1 = 0;
int countForS2 = 0;
while (count < n) {
if (s1[i] < s2[j]) {
str1[i] = 1;
i++;
countForS1++;
}
else {
str2[j] = 1;
j++;
countForS2++;
}
count++;
}
if (countForS1 > countForS2) {
for (int m=countForS2; m < k; m++) {
str2[m] = 1;
}
}
else {
for (int m=countForS1; m < k; m++) {
str1[m] = 1;
}
}
StringBuilder output1 = new StringBuilder();
StringBuilder output2 = new StringBuilder();
for (int x=0; x < n; x++) {
output1.append(str1[x]);
output2.append(str2[x]);
}
String finalOutput1 = output1.toString();
String finalOutput2 = output2.toString();
return finalOutput1 + "\n" + finalOutput2;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] s1 = new int[n];
int[] s2 = new int[n];
for (int i=0; i < n; i++) {
s1[i] = scan.nextInt();
s2[i] = scan.nextInt();
}
System.out.println(test(s1,s2,n));
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
352c665d6cce6b8cea05923ab4ae2634
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package pkg378bsemifinals;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* @author King
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
int n = in.nextInt();
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> b = new ArrayList<>();
ArrayList<Character> resultA = new ArrayList<>();
ArrayList<Character> resultB = new ArrayList<>();
for (int i = 0; i < n; i++) {
a.add(in.nextInt());
resultA.add('0');
b.add(in.nextInt());
resultB.add('0');
}
int k = n / 2;
for (int i = 0; i < k; i++) {
resultA.set(i, '1');
resultB.set(i, '1');
}
int i = 0;
int j = 0;
int count = 0;
while (i < n && j < n) {
if (a.get(i) <= b.get(j)) {
resultA.set(i, '1');
i++;
} else {
resultB.set(j, '1');
j++;
}
count++;
if (count == n) {
break;
}
}
String strA = resultA.stream().map(e->e.toString()).collect(Collectors.joining());
String strB = resultB.stream().map(e->e.toString()).collect(Collectors.joining());
System.out.println(strA);
System.out.println(strB);
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
5b4e85cda7d0bb1c2e0e015ae66829c5
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
public class Semifinals {
InputStream is;
PrintWriter out;
String INPUT = "";
long mod = 1000000007;
void solve() {
int n = ni();
P[] a = new P[n];
P[] b = new P[n];
for (int i = 0; i < n; i++) {
P curr = new P();
curr.time = ni();
a[i] = curr;
curr = new P();
curr.time = ni();
b[i] = curr;
}
List<P> col = new ArrayList<P>();
for (int i = 0; i < n; i++) {
col.add(a[i]);
col.add(b[i]);
}
Collections.sort(col, Comparator.comparingInt(a2 -> a2.time));
for (int i = 0; i < n; i++) {
col.get(i).win = true;
}
for (int i = 0; i < n / 2; i++) {
a[i].win = true;
b[i].win = true;
}
if (n % 2 == 1 && n > 1) {
if (a[n / 2].time >= b[n / 2].time) {
b[n / 2].win = true;
}
if (b[n / 2].time >= a[n / 2].time) {
a[n / 2].win = true;
}
}
for (int i = 0; i < n; i++) {
if (a[i].win == true) {
out.print(1);
} else {
out.print(0);
}
}
out.println();
for (int i = 0; i < n; i++) {
if (b[i].win == true) {
out.print(1);
} else {
out.print(0);
}
}
}
class P {
int time;
boolean win = false;
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Semifinals().run();
}
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
16aab21f683096c63a37aa09eeb7e4a3
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[][] = new int[2][n], ans[][] = new int[2][n];
for(int i = 0; i < n; ++i) {
for(int j = 0; j < 2; ++j) {
a[j][i] = sc.nextInt();
ans[j][i] = 0;
}
}
int x = 0, y = 0;
while (x < n && y < n) {
if (a[0][x] < a[1][y]) {
ans[0][x] = 1;
++x;
} else {
ans[1][y] = 1;
++y;
}
if (x+y == n) break;
}
while (x < n/2) {
ans[0][x] = 1;
++x;
}
while (y < n/2) {
ans[1][y] = 1;
++y;
}
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < n; ++j) System.out.print(ans[i][j]);
System.out.println();
}
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
731c61da3e0126efcc381b53eeb81563
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.util.*;
import javafx.util.Pair;
public class mainsource {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n=in.nextInt();
ArrayList<Integer> semi1,semi2,ans1,ans2;
semi1=new ArrayList<Integer>();
semi2=new ArrayList<Integer>();
ans1=new ArrayList<Integer>();
ans2=new ArrayList<Integer>();
int k=n/2;
for(int i=0;i<n;++i)
{
semi1.add(in.nextInt());
semi2.add(in.nextInt());
ans1.add(0);
ans2.add(0);
}
for(int j=0;j<k;++j)
{
ans1.set(j, 1);
ans2.set(j, 1);
}
int start1=0,start2=0;
for(int j=0;j<n;++j)
{
if(semi1.get(start1)<semi2.get(start2))
{
ans1.set(start1, 1);
++start1;
}
else
{
ans2.set(start2, 1);
++start2;
}
}
for(int i=0;i<n;++i)
System.out.print(ans1.get(i));
System.out.println();
for(int i=0;i<n;++i)
System.out.print(ans2.get(i));
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
1d89dcdd110e1489cd13759727bb95b2
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.util.Scanner;
public class Main {
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] semi1,semi2,ans1,ans2;
semi1 = new int[n];
semi2 = new int[n];
ans1 = new int[n];
ans2 = new int[n];
int k = n / 2;
for (int i = 0; i < n; ++i)
{
semi1[i] = sc.nextInt();
semi2[i] = sc.nextInt();
}
for(int j = 0; j < k; ++j)
{
ans1[j] = 1;
ans2[j] = 1;
}
int start1 = 0, start2 = 0;
for(int j = 0; j < n; ++j)
{
if(semi1[start1] < semi2[start2])
{
ans1[start1] = 1;
++start1;
}
else
{
ans2[start2] = 1;
++start2;
}
}
for(int i = 0; i < n; ++i)
System.out.print(ans1[i]);
System.out.print("\n");
for(int i = 0; i < n; ++i)
System.out.print(ans2[i]);
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
9654a3a3343418f68d8f61e98d4905d8
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
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.util.NoSuchElementException;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Alex
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.ri();
int[] a = new int[n], b = new int[n], ares = new int[n], bres = new int[n];
for(int i = 0; i < n; i++) {
a[i] = in.ri();
b[i] = in.ri();
}
for(int i = 0; i <= (n / 2 - 1); i++) {
ares[i] = 1;
bres[i] = 1;
}
// if (n == 1){
// if (a[0] < b[0]){
// out.printLine(1);
// out.printLine(0);
// }
// else{
// out.printLine(0);
// out.printLine(1);
// }
// return;
// }
int l = 0, r = 0, lookedat = 0, max = 0;
while(lookedat < n){
if (a[l] < b[r]) {
max = a[l];
l++;
}
else {
max = b[r];
r++;
}
lookedat++;
}
for(int i = 0; i < n; i++) {
if (a[i] <= max) ares[i] = 1;
if (b[i] <= max) bres[i] = 1;
}
for(int i = 0; i < n; i++) out.print(ares[i]);
out.printLine();
for(int i = 0; i < n; i++) out.print(bres[i]);
}
}
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 ri(){
return readInt();
}
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 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 void printLine() {
writer.println();
}
public void close() {
writer.close();
}
public void print(int i) {
writer.print(i);
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
687fedbba8a75202bbfb61f2f6918fd3
|
train_001.jsonl
|
1388331000
|
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
|
256 megabytes
|
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String n = scanner.nextLine();
int len = Integer.parseInt(n);
int[][] parts = new int[2][len];
for (int i = 0; i < len; i++) {
String[] data = scanner.nextLine().split(" ");
parts[0][i] = Integer.parseInt(data[0]);
parts[1][i] = Integer.parseInt(data[1]);
}
int i = 0;
int j = 0;
while(i < len && j < len && i + j < len) {
if(parts[0][i] < parts[1][j]) {
i++;
} else if(parts[0][i] > parts[1][j]) {
j++;
}
}
if(i < len/2) {
i = len/2;
}
if(j < len/2) {
j = len/2;
}
char[][] results =new char[2][len];
for(int k = 0; k < len; k++) {
if(k < i) {
results[0][k] = '1';
} else {
results[0][k] = '0';
}
if(k < j) {
results[1][k] = '1';
} else {
results[1][k] = '0';
}
}
System.out.println(new String(results[0]));
System.out.println(new String(results[1]));
scanner.close();
}
}
|
Java
|
["4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110"]
|
1 second
|
["1110\n1100", "1100\n1100"]
|
NoteConsider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
|
Java 8
|
standard input
|
[
"implementation",
"sortings"
] |
bea30e4ba653b9d0af87fc79b9ec8b4f
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
| 1,300 |
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
|
standard output
| |
PASSED
|
23a6388c8f3e8d412287a6d495e74885
|
train_001.jsonl
|
1384102800
|
Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri. Find the maximum of elements from li to ri. That is, calculate the value . Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.
|
256 megabytes
|
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
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 upperBounds[]=new int[n];
Arrays.fill(upperBounds,1000000000);
int currentAdditions[] = new int[n];
int operationsType[]=new int[m];
int operationsLeft[]=new int[m];
int operationsRight[]=new int[m];
int operationsD[]=new int[m];
for(int i=0;i<m;i++) {
operationsType[i]=in.nextInt();
operationsLeft[i]=in.nextInt()-1;
operationsRight[i]=in.nextInt()-1;
operationsD[i]=in.nextInt();
if(operationsType[i]==1) {
for(int j=operationsLeft[i];j<=operationsRight[i];j++) {
currentAdditions[j]+=operationsD[i];
}
} else {
for(int j=operationsLeft[i];j<=operationsRight[i];j++) {
upperBounds[j]=Math.min(upperBounds[j],operationsD[i]-currentAdditions[j]);
}
}
}
// validate
Arrays.fill(currentAdditions,0);
for(int i=0;i<m;i++) {
if(operationsType[i]==1) {
for(int j=operationsLeft[i];j<=operationsRight[i];j++) {
currentAdditions[j]+=operationsD[i];
}
}
else
if(operationsType[i]==2) {
int max=-1000000000;
for(int j=operationsLeft[i];j<=operationsRight[i];j++) {
max=Math.max(max,upperBounds[j]+currentAdditions[j]);
}
if(max!=operationsD[i]) {
out.println("NO");
return;
}
}
}
out.println("YES");
for(int i=0;i<n;i++) {
if(i!=0)
out.print(" ");
out.print(upperBounds[i]);
}
}
}
class InputReader {
StringTokenizer st;
BufferedReader in;
public InputReader(InputStream ins)
{
in = new BufferedReader(new InputStreamReader(ins));
}
public String nextToken()
{
while(st==null || !st.hasMoreTokens())
{
try {
st=new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(nextToken());
}
}
|
Java
|
["4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8", "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13"]
|
1 second
|
["YES\n4 7 4 7", "NO"]
| null |
Java 7
|
standard input
|
[
"constructive algorithms",
"greedy",
"brute force"
] |
ae5757df3be42b74076cdf42f7f897cd
|
The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n, - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n, - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type. The operations are given in the order Levko performed them on his array.
| 1,700 |
In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.
|
standard output
| |
PASSED
|
30ee7f107d31e60d0529e597116ba9b4
|
train_001.jsonl
|
1384102800
|
Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri. Find the maximum of elements from li to ri. That is, calculate the value . Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.
|
256 megabytes
|
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n, m;
n = in.nextInt();
m = in.nextInt();
int l[] = new int[m];
int r[] = new int[m];
int f[] = new int[m];
int d[] = new int[m];
long first[] = new long[n];
long second[] = new long[n];
for (int i = 0; i < n; ++i)
first[i] = 10000000000000L;
for (int i = 0; i < m; ++i) {
f[i] = in.nextInt();
l[i] = in.nextInt();
r[i] = in.nextInt();
d[i] = in.nextInt();
--l[i];
--r[i];
}
for (int i = m - 1; i >= 0; --i) {
if (f[i] == 2) {
for (int j = l[i]; j <= r[i]; ++j)
if (first[j] > d[i]) first[j] = d[i];
}
else {
for (int k = l[i]; k <= r[i]; ++k)
first[k] -= d[i];
}
}
boolean flag = false;
for (int i = 0; i < n; ++i)
if (first[i] > 1000000000)
first[i] = 1000000000;
for (int i = 0; i < n; ++i)
second[i] = first[i];
for (int i = 0; i < m; ++i) {
if (f[i] == 2) {
long max = second[l[i]];
for (int j = l[i] + 1; j <= r[i]; ++j)
if (max < second[j])
max = second[j];
if (max != d[i]) {
flag = true;
break;
}
} else
for (int j = l[i]; j <= r[i]; ++j)
second[j] += d[i];
}
if (flag)
System.out.println("NO");
else {
System.out.println("YES");
for (int i = 0; i < n; ++i)
System.out.print(first[i] + " ");
}
}
}
|
Java
|
["4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8", "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13"]
|
1 second
|
["YES\n4 7 4 7", "NO"]
| null |
Java 7
|
standard input
|
[
"constructive algorithms",
"greedy",
"brute force"
] |
ae5757df3be42b74076cdf42f7f897cd
|
The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n, - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n, - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type. The operations are given in the order Levko performed them on his array.
| 1,700 |
In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.
|
standard output
| |
PASSED
|
586457d7ea211704e6558bd801a3238b
|
train_001.jsonl
|
1384102800
|
Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri. Find the maximum of elements from li to ri. That is, calculate the value . Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.
|
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 elemCnt = nextInt();
int opCnt = nextInt();
int[] ar = new int[elemCnt];
int[] constraint = new int[elemCnt];
Arrays.fill(constraint, 1000000000);
int[][] ops = new int[opCnt][4];
for (int i = 0; i < opCnt; i++) {
ops[i][0] = nextInt();
ops[i][1] = nextInt()-1;
ops[i][2] = nextInt()-1;
ops[i][3] = nextInt();
}
for (int i = opCnt - 1; i >= 0; i--) {
if (ops[i][0] == 1) {
for (int j = ops[i][1]; j <= ops[i][2]; j++) {
constraint[j] -= ops[i][3];
}
} else {
for (int j = ops[i][1]; j <= ops[i][2]; j++) {
constraint[j] = Math.min(constraint[j], ops[i][3]);
}
}
}
for (int i = 0; i < constraint.length; i++) {
ar[i] = constraint[i];
}
// System.out.println(Arrays.toString(ar));
boolean valid = true;
for (int i = 0; i <opCnt; i++) {
if (ops[i][0] == 1) {
for (int j = ops[i][1]; j <= ops[i][2]; j++) {
constraint[j] += ops[i][3];
}
} else {
int max = Integer.MIN_VALUE;
for (int j = ops[i][1]; j <= ops[i][2]; j++) {
max = Math.max(constraint[j], max);
}
if (max != ops[i][3]) {
// System.out.println(max+" "+ops[i][3]+" "+i);
valid = false;
break;
}
}
// System.out.println(Arrays.toString(constraint));
}
PrintWriter out = new PrintWriter(System.out);
if (valid) {
out.println("YES");
for (int i = 0; i < elemCnt; i++) {
out.print(Math.min(ar[i],1000000000) + " ");
}
out.println();
} 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
|
["4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8", "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13"]
|
1 second
|
["YES\n4 7 4 7", "NO"]
| null |
Java 7
|
standard input
|
[
"constructive algorithms",
"greedy",
"brute force"
] |
ae5757df3be42b74076cdf42f7f897cd
|
The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n, - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n, - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type. The operations are given in the order Levko performed them on his array.
| 1,700 |
In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.
|
standard output
| |
PASSED
|
c228379da502e871b01b8dccc4613058
|
train_001.jsonl
|
1384102800
|
Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri. Find the maximum of elements from li to ri. That is, calculate the value . Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.
|
256 megabytes
|
import static java.lang.Math.*;
import static java.util.Arrays.*;
import java.util.*;
import java.io.*;
public class Main {
void solve() {
int n = sc.nextInt();
int m = sc.nextInt();
int[][] query = new int[m][4];
for (int i = 0; i < m; i++)
for (int j = 0; j < 4; j++)
query[i][j] = sc.nextInt();
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
int s = 0;
int mi = (int)1e9;
for (int[] q : query) {
if (q[0] == 1 && q[1] <= i && i <= q[2]) s += q[3];
if (q[0] == 2 && q[1] <= i && i <= q[2]) {
mi = min(mi, (q[3] - s));
}
}
a[i] = mi;
}
if (check(a.clone(), query)) {
out.println("YES");
print(Arrays.copyOfRange(a, 1, a.length));
} else {
out.println("NO");
}
}
boolean check(int[] a, int[][] query) {
int n = a.length - 1;
int m = query.length;
int[] mas = new int[m];
for (int i = 0; i < m; i++) mas[i] = Integer.MIN_VALUE;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < m; j++) {
int[] q = query[j];
if (q[0] == 1 && q[1] <= i && i <= q[2]) a[i] += q[3];
if (q[0] == 2 && q[1] <= i && i <= q[2]) {
mas[j] = max(mas[j], a[i]);
}
}
}
for (int j = 0; j < m; j++) {
int[] q = query[j];
if (q[0] == 2) {
if (mas[j] != q[3])
return false;
}
}
return true;
}
void print(int[] a) {
out.print(a[0]);
for (int i = 1; i < a.length; i++) out.print(" " + a[i]);
out.println();
}
static void tr(Object... os) {
System.err.println(deepToString(os));
}
static void fill(int[][] a, int val) {
for(int i = 0; i < a.length; i++) Arrays.fill(a[i], val);
}
static void fill(int[][][] a, int val) {
for(int i = 0; i < a.length; i++) fill(a[i], val);
}
public static void main(String[] args) throws Exception {
new Main().run();
}
MyScanner sc = null;
PrintWriter out = null;
public void run() throws Exception {
sc = new MyScanner(System.in);
out = new PrintWriter(System.out);
for (;sc.hasNext();) {
solve();
out.flush();
}
out.close();
}
class MyScanner {
String line;
BufferedReader reader;
StringTokenizer tokenizer;
public MyScanner(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public void eat() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
line = reader.readLine();
if (line == null) {
tokenizer = null;
return;
}
tokenizer = new StringTokenizer(line);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public String next() {
eat();
return tokenizer.nextToken();
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public boolean hasNext() {
eat();
return (tokenizer != null && tokenizer.hasMoreElements());
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
}
}
|
Java
|
["4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8", "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13"]
|
1 second
|
["YES\n4 7 4 7", "NO"]
| null |
Java 7
|
standard input
|
[
"constructive algorithms",
"greedy",
"brute force"
] |
ae5757df3be42b74076cdf42f7f897cd
|
The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n, - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n, - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type. The operations are given in the order Levko performed them on his array.
| 1,700 |
In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.
|
standard output
| |
PASSED
|
4bde9a97ac5cae3b435e0e794d2e7d9f
|
train_001.jsonl
|
1384102800
|
Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri. Find the maximum of elements from li to ri. That is, calculate the value . Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.StringTokenizer;
public class LevkoAndArrayRecovery {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
final int MAX_VALUE = 1000000000;
int N = sc.nextInt();
int M = sc.nextInt();
int[][] ops = new int[M][];
for (int i = 0; i < M; i++) {
ops[i] = sc.nextIntArray(4);
}
int[] maxes = new int[N + 1];
int[] diffs = new int[N + 1];
Arrays.fill(maxes, MAX_VALUE);
for (int[] op : ops) {
if (op[0] == 1) {
for (int i = op[1]; i <= op[2]; i++) {
diffs[i] += op[3];
}
} else {
for (int i = op[1]; i <= op[2]; i++) {
maxes[i] = Math.min(maxes[i], op[3] - diffs[i]);
}
}
}
for (int n : maxes) {
if (Math.abs(n) > MAX_VALUE) {
System.out.println("NO");
return;
}
}
int[] arr = Arrays.copyOf(maxes, maxes.length);
for (int[] op : ops) {
if (op[0] == 1) {
for (int i = op[1]; i <= op[2]; i++) {
maxes[i] += op[3];
}
} else {
int tmpMax = Integer.MIN_VALUE;
for (int i = op[1]; i <= op[2]; i++) {
tmpMax = Math.max(tmpMax, maxes[i]);
}
if (tmpMax != op[3]) {
System.out.println("NO");
return;
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= N; i++) {
sb.append(arr[i] + " ");
}
System.out.println("YES");
System.out.println(sb.toString());
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
String str = "";
try { str = br.readLine(); }
catch (IOException e) { e.printStackTrace(); }
return str;
}
}
}
|
Java
|
["4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8", "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13"]
|
1 second
|
["YES\n4 7 4 7", "NO"]
| null |
Java 7
|
standard input
|
[
"constructive algorithms",
"greedy",
"brute force"
] |
ae5757df3be42b74076cdf42f7f897cd
|
The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n, - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n, - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type. The operations are given in the order Levko performed them on his array.
| 1,700 |
In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.
|
standard output
| |
PASSED
|
fe014892b908ba4ab92fbad43f3971f5
|
train_001.jsonl
|
1384102800
|
Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri. Find the maximum of elements from li to ri. That is, calculate the value . Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class ZadC {
public static void main(String... args) throws IOException {
MyScanner sc = new MyScanner();
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Op> ops = new ArrayList<>();
int[] nn = new int[n];
int[] nnt = new int[n];
boolean[] filed = new boolean[n];
for (int i = 0; i < m; i++) {
Op o = new Op();
o.op = sc.nextInt();
o.a = sc.nextInt();
o.b = sc.nextInt();
o.v = sc.nextInt();
ops.add(o);
}
for (int j=ops.size()-1; j>=0; j--){
Op op = ops.get(j);
for (int i = op.a - 1; i < op.b; i++) {
if (op.op == 1) {
if (filed[i]) {
nn[i] -= op.v;
}
} else {
if (!filed[i]) {
filed[i] = true;
nn[i] = op.v;
} else {
if (nn[i] > op.v) {
nn[i] = op.v;
}
}
}
}
}
for (int i = 0; i < n; i++) {
if (!filed[i]) {
nn[i] = 0;
}
nnt[i] = nn[i];
}
boolean good = true;
for (Op op : ops) {
if (op.op == 1) {
for (int i = op.a - 1; i < op.b; i++) {
nnt[i] += op.v;
}
} else {
int max = nnt[op.a - 1];
for (int i = op.a; i < op.b; i++) {
max = (max < nnt[i]) ? nnt[i] : max;
}
if (max < op.v) {
good = false;
break;
}
}
}
if (!good) {
System.out.println("NO");
} else {
System.out.println("YES");
for (int i = 0; i < n; i++) {
System.out.print(nn[i] + " ");
}
}
}
static class Op {
int op;
int a;
int b;
int v;
public Op() {
}
public Op(int op, int a, int b, int v) {
this.op = op;
this.a = a;
this.b = b;
this.v = v;
}
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() throws IOException {
if (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
st = new StringTokenizer(line);
}
return st.nextToken();
}
public int nextInt() throws IOException {
String next = next();
return Integer.parseInt(next);
}
}
}
|
Java
|
["4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8", "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13"]
|
1 second
|
["YES\n4 7 4 7", "NO"]
| null |
Java 7
|
standard input
|
[
"constructive algorithms",
"greedy",
"brute force"
] |
ae5757df3be42b74076cdf42f7f897cd
|
The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n, - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n, - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type. The operations are given in the order Levko performed them on his array.
| 1,700 |
In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.
|
standard output
| |
PASSED
|
ef9bc732165f06c65b4709f8bf6a2590
|
train_001.jsonl
|
1384102800
|
Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri. Find the maximum of elements from li to ri. That is, calculate the value . Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.
|
256 megabytes
|
import java.io.PrintWriter;
import java.util.Scanner;
public class c {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner (System.in);
PrintWriter out = new PrintWriter (System.out);
int n = in.nextInt();
int m = in.nextInt();
int[] add = new int[n+1];
int[] max = new int[n+1];
for (int i = 0;i<=n;i++){
max[i] = 1000000000;
}
int[] t = new int[m+1];
int[] l = new int[m+1];
int[] r = new int[m+1];
int[] d = new int[m+1];
int[] ma = new int[m+1];
for (int i = 0;i<m;i++){
t[i] = in.nextInt();
if (t[i]==1) {
l[i] = in.nextInt();
r[i] = in.nextInt();
d[i] = in.nextInt();
for (int j = l[i];j<=r[i];j++){
add[j]+=d[i];
}
} else {
l[i] = in.nextInt();
r[i] = in.nextInt();
ma[i] = in.nextInt();
for(int j = l[i];j<=r[i];j++){
max[j] = Math.min(ma[i] - add[j],max[j]);
}
}
}
boolean ok = true;
int[] add2 = new int[n+1];
for (int i = 0;i<m;i++){
if (t[i]==1) {
for (int j = l[i];j<=r[i];j++){
add2[j]+=d[i];
}
} else {
boolean ok1 = false;
for(int j = l[i];j<=r[i];j++){
if (max[j]+add2[j] == ma[i]) {
ok1 = true;
//out.println(i+" "+j+" "+max[j]+" "+add2[j]+" "+ma[i]);
break;
}
}
if (!ok1) {
ok = false;
break;
}
}
}
if (ok) {
out.println("YES");
for (int i = 1;i<=n;i++){
out.print(max[i]+" ");
}
} else out.println("NO");
out.flush();
}
}
|
Java
|
["4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8", "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13"]
|
1 second
|
["YES\n4 7 4 7", "NO"]
| null |
Java 7
|
standard input
|
[
"constructive algorithms",
"greedy",
"brute force"
] |
ae5757df3be42b74076cdf42f7f897cd
|
The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n, - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n, - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type. The operations are given in the order Levko performed them on his array.
| 1,700 |
In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.
|
standard output
| |
PASSED
|
e0f51f44f72e17432f04dec4a866f7ec
|
train_001.jsonl
|
1384102800
|
Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri. Find the maximum of elements from li to ri. That is, calculate the value . Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.
|
256 megabytes
|
import java.io.*;
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;
}
double nextDouble() throws IOException{
in.nextToken();
return in.nval;
}
char nextChar() throws IOException{
in.nextToken();
return (char)in.ttype;
}
String nextString() throws IOException{
in.nextToken();
return in.sval;
}
private static final String INPUT = "g.in";
private static final int MAX_VALUE = 1000000000;
class ArrayElement{
public int add;
public int maxVal;
public ArrayElement(){
add = 0;
maxVal = MAX_VALUE;
}
}
class OperationElement{
public int t, l, r, k;
public OperationElement(int t, int l, int r, int k){
this.t = t;
this.l = l;
this.r = r;
this.k = k;
}
}
ArrayElement[] array;
OperationElement[] operations;
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"));
}else{
// in = new StreamTokenizer(new BufferedReader(new FileReader(INPUT)));
// ins = new Scanner(new FileReader(INPUT));
// out = new PrintWriter(System.out);
}
}catch(Exception e){
// in = new StreamTokenizer(new BufferedReader(new FileReader(INPUT)));
// ins = new Scanner(new FileReader(INPUT));
// out = new PrintWriter(System.out);
}
int n = nextInt(), m = nextInt();
array = new ArrayElement[n];
for(int i = 0; i < n; i++){
array[i] = new ArrayElement();
}
operations = new OperationElement[m];
for(int i = 0; i < m; i++){
operations[i] = new OperationElement(nextInt(), nextInt() - 1, nextInt() - 1, nextInt());
proceedOperation(operations[i]);
}
if(checkArray()){
out.println("YES");
for(int i = 0; i < n; i++){
out.printf("%d ", array[i].maxVal);
}
}else{
out.print("NO");
}
out.close();
}
void proceedOperation(OperationElement op){
if(op.t == 1){
for(int i = op.l; i <= op.r; i++){
array[i].add += op.k;
}
}else if(op.t == 2){
for(int i = op.l; i <= op.r; i++){
array[i].maxVal = Math.min(array[i].maxVal, op.k - array[i].add);
}
}
}
boolean checkArray(){
int tmpArray[] = new int[array.length];
for(int i = 0; i < tmpArray.length; i++){
tmpArray[i] = array[i].maxVal;
}
for(int i = 0; i < operations.length; i++){
if(!checkOperation(operations[i], tmpArray)){
return false;
}
}
return true;
}
boolean checkOperation(OperationElement op, int[] arr){
if(op.t == 1){
for(int i = op.l; i <= op.r; i++){
arr[i] += op.k;
}
return true;
}else if(op.t == 2){
int answ = -MAX_VALUE;
for(int i = op.l; i <= op.r; i++){
answ = Math.max(answ, arr[i]);
}
return answ == op.k;
}
return true;
}
}
|
Java
|
["4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8", "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13"]
|
1 second
|
["YES\n4 7 4 7", "NO"]
| null |
Java 7
|
standard input
|
[
"constructive algorithms",
"greedy",
"brute force"
] |
ae5757df3be42b74076cdf42f7f897cd
|
The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n, - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n, - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type. The operations are given in the order Levko performed them on his array.
| 1,700 |
In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.
|
standard output
| |
PASSED
|
dbdd46f96636d57146c0831de4c0c48f
|
train_001.jsonl
|
1384102800
|
Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types: Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri. Find the maximum of elements from li to ri. That is, calculate the value . Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.
|
256 megabytes
|
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
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 double PI = Math.acos(-1.0);
private final int SIZEN = 1300000;
private final int MOD = (int)(1e9 + 7);
private final long MODH = 10000000007L, BASE = 10007;
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[][] op = new int[m][4];
for(int i = 0;i < m;++i)
{
op[i][0] = scan.nextInt();
op[i][1] = scan.nextInt() - 1;
op[i][2] = scan.nextInt() - 1;
op[i][3] = scan.nextInt();
}
final int INF = (int)(1e9);
int[] a = new int[n];
Arrays.fill(a, INF);
for(int i = m - 1;i >= 0;--i)
{
if(1 == op[i][0])
{
for(int j = op[i][1];j <= op[i][2];++j)
{
if(a[j] != INF) a[j] -= op[i][3];
}
}
else
{
for(int j = op[i][1];j <= op[i][2];++j)
{
if(a[j] > op[i][3]) a[j] = op[i][3];
}
}
}
int[] ans = Arrays.copyOf(a, n);
for(int i = 0;i < m;++i)
{
if(1 == op[i][0])
{
for(int j = op[i][1];j <= op[i][2];++j) a[j] += op[i][3];
}
else
{
int maxVal = -INF;
for(int j = op[i][1];j <= op[i][2];++j) maxVal = Math.max(maxVal, a[j]);
if(maxVal != op[i][3])
{
out.println("NO");
return;
}
}
}
out.println("YES");
for(int i = 0;i < n;++i)
{
out.print(ans[i] + " ");
}
}
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 t: String to match.
* @param p: String to be matched.
* @return if can match, first index; otherwise -1.
*/
public int kmpMatch(char[] t, char[] p)
{
int n = t.length;
int m = p.length;
int[] next = new int[m + 1];
next[0] = -1;
int j = -1;
for(int i = 1;i < m;++i)
{
while(j >= 0 && p[i] != p[j + 1])
{
j = next[j];
}
if(p[i] == p[j + 1])
{
++j;
}
next[i] = j;
}
j = -1;
for(int i = 0;i < n;++i)
{
while(j >= 0 && t[i] != p[j + 1])
{
j = next[j];
}
if(t[i] == p[j + 1])
{
++j;
}
if(j == m - 1)
{
return i - m + 1;
}
}
return -1;
}
/**
* 5---Get the hash code of a String
* @param s: input string
* @return hash code
*/
public long hash(String s)
{
long key = 0, t = 1;
for(int i = 0;i < s.length();++i)
{
key = (key + s.charAt(i) * t) % MODH;
t = t * BASE % MODH;
}
return key;
}
/**
* 6---Get x ^ n % MOD quickly.
* @param x: base
* @param n: times
* @return x ^ n % MOD
*/
public long quickMod(long x, long n)
{
long ans = 1;
while(n > 0)
{
if(1 == n % 2)
{
ans = ans * x % MOD;
}
x = x * x % MOD;
n >>= 1;
}
return ans;
}
/**
* 7---judge if a point is located inside a polygon
* @param x0 the x coordinate of the point
* @param y0 the y coordinate of the point
* @return true if it is inside the polygon, otherwise false
*/
/*public boolean contains(double x0, double y0)
{
int cross = 0;
for(int i = 0;i < n;++i)
{
double s = x[i + 1] == x[i] ? 100000000 : (double)(y[i + 1] - y[i]) / (x[i + 1] - x[i]);
boolean b1 = x[i] <= x0 && x0 < x[i + 1];
boolean b2 = x[i + 1] <= x0 && x0 < x[i];
boolean b3 = y0 < s * (x0 - x[i]) + y[i];
if((b1 || b2) && b3) ++cross;
}
return cross % 2 != 0;
}*/
/**
* 8---get the cross of two vectors
* @param x1
* @param y1
* @param x2
* @param y2
* @return cross
*/
public long cross(long x1, long y1, long x2, long y2)
{
return x1 * y2 - x2 * y1;
}
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
|
["4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8", "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13"]
|
1 second
|
["YES\n4 7 4 7", "NO"]
| null |
Java 7
|
standard input
|
[
"constructive algorithms",
"greedy",
"brute force"
] |
ae5757df3be42b74076cdf42f7f897cd
|
The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n, - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n, - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type. The operations are given in the order Levko performed them on his array.
| 1,700 |
In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.
|
standard output
| |
PASSED
|
35c4484e7f46bb4b445961c30935b9d2
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class VanyaandCards {
public static void main(String[] args) {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
try{
String [] line1 = br.readLine().split("\\s+");
int nCards = Integer.parseInt(line1[0]);
int max = Integer.parseInt(line1[1]);
int sum=0;
String [] line2 = br.readLine().split("\\s+");
for (int i=0; i<nCards ;i++){
sum += Integer.parseInt(line2[i]);
}
int count=0;
int sum1 = Math.abs(sum);
while (sum1 >0){
sum1 -= max;
count++;
}
System.out.println(count);
}
catch (Exception e){
}
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
983acafa71704524ba4553dd5db15f45
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int x=input.nextInt();
int sum=0;
while(n-->0)
sum+=input.nextInt();
sum=Math.abs(sum);
int b=sum/x;
if(sum%x!=0)
b++;
System.out.println(b);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
d8b609cf950248fc59d49098fbabf4c2
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static boolean debug = false;
public static Reader in = new Reader();
public static Writer out = new Writer();
public static void main(String[] args) {
int n = in.nextInt();
int x = in.nextInt();
int sum = 0;
for(int i=0; i<n; i++) {
sum += in.nextInt();
}
int count = 0;
while(sum < 0) {
sum += Math.min(-sum, x);
count++;
}
while(sum > 0) {
sum -= Math.min(sum, x);
count++;
}
out.println(count);
out.close();
}
}
class Reader {
private BufferedReader input;
private StringTokenizer line = new StringTokenizer("");
public Reader() {
input = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(String s) {
try {
input = new BufferedReader(new FileReader(s));
} catch(IOException io) { input = new BufferedReader(new InputStreamReader(System.in));}
}
public void fill() {
try {
if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public double nextDouble() {
fill();
return Double.parseDouble(line.nextToken());
}
public String nextWord() {
fill();
return line.nextToken();
}
public int nextInt() {
fill();
return Integer.parseInt(line.nextToken());
}
public long nextLong() {
fill();
return Long.parseLong(line.nextToken());
}
public double readDouble() {
double d = 0;
try {
d = Double.parseDouble(input.readLine());
} catch(IOException io) {io.printStackTrace(); System.exit(0);}
return d;
}
public int readInt() {
int i = 0;
try {
i = Integer.parseInt(input.readLine());
} catch(IOException io) {io.printStackTrace(); System.exit(0);}
return i;
}
public int[] readInts(int n) {
int[] a = new int[n];
for(int i=0; i<n; i++)
a[i] = nextInt();
return a;
}
public void fillInts(int[] a) {
for(int i=0; i<a.length; i++)
a[i] = nextInt();
}
public long readLong() {
long l = 0;
try {
l = Long.parseLong(input.readLine());
} catch(IOException io) {io.printStackTrace(); System.exit(0);}
return l;
}
public String readLine() {
String s = "";
try {
s = input.readLine();
} catch(IOException io) {io.printStackTrace(); System.exit(0);}
return s;
}
}
class Writer {
private BufferedWriter output;
public Writer() {
output = new BufferedWriter(new OutputStreamWriter(System.out));
}
public Writer(String s) {
try {
output = new BufferedWriter(new FileWriter(s));
} catch(Exception ex) { ex.printStackTrace(); System.exit(0);}
}
public void println() {
try {
output.append("\n");
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void print(Object o) {
try {
output.append(o.toString());
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void println(Object o) {
try {
output.append(o.toString()+"\n");
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void printf(String format, Object... args) {
try {
output.append(String.format(format, args));
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void printfln(String format, Object... args) {
try {
output.append(String.format(format, args)+"\n");
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void flush() {
try {
output.flush();
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void close() {
try {
output.close();
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
81db8b41503758c83689a691b0b8d893
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
new A().solve(scanner, out);
scanner.close();
out.close();
}
}
class A {
public void solve(Scanner in, PrintWriter out) {
int n = in.nextInt();
int x = in.nextInt();
int sum = 0;
for (int i =0 ; i < n; i ++) {
sum += in.nextInt();
}
out.println(((Math.abs(sum)) / x) + (0 < (Math.abs(sum) % x) ? 1 : 0));
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
163561f9b424c5db45f574166740460b
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
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.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader s = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n=s.nextInt(),x=s.nextInt();
int sum=0;
while (n-- >0)
{
sum+=s.nextInt();
}
sum=Math.abs(sum);
out.println((sum+(x-1))/x);
out.flush();
out.close();
}
}
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
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
b155824d31be1e7b602de9a3d6b6c90f
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
//если в этой задаче нужно выводить формулу, и оно работает -- я не знаю, как оно работает :)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Codeforces {
private static final int INF = (int) 1e9;
BufferedReader in;
StringTokenizer st;
public static void main(String[] args) throws Exception {
new Codeforces().run();
}
void run() throws Exception {
in = new BufferedReader(new InputStreamReader(System.in));
int n = nextInt();
int x = nextInt();
int[] k = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
sum += nextInt();
}
sum = Math.abs(sum);
System.out.println(sum / x + (sum % x > 0 ? 1 : 0));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.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());
}
class pair implements Comparable<pair> {
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(pair o2) {
if (x < o2.x) {
return -1;
} else {
if (x == o2.x) {
if (y < o2.y) {
return 1;
} else {
return -1;
}
} else {
return 1;
}
}
}
String getVolume() {
return x + " " + y;
}
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
d401c5c8f37ffcdb30237796432bfa30
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Random;
import java.lang.*;
public class zad {
private static BufferedReader in;
private static StringTokenizer tok;
private static PrintWriter out;
final static boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") !=null;
public static void init() throws FileNotFoundException{
if(ONLINE_JUDGE){
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
else{
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("out.txt");
}
}
private static String readString() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
private static int readInt() throws IOException {
return Integer.parseInt(readString());
}
private static double readDouble() throws IOException {
return Double.parseDouble(readString());
}
private static long readLong() throws IOException {
return Long.parseLong(readString());
}
private static float readFloat() throws IOException{
return Float.parseFloat(readString());
}
public static void Solve() throws IOException{
int n =readInt();
int x =readInt();
long sum=0;
for(int i=0;i<n;i++){
sum+=readInt();
}
sum = Math.abs(sum);
out.print(sum%x ==0 ? sum/x : sum/x+1);
}
public static void main(String[] args) throws IOException {
init();
Solve();
in.close();
out.close();
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
c2a43528bc417270df51143c981c44d5
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.Scanner;
public class VanyaAndCards {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int max = in.nextInt();
int sum = 0;
for(int i=0; i<n; i++) {
sum += in.nextInt();
}
if(Math.abs(sum) == 0) {
System.out.println(0);
} else {
System.out.println((int) Math.ceil(Math.abs(sum) * 1.0 / max));
}
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
62f9a0011a133e4d5c34b012f260a18a
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.*;
public class VanyaandCards {
public static void main (String []args) {
Scanner in=new Scanner (System.in);
int n=in.nextInt();
int x=in.nextInt();
int rem=0;
int res=0;
for (int i=0;i<n;i++)
rem+=in.nextInt();
while (rem!=0) {
if (rem>0) {
if (rem<=x){
res++;
rem=0;
} else {
res++;
rem-=x;
}
} else {
if (rem>=(-x)){
res++;
rem=0;
} else {
res++;
rem+=x;
}
}
}
System.out.println(res);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
be1c19039fe404e3c3433f730d742da5
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.Scanner;
public class VanyaAndCards {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int x = in.nextInt();
long sum = 0;
for(int i = 0; i < n; i++){
sum += in.nextInt();
}
long ans = 0;
sum = sum < 0 ? sum*-1 : sum;
while(sum != 0){
if(sum > x) {
sum -= x;
ans++;
} else {
sum = 0;
ans++;
}
}
System.out.println(ans);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
4bd64353c974e2a69c1438f8e7a7da16
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.LinkedList;
import java.util.Scanner;
public class Cards {
public Cards (){
Scanner stream = new Scanner (System.in);
int n = stream.nextInt() ;
int x = stream.nextInt();
int [] arr = new int [n];
for (int i =0 ; i < n ; i++){
arr[i] = stream.nextInt() ;
}
System.out.println(count (arr, x) );
}
public int count (int [] arr , int x){
int count = 0 ;
int sum = 0 ;
for (int i =0 ; i < arr.length ; i++){
sum += arr[i];
}
if(sum > 0 ){
while (sum > 0){
sum -= x ;
count ++ ;
}
}else {
while (sum < 0){
sum += x ;
count ++ ;
}
}
return count ;
}
public static void main (String [] args){
new Cards ();
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
eaf15c8abe8391b5a6abfdc96c6c41d7
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.Scanner;
public class cards {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int m=in.nextInt();
int x;
int sum=0;
for(int i=0;i<n;i++){
x=in.nextInt();
sum+=x;
}
int a=1;
sum=Math.abs(sum);
if(sum==0)
System.out.print(0);
else if(sum<=m)
System.out.print(1);
else{
for(double i=2;i<=sum;i++){
a++;
if(sum/i<=(double)m){
break;
}
}
System.out.print(a);
}
}}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
0d957487e6572eeb5f7eab7b1a1aba2b
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
PrintWriter out;
BufferedReader input;
Main() {
try {
input = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter(new FileWriter("output.txt"), true);
solver();
} catch (Exception ex) {
ex.printStackTrace();
input = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out, true);
solver();
} finally {
out.close();
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
StringTokenizer st = new StringTokenizer("");
private String nextToken() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(input.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
private int nextInt() {
return Integer.parseInt(nextToken());
}
private long nextLong() {
return Long.parseLong(nextToken());
}
public void solver() {
int n = nextInt();
int m = nextInt();
int sum = 0;
for (int i = 0; i < n; i++) {
sum += nextInt();
}
sum = Math.abs(sum);
System.out.println(sum/m + ((sum%m == 0)?0:1));
}
public static void main(String[] args) {
new Main();
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
d1fe771aa89d6d4146cc89add0a042eb
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class cards{
public static void main(String args[]) throws IOException
{
Scanner sc= new Scanner(System.in);
int n=Integer.parseInt(sc.next());
int x=Integer.parseInt(sc.next());
int in;
int sum=0,f=0,find=0;
for(int c=0;c<n;c++)
{
in=Integer.parseInt(sc.next());
sum+=in;
}
if(sum>0)
x=-x;
if(sum<0)
do{
if(sum+x>sum&&sum+x<=0)
{++find;
sum+=x;}
if(Math.abs(sum)>=x)
continue;
else
--x;
}while(sum!=0);
else if(sum>0)
do{
if(sum+x<sum&&sum+x>=0)
{++find;
sum+=x;}
if(Math.abs(sum)>=Math.abs(x))
continue;
else
++x;
}while(sum!=0);
else
find=0;
System.out.println(find);
}}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
87b60827e929787f0b9986a03acc5a0d
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.*;
public class a401
{
public static void main(String ar[])
{
Scanner ob=new Scanner(System.in);
int n=ob.nextInt();
int m=ob.nextInt();
int x=0,sum=0;
for(int i=0;i<n;i++)
{
sum=sum+ob.nextInt();
}
sum=Math.abs(sum);
if(sum>=m)
{
if(sum%m==0)
System.out.println(sum/m);
else
System.out.println(sum/m+1);
}
else if(sum==0)
System.out.println(0);
else
System.out.println(1);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
b33bb6861c74836c68ee2a36d4b9ae96
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.*;
public class Cards {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numCards = input.nextInt();
int maxVal = input.nextInt();
int startVal = 0;
for (int i = 0; i < numCards; i++) {
startVal += input.nextInt();
}
int cardsNeeded = (int)Math.ceil(Math.abs(startVal) / (double)maxVal);
System.out.println(cardsNeeded);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
a194f5fe3f1f0aca2111430f837c4722
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Scanner;
public class A401 {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int x = in.nextInt();
ArrayList<Integer> cards = new ArrayList<>();
int sum=0;
for (int i=0 ; i<n ; i++) {
cards.add(in.nextInt());
sum+=cards.get(i);
}
sum=Math.abs(sum);
int res=0,i=x;
while (sum>0) {
//System.out.println("sum:"+sum+" i:"+i);
if (i<=sum) {sum-=i;
res++;
} else
i--;
}
System.out.println(res);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
b0a98a1ec5dfe92ac8fe158b7e945dea
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.Scanner;
public class VanyAndCards {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int x = input.nextInt();
input.nextLine();
int sum = 0;
int result = 0;
for (int i = 0; i < n; i++) {
sum += input.nextInt();
}
sum = Math.abs(sum);
result = sum / x;
if (sum % x == 0) {
//do nothing
}
else {
result++;
}
System.out.println(result);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
ec103c00aacad85f76745936b3abeb3b
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
int n = inScanner.nextInt();
int x = inScanner.nextInt();
double sum = 0;
for (int i = 0; i < n; i++) {
sum += inScanner.nextInt();
}
System.out.println((int) Math.ceil(((double) Math.abs(sum)) / x));
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
ce521ec62592455b1147dd0c44781a59
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Thread(null, new Solution(), "", 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("input.txt"));
//out = new PrintWriter("output.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(), x = readInt();
int[] mas = new int[n];
int sum = 0;
for(int i = 0; i<n; i++)
{
mas[i] = readInt();
sum += mas[i];
}
sum = Math.abs(sum);
int answer = sum / x;
if(sum % x > 0) answer++;
out.println(answer);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
d0cd19136b755b5d4c1f28c4516980a4
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class CombinationLock {
static class Reader
{
BufferedReader r;
StringTokenizer str;
Reader()
{
r=new BufferedReader(new InputStreamReader(System.in));
}
Reader(String fileName) throws FileNotFoundException
{
r=new BufferedReader(new FileReader(fileName));
}
public String getNextToken() throws IOException
{
if(str==null||!str.hasMoreTokens())
{
str=new StringTokenizer(r.readLine());
}
return str.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(getNextToken());
}
public long nextLong() throws IOException
{
return Long.parseLong(getNextToken());
}
public double nextDouble() throws IOException
{
return Double.parseDouble(getNextToken());
}
public String nextString() throws IOException
{
return getNextToken();
}
public int[] intArray(int n) throws IOException
{
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=nextInt();
return a;
}
public long[] longArray(int n) throws IOException
{
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=nextLong();
return a;
}
public String[] stringArray(int n) throws IOException
{
String a[]=new String[n];
for(int i=0;i<n;i++)
a[i]=nextString();
return a;
}
public long gcd(long a, long b) {
if(b == 0){
return a;
}
return gcd(b, a%b);
}
}
public static void main(String args[]) throws IOException{
Reader r=new Reader();
PrintWriter pr=new PrintWriter(System.out,false);
int cards=r.nextInt();
int limit=r.nextInt();
int arr[]=new int[cards];
int sum=0;
for(int a=0;a<cards;a++)
{
arr[a]=r.nextInt();
sum+=arr[a];
}
if(sum<0)
{
if((sum*(-1))%limit==0) pr.print((((sum)*(-1))/limit));
else pr.print((((sum)*(-1))/limit)+1);
}
else if(sum==0) pr.print(0);
else
{
if(sum%limit==0) pr.print(sum/limit);
else pr.print((sum/limit)+1);
}
pr.flush();
pr.close();
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
5220102b2aff911d60cc702d90f8e214
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Solution {
FastReader fr;
PrintWriter pw;
public void solve() {
int n = fr.nextInt();
int x = fr.nextInt();
int[] cards = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
cards[i] = fr.nextInt();
sum += cards[i];
}
if (Math.abs(sum) == 0) {
pw.print(0);
} else
if (Math.abs(sum) <= x) {
pw.println("1");
} else {
int result = Math.abs(sum) / x;
int diff = Math.abs(sum) % x;
if (diff != 0) {
result++;
}
pw.println(result);
}
}
public void run() throws FileNotFoundException {
fr = new FastReader(System.in);
pw = new PrintWriter(System.out);
solve();
pw.close();
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
}
String next() {
while (st == null || !(st.hasMoreTokens())) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextString() {
return next();
}
}
public static void main(String[] args) throws FileNotFoundException {
new Solution().run();
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
a59ff200051a2e0518cd2722fb3ec509
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class VanyaandCards {
public static void main(String[] args) throws IOException{
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st= new StringTokenizer(in.readLine());
int n=Integer.parseInt(st.nextToken());
int x=Integer.parseInt(st.nextToken());
st= new StringTokenizer(in.readLine());
int sum=0,c=0;
for (int i = 0; i < n; i++) {
int v=Integer.parseInt(st.nextToken());
sum+=v;
}
sum=sum<0?-sum:sum;
for (int i = x; i!=0 && sum!=0; i--)
if(sum-i>=0){
sum-=i;
c++;
i++;
}
System.out.println(c);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
3f7d75c826ac133f8f69004f9af7c750
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.lang.*;
import java.util.Scanner;
public class vanya_and_cards
{
public static void main(String args[])
{
int n,x;
Scanner s=new Scanner(System.in);
n=s.nextInt();
x=s.nextInt();
int i,input,count=0;
int sum=0;
for(i=0;i<n;i++)
{
input=s.nextInt();
sum+=input;
}
if(sum<0)
sum=-sum;
//System.out.println(sum);
if(sum%x==0)
count=sum/x;
else
{
count=sum/x;
count+=1;
}
System.out.println(count);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
f7bacd075807498755a86fc4e68fd2f1
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main (String [] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = sc.nextInt();
int x = sc.nextInt();
int[] array = new int[n];
int sum =0;
for(int i = 0 ; i <n ;++i)
{
array[i]=sc.nextInt();
sum +=array[i];
}
int count =0;
sum = Math.abs(sum);
if(sum%x!=0)count++;
System.out.println(count+sum/x);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
f63a43db71be340d4ddcbf27d5e5c931
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
//package javaapplication1;
import java.io.*;
import java.util.*;
public class JavaApplication1 implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;
void init() throws FileNotFoundException {
if (OJ) {
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");
}
}
@Override
public void run() {
try {
init();
solve();
out.close();
} catch(Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args) {
new Thread(null, new JavaApplication1(), "", (1L << 20) * 256).start();
}
String readString() {
while(!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine());
} catch(Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
return tok.nextToken();
}
int readInt() {
return Integer.parseInt(readString());
}
////////////////////////////////////////////////////////////////////////////////
class Pair implements Comparable {
public int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Object x) {
int cx = ((Pair)x).a;
int cy = ((Pair)x).b;
if (a > cx) {
return 1;
} else if (a < cx) {
return -1;
} else {
if (b > cy) {
return 1;
} else if (b < cy) {
return -1;
} else {
return 0;
}
}
}
}
void solve() {
int n = readInt();
int x = readInt();
int sum = 0;
for(int i = 0; i < n; i++) {
sum += readInt();
}
sum = Math.abs(sum);
out.println((sum + x - 1) / x);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
215f82b893e813e8339caa4e15775611
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int x=sc.nextInt();
int sum=0;
for(int i=0;i<n;i++)
sum+=sc.nextInt();
int t=0;
if(sum<0)
sum*= -1;
while(sum>0 && x>0)
{
int r=sum%x;
t+= sum/x;
sum=r;
x--;
}
System.out.print(t);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
51939e360a822066d6ab6d847ef4ec08
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import static java.lang.Math.abs;
/**
* Created by Dmytry on 3/10/14.
*/
public class Main {
static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st = new StringTokenizer("");
public static void main(String[] args) throws Exception {
int k = NI();
int x = NI();
int sum = 0;
int answer = 0;
int[] arr = new int[k];
for (int i = 0; i < k; i ++) {
arr[i] = NI();
sum += arr[i];
}
if (sum != 0 && abs(sum) <= x) System.out.println(1);
else {
if (abs(sum) % x == 0) {
answer = abs(sum) / x ;
System.out.println(answer);
}
else {
answer = abs(sum) / x + 1;
System.out.println(answer);
}
}
//System.out.println(k + " " + x);
/*for (int q : arr) {
System.out.println(q);
}*/
}
static String NS() throws Exception {
while (!st.hasMoreTokens()) st = new StringTokenizer(stdin.readLine());
return st.nextToken();
}
static int NI() throws Exception {return Integer.parseInt(NS());}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
a96f032c025e03d9390cc65becd12d9d
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.*;
public class Cards {
public static void main(String[]args) {
Scanner sc=new Scanner(System.in);
int found=sc.nextInt();
int maxNum=sc.nextInt();
int sumFound=0;
int j=1;
int needed=0;
do {
sumFound+=sc.nextInt();
j++;
} while(j<=found);
if (sumFound>0)
maxNum=-maxNum;
while (sumFound!=0) {
if (maxNum>0) {
if (sumFound+maxNum<=0) {
needed++;
sumFound+=maxNum;
}
else
maxNum--;
}
else {
if (sumFound+maxNum>=0) {
needed++;
sumFound+=maxNum;
}
else
maxNum++;
}
}
System.out.println(needed);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
20d8ad3135fdc5fa3805d0c657b735d1
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Round235 {
//problem A
public static void main(String[] arg) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] line = bf.readLine().split(" ");
int limit = Integer.parseInt(line[1]);
line = bf.readLine().split(" ");
int sum = 0;
for(int i = 0; i < line.length; i++)
sum += Integer.parseInt(line[i]);
int res = sum/limit;
if (sum%limit != 0 && sum/limit < 0)
res--;
else if (sum%limit != 0 && sum/limit > 0)
res++;
else if (sum/limit == 0 && sum != 0)
res++;
if (res >= 0)
System.out.println(res);
else
System.out.println(-1*res);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
ae9b2f6859a3ec1865956c8c676c0fda
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.Scanner;
public class TaskA {
static Scanner scn = new Scanner(System.in);
public static void main(String[] argc)
{
int n, x, sum = 0;
int a, ans;
n = scn.nextInt();
x = scn.nextInt();
for (int i = 0; i < n; i++)
{
a = scn.nextInt();
sum += a;
}
sum = Math.abs(sum);
if (sum % x == 0)
ans = 0;
else ans = 1;
ans += (sum / x);
System.out.println(ans);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
88866d81083803e3edce78ec6c0fe647
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.Scanner;
/**
* Created by Oleksiy on 10.03.14.
*/
public class A {
public static void main(String[] args) {
int n,x;
int sum = 0;
Scanner sf = new Scanner(System.in);
n = sf.nextInt();
x = sf.nextInt();
for (int i=0; i<n; i++) {
sum += sf.nextInt();
}
int k;
sum = Math.abs(sum);
k = sum / x;
if (sum % x != 0) k++;
System.out.print(k);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
f93c2d96ee240d7186aa4d58378d0e41
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.InputMismatchException;
/**
*
* @author jigsaw
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
InputStream inputstream = System.in;
OutputStream outputstream = System.out;
InputReader in = new InputReader(inputstream);
OutputWriter outt = new OutputWriter(outputstream);
int n = in.readInt();
int x = in.readInt();
int sum = 0;
for(int i=0;i<n;i++)
sum += in.readInt();
if(sum<0)
sum = -sum;
int rem = sum/x;
if(sum%x!=0)
rem++;
if(sum==0)
outt.printLine(0);
else
outt.printLine(rem);
outt.close();
}
}
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 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();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class 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
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
b8efbdf8a56b06b55ce659008e6ae701
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
public class test {
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());
}
public long nextLong() {
return Long.parseLong(next());
}
}
static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
while(testNumber-->0) {
int n = in.nextInt();
int x = in.nextInt();
int sum=0;
for(int i=0;i<n;++i)
sum += in.nextInt();
sum = Math.abs(sum);
if(sum==0)
out.println("0");
else if(sum<=x)
out.println("1");
else if(sum%x==0)
out.println(sum/x);
else
out.println(sum/x + 1);
}
}
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
int test = 1;
solver.solve(test, in, out);
out.close();
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
ac442b3b3462c42bb369e7204e3521e0
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Robin Kumar (krobin_93)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReaderClass in = new InputReaderClass(inputStream);
OutputWriterClass out = new OutputWriterClass(outputStream);
VanyaAndCards solver = new VanyaAndCards();
solver.solve(1, in, out);
out.close();
}
}
class VanyaAndCards {
public void solve(int testNumber, InputReaderClass in, OutputWriterClass out) {
int n = in.readInt();
int m = in.readInt();
int sum = 0;
for (int i = 0; i < n; i++) sum += in.readInt();
if(sum==0) out.printLine(0);
else {
if(sum<0) sum *= -1;
if (sum % m == 0) out.printLine(sum / m);
else out.printLine(sum / m + 1);
}
}
}
class InputReaderClass {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReaderClass(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 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();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriterClass {
private final PrintWriter writer;
public OutputWriterClass(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
outputStream)));
}
public OutputWriterClass(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
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
e89f4f666abd7daabca3fe3eca8ea915
|
train_001.jsonl
|
1394465400
|
Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Task401A {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int sum = 0;
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
sum += Integer.parseInt(st.nextToken());
}
if (sum == 0) {
System.out.println(0);
return;
} else if (sum < 0) {
sum = -sum;
}
int c = 0;
while (sum > 0) {
sum -= x;
c++;
}
System.out.println(c);
}
}
|
Java
|
["3 2\n-1 1 2", "2 3\n-2 -2"]
|
1 second
|
["1", "2"]
|
NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
|
Java 7
|
standard input
|
[
"implementation",
"math"
] |
066906ee58af5163636dac9334619ea7
|
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
| 800 |
Print a single number — the answer to the problem.
|
standard output
| |
PASSED
|
5f39c3ee195591d3ad104899d4828d1c
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
import java.util.Scanner;
public class divisible {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int cases = in.nextInt();
in.nextLine();
for(int t = 0; t < cases ; t++)
{
int a = in.nextInt();
int b = in.nextInt();
int count = 0;
if(b > a)
{
count = b - a;
}
else if(b < a)
{
int x = a/b;
int d = a%b;
if(d != 0)
{
int y = b*(x+1);
count = y - a;
}
}
System.out.println(count);
}
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
d15c5fe7bc9588bed434aece499f61d9
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
import java.util.Scanner;
public class CFP1328A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i = 0; i < t; i++) {
int a = in.nextInt();
int b = in.nextInt();
int x = 0;
if(a % b != 0) {
int c = a / b;
x = (b * (c + 1)) - a;
}
System.out.println(x);
}
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
e9303005c2ac1fc81fc029b643e3a0a7
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner test = new Scanner(System.in);
int x = test.nextInt();
for(int y = 0; y < x;y++)
{
int a = test.nextInt();
int b = test.nextInt();
int jawaban = 0;
int c = 0;
if(a%b != 0) {
c = a / b + 1;
jawaban = b * c - a;
}
System.out.println(jawaban);
}
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
4cf547e293cc92751bb69e0b0e2599c0
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
import java.util.Scanner;
public class Divisibility {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int i = 0; i < t; i++) {
int a = s.nextInt();
int b = s.nextInt();
if(a % b == 0) System.out.println("0"); else
System.out.println(b - a % b);
}
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
e8db0d536e426c09cd557cfaaf7bce67
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Solution
{
public static int findMoves(double a, double b)
{
if (a <= b)
return (int)(b - a);
int i = (int)Math.ceil(a / b);
return (int)((b * i) - a);
}
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++)
{
double a = in.nextDouble();
double b = in.nextDouble();
// int a = (int)(Math.random() * 1e9) + 1;
// int b = (int)(Math.random() * 1e9) + 1;
//
// long start = System.currentTimeMillis();
System.out.println(findMoves(a, b));
// long end = System.currentTimeMillis();
//
// if ((end - start) >= 1000)
// {
// System.out.println("Time Limit Exceeded");
// System.out.println("a=" + a + " b=" + b);
// break;
// }
}
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
12073186bd3b93cb0b3819f6b8e2e645
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
import java.util.Scanner;
public class Solution{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int loop = s.nextInt();
for(int i = 0; i < loop; i++){
int a = s.nextInt();
int b = s.nextInt();
if(a%b == 0){
System.out.println(0);
continue;
}
System.out.println(b-(a%b));
}
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
7622a66a7bb44eaaf81b244fe68a1037
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long t = scanner.nextLong();
for (int i = 0; i < t; i++) {
long a = scanner.nextLong();
long b = scanner.nextLong();
System.out.println((b - (a % b)) % b);
}
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
2c447ba3c8a706f678e9e4e2acb35c96
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
import java.util.Scanner;
public class DivisibilityProblem1328A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//TODO optimalize execution time (<1000ms);
int n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n ; i++) {
String[] input = sc.nextLine().split(" ");
int a = Integer.parseInt(input[0]);
int b = Integer.parseInt(input[1]);
if (a % b == 0) {
System.out.println(0);
} else {
int x_mod = a % b;
if (x_mod != 0) {
a += b - x_mod;
}
System.out.println(b - x_mod);
}
}
sc.close();
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
b8b5f463e86f4a329259a7598b18db2b
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
import java.util.*;
public class Divisibility {
public static void main (String[] args) {
Scanner scan = new Scanner (System.in);
int cases = scan.nextInt();
for (int i = 0; i < cases; i++) {
int a = scan.nextInt();
int b = scan.nextInt();
int x = (int)Math.ceil((double)a/(double)b);
int result = x * b - a;
System.out.println(result);
}
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
b3528eccb048820445b7f2a345a9d6a7
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
// Working program with FastReader
import java.io.*;
import java.util.StringTokenizer;
public class Main {
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;
}
}
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());
}
public Long nextLong() {
return Long.parseLong(next());
}
}
static int divisibility(int a, int b) {
int left = a % b;
int z = b - left;
return z;
}
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
InputReader sc = new InputReader(inputStream);
int t = sc.nextInt();
for(int i = 0; i < t;i++) {
int a = sc.nextInt();
int b = sc.nextInt();
if (a % b == 0) {
System.out.println(0);
}
else{
System.out.println(divisibility(a,b));
}
}
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
59679fb6feec2aa4264fa63b05c62e58
|
train_001.jsonl
|
1585233300
|
You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.
|
256 megabytes
|
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the hourglassSum function below.
static int[] result(int[][] arr, int t) {
int[] result = new int[t];
for (int i = 0; i < arr.length; i++) {
int a = arr[i][0];
int b = arr[i][1];
if (a % b == 0) {
result[i] = 0;
} else {
result[i] = b - (a % b);
}
}
return result;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
int t = scanner.nextInt();
int[][] cases = new int[t][2];
for (int i = 0; i < t; i++) {
cases[i][0] = scanner.nextInt();
cases[i][1] = scanner.nextInt();
}
int[] result = result(cases, t);
for (int i = 0; i < result.length; i++) {
bufferedWriter.write(String.valueOf(result[i]));
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}
}
|
Java
|
["5\n10 4\n13 9\n100 13\n123 456\n92 46"]
|
1 second
|
["2\n5\n4\n333\n0"]
| null |
Java 11
|
standard input
|
[
"math"
] |
d9fd10700cb122b148202a664e7f7689
|
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).
| 800 |
For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.
|
standard output
| |
PASSED
|
dd757735cbbb0dd9cfc1d0e955a01097
|
train_001.jsonl
|
1570374300
|
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!You have $$$n$$$ tickets to sell. The price of the $$$i$$$-th ticket is $$$p_i$$$. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: The $$$x\%$$$ of the price of each the $$$a$$$-th sold ticket ($$$a$$$-th, $$$2a$$$-th, $$$3a$$$-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. The $$$y\%$$$ of the price of each the $$$b$$$-th sold ticket ($$$b$$$-th, $$$2b$$$-th, $$$3b$$$-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the $$$(x + y) \%$$$ are used for environmental activities. Also, it's known that all prices are multiples of $$$100$$$, so there is no need in any rounding.For example, if you'd like to sell tickets with prices $$$[400, 100, 300, 200]$$$ and the cinema pays $$$10\%$$$ of each $$$2$$$-nd sold ticket and $$$20\%$$$ of each $$$3$$$-rd sold ticket, then arranging them in order $$$[100, 200, 300, 400]$$$ will lead to contribution equal to $$$100 \cdot 0 + 200 \cdot 0.1 + 300 \cdot 0.2 + 400 \cdot 0.1 = 120$$$. But arranging them in order $$$[100, 300, 400, 200]$$$ will lead to $$$100 \cdot 0 + 300 \cdot 0.1 + 400 \cdot 0.2 + 200 \cdot 0.1 = 130$$$.Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least $$$k$$$ in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least $$$k$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.lang.*;
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);
Unstoppable solver = new Unstoppable();
int t=in.nextInt();
while(t-->0)
solver.solve(in, out);
out.close();
}
static class Unstoppable {
static long gcd(long a,long b){
if(b==0) return a;
else return gcd(b,a%b);
}
public void solve(InputReader in, PrintWriter out) {
int n=in.nextInt();
long p[]=new long[n];
for(int i=0;i<n;i++) p[i]=in.nextLong();
long x=in.nextLong();
long a=in.nextLong();
long y=in.nextLong();
long b=in.nextLong();
long k=in.nextLong();
long a1[]=new long[n+1];
long a2[]=new long[n+1];
long a3[]=new long[n+1];
long c=(a*b)/gcd(a,b);
if(x<y){
for(int i=0;i<n;i++){
a3[i]=(i+1)/c;
a1[i]=(i+1)/a-a3[i];
a2[i]=(i+1)/b-a3[i];
}
}
else{
for(int i=0;i<n;i++){
a3[i]=(i+1)/c;
a1[i]=(i+1)/b-a3[i];
a2[i]=(i+1)/a-a3[i];
}
}
long sum[]=new long[n+1];
long s=0; Arrays.sort(p);
for(int i=0;i<n;i++){
s+=p[i]; sum[i+1]=s;
}
int ans=-1;
// out.println(c+" "+Arrays.toString(a1)+" "+Arrays.toString(a2)+" "+Arrays.toString(a3));
for(int i=0;i<n;i++)
{
long t1=((sum[n]-sum[(int)(n-a3[i])])/100)*(x+y);
long t2=((sum[(int)(n-a3[i])]-sum[(int)(n-a3[i]-a2[i])])/100)*Math.max(x,y);
long t3=((sum[(int)(n-a3[i]-a2[i])]-sum[(int)(n-a3[i]-a2[i]-a1[i])])/100)*Math.min(x,y);
if(t1+t2+t3>=k){ ans=i+1; break; }
}
out.println(ans);
}
}
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 long nextLong(){
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
Java
|
["4\n1\n100\n50 1\n49 1\n100\n8\n100 200 100 200 100 200 100 100\n10 2\n15 3\n107\n3\n1000000000 1000000000 1000000000\n50 1\n50 1\n3000000000\n5\n200 100 100 100 100\n69 5\n31 2\n90"]
|
2 seconds
|
["-1\n6\n3\n4"]
|
NoteIn the first query the total contribution is equal to $$$50 + 49 = 99 < 100$$$, so it's impossible to gather enough money.In the second query you can rearrange tickets in a following way: $$$[100, 100, 200, 200, 100, 200, 100, 100]$$$ and the total contribution from the first $$$6$$$ tickets is equal to $$$100 \cdot 0 + 100 \cdot 0.1 + 200 \cdot 0.15 + 200 \cdot 0.1 + 100 \cdot 0 + 200 \cdot 0.25 = 10 + 30 + 20 + 50 = 110$$$.In the third query the full price of each ticket goes to the environmental activities.In the fourth query you can rearrange tickets as $$$[100, 200, 100, 100, 100]$$$ and the total contribution from the first $$$4$$$ tickets is $$$100 \cdot 0 + 200 \cdot 0.31 + 100 \cdot 0 + 100 \cdot 0.31 = 62 + 31 = 93$$$.
|
Java 11
|
standard input
|
[
"data structures",
"binary search",
"greedy"
] |
4835d79ad6055a7c9eb5d4566befeafc
|
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of independent queries. Each query consists of $$$5$$$ lines. The first line of each query contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of tickets. The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$100 \le p_i \le 10^9$$$, $$$p_i \bmod 100 = 0$$$) — the corresponding prices of tickets. The third line contains two integers $$$x$$$ and $$$a$$$ ($$$1 \le x \le 100$$$, $$$x + y \le 100$$$, $$$1 \le a \le n$$$) — the parameters of the first program. The fourth line contains two integers $$$y$$$ and $$$b$$$ ($$$1 \le y \le 100$$$, $$$x + y \le 100$$$, $$$1 \le b \le n$$$) — the parameters of the second program. The fifth line contains single integer $$$k$$$ ($$$1 \le k \le 10^{14}$$$) — the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed $$$2 \cdot 10^5$$$.
| 1,600 |
Print $$$q$$$ integers — one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least $$$k$$$ if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print $$$-1$$$.
|
standard output
| |
PASSED
|
fa19301a17c5ac0e4d452d98b1b15aaa
|
train_001.jsonl
|
1570374300
|
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!You have $$$n$$$ tickets to sell. The price of the $$$i$$$-th ticket is $$$p_i$$$. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: The $$$x\%$$$ of the price of each the $$$a$$$-th sold ticket ($$$a$$$-th, $$$2a$$$-th, $$$3a$$$-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. The $$$y\%$$$ of the price of each the $$$b$$$-th sold ticket ($$$b$$$-th, $$$2b$$$-th, $$$3b$$$-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the $$$(x + y) \%$$$ are used for environmental activities. Also, it's known that all prices are multiples of $$$100$$$, so there is no need in any rounding.For example, if you'd like to sell tickets with prices $$$[400, 100, 300, 200]$$$ and the cinema pays $$$10\%$$$ of each $$$2$$$-nd sold ticket and $$$20\%$$$ of each $$$3$$$-rd sold ticket, then arranging them in order $$$[100, 200, 300, 400]$$$ will lead to contribution equal to $$$100 \cdot 0 + 200 \cdot 0.1 + 300 \cdot 0.2 + 400 \cdot 0.1 = 120$$$. But arranging them in order $$$[100, 300, 400, 200]$$$ will lead to $$$100 \cdot 0 + 300 \cdot 0.1 + 400 \cdot 0.2 + 200 \cdot 0.1 = 130$$$.Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least $$$k$$$ in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least $$$k$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main
{
private void solve()throws IOException
{
int n=nextInt();
int p[]=new int[n+1];
for(int i=1;i<=n;i++)
p[i]=nextInt()/100;
Arrays.sort(p,1,n+1);
long sum[]=new long[n+1];
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+p[i];
int x=nextInt();
int a=nextInt();
int y=nextInt();
int b=nextInt();
if(x>y)
{
int temp=x;
x=y;
y=temp;
temp=a;
a=b;
b=temp;
}
long k=nextLong();
for(int i=1;i<=n;i++)
{
int mc=(int)(i/lcm(a,b));
int ma=i/a-mc;
int mb=i/b-mc;
long curr=(sum[n]-sum[n-mc])*(x+y);
curr+=(sum[n-mc]-sum[n-mc-mb])*y;
curr+=(sum[n-mc-mb]-sum[n-mc-mb-ma])*x;
if(curr>=k)
{
out.println(i);
return;
}
}
out.println(-1);
}
long lcm(int a,int b)
{
int gcd=gcd(a,b);
return (1l*a*b)/gcd;
}
int gcd(int a,int b){
if(a==0)
return b;
return gcd(b%a,a);
}
///////////////////////////////////////////////////////////
public void run()throws IOException
{
br=new BufferedReader(new InputStreamReader(System.in));
st=null;
out=new PrintWriter(System.out);
int t=nextInt();
while(t-->0)
solve();
br.close();
out.close();
}
public static void main(String args[])throws IOException{
new Main().run();
}
BufferedReader br;
StringTokenizer st;
PrintWriter out;
String nextToken()throws IOException{
while(st==null || !st.hasMoreTokens())
st=new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine()throws IOException{
return br.readLine();
}
int nextInt()throws IOException{
return Integer.parseInt(nextToken());
}
long nextLong()throws IOException{
return Long.parseLong(nextToken());
}
double nextDouble()throws IOException{
return Double.parseDouble(nextToken());
}
}
|
Java
|
["4\n1\n100\n50 1\n49 1\n100\n8\n100 200 100 200 100 200 100 100\n10 2\n15 3\n107\n3\n1000000000 1000000000 1000000000\n50 1\n50 1\n3000000000\n5\n200 100 100 100 100\n69 5\n31 2\n90"]
|
2 seconds
|
["-1\n6\n3\n4"]
|
NoteIn the first query the total contribution is equal to $$$50 + 49 = 99 < 100$$$, so it's impossible to gather enough money.In the second query you can rearrange tickets in a following way: $$$[100, 100, 200, 200, 100, 200, 100, 100]$$$ and the total contribution from the first $$$6$$$ tickets is equal to $$$100 \cdot 0 + 100 \cdot 0.1 + 200 \cdot 0.15 + 200 \cdot 0.1 + 100 \cdot 0 + 200 \cdot 0.25 = 10 + 30 + 20 + 50 = 110$$$.In the third query the full price of each ticket goes to the environmental activities.In the fourth query you can rearrange tickets as $$$[100, 200, 100, 100, 100]$$$ and the total contribution from the first $$$4$$$ tickets is $$$100 \cdot 0 + 200 \cdot 0.31 + 100 \cdot 0 + 100 \cdot 0.31 = 62 + 31 = 93$$$.
|
Java 11
|
standard input
|
[
"data structures",
"binary search",
"greedy"
] |
4835d79ad6055a7c9eb5d4566befeafc
|
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of independent queries. Each query consists of $$$5$$$ lines. The first line of each query contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of tickets. The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$100 \le p_i \le 10^9$$$, $$$p_i \bmod 100 = 0$$$) — the corresponding prices of tickets. The third line contains two integers $$$x$$$ and $$$a$$$ ($$$1 \le x \le 100$$$, $$$x + y \le 100$$$, $$$1 \le a \le n$$$) — the parameters of the first program. The fourth line contains two integers $$$y$$$ and $$$b$$$ ($$$1 \le y \le 100$$$, $$$x + y \le 100$$$, $$$1 \le b \le n$$$) — the parameters of the second program. The fifth line contains single integer $$$k$$$ ($$$1 \le k \le 10^{14}$$$) — the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed $$$2 \cdot 10^5$$$.
| 1,600 |
Print $$$q$$$ integers — one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least $$$k$$$ if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print $$$-1$$$.
|
standard output
| |
PASSED
|
f849854982bdffdc0a8b6e928b0506b4
|
train_001.jsonl
|
1570374300
|
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!You have $$$n$$$ tickets to sell. The price of the $$$i$$$-th ticket is $$$p_i$$$. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: The $$$x\%$$$ of the price of each the $$$a$$$-th sold ticket ($$$a$$$-th, $$$2a$$$-th, $$$3a$$$-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. The $$$y\%$$$ of the price of each the $$$b$$$-th sold ticket ($$$b$$$-th, $$$2b$$$-th, $$$3b$$$-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the $$$(x + y) \%$$$ are used for environmental activities. Also, it's known that all prices are multiples of $$$100$$$, so there is no need in any rounding.For example, if you'd like to sell tickets with prices $$$[400, 100, 300, 200]$$$ and the cinema pays $$$10\%$$$ of each $$$2$$$-nd sold ticket and $$$20\%$$$ of each $$$3$$$-rd sold ticket, then arranging them in order $$$[100, 200, 300, 400]$$$ will lead to contribution equal to $$$100 \cdot 0 + 200 \cdot 0.1 + 300 \cdot 0.2 + 400 \cdot 0.1 = 120$$$. But arranging them in order $$$[100, 300, 400, 200]$$$ will lead to $$$100 \cdot 0 + 300 \cdot 0.1 + 400 \cdot 0.2 + 200 \cdot 0.1 = 130$$$.Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least $$$k$$$ in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least $$$k$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class C591 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int q = sc.nextInt();
while (q-- > 0) {
int n = sc.nextInt();
Long [] p = new Long[n];
for (int i = 0; i < n; i++) p[i] = sc.nextLong();
int x = sc.nextInt(); int a = sc.nextInt();
int y = sc.nextInt(); int b = sc.nextInt();
long k = sc.nextLong();
//Arrays.sort(p, Comparator.reverseOrder());
int l = 1; int r = n + 1;
while (l < r) {
int mid = (l + r) / 2;
int both = 0; int first = 0; int second = 0;
for (int i = 1; i <= mid; i++) {
if (i % a == 0 && i % b == 0) both++;
else if (i % a == 0) first++;
else if (i % b == 0) second++;
}
ArrayList<Long> nums = new ArrayList<>();
for (int i = 0; i < n; i++) nums.add(p[i]);
Collections.sort(nums, Comparator.reverseOrder());
long res = 0;
for (int i = 0; i < both; i++) res += (nums.get(i) * (x + y)) / 100;
if (x > y) {
for (int i = both; i < both + first; i++) res += (nums.get(i) * x) / 100;
for (int i = both + first; i < both + first + second; i++) res += (nums.get(i) * y) / 100;
} else {
for (int i = both; i < both + second; i++) res += (nums.get(i) * y) / 100;
for (int i = both + second; i < both + first + second; i++) res += (nums.get(i) * x) / 100;
}
if (res < k) {
l = mid + 1;
} else {
r = mid;
}
}
out.println(l == n + 1 ? -1 : l);
}
out.close();
}
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["4\n1\n100\n50 1\n49 1\n100\n8\n100 200 100 200 100 200 100 100\n10 2\n15 3\n107\n3\n1000000000 1000000000 1000000000\n50 1\n50 1\n3000000000\n5\n200 100 100 100 100\n69 5\n31 2\n90"]
|
2 seconds
|
["-1\n6\n3\n4"]
|
NoteIn the first query the total contribution is equal to $$$50 + 49 = 99 < 100$$$, so it's impossible to gather enough money.In the second query you can rearrange tickets in a following way: $$$[100, 100, 200, 200, 100, 200, 100, 100]$$$ and the total contribution from the first $$$6$$$ tickets is equal to $$$100 \cdot 0 + 100 \cdot 0.1 + 200 \cdot 0.15 + 200 \cdot 0.1 + 100 \cdot 0 + 200 \cdot 0.25 = 10 + 30 + 20 + 50 = 110$$$.In the third query the full price of each ticket goes to the environmental activities.In the fourth query you can rearrange tickets as $$$[100, 200, 100, 100, 100]$$$ and the total contribution from the first $$$4$$$ tickets is $$$100 \cdot 0 + 200 \cdot 0.31 + 100 \cdot 0 + 100 \cdot 0.31 = 62 + 31 = 93$$$.
|
Java 11
|
standard input
|
[
"data structures",
"binary search",
"greedy"
] |
4835d79ad6055a7c9eb5d4566befeafc
|
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of independent queries. Each query consists of $$$5$$$ lines. The first line of each query contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of tickets. The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$100 \le p_i \le 10^9$$$, $$$p_i \bmod 100 = 0$$$) — the corresponding prices of tickets. The third line contains two integers $$$x$$$ and $$$a$$$ ($$$1 \le x \le 100$$$, $$$x + y \le 100$$$, $$$1 \le a \le n$$$) — the parameters of the first program. The fourth line contains two integers $$$y$$$ and $$$b$$$ ($$$1 \le y \le 100$$$, $$$x + y \le 100$$$, $$$1 \le b \le n$$$) — the parameters of the second program. The fifth line contains single integer $$$k$$$ ($$$1 \le k \le 10^{14}$$$) — the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed $$$2 \cdot 10^5$$$.
| 1,600 |
Print $$$q$$$ integers — one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least $$$k$$$ if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print $$$-1$$$.
|
standard output
| |
PASSED
|
e24cbbb79e2f04308f81703c2cd8e22b
|
train_001.jsonl
|
1570374300
|
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!You have $$$n$$$ tickets to sell. The price of the $$$i$$$-th ticket is $$$p_i$$$. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: The $$$x\%$$$ of the price of each the $$$a$$$-th sold ticket ($$$a$$$-th, $$$2a$$$-th, $$$3a$$$-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. The $$$y\%$$$ of the price of each the $$$b$$$-th sold ticket ($$$b$$$-th, $$$2b$$$-th, $$$3b$$$-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the $$$(x + y) \%$$$ are used for environmental activities. Also, it's known that all prices are multiples of $$$100$$$, so there is no need in any rounding.For example, if you'd like to sell tickets with prices $$$[400, 100, 300, 200]$$$ and the cinema pays $$$10\%$$$ of each $$$2$$$-nd sold ticket and $$$20\%$$$ of each $$$3$$$-rd sold ticket, then arranging them in order $$$[100, 200, 300, 400]$$$ will lead to contribution equal to $$$100 \cdot 0 + 200 \cdot 0.1 + 300 \cdot 0.2 + 400 \cdot 0.1 = 120$$$. But arranging them in order $$$[100, 300, 400, 200]$$$ will lead to $$$100 \cdot 0 + 300 \cdot 0.1 + 400 \cdot 0.2 + 200 \cdot 0.1 = 130$$$.Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least $$$k$$$ in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least $$$k$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class C591 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int q = sc.nextInt();
while (q-- > 0) {
int n = sc.nextInt();
Long [] p = new Long[n];
for (int i = 0; i < n; i++) p[i] = sc.nextLong();
int x = sc.nextInt(); int a = sc.nextInt();
int y = sc.nextInt(); int b = sc.nextInt();
long k = sc.nextLong();
//Arrays.sort(p, Comparator.reverseOrder());
int l = 1; int r = n + 1;
while (l < r) {
int mid = (l + r) / 2;
int both = 0; int first = 0; int second = 0;
for (int i = 1; i <= mid; i++) {
if (i % a == 0 && i % b == 0) both++;
else if (i % a == 0) first++;
else if (i % b == 0) second++;
}
ArrayList<Long> nums = new ArrayList<>();
for (int i = 0; i < n; i++) nums.add(p[i]);
Collections.sort(nums, Comparator.reverseOrder());
long res = 0;
for (int i = 0; i < both; i++) res += (nums.get(i) * (x + y)) / 100;
if (x > y) {
for (int i = both; i < both + first; i++) res += (nums.get(i) * x) / 100;
for (int i = both + first; i < both + first + second; i++) res += (nums.get(i) * y) / 100;
} else {
for (int i = both; i < both + second; i++) res += (nums.get(i) * y) / 100;
for (int i = both + second; i < both + first + second; i++) res += (nums.get(i) * x) / 100;
}
if (res < k) {
l = mid + 1;
} else {
r = mid;
}
}
out.println(l == n + 1 ? -1 : l);
}
out.close();
}
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["4\n1\n100\n50 1\n49 1\n100\n8\n100 200 100 200 100 200 100 100\n10 2\n15 3\n107\n3\n1000000000 1000000000 1000000000\n50 1\n50 1\n3000000000\n5\n200 100 100 100 100\n69 5\n31 2\n90"]
|
2 seconds
|
["-1\n6\n3\n4"]
|
NoteIn the first query the total contribution is equal to $$$50 + 49 = 99 < 100$$$, so it's impossible to gather enough money.In the second query you can rearrange tickets in a following way: $$$[100, 100, 200, 200, 100, 200, 100, 100]$$$ and the total contribution from the first $$$6$$$ tickets is equal to $$$100 \cdot 0 + 100 \cdot 0.1 + 200 \cdot 0.15 + 200 \cdot 0.1 + 100 \cdot 0 + 200 \cdot 0.25 = 10 + 30 + 20 + 50 = 110$$$.In the third query the full price of each ticket goes to the environmental activities.In the fourth query you can rearrange tickets as $$$[100, 200, 100, 100, 100]$$$ and the total contribution from the first $$$4$$$ tickets is $$$100 \cdot 0 + 200 \cdot 0.31 + 100 \cdot 0 + 100 \cdot 0.31 = 62 + 31 = 93$$$.
|
Java 11
|
standard input
|
[
"data structures",
"binary search",
"greedy"
] |
4835d79ad6055a7c9eb5d4566befeafc
|
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of independent queries. Each query consists of $$$5$$$ lines. The first line of each query contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of tickets. The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$100 \le p_i \le 10^9$$$, $$$p_i \bmod 100 = 0$$$) — the corresponding prices of tickets. The third line contains two integers $$$x$$$ and $$$a$$$ ($$$1 \le x \le 100$$$, $$$x + y \le 100$$$, $$$1 \le a \le n$$$) — the parameters of the first program. The fourth line contains two integers $$$y$$$ and $$$b$$$ ($$$1 \le y \le 100$$$, $$$x + y \le 100$$$, $$$1 \le b \le n$$$) — the parameters of the second program. The fifth line contains single integer $$$k$$$ ($$$1 \le k \le 10^{14}$$$) — the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed $$$2 \cdot 10^5$$$.
| 1,600 |
Print $$$q$$$ integers — one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least $$$k$$$ if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print $$$-1$$$.
|
standard output
| |
PASSED
|
d3dea637ff6919f333493e048e929c4e
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.*;
public class JavaApplication11 {
public static long max = 1;
public static void main(String[] args) {
Scanner io = new Scanner(System.in);
int t = io.nextInt();
while(t-- != 0) {
int n = io.nextInt(), s = io.nextInt(), k = io.nextInt();
int minDiff = Integer.MAX_VALUE;
int[] closed = new int[k];
for(int i = 0; i < k; i++) closed[i] = io.nextInt();
Arrays.sort(closed);
for(int i = s; i <= Math.min(n, s+2000); i++) {
if(Arrays.binarySearch(closed, i) >= 0) continue;
minDiff = Math.min(minDiff, Math.abs(i-s));
break;
}
for(int i = s; i >= Math.max(1, s-2000); i--) {
if(Arrays.binarySearch(closed, i) >= 0) continue;
minDiff = Math.min(minDiff, Math.abs(i-s));
break;
}
System.out.println(minDiff);
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
479dc24978a9359fd2da0e555962579b
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class test {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int tests = Integer.parseInt(bufferedReader.readLine().trim());
for (int i =0; i < tests; i++) {
String[] temp = bufferedReader.readLine().split(" ");
int n = Integer.parseInt(temp[0]);
int s = Integer.parseInt(temp[1]);
int k = Integer.parseInt(temp[2]);
HashMap<Integer, Integer> map = new HashMap<>();
temp = bufferedReader.readLine().split(" ");
for (int j = 0; j < k; j++) {
map.put(Integer.parseInt(temp[j]), 1);
}
int min = n;
if (!map.containsKey(s)) {
min = 0;
}
else {
for (int kk = 1; kk < n+10; kk++) {
int down = s-kk;
int up = s+kk;
if (down > 0 && !map.containsKey(down)) {
min = kk;
break;
}
if (up > 0 && up <= n && !map.containsKey(up)) {
min = kk;
break;
}
}
}
/*
else {
for (int a = s; a <= n; a++) {
if (!map.containsKey(a)) {
if (min > a-s) {
min = a-s;
}
}
}
for (int a = s; a >= 1; a--) {
if (!map.containsKey(a)) {
if (min > s-a) {
min = s-a;
}
}
}
}*/
System.out.println(min);
}
bufferedReader.close();
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
3b9af5e8ef2be19103106123b22f3e53
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
String filename = "";//filename here, System.in/out if no file
FastScanner in;
PrintWriter out;
void solve() {
int t = in.nextInt();
int n = 0;
int s = 0;
int k = 0;
for (int i1 = 0; i1 < t; i1++) {
n = in.nextInt();
s = in.nextInt();
k = in.nextInt();
int a[] = new int[k];
for (int i = 0; i < k; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
int i = 0;
while (a[i] < s && i != k - 1) {
i++;
}
if (i == k - 1 && a[i] < s) {
out.println(0);
} else {
if (a[i] != s) {
out.println(0);
} else {
int min = i;
int max = i;
if (i == k - 1) {
if (a[k - 1] == n)
max = 2000000000;
else
max = a[k - 1] + 1;
} else
while (max < k - 1) {
max++;
if (a[max] - 1 != a[max - 1]) {
max = a[max - 1] + 1;
break;
}
if (max == k - 1) {
if (a[max] != n) {
max = a[max] + 1;
} else
max = 2000000000;
break;
}
}
if (i == 0) {
if (a[0] == 1)
min = -2000000000;
else
min = a[0] - 1;
} else
while (min > 0) {
min--;
if (a[min] + 1 != a[min + 1]) {
min = a[min + 1] - 1;
break;
}
if (min == 0) {
if (a[min] != 1) {
min = a[min] - 1;
} else
min = -2000000000;
break;
}
}
out.println(Math.min(s - min, max - s));
}
}
}
}
void run() throws IOException {
InputStream input = System.in;
OutputStream output = System.out;
try {
File f = new File("input.txt");
if (f.exists() && f.canRead()) {
input = new FileInputStream(f);
output = new FileOutputStream("output.txt");
}
} catch (IOException e) {
}
in = new FastScanner(input);
out = new PrintWriter(new BufferedOutputStream(output));
solve();
in.close();
out.close();
}
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
new Main().run();
}
class FastScanner implements Closeable {
private BufferedReader br;
private StringTokenizer tokenizer;
public FastScanner(InputStream stream) throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(stream));
}
public boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
String s = br.readLine();
if (s == null) {
return false;
}
tokenizer = new StringTokenizer(s);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.hasMoreTokens();
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
@Override
public void close() throws IOException {
br.close();
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
59f7196e072d320f337718f9dd634090
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.*;
import java.math.*;
import java.util.*;
/**
*
* @author Saju
*
*/
public class Main {
private static int dx[] = { 1, 0, -1, 0};
private static int dy[] = { 0, -1, 0, 1};
private static final long INF = (long) (1e15);
private static final int INT_INF = Integer.MAX_VALUE;
private static final long NEG_INF = Long.MIN_VALUE;
private static final int NEG_INT_INF = Integer.MIN_VALUE;
private static final double EPSILON = 1e-10;
private static final int MAX = 1007;
private static final long MOD = 1000000007;
private static final int MAXN = 100007;
private static final int MAXA = 10000009;
private static final int MAXLOG = 22;
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
/*
*/
int test = in.nextInt();
for (int t = 1; t <= test; t++) {
int n = in.nextInt();
int s = in.nextInt();
int k = in.nextInt();
int[] arr = new int[k];
for(int i = 0; i < k; i++) {
arr[i] = in.nextInt();
}
Arrays.sort(arr);
int index = Arrays.binarySearch(arr, s);
if(index < 0) {
out.println(0);
continue;
}
int i = index;
while(i > 0 && (arr[i] - arr[i - 1] == 1)) {
i--;
}
int first = arr[i] - 1;
i = index;
while(i + 1 < k && (arr[i + 1] - arr[i] == 1)) {
i++;
}
int last = arr[i] + 1;
if(first == 0) {
first = INT_INF;
}
if(last == n + 1) {
last = INT_INF;
}
int min = min(abs(first - s), abs(last - s));
out.println(min);
}
out.flush();
out.close();
System.exit(0);
}
private static class Graph{
private int node;
private List<Edge>[] adj;
Graph(int node){
this.node = node;
adj = new ArrayList[this.node];
for(int i = 0; i < node; i++) {
adj[i] = new ArrayList<Main.Edge>();
}
}
void addEdge(int u, int v, long cost) {
Edge e = new Edge(u, v, cost);
Edge e1 = new Edge(v, u, cost);
adj[u].add(e);
adj[v].add(e1);
}
long[] calculateShortestPath(int source) {
long dist[] = new long[node];
PriorityQueue<Node> queue = new PriorityQueue<Main.Node>(new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
int val = Long.compare(o1.distance, o2.distance);
return val;
}
});
Arrays.fill(dist, INT_INF);
queue.add(new Node(source, 0));
dist[source] = 0;
while(!queue.isEmpty()) {
Node nn = queue.remove();
int u = nn.node;
long udist = nn.distance;
if(udist > dist[u]) {
continue;
}
for(Edge e : adj[u]) {
int v = e.v;
long vdist = e.cost;
long arrive = udist + vdist;
if(arrive < dist[v]) {
dist[v] = arrive;
queue.add(new Node(v, dist[v]));
// System.out.println(u + " " + v + " " + vdist + " " + dist[v]);
}
}
}
return dist;
}
}
private static class Node{
int operation;
int node;
long distance;
Node(int operation, int node, long distance){
this.operation = operation;
this.node = node;
this.distance = distance;
}
Node(int node, long distance){
this.node = node;
this.distance = distance;
}
}
private static class Edge{
int u;
int v;
long cost;
Edge(int u, int v, long cost){
this.u = u;
this.v = v;
this.cost = cost;
}
int getNeighbourIndex(int node) {
if(this.u == node) {
return v;
}
return u;
}
}
private static boolean isPalindrome(String str) {
StringBuilder sb = new StringBuilder();
sb.append(str);
String str1 = sb.reverse().toString();
return str.equals(str1);
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder();
sb.append(str);
return sb.reverse().toString();
}
private static double distance(Point p1, Point p2) {
long divx = p2.x - p1.x;
long divy = p2.y - p1.y;
divx = divx * divx;
divy = divy * divy;
return Math.sqrt(divx + divy);
}
private static String getBinaryStr(int n, int j) {
String str = Integer.toBinaryString(n);
int k = str.length();
for(int i = 1; i <= j - k; i++) {
str = "0" + str;
}
return str;
}
// O(log(max(A,M)))
static long modInverse(long a, long m) {
extendedEuclid(a, m);
return (x % m + m) % m;
}
static long x;
static long y;
static long gcdx;
private static void extendedEuclid(long a, long b) {
if (b == 0) {
gcdx = a;
x = 1;
y = 0;
} else {
extendedEuclid(b, a % b);
long temp = x;
x = y;
y = temp - ((a / b) * y);
}
}
private static void generatePrime(int n) {
//O(NloglogN)
boolean arr[] = new boolean[n + 5];
Arrays.fill(arr, true);
for(int i = 2; i * i <=n; i++){
if(arr[i] == true){
for(int j = i * i; j <= n; j+=i){
arr[j] = false;
}
}
}
int count = 0;
int start = 0;
for(int i = 2; i <= n; i++){
if(arr[i] == true){
// System.out.println(i + " ");
count++;
}
if(count == (start * 100) + 1) {
// System.out.println(i);
start++;
}
}
System.out.println();
System.out.println(count);
}
private static Map<Long, Long> primeFactorization(long n, long m){
Map<Long, Long> map = new HashMap<>();
for(long i = 2; i <= Math.sqrt(n); i++){
if(n % i == 0){
long count = 0;
while(n % i == 0){
count++;
n = n / i;
}
long val = count * m;
map.put(i, val);
// System.out.println("i: " + i + ", count: " + count);
}
}
if(n != 1) {
// System.out.println(n);
map.put(n, m);
}
return map;
}
private static class Pair<T>{
T a;
T b;
Pair(T a, T b){
this.a = a;
this.b = b;
}
}
private static class SegmentTree{
int n;
private final int MAXN = 100007;
private long[] tree = new long[MAXN << 2];
private long[] lazy = new long[MAXN << 2];
private long[] arr = new long[MAXN];
/***
*
* arr is 1 based index.
*
* @param arr
*
*/
SegmentTree(int n, long[] arr){
this.n = n;
for(int i = 0; i < arr.length; i++) {
this.arr[i] = arr[i];
}
}
void build(int index, int left, int right) {
if(left == right) {
tree[index] = arr[left];
}
else {
int mid = (left + right) / 2;
build(index * 2, left, mid);
build((index * 2) + 1, mid + 1, right);
tree[index] = max(tree[(index * 2)], tree[(index * 2) + 1]);
}
}
long query(int node, int left, int right, int start, int end) {
if(left > end || right < start) {
return NEG_INF;
}
if(left >= start && right <= end) {
return tree[node];
}
int mid = (left + right) / 2;
long val1 = query(2 * node, left, mid, start, end);
long val2 = query(2 * node + 1, mid + 1, right, start, end);
return max(val1, val2);
}
void update(int node, int left, int right, int idx, long val) {
if(left == right) {
tree[node] += val;
}
else {
int mid = (left + right) / 2;
if(idx <= mid) {
update(2 * node, left, mid, idx, val);
}
else {
update(2 * node + 1, mid + 1, right, idx, val);
}
tree[node] = max(tree[(2 * node) + 1], tree[(2 * node)]);
}
}
void updateRange(int node, int start, int end, int l, int r, long val)
{
if(lazy[node] != 0)
{
// This node needs to be updated
tree[node] = lazy[node]; // Update it
if(start != end)
{
lazy[node*2] = lazy[node]; // Mark child as lazy
lazy[node*2+1] = lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start > end || start > r || end < l) // Current segment is not within range [l, r]
return;
if(start >= l && end <= r)
{
// Segment is fully within range
tree[node] = val;
if(start != end)
{
// Not leaf node
lazy[node*2] = val;
lazy[node*2+1] = val;
}
return;
}
int mid = (start + end) / 2;
updateRange(node*2, start, mid, l, r, val); // Updating left child
updateRange(node*2 + 1, mid + 1, end, l, r, val); // Updating right child
tree[node] = max(tree[node*2], tree[node*2+1]); // Updating root with max value
}
long queryRange(int node, int start, int end, int l, int r)
{
if(start > end || start > r || end < l)
return 0; // Out of range
if(lazy[node] != 0)
{
// This node needs to be updated
tree[node] = lazy[node]; // Update it
if(start != end)
{
lazy[node*2] = lazy[node]; // Mark child as lazy
lazy[node*2+1] = lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start >= l && end <= r) // Current segment is totally within range [l, r]
return tree[node];
int mid = (start + end) / 2;
long p1 = queryRange(node*2, start, mid, l, r); // Query left child
long p2 = queryRange(node*2 + 1, mid + 1, end, l, r); // Query right child
return max(p1, p2);
}
void buildRange(int node, int low, int high){
if(low == high){
tree[node] = arr[low];
return;
}
int mid = (low + high) / 2;
int left = node << 1;
int right = left | 1;
buildRange(left, low, mid);
buildRange(right, mid + 1, high);
tree[node] = max(tree[left], tree[right]);
}
void printSegmentTree() {
System.out.println(Arrays.toString(tree));
}
}
private static class KMP{
private static char[] t;
private static char[] s;
public int kmp(char[]t, char[]s) {
this.t = t;
this.s = s;
return this.kmp();
}
private int kmp() {
List<Integer> prefixTable = getPrefixTable(s);
int match = 0;
int i = 0;
int j = 0;
int n = t.length;
int m = s.length;
while(i < n) {
if(t[i] == s[j]) {
if(j == m - 1) {
match++;
j = prefixTable.get(j - 1);
continue;
}
i++;
j++;
}
else if(j > 0) {
j = prefixTable.get(j - 1);
}
else {
i++;
}
}
return match;
}
/***
1. We compute the prefix values π[i] in a loop by iterating <br/>
from i=1 to i=n−1 (π[0] just gets assigned with 0). <br/><br/>
2. To calculate the current value π[i] we set the variable j <br/>
denoting the length of the best suffix for i−1. Initially j=π[i−1]. <br/>
3. Test if the suffix of length j+1 is also a prefix by <br/><br/>
comparing s[j] and s[i]. If they are equal then we assign π[i]=j+1, <br/>
otherwise we reduce j to π[j−1] and repeat this step. <br/><br/>
4. If we have reached the length j=0 and still don't have a match, <br/>
then we assign π[i]=0 and go to the next index i+1. <br/><br/>
@param
pattern(String)
***/
private List<Integer> getPrefixTable(char[] pattern){
List<Integer> prefixTable = new ArrayList<Integer>();
int n = pattern.length;
for(int i = 0; i < n; i++) {
prefixTable.add(0);
}
for(int i = 1; i < n; i++) {
for(int j = prefixTable.get(i - 1); j >= 0;) {
if(pattern[j] == pattern[i]) {
prefixTable.set(i, j + 1);
break;
}
else if(j > 0){
j = prefixTable.get(j - 1);
}
else {
break;
}
}
}
return prefixTable;
}
}
private static class Point {
long x;
long y;
Point(long x, long y){
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
Point ob = (Point) obj;
if(this.x == ob.x && this.y == ob.y){
return true;
}
return false;
}
@Override
public String toString() {
return this.x + " " + this.y;
}
@Override
public int hashCode() {
return 0;
}
}
private static long pow(int base, int pow) {
long val = 1L;
for(int i = 1; i <= pow; i++) {
val *= base;
}
return val;
}
private static int log(int x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static int log(long x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static long max(long a, long b) {
if (a >= b) {
return a;
}
return b;
}
private static long abs(long a) {
if (a < 0) {
return -a;
}
return a;
}
private static int abs(int a) {
if (a < 0) {
return -a;
}
return a;
}
private static int max(int a, int b) {
if (a >= b) {
return a;
}
return b;
}
private static int min(int a, int b) {
if (a <= b) {
return a;
}
return b;
}
private static long min(long a, long b) {
if (a <= b) {
return a;
}
return b;
}
private static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static long bigMod(long n, long k, long m) {
long ans = 1;
while (k > 0) {
if ((k & 1) == 1) {
ans = (ans * n) % m;
}
n = (n * n) % m;
k >>= 1;
}
return ans;
}
/*
* Returns an iterator pointing to the first element in the range [first,
* last] which does not compare less than val.
*
*/
private static int lowerBoundNew(long[] arr, long num) {
int start = 0;
int end = arr.length - 1;
int index = 0;
int len = arr.length;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
if (arr[mid] > num) {
end = mid - 1;
} else if (arr[mid] < num) {
start = mid + 1;
} else {
while (mid >= 0 && arr[mid] == num) {
mid--;
}
return mid + 1;
}
}
if (arr[mid] < num) {
return mid + 1;
}
return mid;
}
/*
* upper_bound() is a standard library function in C++ defined in the header
* . It returns an iterator pointing to the first element in the range
* [first, last) that is greater than value, or last if no such element is
* found
*
*/
private static int upperBoundNew(long arr[], long num) {
int start = 0;
int end = arr.length - 1;
int index = 0;
int len = arr.length;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
long val = arr[mid];
if (val > num) {
end = mid - 1;
} else if (val < num) {
start = mid + 1;
} else {
while (mid < len && arr[mid] == num) {
mid++;
}
if (mid == len - 1 && arr[mid] == num) {
return mid + 1;
} else {
return mid;
}
}
}
if (arr[mid] < num) {
return mid + 1;
}
return mid;
}
private static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException e) {
return null;
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = null;
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public boolean hasNext() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
e842661c0ddaeff75f74b3284a12b0e8
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.*;
import java.math.*;
import java.util.*;
/**
*
* @author Saju
*
*/
public class Main {
private static int dx[] = { -2, -3, -2, -1, -1, 1};
private static int dy[] = { 1, -1, -1, -2, -3, -2};
private static final long INF = (long) (1e15);
private static final long NEG_INF = Long.MIN_VALUE;
private static final double EPSILON = 1e-10;
private static final int MAX = 1007;
private static final int MOD = 10007;
private static final int MAXN = 100007;
private static final int MAXA = 10000009;
private static final int MAXLOG = 22;
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
/*
*/
int test = in.nextInt();
while(test-- > 0) {
int n = in.nextInt();
int s = in.nextInt();
int k = in.nextInt();
List<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < k; i++) {
int a = in.nextInt();
list.add(a);
}
Integer arr[] = list.toArray(new Integer[list.size()]);
Arrays.sort(arr);
int index = Arrays.binarySearch(arr, s);
// System.out.println(index);
if(index < 0) {
out.println(0);
continue;
}
int first = 0;
int last = 0;
int i = index;
while(index > 0 && (arr[index] - arr[index - 1]) == 1) {
index--;
}
first = index;
index = i;
while(index + 1 < k && (arr[index + 1] - arr[index]) == 1) {
index++;
}
last = index;
// System.out.println(first + " " + last);
int el1 = 0;
int el2 = 0;
el1 = arr[first] - 1;
if(el1 == 0) {
el1 = Integer.MAX_VALUE;
}
el2 = arr[last] + 1;
if(el2 == n + 1) {
el2 = Integer.MAX_VALUE;
}
// System.out.println(el1 + " " + el2);
int ans = min(abs(s - el1), abs(el2 - s));
out.println(ans);
}
out.flush();
out.close();
System.exit(0);
}
private static class Pair<T>{
T a;
T b;
Pair(T a, T b){
this.a = a;
this.b = b;
}
}
private static class SegmentTree{
int n;
private final int MAXN = 100007;
private long[] tree = new long[MAXN << 2];
private long[] lazy = new long[MAXN << 2];
private long[] arr = new long[MAXN];
/***
*
* arr is 1 based index.
*
* @param arr
*
*/
SegmentTree(int n, long[] arr){
this.n = n;
for(int i = 0; i < arr.length; i++) {
this.arr[i] = arr[i];
}
}
void build(int index, int left, int right) {
if(left == right) {
tree[index] = arr[left];
}
else {
int mid = (left + right) / 2;
build(index * 2, left, mid);
build((index * 2) + 1, mid + 1, right);
tree[index] = max(tree[(index * 2)], tree[(index * 2) + 1]);
}
}
long query(int node, int left, int right, int start, int end) {
if(left > end || right < start) {
return NEG_INF;
}
if(left >= start && right <= end) {
return tree[node];
}
int mid = (left + right) / 2;
long val1 = query(2 * node, left, mid, start, end);
long val2 = query(2 * node + 1, mid + 1, right, start, end);
return max(val1, val2);
}
void update(int node, int left, int right, int idx, long val) {
if(left == right) {
tree[node] += val;
}
else {
int mid = (left + right) / 2;
if(idx <= mid) {
update(2 * node, left, mid, idx, val);
}
else {
update(2 * node + 1, mid + 1, right, idx, val);
}
tree[node] = max(tree[(2 * node) + 1], tree[(2 * node)]);
}
}
void updateRange(int node, int start, int end, int l, int r, long val)
{
if(lazy[node] != 0)
{
// This node needs to be updated
tree[node] = lazy[node]; // Update it
if(start != end)
{
lazy[node*2] = lazy[node]; // Mark child as lazy
lazy[node*2+1] = lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start > end || start > r || end < l) // Current segment is not within range [l, r]
return;
if(start >= l && end <= r)
{
// Segment is fully within range
tree[node] = val;
if(start != end)
{
// Not leaf node
lazy[node*2] = val;
lazy[node*2+1] = val;
}
return;
}
int mid = (start + end) / 2;
updateRange(node*2, start, mid, l, r, val); // Updating left child
updateRange(node*2 + 1, mid + 1, end, l, r, val); // Updating right child
tree[node] = max(tree[node*2], tree[node*2+1]); // Updating root with max value
}
long queryRange(int node, int start, int end, int l, int r)
{
if(start > end || start > r || end < l)
return 0; // Out of range
if(lazy[node] != 0)
{
// This node needs to be updated
tree[node] = lazy[node]; // Update it
if(start != end)
{
lazy[node*2] = lazy[node]; // Mark child as lazy
lazy[node*2+1] = lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start >= l && end <= r) // Current segment is totally within range [l, r]
return tree[node];
int mid = (start + end) / 2;
long p1 = queryRange(node*2, start, mid, l, r); // Query left child
long p2 = queryRange(node*2 + 1, mid + 1, end, l, r); // Query right child
return max(p1, p2);
}
void buildRange(int node, int low, int high){
if(low == high){
tree[node] = arr[low];
return;
}
int mid = (low + high) / 2;
int left = node << 1;
int right = left | 1;
buildRange(left, low, mid);
buildRange(right, mid + 1, high);
tree[node] = max(tree[left], tree[right]);
}
void printSegmentTree() {
System.out.println(Arrays.toString(tree));
}
}
private static class KMP{
private static char[] t;
private static char[] s;
public int kmp(char[]t, char[]s) {
this.t = t;
this.s = s;
return this.kmp();
}
private int kmp() {
List<Integer> prefixTable = getPrefixTable(s);
int match = 0;
int i = 0;
int j = 0;
int n = t.length;
int m = s.length;
while(i < n) {
if(t[i] == s[j]) {
if(j == m - 1) {
match++;
j = prefixTable.get(j - 1);
continue;
}
i++;
j++;
}
else if(j > 0) {
j = prefixTable.get(j - 1);
}
else {
i++;
}
}
return match;
}
/***
1. We compute the prefix values π[i] in a loop by iterating <br/>
from i=1 to i=n−1 (π[0] just gets assigned with 0). <br/><br/>
2. To calculate the current value π[i] we set the variable j <br/>
denoting the length of the best suffix for i−1. Initially j=π[i−1]. <br/>
3. Test if the suffix of length j+1 is also a prefix by <br/><br/>
comparing s[j] and s[i]. If they are equal then we assign π[i]=j+1, <br/>
otherwise we reduce j to π[j−1] and repeat this step. <br/><br/>
4. If we have reached the length j=0 and still don't have a match, <br/>
then we assign π[i]=0 and go to the next index i+1. <br/><br/>
@param
pattern(String)
***/
private List<Integer> getPrefixTable(char[] pattern){
List<Integer> prefixTable = new ArrayList<Integer>();
int n = pattern.length;
for(int i = 0; i < n; i++) {
prefixTable.add(0);
}
for(int i = 1; i < n; i++) {
for(int j = prefixTable.get(i - 1); j >= 0;) {
if(pattern[j] == pattern[i]) {
prefixTable.set(i, j + 1);
break;
}
else if(j > 0){
j = prefixTable.get(j - 1);
}
else {
break;
}
}
}
return prefixTable;
}
}
private static class Point {
long x;
long y;
Point(long x, long y){
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
Point ob = (Point) obj;
if(this.x == ob.x && this.y == ob.y){
return true;
}
return false;
}
@Override
public String toString() {
return this.x + " " + this.y;
}
}
private static int pow(int base, int pow) {
int val = 1;
for(int i = 1; i <= pow; i++) {
val *= base;
}
return val;
}
private static int log(int x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static int log(long x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static long max(long a, long b) {
if (a >= b) {
return a;
}
return b;
}
private static long abs(long a) {
if (a < 0) {
return -a;
}
return a;
}
private static int abs(int a) {
if (a < 0) {
return -a;
}
return a;
}
private static int max(int a, int b) {
if (a >= b) {
return a;
}
return b;
}
private static int min(int a, int b) {
if (a <= b) {
return a;
}
return b;
}
private static long min(long a, long b) {
if (a <= b) {
return a;
}
return b;
}
private static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static long bigMod(long n, long k, long m) {
long ans = 1;
while (k > 0) {
if ((k & 1) == 1) {
ans = (ans * n) % m;
}
n = (n * n) % m;
k >>= 1;
}
return ans;
}
/*
* Returns an iterator pointing to the first element in the range [first,
* last] which does not compare less than val.
*
*/
private static int lowerBoundNew(long[] arr, long num) {
int start = 0;
int end = arr.length - 1;
int index = 0;
int len = arr.length;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
if (arr[mid] > num) {
end = mid - 1;
} else if (arr[mid] < num) {
start = mid + 1;
} else {
while (mid >= 0 && arr[mid] == num) {
mid--;
}
return mid + 1;
}
}
if (arr[mid] < num) {
return mid + 1;
}
return mid;
}
/*
* upper_bound() is a standard library function in C++ defined in the header
* . It returns an iterator pointing to the first element in the range
* [first, last) that is greater than value, or last if no such element is
* found
*
*/
private static int upperBoundNew(long[] arr, long num) {
int start = 0;
int end = arr.length - 1;
int index = 0;
int len = arr.length;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
if (arr[mid] > num) {
end = mid - 1;
} else if (arr[mid] < num) {
start = mid + 1;
} else {
while (mid < len && arr[mid] == num) {
mid++;
}
if (mid == len - 1 && arr[mid] == num) {
return mid + 1;
} else {
return mid;
}
}
}
if (arr[mid] < num) {
return mid + 1;
}
return mid;
}
private static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException e) {
return null;
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = null;
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public boolean hasNext() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
83ff9b5dea1dd8bbc53e18474379efff
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner (System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++) {
int n=sc.nextInt();
int s=sc.nextInt();
int k=sc.nextInt();
int []a=new int [k+5];
for(int j=1;j<=k;j++) {
a[j]=sc.nextInt();
}
int cout=0;
for(int j=0;j<=n;j++) {
if(s+j<=n) {
int f=0;
for(int g=1;g<=k;g++) {
if(a[g]==(s+j))
f=1;
}
if(f==0)
break;
}
if(s-j>0) {
int f=0;
for(int g=1;g<=k;g++) {
if(a[g]==(s-j))
f=1;
}
if(f==0)
break;
}
cout++;
}
System.out.println(cout);
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
dd1f3247d9f8e1eb9b2fa18fc790e31d
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.*;
public class A1293 {
public static int runn(int a,int b, int[] mang, int k, int n) {
int u=0;
int r=0;
for(int i=0; i<k; i++) {
if((a==mang[i] && a<=n)|| a>n) {
u=1;
}
if((b==mang[i]&& b>0)|| b<=0) {
r=1;
}
if((u==1 && r==1)) {
return 0;
}
}
return 1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t;
t=sc.nextInt();
while(t>0) {
int n, s, k, a;
n=sc.nextInt();
s=sc.nextInt();
k=sc.nextInt();
int[] mangk=new int [1000];
for(int i=0; i<k; i++) {
mangk[i]=sc.nextInt();
}
a=-1;
int ok=0;
while(ok==0) {
a++;
ok=runn(a+s,s-a, mangk, k, n);
}
System.out.println(a);
t--;
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
b6e0066e9ebde0e70433bcbaa1ec7c1e
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
static int ar[];
static int di[];
static LinkedList<Integer> al[];
static int ll[];
static boolean p[];
static int v;
static long mod=1000000007l;
static HashSet<Integer> h=new HashSet<>();
static int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;
static long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE;
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int max(int a,int b){return Math.max(a,b);}
static int min(int a,int b){return Math.min(a,b);}
static int abs(int a){return Math.abs(a);}
static long max(long a,long b){return Math.max(a,b);}
static long min(long a,long b){return Math.min(a,b);}
static long abs(long a){return Math.abs(a);}
static int sq(int a){return (int)Math.sqrt(a);}
static long sq(long a){return (long)Math.sqrt(a);}
static int ncr(int n,int c,long m)
{
long a=1l;
for(int x=n-c+1;x<=n;x++)a=((a*x)%m);
long b=1l;
for(int x=2;x<=c;x++)b=((b*x)%m);
long v=(div((int)b,m-2,m)%m);
return (int)((a*v)%m);
}
static int fib(int n)
{
double p=(1+Math.sqrt(5))/2;
return (int)Math.round(Math.pow(p,n)/Math.sqrt(5));
}
static boolean[] sieve(int n)
{
boolean bo[]=new boolean[n+1];
bo[0]=true;bo[1]=true;
for(int x=4;x<=n;x+=2)bo[x]=true;
for(int x=3;x*x<=n;x+=2)
{
if(!bo[x])
{
for(int y=x*x;y<=n;y+=x)bo[y]=true;
}
}
return bo;
}
static int[] fac(int n)
{
int bo[]=new int[n+1];
bo[0]=0;bo[1]=1;
for(int x=1;x<=n;x++)
{
for(int y=x;y<=n;y+=x)bo[y]++;
}
return bo;
}
static int gcd(int a,int b)
{
if(b==0)return a;
return gcd(b,a%b);
}
static long div(long a,long b,long m)
{
long r=1l;
a%=m;
while(b>0)
{
if((b&1)==1)r=(r*a)%m;
b>>=1;
a=(a*a)%m;
}
return r;
}
static int i()throws IOException
{
if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
static long l()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Long.parseLong(st.nextToken());
}
static String s()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return st.nextToken();
}
static double d()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Double.parseDouble(st.nextToken());
}
static char c()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return st.nextToken().charAt(0);
}
static boolean b()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Boolean.parseBoolean(st.nextToken());
}
static void p(Object p){System.out.print(p);}
static void p(String p){System.out.print(p);}
static void p(int p){System.out.print(p);}
static void p(double p){System.out.print(p);}
static void p(long p){System.out.print(p);}
static void p(char p){System.out.print(p);}
static void p(boolean p){System.out.print(p);}
static void pl(Object p){System.out.println(p);}
static void pl(String p){System.out.println(p);}
static void pl(int p){System.out.println(p);}
static void pl(char p){System.out.println(p);}
static void pl(double p){System.out.println(p);}
static void pl(long p){System.out.println(p);}
static void pl(boolean p){System.out.println(p);}
static void pl(){System.out.println();}
static int[] ari(int n)throws IOException
{
int ar[]=new int[n];
st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++) ar[x]=Integer.parseInt(st.nextToken());
return ar;
}
static int[][] ari(int n,int m)throws IOException
{
int ar[][]=new int[n][m];
for(int x=0;x<n;x++)
{
st=new StringTokenizer(br.readLine());
for(int y=0;y<m;y++)ar[x][y]=Integer.parseInt(st.nextToken());
}
return ar;
}
static long[] arl(int n)throws IOException
{
long ar[]=new long[n];
st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++) ar[x]=Long.parseLong(st.nextToken());
return ar;
}
static long[][] arl(int n,int m)throws IOException
{
long ar[][]=new long[n][m];
for(int x=0;x<n;x++)
{
st=new StringTokenizer(br.readLine());
for(int y=0;y<m;y++)ar[x][y]=Long.parseLong(st.nextToken());
}
return ar;
}
static String[] ars(int n)throws IOException
{
String ar[]=new String[n];
st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++) ar[x]=st.nextToken();
return ar;
}
static double[] ard(int n)throws IOException
{
double ar[]=new double[n];
st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++)ar[x]=Double.parseDouble(st.nextToken());
return ar;
}
static double[][] ard(int n,int m)throws IOException
{
double ar[][]=new double[n][m];
for(int x=0;x<n;x++)
{
st=new StringTokenizer(br.readLine());
for(int y=0;y<m;y++)ar[x][y]=Double.parseDouble(st.nextToken());
}
return ar;
}
static char[] arc(int n)throws IOException
{
char ar[]=new char[n];
st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++)ar[x]=st.nextToken().charAt(0);
return ar;
}
static char[][] arc(int n,int m)throws IOException
{
char ar[][]=new char[n][m];
for(int x=0;x<n;x++)
{
String s=br.readLine();
for(int y=0;y<m;y++)ar[x][y]=s.charAt(y);
}
return ar;
}
static void p(int ar[])
{
StringBuilder sb=new StringBuilder("");;
for(int a:ar) sb.append(a+" ");
System.out.println(sb);
}
static void p(int ar[][])
{
StringBuilder sb;
for(int a[]:ar)
{
sb=new StringBuilder("");
for(int aa:a) sb.append(aa+" ");
System.out.println(sb);
}
}
static void p(long ar[])
{
StringBuilder sb=new StringBuilder("");
for(long a:ar)sb.append(a+" ");
System.out.println(sb);
}
static void p(long ar[][])
{
StringBuilder sb;
for(long a[]:ar)
{
sb=new StringBuilder("");
for(long aa:a)sb.append(aa+" ");
System.out.println(sb);
}
}
static void p(String ar[])
{
StringBuilder sb=new StringBuilder("");
for(String a:ar)sb.append(a+" ");
System.out.println(sb);
}
static void p(double ar[])
{
StringBuilder sb=new StringBuilder("");
for(double a:ar)sb.append(a+" ");
System.out.println(sb);
}
static void p(double ar[][])
{
StringBuilder sb;
for(double a[]:ar)
{
sb=new StringBuilder("");
for(double aa:a)sb.append(aa+" ");
System.out.println(sb);
}
}
static void p(char ar[])
{
StringBuilder sb=new StringBuilder("");
for(char aa:ar)sb.append(aa+" ");
System.out.println(sb);
}
static void p(char ar[][])
{
StringBuilder sb;
for(char a[]:ar)
{
sb=new StringBuilder("");
for(char aa:a)sb.append(aa+" ");
System.out.println(sb);
}
}
static public void main(String[] args)throws Exception
{
st=new StringTokenizer(br.readLine());
int t=i();
o:
while(t-->0)
{
int n=i();
int s=i();
int k=i();
TreeSet<Integer> ts=new TreeSet<>();
for(int x=0;x<k;x++)ts.add(i());
if(!ts.contains(s))pl(0);
else
{
int ar[]=new int[ts.size()];
int i=-1;
for(int e:ts)ar[++i]=e;
int in=Arrays.binarySearch(ar,s);
int m1=-1;
int m2=-1;
int v=s;
//pl(in);
for(int x=in;x>=0;x--)
{
if(ar[x]!=v)
{
m1=v;
break;
}
v--;
}
if(m1==-1)m1=ar[0]-1;
v=s;
for(int x=in;x<ar.length;x++)
{
if(ar[x]!=v)
{
m2=v;
break;
}
v++;
}
if(m2==-1)m2=ar[ar.length-1]+1;
if(m1==0)pl(m2-s);
else if(m2==n+1)pl(s-m1);
else
{
pl(min(m2-s,s-m1));
}
}
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
857fdcf2a11cb9892940a951b97bed70
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class cf8 {
static long mod = (long) Math.pow(10, 9) + 7;
static FastScanner f = new FastScanner(System.in);
static Scanner S = new Scanner(System.in);
public static void main(String[] args) {
int t = f.nextInt();
while(t-- > 0) {
int n = f.nextInt();
int s = f.nextInt();
int k = f.nextInt();
HashMap<Integer , Integer> hm = new HashMap<Integer , Integer>();
for(int i = 0; i < k; i++) {
int x = f.nextInt();
hm.put(x - 1 , 1);
}
int m1 = 0, flag1 = 0;
for(int i = s - 1; i < n; i++) {
if(!hm.containsKey(i)) {
flag1 = 1;
break;
}
else
m1++;
}
int m2 = 0 , flag2 = 0;
for(int i = s - 1; i >= 0; i--) {
if(!hm.containsKey(i)) {
flag2 = 1;
break;
}
else
m2++;
}
int res = Integer.MAX_VALUE;
if(flag1 == 1)
res = m1;
if(flag2 == 1)
res = Math.min(res , m2);
pn(res);
}
}
/******************************END OF MAIN PROGRAM*******************************************/
// Fast Scanner Alternative of Scanner
// Uses Implementation of BufferedReader Class
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new
InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
// Print in console
static void pn(Object o){System.out.println(o);}
static void p(Object o){System.out.print(o);}
static void pni(Object o){System.out.println(o);System.out.flush();}
//GCD of two integers
static int gcd(int a, int b) {
if(b == 0) return a;
else {
return gcd(b, a % b);
}
}
// Input int array
static int[] inpint(int n) {
int arr[] = new int[n];
for(int i = 0; i < n; i++)
arr[i] = f.nextInt();
return arr;
}
//Input long array
static long[] inplong(int n) {
long arr[] = new long[n];
for(int i = 0; i < n; i++)
arr[i] = f.nextLong();
return arr;
}
// GCD of a array of integers
static int gcdarray(int a[]) {
int res = a[0];
for(int i = 1; i < a.length; i++) {
res = gcd(a[i] , res);
}
return res;
}
// Return boolean sieve of first n prime nos.
static boolean[] sieve(int n) {
boolean isprime[] = new boolean[n + 1];
for(int i = 0; i <= n;++i) {
isprime[i] = true;
}
isprime[0] = false;
isprime[1] = false;
for(int i = 2; i * i <= n; ++i) {
if(isprime[i] == true) {
for(int j = i * i; j <= n;j += i)
isprime[j] = false;
}
}
return isprime;
}
// Return HashSet of factors of a number
static HashSet<Long> factors(long n) {
HashSet<Long> hs = new HashSet<Long>();
for(long i = 1; i <= (long)Math.sqrt(n); i++) {
if(n % i == 0) {
hs.add(i);
hs.add(n / i);
}
}
return hs;
}
//Is n prime ?
static boolean isPrime(int n) {
if(n == 1) return false;
int i = 2;
while((i * i) <= n) {
if(n % i == 0) return false;
i += 1;
}
return true;
}
// Gives next (Lexicographical) permutation of String
public static String nextPerm(String s) {
StringBuffer sb = new StringBuffer(s);
String ans = "No Successor";
int ii = -1 , jj = -1;
for(int i = 0; i < s.length() - 1; i++) {
if((int)s.charAt(i) < (int)s.charAt(i + 1))
ii = i;
}
for(int j = ii + 1; j < s.length() && j != 0; j++) {
if((int)s.charAt(j) > (int)s.charAt(ii))
jj = j;
}
if(ii == -1)
return ans;
else {
char tempi = s.charAt(ii);
char tempj = s.charAt(jj);
sb.setCharAt(jj , tempi);
sb.setCharAt(ii , tempj);
StringBuffer sub = new StringBuffer(sb.substring(ii + 1));
sub.reverse();
int v = sub.length();
while(v-- > 0) {
sb.deleteCharAt(sb.length() - 1);
}
sb.append(sub);
ans = sb.toString();
return ans;
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
64435f6b3af391c276c2aeb157d83b45
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CodeForcesMain {
private void code() throws IOException {
int times = readlineInt();
for (int t = 0; t < times; t++) {
readlineInts();
int n = intLinesTmp[0];
int s = intLinesTmp[1];
int k = intLinesTmp[2];
Set<Integer> cls = Arrays.stream(readlineInts()).boxed().collect(Collectors.toSet());
for (int i = 0; i < Integer.MAX_VALUE; i++) {
int up = s - i;
int down = s + i;
if (0 < up && up <= n && !cls.contains(up)){
System.out.println(i);
break;
}
if (0 < down && down <= n && !cls.contains(down)){
System.out.println(i);
break;
}
}
}
}
private BufferedReader bf;
public static void main(String[] args) throws IOException {
CodeForcesMain cfm = new CodeForcesMain();
boolean debug = false;
Reader reader = debug ? new FileReader("input.txt") : new InputStreamReader(System.in);
cfm.bf = new BufferedReader(reader);
cfm.code();
cfm.bf.close();
}
private int[] intLinesTmp;
private int[] readlineInts() throws IOException {
intLinesTmp = Stream.of(bf.readLine().split("\\s")).mapToInt(Integer::parseInt).toArray();
return intLinesTmp;
}
private int readlineInt() throws IOException {
return Integer.parseInt(bf.readLine());
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
a1a816ee4ee2d14a3ba9068ffebfe620
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CodeForcesMain {
private void code() throws IOException {
int times = readlineInt();
for (int t = 0; t < times; t++) {
readlineInts();
int n = intLinesTmp[0];
int s = intLinesTmp[1];
int k = intLinesTmp[2];
Set<Integer> cls = Arrays.stream(readlineInts()).boxed().collect(Collectors.toSet());
for (int i = 0; i < Integer.MAX_VALUE; i++) {
int up = s - i;
int down = s + i;
if (0 < up && up <= n && !cls.contains(up)){
System.out.println(i);
break;
}
if (0 < down && down <= n && !cls.contains(down)){
System.out.println(i);
break;
}
}
}
}
private BufferedReader bf;
public static void main(String[] args) throws IOException {
CodeForcesMain cfm = new CodeForcesMain();
boolean debug = args.length>0;
Reader reader = debug ? new FileReader("input.txt") : new InputStreamReader(System.in);
cfm.bf = new BufferedReader(reader);
cfm.code();
cfm.bf.close();
}
private int[] intLinesTmp;
private int[] readlineInts() throws IOException {
intLinesTmp = Stream.of(bf.readLine().split("\\s")).mapToInt(Integer::parseInt).toArray();
return intLinesTmp;
}
private int readlineInt() throws IOException {
return Integer.parseInt(bf.readLine());
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
1720d765d684acb934f065ff5c2178a2
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Q1 {
public static void main(String[] args) {
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
int t =in.nextInt();
while(t-->0) {
int n = in.nextInt(),s=in.nextInt(),k=in.nextInt();
HashSet<Integer> set=new HashSet<>();
for(int i=0;i<k;i++){
int temp=in.nextInt();
set.add(temp);
}
int ans=1100;
for(int i =s-1;i>0 ;i--){
if(!set.contains(i)){
ans=Math.min(ans,s-i);
break;
}
}
for(int i =s;i<=n;i++){
if(!set.contains(i)){
ans=Math.min(ans,i-s);
break;
}
}
out.println(ans);
}
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public int[] shuffle(int[] arr) {
Random r = new Random();
for (int i = 1, j; i < arr.length; i++) {
j = r.nextInt(i);
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
return arr;
}
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(System.in), 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 char nextChar() {
return next().charAt(0);
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
// public static void main(String[] args) {
//
// InputReader in = new InputReader();
// PrintWriter out = new PrintWriter(System.out);
// int t =in.nextInt();
//
// while(t-->0){
// int N=in.nextInt();
// int arr[]=new int[N];
// for(int i=0 ;i<N;i++)
// arr[i]=in.nextInt();
//
// ArrayList<Integer> set1=new ArrayList<>(),set2=new ArrayList<>(),set3=new ArrayList<>();
// double xval=in.nextInt(),x=in.nextInt(),yval=in.nextInt(),y=in.nextInt();
// long k=in.nextLong();
// long ans =0;
//
// if(xval<yval){
// double temp=xval;
// xval=yval;
// yval=temp;
//
// temp=x;
// x=y;
// y=temp;
// }
// xval=xval/100; yval=yval/100;
//
// for(int i =0;i<N;i++){
// if((i+1)%x==0 && (i+1)%y==0)
// set1.add(i);
// else if ((i+1)%x==0 )
// set2.add(i);
// else if((i+1)%y==0)
// set3.add(i);
// }
// arr=in.shuffle(arr);
//
// int a =0,b=0,c=0,add=0;
//
// for(int i=N-1;i>=0;i--){
//
// int temp=0;
//
// if(a==set1.size() && b==set2.size() && c==set3.size())
// continue;
// else if (a==set1.size() && b==set2.size() ){
// ans += (yval) * arr[i];
// c++;
// temp=11;
// }else if(a==set1.size()){
// if (set2.get(b) <set3.get(c)) {
// ans += (xval) * arr[i];
// b++;
// } else {
// ans += (yval) * arr[i];
// c++;
// }
// }else{
// if (set1.get(a) < set2.get(b) && set1.get(a) < set3.get(c)) {
// ans += (xval + yval) * arr[i];
// a++;
// } else if (set2.get(b)<set3.get(c)) {
// ans += (xval) * arr[i];
// b++;
// } else {
// ans += (yval) * arr[i];
// c--;
// }
// }
//
//
//
// if(ans>=k){
// break;
// }
//
// }
//
// if(ans<k)
// out.println(-1);
// else
// out.println(add);
//
//
//
// }
//
// out.close();
// }
//
//import java.io.*;
//import java.util.*;
//
//
//public class Q3 {
//
//
// // tunnel wala Question tourist
//
//// public static void main(String[] args) {
////
//// InputReader in = new InputReader();
//// PrintWriter out = new PrintWriter(System.out);
////
//// int N=in.nextInt();
//// int arr1[]=new int [N],arr2[]=new int[N];
//// int ans =0;
////
//// for(int i =0;i<N;i++){
//// arr1[i]=in.nextInt();
//// }
////
//// HashMap<Integer,Integer>map=new HashMap<>();
////
//// for(int j=0;j<N;j++){
//// int num=in.nextInt();
//// arr2[j]=num;
//// map.put(num,N-j);
//// }
//// int a[]=new int [N+1];
////
//// boolean flag=false;
//// for(int i =0;i<N;i++) {
//// int num = arr1[i];
//// int val=map.get(num);
//// if(val>(N-i))
//// ans++;
//// else if(val==N-i && !flag){
//// ans++;
////
//// }
//// a[arr1[i]]++;
//// a[arr2[N-i-1]]++;
////
//// if(a[arr1[i]]!=2 || a[arr2[N-i-1]]!=2)
//// flag=false;
//// else
//// flag=true;
////
//// }
//// out.println(ans);
////
//// out.close();
////
////
//// }
//
// static class InputReader {
// public BufferedReader reader;
// public StringTokenizer tokenizer;
//
//
// public int[] shuffle(int[] arr) {
// Random r = new Random();
// for (int i = 1, j; i < arr.length; i++) {
// j = r.nextInt(i);
// arr[i] = arr[i] ^ arr[j];
// arr[j] = arr[i] ^ arr[j];
// arr[i] = arr[i] ^ arr[j];
// }
// return arr;
// }
//
// public InputReader() {
// reader = new BufferedReader(new InputStreamReader(System.in), 32768);
// tokenizer = null;
// }
//
// public InputReader(InputStream stream) {
// reader = new BufferedReader(new InputStreamReader(System.in), 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 char nextChar() {
// return next().charAt(0);
// }
//
// public int nextInt() {
// return Integer.parseInt(next());
// }
//
// public long nextLong() {
// return Long.parseLong(next());
// }
//
// public double nextDouble() {
// return Double.parseDouble(next());
// }
//
//
// }
//
//}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
7af883723bc5991d9235a9e084a66109
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.*;
public class cf1293a {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
T:
for (int tc = 0; tc < t; tc++) {
int n = in.nextInt();
int s = in.nextInt();
int k = in.nextInt();
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < k; i++) {
set.add(in.nextInt());
}
int i = 0;
while (true) {
if (s + i <= n) {
if (!set.contains(s + i)) {
System.out.println(i);
continue T;
}
}
if (s - i > 0) {
if (!set.contains(s - i)) {
System.out.println(i);
continue T;
}
}
i++;
}
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
049186000dd28973b062d23517d3a956
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class connerAndTheArcMarkland {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i = 0; i<n; i++) {
int f = in.nextInt();
int s = in.nextInt();
int k= in.nextInt();
int[] arr = new int[k];
for(int j = 0; j<k; j++) {
arr[j] = in.nextInt();
}
Arrays.sort(arr);
int val = Integer.MAX_VALUE;
for(int j = s; j>=s - 2000 && j>=1;j--) {
if(Arrays.binarySearch(arr, j)>=0)
continue;
else {
val = (s - j);
break;
}
}
for(int j = s; j<=2000+s && j<=f;j++) {
if(Arrays.binarySearch(arr, j)>=0)
continue;
else {
val = Math.min(val,(j - s));
break;
}
}
System.out.println(val);
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
a2620a98a0f6296d79b437de0bef55b4
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.*;
import java.lang.*;
import java.util.*;
public class MyClass {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while(T-- > 0)
{
String in[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(in[0]);
int s = Integer.parseInt(in[1]);
int k = Integer.parseInt(in[2]);
in = br.readLine().trim().split(" ");
int closed[] = new int[k];
for(int i=0 ; i<k ; i++)
{
closed[i] = Integer.parseInt(in[i]);
}
Arrays.sort(closed); // n logn
int floor = Arrays.binarySearch(closed, s);
int nearestDist=-1;
if(floor < 0) nearestDist = 0;
else
{
int upperNearestDist = upperDist(closed, floor, k-1, n);
int lowerNearestDist = lowerDist(closed, floor, 0, 1);
// System.out.println(">>>" + upperNearestDist + " " + lowerNearestDist);
nearestDist = upperNearestDist < lowerNearestDist ? upperNearestDist : lowerNearestDist;
// nearestDist = upperNearestDist;
}
System.out.println(nearestDist);
}
}
static int upperDist(int arr[], int ind, int high, int end)
{
int i = ind, dist = 0;
while(i < high && arr[i] + 1 == arr[i + 1] )
{
dist++;
i++;
}
if(arr[i] == end) return Integer.MAX_VALUE;
else return dist + 1;
}
static int lowerDist(int arr[], int ind, int low, int start)
{
int i = ind, dist = 0;
while(i > low && arr[i] - 1 == arr[i - 1])
{
dist++;
i--;
}
if(arr[i] == start) return Integer.MAX_VALUE;
else return dist + 1;
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
59a455fe069e2d651be56e7aa68a06d1
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.*;
import java.lang.*;
import java.util.*;
public class MyClass {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while(T-- > 0)
{
String in[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(in[0]);
int s = Integer.parseInt(in[1]);
int k = Integer.parseInt(in[2]);
in = br.readLine().trim().split(" ");
int closed[] = new int[k];
for(int i=0 ; i<k ; i++)
{
closed[i] = Integer.parseInt(in[i]);
}
Arrays.sort(closed); // n logn
int floor = Arrays.binarySearch(closed, s);
int nearestDist=-1;
if(floor < 0) nearestDist = 0;
else
{
int upperNearestDist = upperDist(closed, floor, k-1, n);
int lowerNearestDist = lowerDist(closed, floor, 0, 1);
// System.out.println(">>>" + upperNearestDist + " " + lowerNearestDist);
nearestDist = upperNearestDist < lowerNearestDist ? upperNearestDist : lowerNearestDist;
// nearestDist = upperNearestDist;
}
System.out.println(nearestDist);
}
//
}
static int upperDist(int arr[], int ind, int high, int end)
{
int i = ind, dist = 0;
while(i < high && arr[i] + 1 == arr[i + 1] )
{
dist++;
i++;
}
if(arr[i] == end) return Integer.MAX_VALUE;
else return dist + 1;
}
static int lowerDist(int arr[], int ind, int low, int start)
{
int i = ind, dist = 0;
while(i > low && arr[i] - 1 == arr[i - 1])
{
dist++;
i--;
}
if(arr[i] == start) return Integer.MAX_VALUE;
else return dist + 1;
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
a43154204fffc092ffbb2ed7ed573770
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.awt.*;
import java.nio.file.Paths;
import java.util.*;
import java.io.*;
public class TaskE {
//================================================
//Константы.......................................
//================================================
static final long N = (long) (1e5 + 10);
static final long M = (long) (1e7 + 1000);
static final long mod = (long) (1e9 + 7);
static final long INF = (long) (1e18);
static final long inf = (long) (-1e18);
//================================================
//================================================
// O(long(n))
static long binpow(long a, long n){
if (n == 0) return 1;
if (n % 2 == 1){
return binpow(a, n-1) * a;
}else {
long b = binpow(a, n/2);
return b * b;
}
}
// O(log(min(a,b))
static long gcd(long a, long b){
if (b == 0) return a;
else return gcd(b, a % b);
}
//===============================================
//===============================================
void run(){
/*try {
Scanner in = new Scanner(Paths.get("symposium.in"), "UTF-8");
PrintWriter out = new PrintWriter("symposium.out", "UTF-8");
slove(in, out);
}catch (IOException e){
throw new Error(e);
}*/
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
slove(in, out);
}
//=================================================
// РЕШЕНИЕ .. .. .. . .. .. .. .. ... ... .... .. .
//=================================================
void slove(Scanner in, PrintWriter out){
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
int s = in.nextInt();
int k = in.nextInt();
int[] ans = new int[k];
for (int j = 0; j < k; j++) {
ans[j] = in.nextInt();
}
Arrays.sort(ans);
if (Arrays.binarySearch(ans, s) < 0){
out.println(0);
out.flush();
}
else {
int tempS = s;
int higth = -1;
int low = -1;
for (int j = 0; j < n;j++){
tempS = tempS + 1;
if (tempS > n) break;
if (Arrays.binarySearch(ans, tempS) < 0){
higth = tempS;
break;
}
}
tempS = s;
for (int j = 0; j < n;j++){
tempS = tempS - 1;
if (tempS == 0) break;
if (Arrays.binarySearch(ans, tempS) < 0){
low = tempS;
break;
}
}
if (low == -1 && higth != -1){
out.println(Math.abs(s - higth));
out.flush();
}else if (higth == -1 && low != -1){
out.println(Math.abs(s - low));
out.flush();
}else if (higth != -1 && low != -1){
out.println(Math.min(Math.abs(s - low), Math.abs(s - higth)));
out.flush();
}
}
}
}
//================================================
//================================================
public static void main(String[] args) {
new TaskE().run();
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
5b7adcc583f9cae1bf7bb0cfc93bb03b
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class k {
public static void main(String []args) throws IOException {
PrintWriter pw =new PrintWriter(System.out);
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int k=0;k<t;k++) {
int n=sc.nextInt();
int r=sc.nextInt();
int m=sc.nextInt();
HashSet<Integer>ts=new HashSet<>();
for(int i=0;i<m;i++)ts.add((int)sc.nextInt());
int ans=0;int ans2=0;
for (int i=r;i>0;i--) {
if(!ts.contains(i)) {ans=i;break;}
}
for(int i=r;i<=n;i++)if(!ts.contains(i)) {ans2=i;break;}
if(ans==0)pw.println(ans2-r);else if(ans2==0)pw.println(r-ans);else pw.println(Math.min(r-ans, ans2-r)); pw.flush();
}}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
62626fbfca902a0014dddfdeaef377bd
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.*;
public class Codeforce {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- != 0){
int n = sc.nextInt(), s = sc.nextInt(), k = sc.nextInt();
ArrayList<Integer> lst = new ArrayList();
for(int i = 1; i <=k; i++){
lst.add(sc.nextInt());
}
int cost=0;
for(int i = s; (s-cost)>0 || (s+cost)<=n; i++){
if(s-cost > 0 && !lst.contains(s-cost)){
break;
}else if(s+cost <=n && !lst.contains(s+cost)){
break;
}
cost++;
}
System.out.println(cost);
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
f2c7580767afe807f8a16eda29e10895
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.*;
public class Codeforce {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- != 0) {
int n = sc.nextInt();
int s = sc.nextInt();
int k = sc.nextInt();
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < k; i++) {
int x = sc.nextInt();
set.add(x);
}
for (int d = 0; d <= 1001; d++) {
int right = s + d;
if (right <= n && !set.contains(right)) {
System.out.println(d);
break;
}
int left = s - d;
if (left >= 1 && !set.contains(left)) {
System.out.println(d);
break;
}
}
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
8f84e27e2fd821cf3625d65f1e272f29
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.HashSet;
import java.util.Scanner;
public class conner {
// 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;
// }
// }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
int s = in.nextInt()-1;
int k = in.nextInt();
HashSet<Integer> closed = new HashSet<>();
for(int j = 0; j<k; j++)
{
closed.add(in.nextInt()-1);
}
//ArrayDeque<Integer> q = new ArrayDeque<>();
int countup = 0;
int countd= 0;
//int curr = s;
for(int p = s; p<n; p++)
{
if(!closed.contains(p))
{
break;
}
countup++;
if(p == n-1)
{
countup = Integer.MAX_VALUE;
}
}
for(int p = s; p>=0; p--)
{
if(!closed.contains(p))
{
break;
}
countd++;
if(p == 0)
{
countd = Integer.MAX_VALUE;
}
}
System.out.println(Math.min(countup, countd));
}
}
}
/*
5
5 2 3
1 2 3
4 3 3
4 1 2
10 2 6
1 2 3 4 5 7
2 1 1
2
100 76 8
76 75 36 67 41 74 10 77
*/
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
9231967bb24e463d8e4f0c147d6f5eb1
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class A {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int s = scanner.nextInt() - 1;
int k = scanner.nextInt();
Set<Integer> empty = new HashSet<>();
for (int j = 0; j < k; j++) {
empty.add(scanner.nextInt() - 1);
}
int q = 0;
while (true) {
if ((s-q < 0 || empty.contains(s-q)) && (s + q >= n || empty.contains(s+q))) {
q++;
} else {
System.out.println(q);
break;
}
}
}
scanner.close();
}
}
/*
1
10 2 6
1 2 3 4 5 7
*/
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
fc24a48745849cfe923adfee1258c700
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.*;
import java.text.DecimalFormat;
import java.io.*;
public class FedorNewGame {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine().trim());
while (t>0)
{
String inp[]=br.readLine().split(" ");
int n=Integer.parseInt(inp[0]);
int s=Integer.parseInt(inp[1]);
int k=Integer.parseInt(inp[2]);
int arr[]=new int[k];
int diff=Integer.MAX_VALUE;
int ans=-1;
String input[]=br.readLine().split(" ");
Set<Integer> set=new HashSet<>();
for (int i=0;i<k;i++)
{
int num=Integer.parseInt(input[i]);
set.add(num);
}
for (int i=0;i<n;i++)
{
int fu=s+i;
int fb=s-i;
if (fu>0 && fu<=n)
{
if (!set.contains(fu))
{
ans=i;
break;
}
}
if (fb>0 && fb<=n)
{
if (!set.contains(fb))
{
ans=i;
break;
}
}
}
System.out.println(ans);
t--;
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
bfc91ed2368c1d2f16b783f4695f2fe4
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.BigInteger;
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);
Solve solver = new Solve();
solver.solve(1, in, out);
out.close();
}
static class Solve {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int plant, act, clos;
for(int i = in.nextInt();i>0;i--) {
plant = in.nextInt();
act = in.nextInt();
clos = in.nextInt();
int m = plant-1;
if(m>2000)m=2000;
int arr[] = new int[m+1];
for(int o = 0;o<clos;o++) {
int num = in.nextInt();
if(num<=act+m&num>=act-m) {
arr[Math.abs(num-act)]++;
}
}
if(arr[0]==0) {
System.out.println(0);
continue;
}
for(int o = 1;o<2000;o++) {
if(act-o<=0|act+o>plant) {
if(arr[o]!=1) {
System.out.println(o);
break;
}
}
else {
if(arr[o]!=2) {
System.out.println(o);
break;
}
}
}
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(String string) {
writer.println(string);
}
public void println(int number) {
writer.println(number);
}
public void println(long number) {
writer.println(number);
}
public void println(double number) {
writer.println(number);
}
public void print(long number) {
writer.print(number);
}
public void print(int number) {
writer.print(number);
}
public void print(String string) {
writer.print(string);
}
public void print(double number) {
writer.print(number);
}
}
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public String nextLine() {
StringBuilder sb = new StringBuilder();
int b = readByte();
while (b!=10) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.