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 | 79dae96c669dc7b4bb778f5f7d19addb | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.InputMismatchException;
/**
* Created by assalielmehdi on 8/15/17.
*/
public class CF469_D2_B {
private InputReader in;
private PrintWriter out;
private StringBuilder sb;
private boolean onlineJudge = System.getProperty("ONLINE_JUDGE") != null;
private CF469_D2_B() throws Exception {
long s = System.currentTimeMillis();
init();
solve();
if (!onlineJudge)
sb.append(sb.charAt(sb.length() - 1) == '\n' ? "" : '\n').append("[OK in : ").append(System.currentTimeMillis() - s).append(" ms]");
out.print(sb);
out.close();
}
public static void main(String args[]) throws Exception {
new CF469_D2_B();
}
private void init() throws Exception {
in = new InputReader(onlineJudge ? System.in : new FileInputStream(new File("in.txt")));
out = new PrintWriter(System.out);
sb = new StringBuilder();
}
private void solve() throws Exception {
int p = in.nextInt(), q = in.nextInt(), l = in.nextInt(), r = in.nextInt();
int[] a = new int[p], b = new int[p], c = new int[q], d = new int[q];
for (int i = 0; i < p; i++) {
a[i] = in.nextInt();
b[i] = in.nextInt();
}
for (int i = 0; i < q; i++) {
c[i] = in.nextInt();
d[i] = in.nextInt();
}
int ans = 0;
externalLoop:
for (int t = l; t <= r; t++) {
for (int i = 0; i < q; i++) {
int ci = c[i] + t;
int di = d[i] + t;
for (int j = 0; j < p; j++) {
if ((ci <= a[j] && a[j] <= di) || (ci <= b[j] && b[j] <= di) || (a[j] <= ci && ci <= b[j]) || (a[j] <= di && di <= b[j])) {
ans++;
continue externalLoop;
}
}
}
}
sb.append(ans);
}
class InputReader {
private int lenbuf = 0, ptrbuf = 0;
private InputStream in;
private byte[] inbuf = new byte[1024];
InputReader(InputStream in) {
this.in = in;
}
private int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = in.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;
}
long nextLong() {
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();
}
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[][] nextLongMatrix(int n, int m) {
long[][] map = new long[n][];
for (int i = 0; i < n; i++) map[i] = nextLongArray(m);
return map;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextDouble();
return a;
}
double[][] nextDoubleMatrix(int n, int m) {
double[][] map = new double[n][];
for (int i = 0; i < n; i++) map[i] = nextDoubleArray(m);
return map;
}
String next() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
String[] nextArray(int n) {
String[] arr = new String[n];
for (int i = 0; i < n; i++) arr[i] = next();
return arr;
}
String[][] nextMatrix(int n, int m) {
String[][] map = new String[n][];
for (int i = 0; i < n; i++) map[i] = nextArray(m);
return map;
}
String nextLine() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b) && b != ' ')) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
char nextChar() {
return (char) skip();
}
char[] nextCharArray(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);
}
char[][] nextCharMatrix(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) map[i] = nextCharArray(m);
return map;
}
int nextInt() {
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();
}
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[][] nextIntMatrix(int n, int m) {
int[][] map = new int[n][];
for (int i = 0; i < n; i++) map[i] = nextIntArray(m);
return map;
}
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 25cc7d5382b6b0793140481953c04318 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes |
import java.util.Scanner;
import java.util.*;
public class abdelmagied {
public static class pair{
int first , second;
pair(int first ,int second){
this.first = first;
this.second = second;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int p = sc.nextInt();
int q = sc.nextInt();
int l = sc.nextInt();
int r = sc.nextInt();
ArrayList<pair> y = new ArrayList<pair>();
ArrayList<pair> x = new ArrayList<pair>();
for(int i = 0 ; i < p ; i++) {
int first = sc.nextInt() ;
int second = sc.nextInt();
y.add(new pair(first , second));
}
for(int i = 0 ; i < q ; i++) {
int first = sc.nextInt() ;
int second = sc.nextInt();
x.add(new pair(first , second));
}
Set<Integer> result = new HashSet<Integer>();
for(int i = l ; i <= r ; i++) {
for(int j = 0 ; j < q ; j++) {
for(int k = 0 ; k < p ; k++) {
if(x.get(j).first + i > y.get(k).second ||x.get(j).second + i < y.get(k).first );
else
result.add(i);
}
}
}
System.out.println(result.size());
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | fe8e5a93f875a38e0683ec2f25a955c5 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes |
import java.util.Scanner;
public class ChatOnline {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int p = sc.nextInt();
int q = sc.nextInt();
int l = sc.nextInt();
int r = sc.nextInt();
int[][] z = new int[p][2];
for (int i = 0; i < p; i++) {
z[i][0] = sc.nextInt();
z[i][1] = sc.nextInt();
}
boolean[] ok = new boolean[1001];
int result = 0;
for (int i = 0; i < q; i++) {
int c = sc.nextInt();
int d = sc.nextInt();
for (int j = l; j <= r; j++) {
int left = c + j;
int right = d + j;
for (int[] x : z) {
if ((left >= x[0] && left <= x[1]) || (right >= x[0] && right <= x[1]) || (left <= x[0] && right >= x[1])) {
if (!ok[j]) {
result++;
ok[j] = true;
}
break;
}
}
}
}
System.out.println(result);
sc.close();
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 25d019650355e50e218cbb70d797c0d0 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.util.*;
public class Chat {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int p = sc.nextInt();
int q = sc.nextInt();
int l = sc.nextInt();
int r = sc.nextInt();
int a[] = new int[p];
int b[] = new int[p];
int c[] = new int[q];
int d[] = new int[q];
for (int i = 0; i < p; i++) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
}
for (int i = 0; i < q; i++) {
c[i] = sc.nextInt();
d[i] = sc.nextInt();
}
int count = 0;
for (int i = l; i <= r; i++) {
X: for (int j = 0; j < q; j++) {
for (int k = 0; k < p; k++) {
if (((c[j] + i) == a[k]) || ((c[j] + i) == b[k])) {
count++;
break X;
} else if (((d[j] + i) == a[k]) || ((d[j] + i) == b[k])) {
count++;
break X;
} else if (((c[j] + i) < a[k]) && ((d[j] + i) > a[k])) {
count++;
break X;
} else if (((c[j] + i) > a[k]) && ((c[j] + i) < b[k])) {
count++;
break X;
} else if (((d[j] + i) > a[k]) && ((d[j] + i) < b[k])) {
count++;
break X;
}
}
}
}
System.out.println(count);
}
} | Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 2ecf5c45375eb0d149ebee5762040161 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.util.*;
public class Chat {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int p = sc.nextInt();
int q = sc.nextInt();
int l = sc.nextInt();
int r = sc.nextInt();
int a[] = new int[p];
int b[] = new int[p];
int c[] = new int[q];
int d[] = new int[q];
for (int i = 0; i < p; i++) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
}
for (int i = 0; i < q; i++) {
c[i] = sc.nextInt();
d[i] = sc.nextInt();
}
int count = 0;
for (int i = l; i <= r; i++) {
X: for (int j = 0; j < q; j++) {
for (int k = 0; k < p; k++) {
if (((c[j] + i) == a[k]) || ((c[j] + i) == b[k])) {
count++;
break X;
} else if (((d[j] + i) == a[k]) || ((d[j] + i) == b[k])) {
count++;
break X;
} else if (((c[j] + i) < a[k]) && ((d[j] + i) > a[k])) {
count++;
break X;
} else if (((c[j] + i) > a[k]) && ((c[j] + i) < b[k])) {
count++;
break X;
}
}
}
}
System.out.println(count);
}
} | Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | ed690e4150a260ceacfe8e322cccaf65 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.util.Scanner;
public class Chat_Online {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int p = s.nextInt();
int q = s.nextInt();
int l = s.nextInt();
int r = s.nextInt();
boolean visited_a[] = new boolean[1001];
boolean visited_b[] = new boolean[1001];
int a = 0, b = 0, c = 0, d = 0;
for (int i = 0; i < p; i++) {
a = s.nextInt();
b = s.nextInt();
for (int j = a; j <= b; j++) {
visited_a[j] = true;
}
}
for (int i = 0; i < q; i++) {
c = s.nextInt();
d = s.nextInt();
for (int j = c; j <= d; j++) {
visited_b[j] = true;
}
}
int count = 0;
for (int i = l; i <= r; i++) {
for (int k = 0; k < 1001; k++) {
if (k + i > 1000) {
break;
}
if (visited_b[k] && visited_a[k + i]) {
count++;
k = 1001;
}
}
}
System.out.println(count);
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | a5f5386499627d9513163aee77bbd3c5 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.io.*;
import java.util.*;
public class d {
static ArrayList<Integer>[]g;
public static void main(String args[]) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int p = sc.nextInt(), q = sc.nextInt(), l = sc.nextInt(), r = sc.nextInt();
int[] a = new int[1001];
int[] b = new int[1001];
for (int i = 0; i < p; i++) {
int x =sc.nextInt(),y=sc.nextInt();
for(int j=x;j<=y;j++){
a[j]++;
}
}
for (int i = 0; i < q;i++) {
int x =sc.nextInt(),y=sc.nextInt();
for(int j=x;j<=y;j++){
b[j]++;
}
}
int ans = 0;
for (int i = l; i <= r; i++) {
for (int j = 0; j <= 1000; j++) {
if (b[j] != 0) {
if (i+j<=1000&&a[i + j] != 0) {
ans++;
break;
}
}
}
}
out.print(ans);
out.close();
}
public static void shuffle(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static void shuffle(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
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.valueOf(next());
}
public double nextDouble() {
return Double.valueOf(next());
}
String nextLine() throws IOException {
return reader.readLine();
}
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | fa2205e4995d7d41a5dc1243c6a929dc | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes |
import java.io.*;
import java.util.*;
public class ChatOnline {
static class os {
int s, e;
public os(int s, int e) {
this.s = s;
this.e = e;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
int p = in.nextInt();
int l = in.nextInt();
int r = in.nextInt();
os x[] = new os[q];
os y[] = new os[p];
for (int i = 0; i < q; i++) {
x[i] = new os(in.nextInt(), in.nextInt());
}
for (int i = 0; i < p; i++) {
y[i] = new os(in.nextInt(), in.nextInt());
}
int re = 0;
for (int k = l; k <= r; k++) {
ss:
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
if (y[i].e + k < x[j].s || y[i].s + k > x[j].e) {
} else {
re++;
break ss;
}
}
}
}
System.out.println(re);
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 43f8dbaa7ced9102f38674a7b75c256b | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes |
import java.io.*;
import java.util.*;
public class ChatOnline {
static class os {
int s,e;
public os(int s, int e) {
this.s = s;
this.e = e;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
int p = in.nextInt();
int l = in.nextInt();
int r = in.nextInt();
os x[]=new os[q];
os y[]=new os[p];
int max =0;
for (int i = 0; i < q; i++)x[i]= new os(in.nextInt(),in.nextInt());
for (int i = 0; i < p; i++){y[i]= new os(in.nextInt(),in.nextInt());max =Math.max(max, y[i].e);}
max = r-max+1;
long re = 0;
int z =0;
for (int k = l; k <=r; k++) {
ss:for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
if (y[i].e+k<x[j].s||y[i].s+k>x[j].e) {
}else { re++;break ss;}
}
}
//z++;
}
System.out.println(re);
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 08e65dc8a9fd079d4281002ab39c293e | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int p = Integer.parseInt(s[0]);
int q = Integer.parseInt(s[1]);
int a[] = new int[2*p];
int b[] = new int[2*q];
int l = Integer.parseInt(s[2]);
int r = Integer.parseInt(s[3]);
int x =0;
for(int i =0 ; i < p ; i++){
String s1[] = br.readLine().split(" ");
a[x++] = Integer.parseInt(s1[0]);
a[x++] = Integer.parseInt(s1[1]);
}
x = 0;
for(int i =0 ; i < q ; i++){
String s2[] = br.readLine().split(" ");
b[x++] = Integer.parseInt(s2[0]);
b[x++] = Integer.parseInt(s2[1]);
}
/*System.out.println("a");
for(int i = 0 ; i < 2*p; i++){
System.out.print(a[i] + " ");
}
System.out.println();
System.out.println("b");
for(int i =0 ; i < 2*q ; i++){
System.out.print(b[i] + " ");
}
System.out.println();*/
int c = 0 ;
int z = 0;
int f= 0;
int y = 0;
int w =0;
for(int i = l ; i <= r; i++){
inner:
for(int j = 0 ; j < 2*q-1 ; j=j+2){
for(int k = 0 ; k < 2*p-1 ; k=k+2){
z=(b[j]+i);
f =(b[j+1]+i);
y = a[k];
w = a[k+1];
//System.out.println(y+" "+w +" "+ i+ " "+z+" "+f);
if((y < z) && (w > z) && (k%2 == 0)){
c++;
//System.out.println("1st " + y +" "+ z +" i"+ i+" j" + j+" k"+k+" "+w+" "+f);
break inner;
}
if((y == z) || (y == f) || (w == z) || (w == f)){
c++;
//System.out.println("2nd " + y +" "+ z +" i"+ i+" j" + j+" k"+k+" "+w+" "+f);
break inner;
}
if((z < y) && (f > y) && (f < w) && (k%2 == 0)){
c++;
//System.out.println("3rd " + y +" "+ z +" i"+ i+" j" + j+" k"+k+" "+w+" "+f);
break inner;
}
if((z < y) && (f > w) && (k%2 == 0)){
c++;
//System.out.println("4th " + y +" "+ z +" i"+ i+" j" + j+" k"+k+" "+w+" "+f);
break inner;
}
}
}
}
System.out.println(c);
}
} | Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | bca470b7e57d6c18bdf9b5b8c56028b1 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class test {
int max = 0;
long max1 = 0;
boolean visited[][];
int parent[];
boolean bi=true;
StringBuilder cout=new StringBuilder("");
int color[];
PrintStream out = System.out;
int signx[] = {1,-1,0 ,0, 1, -1,-1,1};
int signy[]= {0,0,1,-1, 1,-1,1,-1};
public static void main(String args[]) throws IOException {
test obj = new test();
obj.start();
}
void start() throws IOException {
// Reader sc = new Reader();
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc=new Scanner(System.in);
int p=sc.nextInt();
int q=sc.nextInt();
int l=sc.nextInt();
int r=sc.nextInt();
int count=0;
boolean x[]=new boolean[1001];
boolean y[]=new boolean[1001];
boolean t[]=new boolean[r-l+1];
while(p-->0)
{
int a=sc.nextInt();
int b=sc.nextInt();
for(int i=a;i<=b;i++)
{
x[i]=true;
}
}
int temp=q;
while (q-->0) {
int a=sc.nextInt();
int b=sc.nextInt();
for(int i=l;i<=r;i++)
{
for(int j=a+i;j<=b+i&&j<=1000;j++)
{
if(x[j])
{
t[i-l]=true;
break;
}
}
}
}
for(int i=0;i<=r-l;i++)
{
if(t[i])
count++;
}
out.println(count);
}
//<---------------------------------------------------------------------------------------------------------------->//
//printing 2-d array
private void println(int[][] a,int n,int m) {
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
void dfs(char a[][],char t,int i,int j,int n,int m,int p,int q)
{
// out.print(" "+i+" "+j+" "+t);
visited[i][j]=true;
for(int c=0;c<4;c++)
{
int temp=i+signx[c];
int temp1=j+signy[c];
if(temp>=0&&temp<n&&temp1>=0&&temp1<m)
{
if(a[temp][temp1]==t&&(temp!=p||temp1!=q)&&!visited[temp][temp1])
dfs(a,t,temp,temp1,n,m,i,j);
else if(a[temp][temp1]==t&&(temp!=p||temp1!=q)&&visited[temp][temp1]) {
max = 1;
}
}
}
}
void println(ArrayList<Long> a)
{
for (Long b:a
) {
System.out.print(b+" ");
}
}
//modified dfs for chessboard, matrix
void dfs(char a[][],int i,int j,char t,int n,int m,int d,int p,int q,boolean visited[][])
{
}
//constructing segment tree
private void constructseg(long[] seg, long[] input, int p, int q, int pos) {
if(p==q)
{
seg[pos]=input[p];
return;
}
int mid=(q+p)/2;
constructseg(seg,input,p,mid,2*pos+1);
constructseg(seg,input,mid+1,q,2*pos+2);
seg[pos]=seg[2*pos+1]+seg[2*pos+2];
}
/* void bipartite(ArrayList<Integer> a[],int i)
{
int presentcolor=color[i];
int nextcolor;
if(color[i]==0) {
nextcolor=1;
b.add(i);
}
else
{
nextcolor=0;
c.add(i);
}
for(int j=0;j<a[i].size();j++)
{
if(color[a[i].get(j)]==presentcolor)
bi=false;
else if(color[a[i].get(j)]==-1)
{
color[a[i].get(j)]=nextcolor;
bipartite(a,a[i].get(j));
}
}
}*/
//traversing through a arrayList graph
void dfs(ArrayList<Integer> node[],int i)
{
/* visited[i]=true;
/* if(cat[i]==1) {
count++;
}
else
count=0;
if(count>k) {
return;
}
if(node[i].size()==1&&i!=0) {
max += 1;
}
for (int j = 0; j < node[i].size(); j++) {
if(!visited[node[i].get(j)]) {
max--;
dfs(node, node[i].get(j));
}
}*/
}
//printing arraylist array
void println(ArrayList<Integer> a[])
{
for (ArrayList<Integer> b:a
) {
System.out.println(b.toString());
}
}
//sorting 2 array based on a, Complexity is O(n^2)
void sort(int a[],int b[],int n)
{
for(int i=1;i<n;i++)
{
int index=i;
for(int j=i-1;j>=0;j--)
{
// println(a);
if(a[index]<a[j])
{
int temp=a[index];
int temp2=b[index];
a[index]=a[j];
b[index]=b[j];
a[j]=temp;
b[j]=temp2;
index=j;
}
else
break;
}
}
}
// to create and initialize Integer array
private int[] intArray(int a, Scanner sc) {
int temp[]=new int[a];
for(int i=0;i<a;i++) {
temp[i]=sc.nextInt();
}
return temp;
}
private int[] intArray(int a, Reader sc) throws IOException {
int temp[]=new int[a];
for(int i=0;i<a;i++) {
temp[i]=sc.nextInt();
}
return temp;
}// / to create and initialize Long Array
private long[] longArray(int a, Scanner sc) {
long temp[]=new long[a];
for(int i=0;i<a;i++) {
temp[i]=sc.nextLong();
}
return temp;
}
// displaying Map contents key--value
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
// Return arrayList of minimum factor of number
// 24= 2*2*2*3;
private ArrayList<Integer> minFactor(int a) {
ArrayList<Integer> obj=new ArrayList<>();
for(int i=2;i<=a;i++)
{
while(a!=0&&a%i==0)
{
obj.add(i);
a/=i;
}
}
return obj;
}
// check whether the number is prime or not
public static boolean isPrimeNumber(int i) {
int factors = 0;
int j = 1;
while(j <= i)
{
if(i % j == 0)
{
factors++;
}
j++;
}
return (factors == 2);
}
// creating and initializing arrayList
private void initArrayList(ArrayList<Integer>[] tempa) {
for(int i=0;i<tempa.length;i++)
tempa[i]=new ArrayList<>();
}
// return factorial contents as ArrayList
private ArrayList<Integer> factdigit(int i)
{
ArrayList<Integer> temp=new ArrayList<>();
for(int j=i;j>1;j--)
{
temp.add(j);
}
return temp;
}
//printing Integer array
private void println(int a[])
{
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
}
int factorial(int n){
if (n == 0)
return 1;
else
return((n %1000000007)* (factorial(n-1)%1000000007));
}
}
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[300]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
//Pair implementation
class Pair<A, C> {
public A a;
public C c;
Pair(A a, C c) {
this.a = a;
this.c = c;
}
A getKey() {
return this.a;
}
C getValue() {
return this.c;
}
void print() {
System.out.println(this.a + " " + this.c);
}
} | Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 03a6633a33d57902cbc2e5671a6c9af4 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.util.*;
import java.io.*;
public class Main469B
{
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
int p=sc.nextInt();
int q=sc.nextInt();
int l=sc.nextInt();
int r=sc.nextInt();
int[] marked=new int[1000001];
for(int i=0;i<p;i++)
{
int x=sc.nextInt();
int y=sc.nextInt();
for(int j=x;j<=y;j++)
marked[j]++;
}
int[] c=new int[q];
int[] d=new int[q];
for(int i=0;i<q;i++)
{
c[i]=sc.nextInt();
d[i]=sc.nextInt();
}
int cnt=0;
for(int i=l;i<=r;i++)
{
boolean flag=false;
for(int j=0;j<q;j++)
{
for(int k=c[j]+i;k<=(d[j]+i);k++)
{
if(marked[k]>0)
{
flag=true;
break;
}
}
if(flag)
break;
}
if(flag)
cnt++;
}
out.println(cnt);
out.flush();
}
static class Scanner
{
BufferedReader br;
StringTokenizer tk=new StringTokenizer("");
public Scanner(InputStream is)
{
br=new BufferedReader(new InputStreamReader(is));
}
public int nextInt() throws IOException
{
if(tk.hasMoreTokens())
return Integer.parseInt(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextInt();
}
public long nextLong() throws IOException
{
if(tk.hasMoreTokens())
return Long.parseLong(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextLong();
}
public String next() throws IOException
{
if(tk.hasMoreTokens())
return (tk.nextToken());
tk=new StringTokenizer(br.readLine());
return next();
}
public String nextLine() throws IOException
{
tk=new StringTokenizer("");
return br.readLine();
}
public double nextDouble() throws IOException
{
if(tk.hasMoreTokens())
return Double.parseDouble(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextDouble();
}
public char nextChar() throws IOException
{
if(tk.hasMoreTokens())
return (tk.nextToken().charAt(0));
tk=new StringTokenizer(br.readLine());
return nextChar();
}
public int[] nextIntArray(int n) throws IOException
{
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=nextInt();
return a;
}
public long[] nextLongArray(int n) throws IOException
{
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=nextLong();
return a;
}
public int[] nextIntArrayOneBased(int n) throws IOException
{
int a[]=new int[n+1];
for(int i=1;i<=n;i++)
a[i]=nextInt();
return a;
}
public long[] nextLongArrayOneBased(int n) throws IOException
{
long a[]=new long[n+1];
for(int i=1;i<=n;i++)
a[i]=nextLong();
return a;
}
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 91658e0fc145c3c31a80a767de5dadd5 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.util.*;
import java.io.*;
//267630EY
public class Main469B2
{
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
int p=sc.nextInt();
int q=sc.nextInt();
int l=sc.nextInt();
int r=sc.nextInt();
boolean[] marked=new boolean[10000];
for(int i=0;i<p;i++)
{
int x=sc.nextInt();
int y=sc.nextInt();
for(int j=x;j<=y;j++)
{
marked[j]=true;
}
}
int[] c=new int[q];
int[] d=new int[q];
int count=0;
for(int i=0;i<q;i++)
{
c[i]=sc.nextInt();
d[i]=sc.nextInt();
}
for(int i=l;i<=r;i++)
{
boolean flag=false;
for(int j=0;j<q;j++)
{
for(int k=c[j]+i;k<=d[j]+i;k++)
{
if(marked[k])
{
flag=true;
break;
}
}
}
if(flag)
count++;
}
out.println(count);
out.flush();
}
static class Scanner
{
BufferedReader br;
StringTokenizer tk=new StringTokenizer("");
public Scanner(InputStream is)
{
br=new BufferedReader(new InputStreamReader(is));
}
public int nextInt() throws IOException
{
if(tk.hasMoreTokens())
return Integer.parseInt(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextInt();
}
public long nextLong() throws IOException
{
if(tk.hasMoreTokens())
return Long.parseLong(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextLong();
}
public String next() throws IOException
{
if(tk.hasMoreTokens())
return (tk.nextToken());
tk=new StringTokenizer(br.readLine());
return next();
}
public String nextLine() throws IOException
{
tk=new StringTokenizer("");
return br.readLine();
}
public double nextDouble() throws IOException
{
if(tk.hasMoreTokens())
return Double.parseDouble(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextDouble();
}
public char nextChar() throws IOException
{
if(tk.hasMoreTokens())
return (tk.nextToken().charAt(0));
tk=new StringTokenizer(br.readLine());
return nextChar();
}
public int[] nextIntArray(int n) throws IOException
{
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=nextInt();
return a;
}
public long[] nextLongArray(int n) throws IOException
{
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=nextLong();
return a;
}
public int[] nextIntArrayOneBased(int n) throws IOException
{
int a[]=new int[n+1];
for(int i=1;i<=n;i++)
a[i]=nextInt();
return a;
}
public long[] nextLongArrayOneBased(int n) throws IOException
{
long a[]=new long[n+1];
for(int i=1;i<=n;i++)
a[i]=nextLong();
return a;
}
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 51e741cbe53db8f7fc0f4a011ad35d39 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int p = s.nextInt();
int q = s.nextInt();
int l = s.nextInt();
int r = s.nextInt();
int[] a = new int[p];
int[] b = new int[p];
int[] c = new int[q];
int[] d = new int[q];
for(int i = 0;i<p;i++){
a[i] = s.nextInt();
b[i] = s.nextInt();
}
for(int i = 0;i<q;i++){
c[i] = s.nextInt();
d[i] = s.nextInt();
}
int count=0;
boolean flag = false;
for(int i = l;i<=r;i++){
flag = false;
for(int j = 0;j<q;j++){
int start = c[j]+i;
int end = d[j]+i;
for(int k = 0;k<p;k++){
if(start<a[k] && end<a[k]){
continue;
}else if(start>b[k] && end>b[k]){
continue;
}else{
flag = true;
count++;
break;
}
}
if(flag == true)
break;
}
}
System.out.println(count);
}
} | Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | da7bbd56589d91458ad2085bc9429b2c | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import sun.nio.cs.ext.MacHebrew;
import java.util.*;
public class Student {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int n= input.nextInt();
int m= input.nextInt();
int l= input.nextInt();
int r= input.nextInt();
boolean [] vis= new boolean[r-l + 1];
int [][] nIntervals=new int[n][2];
int [][] mIntervals= new int[m][2];
for(int i= 0; i< n ; i++){
int a= input.nextInt();
int b= input.nextInt();
nIntervals[i][0] = a;
nIntervals[i][1]= b;
}
for(int i= 0; i< m; i++){
int a= input.nextInt();
int b= input.nextInt();
mIntervals[i][0] = a;
mIntervals[i][1]= b;
}
for(int i= 0; i< n; i++){
int a= nIntervals[i][0];
int b= nIntervals[i][1];
for(int j= 0; j< m; j++){
int a0= mIntervals[j][0];
int b0= mIntervals[j][1];
int max= b - a0;
int min= a - b0;
if(min < l){
min = l;
}
if(max > r){
max = r;
}
for(int k = min; k <= max; k++){
vis[k-l]= true;
}
}
}
int sum = 0;
for(boolean val:vis){
if(val){
sum++;
}
}
System.out.println(sum);
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | b40176a18ef2958e9cf2dbea352d2702 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.nextLine();
String[] split = input.split(" ");
int p = Integer.parseInt(split[0]);
int q = Integer.parseInt(split[1]);
int l = Integer.parseInt(split[2]);
int r = Integer.parseInt(split[3]);
String[] ps = new String[p];
String[] qs = new String[q];
for(int i = 0; i < p; i++)
{
ps[i] = in.nextLine();
}
for(int i = 0; i < q; i++)
{
qs[i] = in.nextLine();
}
int[] myList = new int[r+1];
for(int i = l; i < r+1; i++)
{
myList[i] = -1;
}
int count = 0;
for(int i = 0; i < q; i++)
{
String qTemp = qs[i];
String[] qSplit = qTemp.split(" ");
int qStart = Integer.parseInt(qSplit[0]);
int qEnd = Integer.parseInt(qSplit[1]);
for(int j = 0; j < p; j++)
{
String pTemp = ps[j];
String[] pSplit = pTemp.split(" ");
int pStart = Integer.parseInt(pSplit[0]);
int pEnd = Integer.parseInt(pSplit[1]);
if(qStart > pEnd)
{
continue;
}
int diffStart = pStart - qEnd;
int diffEnd = pEnd - qStart;
if(diffStart < 0)
diffStart = 0;
if(diffEnd < 0)
diffEnd = 0;
for(int k = diffStart; k <= diffEnd; k++)
{
if(k >= l && k <= r)
{
if(myList[k] == k)
{
continue;
}
else
{
myList[k] = k;
count++;
}
}
}
}
}
System.out.println(count);
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 0efcfab958a7a15d0a33319a9fea3e8e | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.util.*;
public class Solution {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int p,q,l,r;
int counter = 0;
p = input.nextInt();
q = input.nextInt();
l = input.nextInt();
r = input.nextInt();
int[] fixed = new int[1001];
int[] temp;
int[][] free = new int[q][2];
for(int i=0; i<p; i++) {
fixed[input.nextInt()]++;
int t = input.nextInt();
if(t<1000)
fixed[t+1]--;
}
for(int i=0; i<q; i++) {
free[i][0] = input.nextInt();
free[i][1] = input.nextInt();
}
for(int i=l; i<=r; i++) {
temp = fixed.clone();
for(int[] a: free) {
if(a[0]+i<=1000)
temp[a[0]+i]++;
if(a[1]+i+1<1000)
temp[a[1]+i+1]--;
}
if(temp[0]>1) {
counter++;
} else {
for(int j=1; j<=1000; j++) {
temp[j] += temp[j-1];
if(temp[j] > 1) {
counter++;
break;
}
}
}
}
System.out.println(counter);
}
} | Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | cbdffec2838dae9ace6f39c65e0029f4 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
Reader.init(System.in);
int p = in.nextInt();
int q = in.nextInt();
int l = in.nextInt();
int r = in.nextInt();
ArrayList<Integer> arr1 = new ArrayList();
ArrayList<Integer> arr2 = new ArrayList();
TreeSet<Integer> tree = new TreeSet();
ArrayList<Integer> ar1 = new ArrayList();
ArrayList<Integer> ar2 = new ArrayList();
int[] a = new int[100000];
for (int i = 0; i < p; i++) {
arr1.add(in.nextInt());
arr2.add(in.nextInt());
}
for (int i = 0; i < q; i++) {
ar1.add(in.nextInt());
ar2.add(in.nextInt());
}
for (int i = 0; i < q; i++) {
for (int k = 0; k < p; k++) {
for (int j = l; j <= r; j++) {
if (arr1.get(k) <= ar2.get(i) + j && arr2.get(k) >= ar1.get(i) + j) {
tree.add(j);
}
}
}
}
System.out.println(tree.size());
}
static int prime(int p) {
int t = (int) sqrt(Integer.parseInt(p + ""));
if (t <= 1) {
return 0;
}
for (long i = 2; i <= t; i++) {
if (p % i == 0) {
return 0;
}
}
return 1;
}
static int Obe(long a, long b) {
BigInteger b1 = new BigInteger("" + a);
BigInteger b2 = new BigInteger("" + b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
static int che(int[] w) {
Set<Integer> list = new LinkedHashSet<>();
for (int i = 0; i < w.length; i++) {
list.add(w[i]);
}
return list.size();
}
private static ArrayList<BigInteger> fibCache = new ArrayList<BigInteger>();
static {
fibCache.add(BigInteger.ZERO);
fibCache.add(BigInteger.ONE);
}
public static BigInteger fib(int n) {
if (n >= fibCache.size()) {
fibCache.add(n, fib(n - 1).add(fib(n - 2)));
}
return fibCache.get(n);
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 610a44b3088506f9bfc099daee4073f4 | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static void main(String[] args) {
FastScanner sc =new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt(),m=sc.nextInt(),l=sc.nextInt(),r=sc.nextInt();
Set<Integer> set =new HashSet<>(),time=new HashSet<>();
for(int i=l;i<=r;i++) time.add(i);
for(int i=0;i<n;i++){
int p=sc.nextInt(),q=sc.nextInt();
for(int j=p;j<=q;j++) set.add(j);
}
int cnt=0;
for(int i=0;i<m;i++){
int p=sc.nextInt(),q=sc.nextInt();
for(int j=l;j<=r;j++){
for(int k=p;k<=q;k++){
if(set.contains(j+k) && time.contains(j)){
cnt++;
time.remove(j);
break;
}
if(set.contains(k+j) && time.contains(j)){
cnt++;
time.remove(j);
break;
}
}
}
}
out.println(cnt);
out.close();
}
}
class FastScanner{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st =new StringTokenizer("");
String next(){
if(!st.hasMoreTokens()){
try{
st=new StringTokenizer(br.readLine());
}
catch(Exception e){
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
} | Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | 8661adbec70e0b6b34e76ea3f4a95d9e | train_002.jsonl | 1411218000 | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main
{
public static void main(String[] args)
{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB
{
public void solve(int testNumber, Scanner in, PrintWriter out)
{
int p = in.nextInt();
int q = in.nextInt();
int l = in.nextInt();
int r = in.nextInt();
int[] little_X = new int[2500];
for (int i = 0; i < p; i++)
{
int start = in.nextInt();
int end = in.nextInt();
little_X[start]++;
little_X[end + 1]--;
}
for (int i = 1; i < little_X.length; i++)
{
little_X[i] += little_X[i - 1];
}
int[][] little_Z_times = new int[2500][2];
for (int i = 0; i < q; i++)
{
little_Z_times[i][0] = in.nextInt();
little_Z_times[i][1] = in.nextInt();
}
int moments = 0;
for (int t = l; t <= r; t++)
{
int[] little_Z = new int[2500];
for (int i = 0; i < q; i++)
{
int start = little_Z_times[i][0] + t;
int end = little_Z_times[i][1] + t;
little_Z[start]++;
little_Z[end + 1]--;
}
for (int i = 1; i < little_Z.length; i++)
{
little_Z[i] += little_Z[i - 1];
}
boolean good = false;
for (int i = 0; i < 2500; i++)
{
if (little_X[i] >= 1 && little_Z[i] >= 1)
{
good = true;
break;
}
}
if (good) moments++;
}
out.println(moments);
}
}
}
| Java | ["1 1 0 4\n2 3\n0 1", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17"] | 1 second | ["3", "20"] | null | Java 8 | standard input | [
"implementation"
] | aa77158bf4c0854624ddd89aa8b424b3 | The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j. | 1,300 | Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation. | standard output | |
PASSED | af487b4e8bb91a0703e43eea4706b199 | train_002.jsonl | 1565188500 | The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given an integer $$$n$$$. You have to find a sequence $$$s$$$ consisting of digits $$$\{1, 3, 7\}$$$ such that it has exactly $$$n$$$ subsequences equal to $$$1337$$$.For example, sequence $$$337133377$$$ has $$$6$$$ subsequences equal to $$$1337$$$: $$$337\underline{1}3\underline{3}\underline{3}7\underline{7}$$$ (you can remove the second and fifth characters); $$$337\underline{1}\underline{3}3\underline{3}7\underline{7}$$$ (you can remove the third and fifth characters); $$$337\underline{1}\underline{3}\underline{3}37\underline{7}$$$ (you can remove the fourth and fifth characters); $$$337\underline{1}3\underline{3}\underline{3}\underline{7}7$$$ (you can remove the second and sixth characters); $$$337\underline{1}\underline{3}3\underline{3}\underline{7}7$$$ (you can remove the third and sixth characters); $$$337\underline{1}\underline{3}\underline{3}3\underline{7}7$$$ (you can remove the fourth and sixth characters). Note that the length of the sequence $$$s$$$ must not exceed $$$10^5$$$.You have to answer $$$t$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class PrintA1337String {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(in.nextInt(), in, out);
out.close();
}
static class TaskA {
long mod = (long)(998244353l);
public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
while(testNumber-->0){
long n = in.nextLong();
ArrayList<Long> c = new ArrayList<>();
while(n!=0)
n = cal(c , n);
ArrayList<Character> ans = new ArrayList<>();
ans.add('7');
long last = 0;
for(int i=c.size()-1;i>=0;i--){
long new1 = c.get(i) - last;
last = c.get(i);
for(long j = 0;j<new1;j++)
ans.add('3');
ans.add('1');
}
for(int i=ans.size()-1;i>=0;i--)
out.print(ans.get(i));
out.println();
}
}
public long cal(ArrayList<Long> a , long n){
long x = (1 + (long)Math.sqrt(1+8*n))/2;
a.add(x);
n -= (x*(x-1))/2;
// System.out.println(n);
return n;
}
public void sieve(int a[]){
a[0] = a[1] = 1;
int i;
for(i=2;i*i<=a.length;i++){
if(a[i] != 0)
continue;
a[i] = i;
for(int k = (i)*(i);k<a.length;k+=i){
if(a[k] != 0)
continue;
a[k] = i;
}
}
}
public int [][] matrixExpo(int c[][] , int n){
int a[][] = new int[c.length][c[0].length];
int b[][] = new int[a.length][a[0].length];
for(int i=0;i<c.length;i++)
for(int j=0;j<c[0].length;j++)
a[i][j] = c[i][j];
for(int i=0;i<a.length;i++)
b[i][i] = 1;
while(n!=1){
if(n%2 == 1){
b = matrixMultiply(a , a);
n--;
}
a = matrixMultiply(a , a);
n/=2;
}
return matrixMultiply(a , b);
}
public int [][] matrixMultiply(int a[][] , int b[][]){
int r1 = a.length;
int c1 = a[0].length;
int c2 = b[0].length;
int c[][] = new int[r1][c2];
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
for(int k=0;k<c1;k++)
c[i][j] += a[i][k]*b[k][j];
}
}
return c;
}
public long nCrPFermet(int n , int r , long p){
if(r==0)
return 1l;
long fact[] = new long[n+1];
fact[0] = 1;
for(int i=1;i<=n;i++)
fact[i] = (i*fact[i-1])%p;
long modInverseR = pow(fact[r] , p-2 , 1l , p);
long modInverseNR = pow(fact[n-r] , p-2 , 1l , p);
long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
return w;
}
public long pow(long a , long b , long res , long mod){
if(b==0)
return res;
if(b==1)
return (res*a)%mod;
if(b%2==1){
res *= a;
res %= mod;
b--;
}
// System.out.println(a + " " + b + " " + res);
return pow((a*a)%mod , b/2 , res , mod);
}
public long pow(long a , long b , long res){
if(b == 0)
return res;
if(b==1)
return res*a;
if(b%2==1){
res *= a;
b--;
}
return pow(a*a , b/2 , res);
}
public void swap(int a[] , int p1 , int p2){
int x = a[p1];
a[p1] = a[p2];
a[p2] = x;
}
public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
if (start > end) {
return;
}
int mid = (start + end) / 2;
a.add(mid);
sortedArrayToBST(a, start, mid - 1);
sortedArrayToBST(a, mid + 1, end);
}
class Combine{
long value;
long delete;
Combine(long val , long delete){
this.value = val;
this.delete = delete;
}
}
class Sort2 implements Comparator<Combine>{
public int compare(Combine a , Combine b){
if(a.value > b.value)
return 1;
else if(a.value == b.value && a.delete>b.delete)
return 1;
else if(a.value == b.value && a.delete == b.delete)
return 0;
return -1;
}
}
public int lowerLastBound(ArrayList<Integer> a , int x){
int l = 0;
int r = a.size()-1;
if(a.get(l)>=x)
return -1;
if(a.get(r)<x)
return r;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a.get(mid) == x && a.get(mid-1)<x)
return mid-1;
else if(a.get(mid)>=x)
r = mid-1;
else if(a.get(mid)<x && a.get(mid+1)>=x)
return mid;
else if(a.get(mid)<x && a.get(mid+1)<x)
l = mid+1;
}
return mid;
}
public int upperFirstBound(ArrayList<Integer> a , Integer x){
int l = 0;
int r = a.size()-1;
if(a.get(l)>x)
return l;
if(a.get(r)<=x)
return r+1;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a.get(mid) == x && a.get(mid+1)>x)
return mid+1;
else if(a.get(mid)<=x)
l = mid+1;
else if(a.get(mid)>x && a.get(mid-1)<=x)
return mid;
else if(a.get(mid)>x && a.get(mid-1)>x)
r = mid-1;
}
return mid;
}
public int lowerLastBound(int a[] , int x){
int l = 0;
int r = a.length-1;
if(a[l]>=x)
return -1;
if(a[r]<x)
return r;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a[mid] == x && a[mid-1]<x)
return mid-1;
else if(a[mid]>=x)
r = mid-1;
else if(a[mid]<x && a[mid+1]>=x)
return mid;
else if(a[mid]<x && a[mid+1]<x)
l = mid+1;
}
return mid;
}
public int upperFirstBound(long a[] , long x){
int l = 0;
int r = a.length-1;
if(a[l]>x)
return l;
if(a[r]<=x)
return r+1;
int mid = -1;
while(l<=r){
mid = (l+r)/2;
if(a[mid] == x && a[mid+1]>x)
return mid+1;
else if(a[mid]<=x)
l = mid+1;
else if(a[mid]>x && a[mid-1]<=x)
return mid;
else if(a[mid]>x && a[mid-1]>x)
r = mid-1;
}
return mid;
}
public long log(float number , int base){
return (long) Math.floor((Math.log(number) / Math.log(base)));
}
public long gcd(long a , long b){
if(a<b){
long c = b;
b = a;
a = c;
}
if(b == 0)
return a;
return gcd(b , a%b);
}
public void print2d(long a[][] , PrintWriter out){
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++)
out.print(a[i][j] + " ");
out.println();
}
out.println();
}
public void print1d(int a[] , PrintWriter out){
for(int i=0;i<a.length;i++)
out.print(a[i] + " ");
out.println();
out.println();
}
}
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());
}
}
}
| Java | ["2\n6\n1"] | 1 second | ["113337\n1337"] | null | Java 11 | standard input | [
"combinatorics",
"constructive algorithms",
"math",
"strings"
] | 9aabacc9817722dc11335eccac5d65ac | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of queries. Next $$$t$$$ lines contains a description of queries: the $$$i$$$-th line contains one integer $$$n_i$$$ ($$$1 \le n_i \le 10^9$$$). | 1,900 | For the $$$i$$$-th query print one string $$$s_i$$$ ($$$1 \le |s_i| \le 10^5$$$) consisting of digits $$$\{1, 3, 7\}$$$. String $$$s_i$$$ must have exactly $$$n_i$$$ subsequences $$$1337$$$. If there are multiple such strings, print any of them. | standard output | |
PASSED | 396fb427a9641a89041cfe9cb74a024b | train_002.jsonl | 1565188500 | The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given an integer $$$n$$$. You have to find a sequence $$$s$$$ consisting of digits $$$\{1, 3, 7\}$$$ such that it has exactly $$$n$$$ subsequences equal to $$$1337$$$.For example, sequence $$$337133377$$$ has $$$6$$$ subsequences equal to $$$1337$$$: $$$337\underline{1}3\underline{3}\underline{3}7\underline{7}$$$ (you can remove the second and fifth characters); $$$337\underline{1}\underline{3}3\underline{3}7\underline{7}$$$ (you can remove the third and fifth characters); $$$337\underline{1}\underline{3}\underline{3}37\underline{7}$$$ (you can remove the fourth and fifth characters); $$$337\underline{1}3\underline{3}\underline{3}\underline{7}7$$$ (you can remove the second and sixth characters); $$$337\underline{1}\underline{3}3\underline{3}\underline{7}7$$$ (you can remove the third and sixth characters); $$$337\underline{1}\underline{3}\underline{3}3\underline{7}7$$$ (you can remove the fourth and sixth characters). Note that the length of the sequence $$$s$$$ must not exceed $$$10^5$$$.You have to answer $$$t$$$ independent queries. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class d {
static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
// static Scanner s=new Scanner(System.in);
public static void main(String[] args) throws IOException {
StringBuilder sb = new StringBuilder();
String[] s1=s();
int t=i(s1[0]);
while (t-- > 00) {
String[] s2 = s();
int n = i(s2[0]);
long n1 = 0;
long n3 = 2;
long n7 = 0;
while (true) {
if (n3 * (n3 - 1) / 2 > n) {
n3--;
break;
}
n3++;
}
long rem = n - (n3 * (n3 - 1) / 2);
sb.append("133");
for (int i = 1; i <= rem; i++) {
sb.append("7");
}
{
for (int i = 1; i <= n3 - 2; i++) {
sb.append("3");
}
sb.append("7\n");
}//else sb.append("7\n");}
}
System.out.println(sb.toString());
}
static int count=0;static long ans=1;static int mod;
static HashMap<Integer,Integer> h;
static void dfs(ArrayList<Integer>[] adj,int[] vis,int i,int flag,long[] pot){
h.put(i,flag);
if(adj[i]==null) return;
for(Integer j:adj[i]){
if(h.containsKey(j)){
count+=flag-h.get(j)+1;
ans=(ans*(pot[flag-h.get(j)+1]-2))%mod;
}
else if(vis[j]!=0){
continue;
}
else{
dfs(adj,vis,j,flag+1,pot);
}
}
}
static void bfs(char[][] a,int i,int j,int n,int m){
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static String[] s() throws IOException {
return s.readLine().trim().split("\\s+");
}
static int i(String ss) {
return Integer.parseInt(ss);
}
static long l(String ss) {
return Long.parseLong(ss);
}
}
class Student12 {
int l;long r;
public Student12(int l, long r) {
this.l = l;
this.r = r;
}
public String toString()
{
return this.l+" ";
}
}
class Sortbyroll12 implements Comparator<Student12> {
public int compare(Student12 a, Student12 b){
if(b.r<a.r) return -1;
else if(b.r==a.r) return 0;
return 1;
// return b.r- a.r;
// return (int) a.l-(int) b.l;
/* if(a.r<b.r) return -1;
else if(a.r==b.r){
if(a.r==b.r){
return 0;
}
if(a.r<b.r) return -1;
return 1;}
return 1; */}
} | Java | ["2\n6\n1"] | 1 second | ["113337\n1337"] | null | Java 11 | standard input | [
"combinatorics",
"constructive algorithms",
"math",
"strings"
] | 9aabacc9817722dc11335eccac5d65ac | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of queries. Next $$$t$$$ lines contains a description of queries: the $$$i$$$-th line contains one integer $$$n_i$$$ ($$$1 \le n_i \le 10^9$$$). | 1,900 | For the $$$i$$$-th query print one string $$$s_i$$$ ($$$1 \le |s_i| \le 10^5$$$) consisting of digits $$$\{1, 3, 7\}$$$. String $$$s_i$$$ must have exactly $$$n_i$$$ subsequences $$$1337$$$. If there are multiple such strings, print any of them. | standard output | |
PASSED | 5dbffa9af2731d9982265a9f132a6fae | train_002.jsonl | 1565188500 | The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given an integer $$$n$$$. You have to find a sequence $$$s$$$ consisting of digits $$$\{1, 3, 7\}$$$ such that it has exactly $$$n$$$ subsequences equal to $$$1337$$$.For example, sequence $$$337133377$$$ has $$$6$$$ subsequences equal to $$$1337$$$: $$$337\underline{1}3\underline{3}\underline{3}7\underline{7}$$$ (you can remove the second and fifth characters); $$$337\underline{1}\underline{3}3\underline{3}7\underline{7}$$$ (you can remove the third and fifth characters); $$$337\underline{1}\underline{3}\underline{3}37\underline{7}$$$ (you can remove the fourth and fifth characters); $$$337\underline{1}3\underline{3}\underline{3}\underline{7}7$$$ (you can remove the second and sixth characters); $$$337\underline{1}\underline{3}3\underline{3}\underline{7}7$$$ (you can remove the third and sixth characters); $$$337\underline{1}\underline{3}\underline{3}3\underline{7}7$$$ (you can remove the fourth and sixth characters). Note that the length of the sequence $$$s$$$ must not exceed $$$10^5$$$.You have to answer $$$t$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
public class r70p4{
public static void main(String args[])throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(reader.readLine());
long query[] = new long[t], max = 0;
for(int i=0; i<t; i++){
query[i] = Long.parseLong(reader.readLine());
max = Math.max(query[i], max);
}
TreeMap<Long, Integer> map = new TreeMap<>();
long sum = 1;
int no = 1;
while(sum <= max){
map.put(sum, no+1);
no++;
sum += no;
}
// System.out.println(map);
for(int tc=0; tc<t; tc++){
long q = query[tc];
if(q == 0)
pw.println("7331");
else{
TreeMap<Integer, Integer> ans = new TreeMap<>();
while(q > 0){
long key = map.floorKey(q);
ans.put(map.get(key), ans.getOrDefault(map.get(key), 0)+1);
q -= key;
}
// System.out.println(ans);
pw.print("13");
int count = 1;
for(Map.Entry<Integer, Integer> entry : ans.entrySet()){
int req = entry.getKey(), k = entry.getValue();
while(count < req){
pw.print("3");
count++;
}
while(k-->0)
pw.print("7");
}
pw.println();
}
}
pw.flush();
pw.close();
reader.close();
}
} | Java | ["2\n6\n1"] | 1 second | ["113337\n1337"] | null | Java 11 | standard input | [
"combinatorics",
"constructive algorithms",
"math",
"strings"
] | 9aabacc9817722dc11335eccac5d65ac | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of queries. Next $$$t$$$ lines contains a description of queries: the $$$i$$$-th line contains one integer $$$n_i$$$ ($$$1 \le n_i \le 10^9$$$). | 1,900 | For the $$$i$$$-th query print one string $$$s_i$$$ ($$$1 \le |s_i| \le 10^5$$$) consisting of digits $$$\{1, 3, 7\}$$$. String $$$s_i$$$ must have exactly $$$n_i$$$ subsequences $$$1337$$$. If there are multiple such strings, print any of them. | standard output | |
PASSED | 7332a716fc45f6aee018868f1a216c1e | train_002.jsonl | 1565188500 | The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given an integer $$$n$$$. You have to find a sequence $$$s$$$ consisting of digits $$$\{1, 3, 7\}$$$ such that it has exactly $$$n$$$ subsequences equal to $$$1337$$$.For example, sequence $$$337133377$$$ has $$$6$$$ subsequences equal to $$$1337$$$: $$$337\underline{1}3\underline{3}\underline{3}7\underline{7}$$$ (you can remove the second and fifth characters); $$$337\underline{1}\underline{3}3\underline{3}7\underline{7}$$$ (you can remove the third and fifth characters); $$$337\underline{1}\underline{3}\underline{3}37\underline{7}$$$ (you can remove the fourth and fifth characters); $$$337\underline{1}3\underline{3}\underline{3}\underline{7}7$$$ (you can remove the second and sixth characters); $$$337\underline{1}\underline{3}3\underline{3}\underline{7}7$$$ (you can remove the third and sixth characters); $$$337\underline{1}\underline{3}\underline{3}3\underline{7}7$$$ (you can remove the fourth and sixth characters). Note that the length of the sequence $$$s$$$ must not exceed $$$10^5$$$.You have to answer $$$t$$$ independent queries. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
// 9:18.049
public class cf1202d {
public static void main(String[] args) throws IOException {
int t = ri();
while (t --> 0) {
List<Integer> threes = new ArrayList<>();
int beg = 44721;
int n = ri();
while (n > 0) {
while (n >= (long) beg * (beg + 1) / 2) {
threes.add(beg);
n -= (long) beg * (beg + 1) / 2;
}
--beg;
}
StringBuilder ans = new StringBuilder();
ans.append(73);
int last = 0;
for (int i = threes.size() - 1; i >= 0; --i) {
int x = threes.get(i) - last;
for (int j = 0; j < x; ++j) {
ans.append(3);
}
ans.append(1);
last = threes.get(i);
}
prln(ans.reverse().toString());
}
close();
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __rand = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int) d;}
static int cei(double d) {return (int) ceil(d);}
static long fll(double d) {return (long) d;}
static long cel(double d) {return (long) ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int lcm(int a, int b) {return a * b / gcf(a, b);}
static long lcm(long a, long b) {return a * b / gcf(a, b);}
static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// graph util
static List<List<Integer>> g(int n) {List<List<Integer>> g = new ArrayList<>(); for (int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;}
static List<Set<Integer>> sg(int n) {List<Set<Integer>> g = new ArrayList<>(); for (int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;}
static void c(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);}
static void cto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);}
static void dc(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);}
static void dcto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static double rd() throws IOException {return Double.parseDouble(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
static char[] rcha() throws IOException {return rline().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
static List<List<Integer>> rg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
static void rg(List<List<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
static List<List<Integer>> rdg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
static void rdg(List<List<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
static List<Set<Integer>> rsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
static void rsg(List<Set<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
static List<Set<Integer>> rdsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
static void rdsg(List<Set<Integer>> g, int m) throws IOException {for (int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {prln("yes");}
static void pry() {prln("Yes");}
static void prY() {prln("YES");}
static void prno() {prln("no");}
static void prn() {prln("No");}
static void prN() {prln("NO");}
static void pryesno(boolean b) {prln(b ? "yes" : "no");};
static void pryn(boolean b) {prln(b ? "Yes" : "No");}
static void prYN(boolean b) {prln(b ? "YES" : "NO");}
static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
static void h() {prln("hlfd"); flush();}
static void flush() {__out.flush();}
static void close() {__out.close();}} | Java | ["2\n6\n1"] | 1 second | ["113337\n1337"] | null | Java 11 | standard input | [
"combinatorics",
"constructive algorithms",
"math",
"strings"
] | 9aabacc9817722dc11335eccac5d65ac | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of queries. Next $$$t$$$ lines contains a description of queries: the $$$i$$$-th line contains one integer $$$n_i$$$ ($$$1 \le n_i \le 10^9$$$). | 1,900 | For the $$$i$$$-th query print one string $$$s_i$$$ ($$$1 \le |s_i| \le 10^5$$$) consisting of digits $$$\{1, 3, 7\}$$$. String $$$s_i$$$ must have exactly $$$n_i$$$ subsequences $$$1337$$$. If there are multiple such strings, print any of them. | standard output | |
PASSED | c3fb08182ab206be66decfc2c731c41b | train_002.jsonl | 1565188500 | The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given an integer $$$n$$$. You have to find a sequence $$$s$$$ consisting of digits $$$\{1, 3, 7\}$$$ such that it has exactly $$$n$$$ subsequences equal to $$$1337$$$.For example, sequence $$$337133377$$$ has $$$6$$$ subsequences equal to $$$1337$$$: $$$337\underline{1}3\underline{3}\underline{3}7\underline{7}$$$ (you can remove the second and fifth characters); $$$337\underline{1}\underline{3}3\underline{3}7\underline{7}$$$ (you can remove the third and fifth characters); $$$337\underline{1}\underline{3}\underline{3}37\underline{7}$$$ (you can remove the fourth and fifth characters); $$$337\underline{1}3\underline{3}\underline{3}\underline{7}7$$$ (you can remove the second and sixth characters); $$$337\underline{1}\underline{3}3\underline{3}\underline{7}7$$$ (you can remove the third and sixth characters); $$$337\underline{1}\underline{3}\underline{3}3\underline{7}7$$$ (you can remove the fourth and sixth characters). Note that the length of the sequence $$$s$$$ must not exceed $$$10^5$$$.You have to answer $$$t$$$ independent queries. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.InputMismatchException;
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 {
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Input in = new Input(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DPrintA1337String solver = new DPrintA1337String();
int testCount = Integer.parseInt(in.next());
for(int i = 1; i<=testCount; i++)
solver.solve(i, in, out);
out.close();
}
}
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1<<29);
thread.start();
thread.join();
}
static class DPrintA1337String {
public DPrintA1337String() {
}
public void solve(int testNumber, Input in, PrintWriter pw) {
long n = in.nextInt();
long i;
for(i = 2; i*(i-1) >> 1<=n; i++) ;
i--;
long rem = n-(i*(i-1) >> 1);
pw.print(133);
for(int j = 0; j<rem; j++) {
pw.print(7);
}
for(int j = 0; j<i-2; j++) {
pw.print(3);
}
pw.print(7);
pw.println();
}
}
static class Input {
BufferedReader br;
StringTokenizer st;
public Input(InputStream is) {
br = new BufferedReader(new InputStreamReader(is), 1<<16);
st = null;
}
public boolean hasNext() {
try {
while(st==null||!st.hasMoreTokens()) {
String s = br.readLine();
if(s==null) {
return false;
}
st = new StringTokenizer(s);
}
return true;
}catch(Exception e) {
return false;
}
}
public String next() {
if(!hasNext()) {
throw new InputMismatchException();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["2\n6\n1"] | 1 second | ["113337\n1337"] | null | Java 11 | standard input | [
"combinatorics",
"constructive algorithms",
"math",
"strings"
] | 9aabacc9817722dc11335eccac5d65ac | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of queries. Next $$$t$$$ lines contains a description of queries: the $$$i$$$-th line contains one integer $$$n_i$$$ ($$$1 \le n_i \le 10^9$$$). | 1,900 | For the $$$i$$$-th query print one string $$$s_i$$$ ($$$1 \le |s_i| \le 10^5$$$) consisting of digits $$$\{1, 3, 7\}$$$. String $$$s_i$$$ must have exactly $$$n_i$$$ subsequences $$$1337$$$. If there are multiple such strings, print any of them. | standard output | |
PASSED | 2ffb5732921421772e7475b75da08022 | train_002.jsonl | 1565188500 | The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given an integer $$$n$$$. You have to find a sequence $$$s$$$ consisting of digits $$$\{1, 3, 7\}$$$ such that it has exactly $$$n$$$ subsequences equal to $$$1337$$$.For example, sequence $$$337133377$$$ has $$$6$$$ subsequences equal to $$$1337$$$: $$$337\underline{1}3\underline{3}\underline{3}7\underline{7}$$$ (you can remove the second and fifth characters); $$$337\underline{1}\underline{3}3\underline{3}7\underline{7}$$$ (you can remove the third and fifth characters); $$$337\underline{1}\underline{3}\underline{3}37\underline{7}$$$ (you can remove the fourth and fifth characters); $$$337\underline{1}3\underline{3}\underline{3}\underline{7}7$$$ (you can remove the second and sixth characters); $$$337\underline{1}\underline{3}3\underline{3}\underline{7}7$$$ (you can remove the third and sixth characters); $$$337\underline{1}\underline{3}\underline{3}3\underline{7}7$$$ (you can remove the fourth and sixth characters). Note that the length of the sequence $$$s$$$ must not exceed $$$10^5$$$.You have to answer $$$t$$$ independent queries. | 256 megabytes | import java.util.*;
import java.io.*;
public class D70 {
static ArrayList<Integer> ret;
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while (t > 0) {
int n = sc.nextInt();
ret = new ArrayList<>();
decomposition(n);
Collections.sort(ret);
StringBuilder ans = new StringBuilder();
ans.append(1);
for (int j = 0; j < ret.get(0); j++) ans.append(3);
ans.append(7);
for (int i = 0; i < ret.size() - 1; i++) {
int times = ret.get(i + 1) - ret.get(i);
for (int j = 0; j < times; j++) ans.append(3);
ans.append(7);
}
out.println(ans.toString());
t--;
}
out.close();
}
static void decomposition(int n) {
if (n == 0) return;
if (n == 1) {
ret.add(2);
return;
}
for (int i = (int) Math.sqrt(n) - 1; i <= n; i++) {
if (nC2(i) <= n && nC2(i + 1) > n) {
n -= nC2(i);
ret.add(i); break;
}
}
decomposition(n);
}
static long nC2(int n) {
return ((long) n * (n - 1)) / 2;
}
//-----------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 | ["2\n6\n1"] | 1 second | ["113337\n1337"] | null | Java 11 | standard input | [
"combinatorics",
"constructive algorithms",
"math",
"strings"
] | 9aabacc9817722dc11335eccac5d65ac | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of queries. Next $$$t$$$ lines contains a description of queries: the $$$i$$$-th line contains one integer $$$n_i$$$ ($$$1 \le n_i \le 10^9$$$). | 1,900 | For the $$$i$$$-th query print one string $$$s_i$$$ ($$$1 \le |s_i| \le 10^5$$$) consisting of digits $$$\{1, 3, 7\}$$$. String $$$s_i$$$ must have exactly $$$n_i$$$ subsequences $$$1337$$$. If there are multiple such strings, print any of them. | standard output | |
PASSED | f172e98b662201074571b7ebe7c53350 | train_002.jsonl | 1338132600 | In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible. | 256 megabytes | /**
* Created with IntelliJ IDEA.
* User: Zakhar_Voit
* Date: 28.05.12
* Time: 1:52
*/
import java.io.*;
import java.util.*;
import static java.lang.System.exit;
public class B {
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
int n, ans = 0;
int[] a;
void init() {
}
void read() throws IOException {
n = nextInt();
a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
}
void solve() {
for (int i = 1; i <= 1000; i++) {
boolean t = true;
if (a[0] <= i) {
ans = i;
break;
}
for (int j = 0; j < n; ) {
if (j == n - 1) {
if (a[j] <= i) t = false;
break;
}
if (j < n - 2 && a[j + 2] > i) j = j + 2;
else if (j < n - 1 && a[j + 1] > i) j++;
else {
t = false;
break;
}
}
if (!t) {
ans = i;
break;
}
}
}
void write() {
out.println(ans);
}
public void run() throws IOException {
openIO();
init();
read();
solve();
write();
out.close();
}
static public void main(String[] args) throws IOException {
new Thread(null, new Runnable() {
public void run() {
try {
new B().run();
} catch (Throwable e) {
e.printStackTrace();
exit(999);
}
}
}, "1", 1 << 23).start();
}
public void openIO() throws IOException {
if (new File("input.txt").exists()) {
System.setIn(new FileInputStream("input.txt"));
out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
} else {
out = new PrintWriter(System.out);
}
in = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
} | Java | ["4\n10 3 5 10", "5\n10 2 8 3 5"] | 2 seconds | ["5", "5"] | NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted. | Java 7 | standard input | [
"implementation",
"brute force"
] | d526af933b5afe9abfdf9815e9664144 | The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103). | 1,100 | Print a single number — the sought number of days. | standard output | |
PASSED | f419431f60413af069e14087c2f9a022 | train_002.jsonl | 1338132600 | In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible. | 256 megabytes | import java.util.*;
public class WalkingInTheRain {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = in.nextInt();
}
int time = 0;
while (true) {
if (a[0] - time <= 0 || a[n - 1] - time <= 0)
break;
boolean done = false;
for (int i = 0; i < n - 1; ++i) {
if (a[i] - time <= 0) continue;
if (a[i + 1] - time <= 0 && (i + 2 >= n || a[i + 2] - time <= 0)) {
done = true;
break;
}
}
if (done) break;
++time;
}
System.out.println(time);
in.close();
System.exit(0);
}
} | Java | ["4\n10 3 5 10", "5\n10 2 8 3 5"] | 2 seconds | ["5", "5"] | NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted. | Java 7 | standard input | [
"implementation",
"brute force"
] | d526af933b5afe9abfdf9815e9664144 | The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103). | 1,100 | Print a single number — the sought number of days. | standard output | |
PASSED | cf6322877a553c10f5f2727a238d7ebb | train_002.jsonl | 1338132600 | In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible. | 256 megabytes | import java.io.*;
import java.util.*;
public class TaskB implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer stok;
String nextToken() {
try {
while (stok == null || !stok.hasMoreTokens()) {
stok = new StringTokenizer(in.readLine());
}
} catch (Exception e) {
return null;
}
return stok.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
void solve() throws IOException {
int n = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
for (int ans = 1; ; ans++) {
if (a[0] <= ans || a[n - 1] <= ans) {
out.println(ans);
return;
}
for (int i = 1; i < n - 2; i++) {
if (a[i] <= ans && a[i + 1] <= ans) {
out.println(ans);
return;
}
}
}
}
public void run() {
try {
try {
in = new BufferedReader(new FileReader("taskb.in"));
out = new PrintWriter("taskb.out");
} catch (Exception e1) {
try {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
} catch (Exception e2) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
}
solve();
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws IOException {
try {
Locale.setDefault(Locale.US);
} catch (Exception e) {
}
new Thread(new TaskB()).start();
}
} | Java | ["4\n10 3 5 10", "5\n10 2 8 3 5"] | 2 seconds | ["5", "5"] | NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted. | Java 7 | standard input | [
"implementation",
"brute force"
] | d526af933b5afe9abfdf9815e9664144 | The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103). | 1,100 | Print a single number — the sought number of days. | standard output | |
PASSED | 6bfed82b9b7adb7d37a3ae6e74ded0ba | train_002.jsonl | 1338132600 | In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible. | 256 megabytes | import java.awt.Point;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.math.BigInteger;
import static java.math.BigInteger.*;
import java.io.*;
import java.security.PublicKey;
import java.util.*;
public class Main {
void solve()throws Exception {
int n = nextInt();
int []arr = new int[n];
for (int i = 0; i < n; ++i) {
arr[i] = nextInt();
}
int ans = Math.min(arr[0], arr[n - 1]);
for (int i = 1; i < n - 2; ++i) {
ans = Math.min(ans, Math.max(arr[i], arr[i + 1]));
}
System.out.println(ans);
}
/*
***********************************************************************
*/
PrintWriter writer;
BufferedReader reader;
StringTokenizer stk;
void run()throws Exception {
stk = null;
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
}
int nextInt()throws Exception {
return Integer.parseInt(nextToken());
}
long nextLong()throws Exception {
return Long.parseLong(nextToken());
}
double nextDouble()throws Exception {
return Double.parseDouble(nextToken());
}
String nextString()throws Exception {
return nextToken();
}
String nextLine()throws Exception {
return reader.readLine();
}
String nextToken()throws Exception {
if(stk == null || !stk.hasMoreTokens()) {
stk = new StringTokenizer(nextLine());
return nextToken();
}
return stk.nextToken();
}
public static void main(String[]args) throws Exception {
new Main().run();
}
}
| Java | ["4\n10 3 5 10", "5\n10 2 8 3 5"] | 2 seconds | ["5", "5"] | NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted. | Java 7 | standard input | [
"implementation",
"brute force"
] | d526af933b5afe9abfdf9815e9664144 | The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103). | 1,100 | Print a single number — the sought number of days. | standard output | |
PASSED | 6be8b10d259900e4a100054895841cb2 | train_002.jsonl | 1338132600 | In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible. | 256 megabytes | import java.util.Scanner;
public class B_192_Walking_in_the_Rain {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int[] days=new int[n];
int i,j;
for(i=0;i<n;i++)
days[i]=input.nextInt();
int min=1000;
int tmp;
outer:
for(i=0;i<n;i++){
if(days[i]>=min)
continue;
else{
tmp=days[i];
for(j=0;j<n;j++){
if(j<=n-2&&days[j]-tmp<=0&&days[j+1]-tmp<=0){
min=tmp;
continue outer;
}
else if(days[0]-tmp<=0){
min=tmp;
continue outer;
}
else if(days[n-1]-tmp<=0){
min=tmp;
continue outer;
}
}
}
}
System.out.println(min);
}
}
| Java | ["4\n10 3 5 10", "5\n10 2 8 3 5"] | 2 seconds | ["5", "5"] | NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted. | Java 7 | standard input | [
"implementation",
"brute force"
] | d526af933b5afe9abfdf9815e9664144 | The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103). | 1,100 | Print a single number — the sought number of days. | standard output | |
PASSED | d1405e34627bf09f6f844e75c86ddad8 | train_002.jsonl | 1338132600 | In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible. | 256 megabytes | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author codeKNIGHT
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n=in.nextInt(),i,a[]=new int[n],b[]=new int[n];
for(i=0;i<n;i++)
a[i]=in.nextInt();
int min=Math.min(a[0],a[a.length-1]);
if(n<=3)
out.println(min);
else
{
b[0]=a[0];
b[1]=Math.min(a[0],a[1]);
for(i=2;i<n;i++)
{
b[i]=Math.max(b[i-1],b[i-2]);
b[i]=Math.min(b[i],a[i]);
}
out.println(b[n-1]);
}
}
}
| Java | ["4\n10 3 5 10", "5\n10 2 8 3 5"] | 2 seconds | ["5", "5"] | NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted. | Java 7 | standard input | [
"implementation",
"brute force"
] | d526af933b5afe9abfdf9815e9664144 | The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103). | 1,100 | Print a single number — the sought number of days. | standard output | |
PASSED | 6f8c3c42033d50f96266ec7403d6bbbb | train_002.jsonl | 1338132600 | In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible. | 256 megabytes | import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Iterator;
public class Main {
/** GENERAL CONFIGURATION **/
public static final int TIME_LIMIT = 2;
public static final long MEMORY_LIMIT = 64 * 1024 * 1000;
public static final int INPUT_OUTPUT = InputOutputMode.CONSOLE;
/** EXTENDS CONFIGURATION **/
public static final String INPUT_FILE_NAME = "input.txt";
public static final String OUTPUT_FILE_NAME = "output.txt";
/** INITIALIZATION IO **/
public static final InputReader in = Prepare.input();
public static final OutputWriter out = Prepare.output();
public static void main(String[] args) throws IOException {
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
}
class Task {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int[]tab = new int[n];
for(int i = 0; i < n; i++) {
tab[i] = in.readInt();
}
int days = 0;
while (true) {
days++;
if (!check(tab, days)) {
break;
}
}
out.printLine(days);
}
public boolean check(int[]tab, int days) {
boolean flag = false;
for (int i = 0; i < tab.length; i++) {
if (tab[i] - days <= 0) {
if (flag == true) return false;
else flag = true;
}
else {
flag = false;
}
}
if (tab[0] - days <= 0 || tab[tab.length - 1] - days <= 0) return false;
return true;
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(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();
}
}
class InputOutputMode {
public static final int CONSOLE = 0;
public static final int FILE = 1;
}
class Prepare {
public static InputStream input;
public static OutputStream output;
@SuppressWarnings("unused")
public static InputReader input() {
try {
if (Main.INPUT_OUTPUT == InputOutputMode.CONSOLE) {
input = System.in;
}
else if (Main.INPUT_OUTPUT == InputOutputMode.FILE){
input = new FileInputStream(new File(Main.INPUT_FILE_NAME));
}
}
catch (IOException ex)
{
System.err.println("Some problem with input settings.");
System.exit(0);
}
return new InputReader(input);
}
@SuppressWarnings("unused")
public static OutputWriter output() {
try {
if (Main.INPUT_OUTPUT == InputOutputMode.CONSOLE) {
output = System.out;
}
else if (Main.INPUT_OUTPUT == InputOutputMode.FILE){
output = new FileOutputStream(new File(Main.OUTPUT_FILE_NAME));
}
}
catch (IOException ex) {
System.err.println("Some problem with output settings.");
System.exit(0);
}
return new OutputWriter(output);
}
} | Java | ["4\n10 3 5 10", "5\n10 2 8 3 5"] | 2 seconds | ["5", "5"] | NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted. | Java 7 | standard input | [
"implementation",
"brute force"
] | d526af933b5afe9abfdf9815e9664144 | The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103). | 1,100 | Print a single number — the sought number of days. | standard output | |
PASSED | ba2a5dd792b0e30092fdf751400ac875 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C {
FastScanner in;
PrintWriter out;
public void run() {
//System.out.println((int)'a');
try {
in = new FastScanner(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void solve() throws IOException {
int n = in.nextInt();
int p = in.nextInt();
String s = in.next();
boolean impossible = false;
StringBuilder result = new StringBuilder("01" + s);
int k = 0;
for (k = result.length()-1; k>=2; k--) {
if (k == 2 && result.charAt(k) == 96+p) {
impossible = true;
break;
}
if (result.charAt(k) == 96+p) {
continue;
}
result.setCharAt(k, inc(result.charAt(k)));
while (
(result.charAt(k) == result.charAt(k-2)
|| result.charAt(k) == result.charAt(k-1))
&& result.charAt(k) < 96+p) {
result.setCharAt(k, inc(result.charAt(k)));
}
if ((result.charAt(k) != result.charAt(k-2)
&& result.charAt(k) != result.charAt(k-1))) {
break;
}
}
if (impossible) {
out.println("NO");
return;
}
if (k < result.length()-1) {
result.delete(k+1, result.length());
}
//result.delete(0, 2);
//System.out.println(result);
if (result.length() < n+2) {
for (int i=result.length()-1; result.length()<n+2; i++) {
result.append('a');
k = result.length()-1;
while (
(result.charAt(k) == result.charAt(k-1)
|| result.charAt(k) == result.charAt(k-2))
&& result.charAt(k) < 96+p) {
result.setCharAt(k, inc(result.charAt(k)));
}
if (result.charAt(k) == result.charAt(k-1)) {
impossible = true;
break;
}
/*while (result.charAt(k) == result.charAt(k-2) && result.charAt(k) < 96+p) {
result.setCharAt(k, inc(result.charAt(k)));
}
if (result.charAt(k) == result.charAt(k-2)) {
impossible = true;
break;
}*/
}
}
if (impossible) {
out.println("NO");
} else {
out.println(result.substring(2));
}
}
private char inc(char c) {
return (char)(c+1);
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStreamReader in) {
br = new BufferedReader(in);
}
String nextLine() {
String str = null;
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
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());
}
}
public static void main(String[] arg) {
C o = new C();
o.run();
}
} | Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | 2145f4d5b97a3d4be3c4b786769628c4 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(in, out);
out.close();
}
}
class TaskC {
public void solve(InputReader in, OutputWriter out) {
int n = in.nextInt();
int p = in.nextInt();
String line = in.next();
int tab[] = new int[n];
for (int i = 0; i < n; i++) {
tab[i] = line.charAt(i) - 'a';
}
if(find(tab, p)) {
printTab(tab);
} else {
out.printLine("NO");
}
}
private boolean find(int[] tab, int p) {
int i = tab.length - 1;
while( i < tab.length ) {
if(i < 0) {
return false;
}
tab[i]++;
if(tab[i] == p) {
tab[i] = -1;
i--;
continue;
}
if(i > 0 && tab[i] == tab[i-1]) continue;
if(i > 1 && tab[i] == tab[i-2]) continue;
i++;
}
return true;
}
private void printTab(int[] tab) {
for (int i = 0; i < tab.length; i++) {
System.out.print((char)(tab[i] + 'a'));
}
}
}
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());
}
}
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 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | 4cd37ceebee706687f303dc083fcb408 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.util.Scanner;
public class TestC {
public static void main (String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int p = sc.nextInt();
String s = sc.next();
int k =s.length();
int end_abc = 97+p;
char[] slovo = s.toCharArray();
int position=k-1;
while (position>=0){
if (slovo[position]<end_abc-1){
slovo[position]++;
if (!checkPalindrom(slovo, position))
break;
} else
position--;
}
if (position<0){
System.out.println("NO");
return;
}
for (int i = position+1; i<k; i++){
slovo[i]='a';
while (slovo[i]<end_abc){
if (!checkPalindrom(slovo, i)) break;
else slovo[i]++;
}
if (slovo[i]==end_abc){
System.out.println("NO");
return;
}
}
for (int i=0; i<k; i++){
System.out.print(slovo[i]);
}
}
public static boolean checkPalindrom(char[] array, int tek_position){
if (tek_position-1>=0) {
if (array[tek_position]==array[tek_position-1]) return true;
}
if (tek_position-2>=0) {
if (array[tek_position]==array[tek_position-2]) return true;
}
return false;
}
} | Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | ccae76d02396d50ac9794349efb0ade4 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes |
import java.util.Scanner;
public class p3
{
public static void main(String[] ar4gs)
{
new p3().start();
}
int p;
public void start()
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
p=in.nextInt();
p+=97;
in.nextLine();
String s=in.nextLine();
byte[] arr=s.getBytes();
if(inc(arr,arr.length-1))
{
for(int i=0;i<arr.length;i++)
System.out.print((char)arr[i]);
System.out.println();
}
else
{
System.out.println("NO");
}
}
public boolean inc(byte[] a,int i)
{
// a[i]++;
// if(a[i]>p)
// {
// if(i==0)
// return false;
// a[i]=97;
// if(!inc(a,i-1))
// return false;
// }
while(true)
{
a[i]++;
if(a[i]>=p)
{
if(i==0)
return false;
a[i]=97;
if(!inc(a,i-1))
return false;
else
{
if(!func(a,i))
return inc(a,i);
}
}
if(func(a,i))
return true;
}
}
public boolean func(byte[] a,int i)
{
if(!(i-1<0) && a[i-1]==a[i])
{
return false;
}
if(!(i-2<0) && a[i-2]==a[i])
{
return false;
}
// if(!(i+1>=a.length) && a[i+1]==a[i])
// {
// return false;
// }
// if(!(i+2>=a.length) && a[i+2]==a[i])
// {
// return false;
// }
return true;
}
}
| Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | f605ec7dd0d04cf8d63a213623dfb486 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.util.Scanner;
import java.io.PrintStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
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;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
int n, p;
char[] ar;
private boolean least(int k) {
System.err.println("Least: " + new String(ar) + " " + k);
for (int i = k; i < ar.length; i++) {
boolean charSet = false;
for (char c = 'a'; c < ('a' + p); c++) {
if (ar[i-1] == c) {
continue;
}
if (i >= 2 && ar[i-2] == c) {
continue;
}
ar[i] = c;
charSet = true;
break;
}
if (!charSet) return false;
}
return true;
}
private boolean next(int k) {
char curr = ar[k];
if (curr == 'a' + p - 1) return false;
for (char c = (char) (curr + 1); c < ('a' + p); c++) {
if (k >= 1 && ar[k-1] == c) continue;
if (k >= 2 && ar[k-2] == c) continue;
ar[k] = c;
if (k <= ar.length - 2) {
if (least(k+1)) return true;
} else {
return true;
}
}
return false;
}
public void solve(int testNumber, Scanner in, PrintWriter out) {
n = in.nextInt();
p = in.nextInt();
ar = in.next().toCharArray();
for (int i = ar.length - 1; i >= 0; i--) {
if (next(i)) {
out.println(new String(ar));
System.err.println(new String(ar));
return;
}
}
System.err.println("NO");
out.println("NO");
}
}
| Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | 9612be5a1cac6a474805638f734d5b45 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) throws NumberFormatException,
IOException {
Stdin in = new Stdin();
PrintWriter out = new PrintWriter(System.out);
int n = in.readInt();
int p = in.readInt();
char[] s = in.readNext().toCharArray();
char replace;
boolean find = false;
for (int i = s.length - 1; i >= 0; i--) {
for (int j = s[i] - 'a' + 1; j < p; j++) {
replace = (char) ('a' + j);
if (i - 1 >= 0 && s[i - 1] == replace)
continue;
if (i - 2 >= 0 && s[i - 2] == replace)
continue;
s[i] = replace;
find = true;
for (int k = i + 1; k < s.length; k++) {
s[k] = '0';
for (int h = 0; h < p; h++) {
if (k - 1 >= 0 && s[k - 1] - 'a' == h)
continue;
if (k - 2 >= 0 && s[k - 2] - 'a' == h)
continue;
s[k]=(char)('a'+h);
break;
}
if(s[k]=='0'){
find=false;
break;
}
}
i=-1;
break;
}
}
if(find){
for(char c:s){
out.print(c);
}
}
else
out.println("NO");
out.flush();
out.close();
}
private static class Stdin {
InputStreamReader read;
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
private Stdin() {
read = new InputStreamReader(System.in);
br = new BufferedReader(read);
}
private String readNext() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
private int readInt() throws IOException, NumberFormatException {
return Integer.parseInt(readNext());
}
private long readLong() throws IOException, NumberFormatException {
return Long.parseLong(readNext());
}
}
}
| Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | aa51a725319c119098547cd7d829b9ff | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Main {
MyScanner sc = new MyScanner();
Scanner sc2 = new Scanner(System.in);
final int MOD = 1000000007;
int[] dx = { 1, -1, 1, -1 };
int[] dy = { 1, 1, -1, -1 };
void run() {
int n = sc.nextInt();
int p = sc.nextInt();
char max = (char) ('a' + p);
char[] c = sc.next().toCharArray();
for (int i = n - 1; i >= 0; i--) {
char add = (char) (c[i] + 1);
for (char s = add; s < max; s++) {
if (i == 0 || palindromeCheck(c, s, i)) {
c[i] = s;
for (int j = i + 1; j < n; j++) {
for (char t = 'a'; t < max; t++) {
if (palindromeCheck(c, t, j)) {
c[j] = t;
break;
}
}
}
System.out.println(String.valueOf(c));
return;
}
}
}
System.out.println("NO");
}
boolean palindromeCheck(char[] c, char next, int index) {
if (index > 1) {
return c[index - 1] != next && c[index - 2] != next;
} else {
return c[index - 1] != next;
}
}
public static void main(String[] args) {
new Main().run();
}
void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void debug2(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
void debug2(long[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
boolean inner(int h, int w, int limH, int limW) {
return 0 <= h && h < limH && 0 <= w && w < limW;
}
void swap(int[] x, int a, int b) {
int tmp = x[a];
x[a] = x[b];
x[b] = tmp;
}
// find minimum i (a[i] >= border)
int lower_bound(int a[], int border) {
int l = -1;
int r = a.length;
while (r - l > 1) {
int mid = (l + r) / 2;
if (border <= a[mid]) {
r = mid;
} else {
l = mid;
}
}
// r = l + 1
return r;
}
boolean palindrome(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
class MyScanner {
int nextInt() {
try {
int c = System.in.read();
while (c != '-' && (c < '0' || '9' < c))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
int[] nextIntArray(int n) {
int[] in = new int[n];
for (int i = 0; i < n; i++) {
in[i] = nextInt();
}
return in;
}
int[][] nextInt2dArray(int n, int m) {
int[][] in = new int[n][m];
for (int i = 0; i < n; i++) {
in[i] = nextIntArray(m);
}
return in;
}
double[] nextDoubleArray(int n) {
double[] in = new double[n];
for (int i = 0; i < n; i++) {
in[i] = nextDouble();
}
return in;
}
long[] nextLongArray(int n) {
long[] in = new long[n];
for (int i = 0; i < n; i++) {
in[i] = nextLong();
}
return in;
}
char[][] nextCharField(int n, int m) {
char[][] in = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
in[i][j] = s.charAt(j);
}
}
return in;
}
}
}
| Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | 4d9b03867bc2aa4ce1a84dba79fdbb1d | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class MainC {
MyScanner sc = new MyScanner();
Scanner sc2 = new Scanner(System.in);
final int MOD = 1000000007;
int[] dx = { 1, 0, 0, -1 };
int[] dy = { 0, 1, -1, 0 };
void run() {
int n = sc.nextInt();
int p = sc.nextInt();
char max = (char) ('a' + p);
char[] c = sc.next().toCharArray();
for (int i = n - 1; i >= 0; i--) {
char add = (char) (c[i] + 1);
for (char u = add; u < max; u++) {
if (palindromeCheck(c, u, i)) {
c[i] = u;
for (int j = i + 1; j < n; j++) {
for (char uu = 'a'; uu < max; uu++) {
if (palindromeCheck(c, uu, j)) {
c[j] = uu;
break;
}
}
}
System.out.println(String.valueOf(c));
return;
}
}
}
System.out.println("NO");
}
boolean palindromeCheck(char[] c, char add, int index) {
if (index > 1) {
return c[index - 2] != add && c[index - 1] != add;
} else if (index > 0) {
return c[index - 1] != add;
} else {
return true;
}
}
public static void main(String[] args) {
new MainC().run();
}
void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void debug2(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
class MyScanner {
int nextInt() {
try {
int c = System.in.read();
while (c != '-' && (c < '0' || '9' < c))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
}
}
| Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | d4dbf5a18b9dc1ca634b205e9177e746 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Main {
MyScanner sc = new MyScanner();
Scanner sc2 = new Scanner(System.in);
final int MOD = 1000000007;
int[] dx = { 1, -1, 1, -1 };
int[] dy = { 1, 1, -1, -1 };
void run() {
int n = sc.nextInt();
int p = sc.nextInt();
char max = (char) ('a' + p);
char[] c = sc.next().toCharArray();
for (int i = n - 1; i >= 0; i--) {
char add = (char) (c[i] + 1);
for (char s = add; s < max; s++) {
if (palindromeCheck(c, s, i)) {
c[i] = s;
boolean end = false;
for (int j = i + 1; j < n; j++) {
for (char t = 'a'; t < max; t++) {
if (palindromeCheck(c, t, j)) {
c[j] = t;
if (j == n - 1) {
end = true;
}
break;
}
}
}
if (end || i == n - 1) {
System.out.println(String.valueOf(c));
return;
}
}
}
}
System.out.println("NO");
}
boolean palindromeCheck(char[] c, char next, int index) {
if (index > 1) {
return c[index - 1] != next && c[index - 2] != next;
} else if (index > 0) {
return c[index - 1] != next;
} else {
return true;
}
}
public static void main(String[] args) {
new Main().run();
}
void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void debug2(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
void debug2(long[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
boolean inner(int h, int w, int limH, int limW) {
return 0 <= h && h < limH && 0 <= w && w < limW;
}
void swap(int[] x, int a, int b) {
int tmp = x[a];
x[a] = x[b];
x[b] = tmp;
}
// find minimum i (a[i] >= border)
int lower_bound(int a[], int border) {
int l = -1;
int r = a.length;
while (r - l > 1) {
int mid = (l + r) / 2;
if (border <= a[mid]) {
r = mid;
} else {
l = mid;
}
}
// r = l + 1
return r;
}
boolean palindrome(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
class MyScanner {
int nextInt() {
try {
int c = System.in.read();
while (c != '-' && (c < '0' || '9' < c))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
int[] nextIntArray(int n) {
int[] in = new int[n];
for (int i = 0; i < n; i++) {
in[i] = nextInt();
}
return in;
}
int[][] nextInt2dArray(int n, int m) {
int[][] in = new int[n][m];
for (int i = 0; i < n; i++) {
in[i] = nextIntArray(m);
}
return in;
}
double[] nextDoubleArray(int n) {
double[] in = new double[n];
for (int i = 0; i < n; i++) {
in[i] = nextDouble();
}
return in;
}
long[] nextLongArray(int n) {
long[] in = new long[n];
for (int i = 0; i < n; i++) {
in[i] = nextLong();
}
return in;
}
char[][] nextCharField(int n, int m) {
char[][] in = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
in[i][j] = s.charAt(j);
}
}
return in;
}
}
}
| Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | 42b63dc57095ae9fb7ef08229804b614 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Main {
MyScanner sc = new MyScanner();
Scanner sc2 = new Scanner(System.in);
final int MOD = 1000000007;
int[] dx = { 1, -1, 1, -1 };
int[] dy = { 1, 1, -1, -1 };
void run() {
int n = sc.nextInt();
int p = sc.nextInt();
char max = (char) ('a' + p);
char[] c = sc.next().toCharArray();
for (int i = n - 1; i >= 0; i--) {
char add = (char) (c[i] + 1);
for (char s = add; s < max; s++) {
if (palindromeCheck(c, s, i)) {
c[i] = s;
// boolean end = false;
for (int j = i + 1; j < n; j++) {
for (char t = 'a'; t < max; t++) {
if (palindromeCheck(c, t, j)) {
c[j] = t;
if (j == n - 1) {
// end = true;
}
break;
}
}
}
System.out.println(String.valueOf(c));
return;
}
}
}
System.out.println("NO");
}
boolean palindromeCheck(char[] c, char next, int index) {
if (index > 1) {
return c[index - 1] != next && c[index - 2] != next;
} else if (index > 0) {
return c[index - 1] != next;
} else {
return true;
}
}
public static void main(String[] args) {
new Main().run();
}
void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void debug2(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
void debug2(long[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
boolean inner(int h, int w, int limH, int limW) {
return 0 <= h && h < limH && 0 <= w && w < limW;
}
void swap(int[] x, int a, int b) {
int tmp = x[a];
x[a] = x[b];
x[b] = tmp;
}
// find minimum i (a[i] >= border)
int lower_bound(int a[], int border) {
int l = -1;
int r = a.length;
while (r - l > 1) {
int mid = (l + r) / 2;
if (border <= a[mid]) {
r = mid;
} else {
l = mid;
}
}
// r = l + 1
return r;
}
boolean palindrome(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
class MyScanner {
int nextInt() {
try {
int c = System.in.read();
while (c != '-' && (c < '0' || '9' < c))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
int[] nextIntArray(int n) {
int[] in = new int[n];
for (int i = 0; i < n; i++) {
in[i] = nextInt();
}
return in;
}
int[][] nextInt2dArray(int n, int m) {
int[][] in = new int[n][m];
for (int i = 0; i < n; i++) {
in[i] = nextIntArray(m);
}
return in;
}
double[] nextDoubleArray(int n) {
double[] in = new double[n];
for (int i = 0; i < n; i++) {
in[i] = nextDouble();
}
return in;
}
long[] nextLongArray(int n) {
long[] in = new long[n];
for (int i = 0; i < n; i++) {
in[i] = nextLong();
}
return in;
}
char[][] nextCharField(int n, int m) {
char[][] in = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
in[i][j] = s.charAt(j);
}
}
return in;
}
}
}
| Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | 5b36e14aa052555b7b252b36eda327f6 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.io.BufferedInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
public class Main {
//public static final int C = 1000000007;
//static BigDecimal map[][];
//static int N;
//static int M;
public static void main(String[] args) {
//StringBuilder sb = new StringBuilder();
BufferedInputStream bs = new BufferedInputStream(System.in);
Scanner sc = new Scanner(bs);
int n = sc.nextInt();
int p = sc.nextInt();
String str = sc.next();
//ArrayList<Integer> al = new ArrayList<Integer>();
/*int a[] = new int[n];
for (int i=0; i < n; i++) {
a[i] = sc.nextInt();
}*/
//HashMap<Integer, ArrayList<Integer>> hm = new HashMap<Integer, ArrayList<Integer>>();
String ans = "NO";
char c[] = str.toCharArray();
c = rep(c, p);
if(c.length != 0) {
ans = new String(c);
}
System.out.println(ans);
}
static char[] rep(char[] c, int p) {
c = c.clone();
LOOP:while(true) {
if (c[c.length-1]+1 <= 'a'+(p-1)) {
c[c.length-1]++;
//System.out.println(c);
LOOP2:for (int i=c.length-2; i >= 0; i--) {
for (int t=0; t < (c.length - i)/2;t++) {
//System.out.println("chaek:" + (i+t)+c[i + t] +":"+ (c.length-1-t)+c[c.length-1-t]);
if (c[i + t] != c[c.length-1-t]) continue LOOP2;
}
continue LOOP;
}
return c;
}else {
String str = new String(c);
str = str.substring(0, str.length()-1);
if (str.isEmpty()) return str.toCharArray();
c = rep(str.toCharArray(),p);
if (c.length == 0) return c;
str = new String(c);
str = str.concat((char)('a'-1)+"");
c = str.toCharArray();
continue LOOP;
}
}
}
}
| Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | 95c066c2e82269c7ca145ec35825abc0 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Solution{
///////////////////////////////////////////////////////////////////////////
static class FastScanner{
BufferedReader s;
StringTokenizer st;
public FastScanner(){
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(System.in));
}
public FastScanner(File f) throws FileNotFoundException{
st = new StringTokenizer("");
s = new BufferedReader (new FileReader(f));
}
public int nextInt() throws IOException{
if(st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
public double nextDouble() throws IOException{
if(st.hasMoreTokens())
return Double.parseDouble(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextDouble();
}
}
public long nextLong() throws IOException{
if(st.hasMoreTokens())
return Long.parseLong(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextLong();
}
}
public String nextString() throws IOException{
if(st.hasMoreTokens())
return st.nextToken();
else{
st = new StringTokenizer(s.readLine());
return nextString();
}
}
public String readLine() throws IOException{
return s.readLine();
}
public void close() throws IOException{
s.close();
}
}
////////////////////////////////////////////////////////////////////
// Number Theory
long pow(long a,long b,long mod){
long x = 1; long y = a;
while(b > 0){
if(b % 2 == 1){
x = (x*y);
x %= mod;
}
y = (y*y);
y %= mod;
b /= 2;
}
return x;
}
int divisor(long x,long[] a){
long limit = x;
int numberOfDivisors = 0;
for (int i=1; i < limit; ++i) {
if (x % i == 0) {
limit = x / i;
if (limit != i) {
numberOfDivisors++;
}
numberOfDivisors++;
}
}
return numberOfDivisors;
}
void findSubsets(int array[]){
long numOfSubsets = 1 << array.length;
for(int i = 0; i < numOfSubsets; i++){
int pos = array.length - 1;
int bitmask = i;
while(bitmask > 0){
if((bitmask & 1) == 1)
ww.print(array[pos]+" ");
bitmask >>= 1;
pos--;
}
ww.println();
}
}
public static int gcd(int a, int b){
return b == 0 ? a : gcd(b,a%b);
}
public static int lcm(int a,int b, int c){
return lcm(lcm(a,b),c);
}
public static int lcm(int a, int b){
return a*b/gcd(a,b);
}
////////////////////////////////////////////////////////////////////
// private static FastScanner s = new FastScanner(new File("input.txt"));
// private static PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
private static FastScanner s = new FastScanner();
private static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));
// private static Scanner s = new Scanner(System.in);
@SuppressWarnings("unused")
private static int[][] states = { {-1,0} , {1,0} , {0,-1} , {0,1} };
////////////////////////////////////////////////////////////////////
public static void main(String[] args) throws IOException{
new Solution().solve();
}
////////////////////////////////////////////////////////////////////
int n;
int p;
char[] arr;
char[] ans;
boolean dfs(int i, boolean set){
if(i == n) return true;
boolean status = false;
for(char c = set ? 'a' : arr[i] ; c - 'a' < p ; c++){
ans[i] = c;
if(i > 0 && ans[i] == ans[i-1]) continue;
if(i > 1 && ans[i] == ans[i-2]) continue;
status = dfs(i+1,set);
if(status){
return true;
}
set = true;
}
return false;
}
void solve() throws IOException{
n = s.nextInt();
p = s.nextInt();
arr = s.nextString().toCharArray();
ans = new char[n];
arr[n-1]++;
if(dfs(0,false)){
for(int i=0;i<n;i++)
ww.print(ans[i]);
ww.println();
}else ww.println("NO");
s.close();
ww.close();
}
} | Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | cef607f80cef608a4e58197370e67f5e | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.util.Scanner;
import java.io.PrintStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author about@manhquynh.net
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int p = in.nextInt();
char[] word = in.next().toCharArray();
boolean found = false;
for (int i = n - 1; i >= 0 && !found; --i) {
for (int j = word[i] + 1; j < p + 'a'; ++j) {
if (i > 0 && j == word[i - 1]) continue;
if (i > 1 && j == word[i - 2]) continue;
word[i] = (char)j;
for (int t = i + 1; t < n; ++t)
for (int c = 'a'; c < p + 'a'; ++c) {
if (t > 0 && c == word[t - 1]) continue;
if (t > 1 && c == word[t - 2]) continue;
word[t] = (char)c;
break;
}
found = true;
break;
}
}
if (found)
System.out.print(word);
else
System.out.print("NO");
}
}
| Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | 082bacf8934afa814d1e2a47d90058e3 | train_002.jsonl | 1410103800 | Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist. | 256 megabytes | import java.util.Scanner;
public class NoPalindromes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int p = sc.nextInt();
String s = sc.next();
char[] chars = s.toCharArray();
int[] nums = new int[chars.length];
for (int i = 0; i < chars.length; i++) {
nums[i] = chars[i] - 'a';
}
int foundPos = -1;
for (int i = nums.length - 1; i >= 0; i--) {
//try next tolerable by change char at i position
for (int j = nums[i] + 1; j < p; j++) {
if (i > 0 && nums[i - 1] == j || i > 1 && nums[i - 2] == j) {
continue;
}
foundPos = i;
nums[i] = j;
break;
}
if (foundPos >= 0) {
break;
}
}
if (foundPos == -1) {
System.out.println("NO");
} else {
//build next tolerable
for (int i = foundPos + 1; i < nums.length; i++) {
int j = 0;
while (j < p) {
if (i > 0 && nums[i - 1] == j || i > 1 && nums[i - 2] == j) {
j++;
} else {
break;
}
}
nums[i] = j;
}
StringBuilder sb = new StringBuilder();
for (int num : nums) {
sb.append(Character.toChars(num + 'a'));
}
System.out.println(sb.toString());
}
}
} | Java | ["3 3\ncba", "3 4\ncba", "4 4\nabcd"] | 1 second | ["NO", "cbd", "abda"] | NoteString s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.A palindrome is a string that reads the same forward or reversed. | Java 7 | standard input | [
"brute force"
] | 788ae500235ca7b7a7cd320f745d1070 | The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition). | 1,700 | If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). | standard output | |
PASSED | 5cdede82ce4f54ff6ed572c7e7916aff | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution{
///////////////////////////////////////////////////////////////////////////
static class FastScanner{
BufferedReader s;
StringTokenizer st;
public FastScanner(InputStream InputStream){
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(InputStream));
}
public FastScanner(File f) throws FileNotFoundException{
st = new StringTokenizer("");
s = new BufferedReader (new FileReader(f));
}
public int nextInt() throws IOException{
if(st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
public BigInteger big() throws IOException{
if(st.hasMoreTokens())
return new BigInteger(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return big();
}
}
public double nextDouble() throws IOException{
if(st.hasMoreTokens())
return Double.parseDouble(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextDouble();
}
}
public long nextLong() throws IOException{
if(st.hasMoreTokens())
return Long.parseLong(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextLong();
}
}
public String nextString() throws IOException{
if(st.hasMoreTokens())
return st.nextToken();
else{
st = new StringTokenizer(s.readLine());
return nextString();
}
}
public String readLine() throws IOException{
return s.readLine();
}
public void close() throws IOException{
s.close();
}
}
////////////////////////////////////////////////////////////////////
// Number Theory
long pow(long a,long b,long mod){
long x = 1; long y = a;
while(b > 0){
if(b % 2 == 1){
x = (x*y);
x %= mod;
}
y = (y*y);
y %= mod;
b /= 2;
}
return x;
}
int divisor(long x,long[] a){
long limit = x;
int numberOfDivisors = 0;
for (int i=1; i < limit; ++i) {
if (x % i == 0) {
limit = x / i;
if (limit != i) {
numberOfDivisors++;
}
numberOfDivisors++;
}
}
return numberOfDivisors;
}
void findSubsets(int array[]){
long numOfSubsets = 1 << array.length;
for(int i = 0; i < numOfSubsets; i++){
@SuppressWarnings("unused")
int pos = array.length - 1;
int bitmask = i;
while(bitmask > 0){
if((bitmask & 1) == 1)
// ww.print(array[pos]+" ");
bitmask >>= 1;
pos--;
}
// ww.println();
}
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b,a%b);
}
public static long lcm(int a,int b, int c){
return lcm(lcm(a,b),c);
}
public static long lcm(long a, long b){
return (a*b/gcd(a,b));
}
public static long invl(long a, long mod) {
long b = mod;
long p = 1, q = 0;
while (b > 0) {
long c = a / b;
long d;
d = a;
a = b;
b = d % b;
d = p;
p = q;
q = d - c * q;
}
return p < 0 ? p + mod : p;
}
////////////////////////////////////////////////////////////////////
// FastScanner s = new FastScanner(new File("a.pdf"));
// PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
static InputStream inputStream = System.in;
static FastScanner s = new FastScanner(inputStream);
static OutputStream outputStream = System.out;
static PrintWriter ww = new PrintWriter(new OutputStreamWriter(outputStream));
// private static Scanner s = new Scanner(System.in);
@SuppressWarnings("unused")
private static int[][] states = { {-1,0} , {1,0} , {0,-1} , {0,1} };
////////////////////////////////////////////////////////////////////
public static void main(String[] args) throws Exception{
new Solution().solve();
s.close();
ww.close();
}
////////////////////////////////////////////////////////////////////
void solve() throws Exception{
long a = s.nextLong();
long b = s.nextLong();
long n = s.nextLong();
int max = (int)10e5+100;
long[] val = new long[max];
long[] sum = new long[max];
val[1] = a;
sum[1] = a;
for(int i=2;i<max;i++){
val[i] = a + (i-1)*b;
sum[i] += sum[i-1] + val[i];
}
// for(int i=1;i<=n;i++) System.out.println(val[i]+" "+sum[i]);
while(n-->0){
int l = s.nextInt();
int t = s.nextInt();
int m = s.nextInt();
if(val[l] > t){
ww.println(-1);
continue;
}
if(val[l] == t){
ww.println(l);
continue;
}
long tot = (long)t*m;
int low = l;
int high = max-1;
long maxi = 0;
while(low+1 < high){
// System.out.println(low+" "+high);
int mid = (low+high)/2;
maxi = sum[mid] - sum[l-1];
// System.out.println(maxi+" "+low+" "+mid+" "+high);
if(maxi <= tot && val[mid] <= t)
low = mid;
else
high = mid;
}
maxi = sum[high] - sum[l-1];
if(val[high] <= t && maxi <= tot) ww.println(high);
else ww.println(low);
// System.out.println(high+" "+low);
}
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | d24e0f8247667ae3d73cafe70c7cc5bb | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution{
///////////////////////////////////////////////////////////////////////////
static class FastScanner{
BufferedReader s;
StringTokenizer st;
public FastScanner(InputStream InputStream){
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(InputStream));
}
public FastScanner(File f) throws FileNotFoundException{
st = new StringTokenizer("");
s = new BufferedReader (new FileReader(f));
}
public int nextInt() throws IOException{
if(st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
public BigInteger big() throws IOException{
if(st.hasMoreTokens())
return new BigInteger(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return big();
}
}
public double nextDouble() throws IOException{
if(st.hasMoreTokens())
return Double.parseDouble(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextDouble();
}
}
public long nextLong() throws IOException{
if(st.hasMoreTokens())
return Long.parseLong(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextLong();
}
}
public String nextString() throws IOException{
if(st.hasMoreTokens())
return st.nextToken();
else{
st = new StringTokenizer(s.readLine());
return nextString();
}
}
public String readLine() throws IOException{
return s.readLine();
}
public void close() throws IOException{
s.close();
}
}
////////////////////////////////////////////////////////////////////
// Number Theory
long pow(long a,long b,long mod){
long x = 1; long y = a;
while(b > 0){
if(b % 2 == 1){
x = (x*y);
x %= mod;
}
y = (y*y);
y %= mod;
b /= 2;
}
return x;
}
int divisor(long x,long[] a){
long limit = x;
int numberOfDivisors = 0;
for (int i=1; i < limit; ++i) {
if (x % i == 0) {
limit = x / i;
if (limit != i) {
numberOfDivisors++;
}
numberOfDivisors++;
}
}
return numberOfDivisors;
}
void findSubsets(int array[]){
long numOfSubsets = 1 << array.length;
for(int i = 0; i < numOfSubsets; i++){
@SuppressWarnings("unused")
int pos = array.length - 1;
int bitmask = i;
while(bitmask > 0){
if((bitmask & 1) == 1)
// ww.print(array[pos]+" ");
bitmask >>= 1;
pos--;
}
// ww.println();
}
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b,a%b);
}
public static long lcm(int a,int b, int c){
return lcm(lcm(a,b),c);
}
public static long lcm(long a, long b){
return (a*b/gcd(a,b));
}
public static long invl(long a, long mod) {
long b = mod;
long p = 1, q = 0;
while (b > 0) {
long c = a / b;
long d;
d = a;
a = b;
b = d % b;
d = p;
p = q;
q = d - c * q;
}
return p < 0 ? p + mod : p;
}
////////////////////////////////////////////////////////////////////
// FastScanner s = new FastScanner(new File("a.pdf"));
// PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
static InputStream inputStream = System.in;
static FastScanner s = new FastScanner(inputStream);
static OutputStream outputStream = System.out;
static PrintWriter ww = new PrintWriter(new OutputStreamWriter(outputStream));
// private static Scanner s = new Scanner(System.in);
@SuppressWarnings("unused")
private static int[][] states = { {-1,0} , {1,0} , {0,-1} , {0,1} };
////////////////////////////////////////////////////////////////////
public static void main(String[] args) throws Exception{
new Solution().solve();
s.close();
ww.close();
}
////////////////////////////////////////////////////////////////////
void solve() throws Exception{
long a = s.nextLong();
long b = s.nextLong();
long n = s.nextLong();
int max = (int)10e6+100;
long[] val = new long[max];
long[] sum = new long[max];
val[1] = a;
sum[1] = a;
for(int i=2;i<max;i++){
val[i] = a + (i-1)*b;
sum[i] += sum[i-1] + val[i];
}
// for(int i=1;i<=n;i++) System.out.println(val[i]+" "+sum[i]);
while(n-->0){
int l = s.nextInt();
int t = s.nextInt();
int m = s.nextInt();
if(val[l] > t){
ww.println(-1);
continue;
}
if(val[l] == t){
ww.println(l);
continue;
}
long tot = (long)t*m;
int low = l;
int high = max-1;
long maxi = 0;
while(low+1 < high){
// System.out.println(low+" "+high);
int mid = (low+high)/2;
maxi = sum[mid] - sum[l-1];
// System.out.println(maxi+" "+low+" "+mid+" "+high);
if(maxi <= tot && val[mid] <= t)
low = mid;
else
high = mid;
}
maxi = sum[high] - sum[l-1];
if(val[high] <= t && maxi <= tot) ww.println(high);
else ww.println(low);
// System.out.println(high+" "+low);
}
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 35c8babdd315aeef1c18bb461e2c035b | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
static BufferedReader in;
static PrintWriter out;
public static long getAi(long A, long B, long index) {
return A + (index-1)*B;
}
public static long getSum(long A, long B, long end) {
return end*(getAi(A,B,1)+getAi(A,B,end))/2;
}
public static long getSum(long A, long B, long start, long end) {
if (start==1) return getSum(A,B,end);
else return getSum(A,B,end) - getSum(A,B,start-1);
}
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
String[] tok = rst();
long A = Long.parseLong(tok[0]);
long B = Long.parseLong(tok[1]);
long n = Long.parseLong(tok[2]);
for (int q=0;q<n;q++) {
tok = rst();
long l = Long.parseLong(tok[0]);
long t = Long.parseLong(tok[1]);
long m = Long.parseLong(tok[2]);
long ans = 0;
if (getAi(A,B,l)>t) {
out.println(-1);
} else {
long lo = l, hi = (t-A)/B + 2, mid;
while (getAi(A,B,hi)>t) hi--;
long sum;
while (hi-lo>1) {
mid = (hi+lo) >>> 1;
sum = getSum(A,B,l,mid);
if (sum>t*m) {
hi = mid-1;
} else if (sum==t*m) {
lo = hi = mid;
break;
} else {
lo = mid;
}
}
sum = getSum(A,B,l,hi);
if (sum<=t*m) ans = hi;
else {
sum = getSum(A,B,l,lo);
if (sum<=t*m) ans = lo;
else ans = -1;
}
out.println(ans);
}
}
in.close();
out.close();
}
public static int ri() throws IOException {
return Integer.parseInt(in.readLine());
}
public static long rl() throws IOException {
return Long.parseLong(in.readLine());
}
public static String rs() throws IOException {
return in.readLine();
}
public static String[] rst() throws IOException {
return in.readLine().split(" ");
}
public static int[] rit() throws IOException {
String[] tok = in.readLine().split(" ");
int[] res = new int[tok.length];
for (int i = 0; i < tok.length; i++) {
res[i] = Integer.parseInt(tok[i]);
}
return res;
}
public static long[] rlt() throws IOException {
String[] tok = in.readLine().split(" ");
long[] res = new long[tok.length];
for (int i = 0; i < tok.length; i++) {
res[i] = Long.parseLong(tok[i]);
}
return res;
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 15691596fc6ba7fb8060e5b4d3f7a8a4 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | //535C
import java.util.Scanner;
public class TavasAndKarafs {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int n = sc.nextInt();
for (int _ = 0; _ < n; _++) {
int l = sc.nextInt();
int t = sc.nextInt();
int m = sc.nextInt();
int i = l-1;
int j = (t-a)/b+2;
for (int k = (i+j+1)/2; i < j-1; k = (i+j+1)/2) {
if (edible(a,b,l,k,t,m)) {
i = k;
}
else {
j = k;
}
}
if (i == l-1)
System.out.println(-1);
else
System.out.println(i);
}
}
private static boolean edible(int a, int b, long l, long r, int t, int m) {
if (m >= r - l + 1) {
return t >= a + b * (r - 1);
}
long total = a * (r - l + 1) + b * (r * (r - 1) - (l - 1) * (l - 2)) / 2L;
total = (total + m - 1) / m;
if (total <= a + b * (r - 1)) {
total = a + b * (r - 1);
}
return t >= total;
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 4a69eb97e61a963252f799ec7f797873 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | //package codeforces.cfr299div2;
import java.util.Scanner;
/**
* Created by bistrashkin on 4/14/15.
*/
public class C {
private static boolean canEat(long A, long B, long l, long m, long t, long r) {
long count = r - l + 1;
long first = A + B * (l - 1);
long last = A + B * (r - 1);
return last <= t && ((first + last) * count) / 2 <= m * t;
}
private static long binarySearch(long A, long B, long l, long m, long t) {
long maxR = (t - A) / B + 1;
long minR = l;
if (!canEat(A, B, l, m, t, minR)) {
return -1;
}
while (maxR - minR > 1) {
long midR = (minR + maxR) / 2;
if (canEat(A, B, l, m, t, midR)) {
minR = midR;
} else {
maxR = midR - 1;
}
}
return canEat(A, B, l, m, t, maxR) ? maxR : minR;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long A = in.nextLong();
long B = in.nextLong();
long n = in.nextLong();
for (long i = 0; i < n; i++) {
long l = in.nextLong();
long t = in.nextLong();
long m = in.nextLong();
System.out.println(binarySearch(A, B, l, m, t));
}
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | c04deac2869afeaf44fba74531ff5657 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.*;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class C{
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
C solver = new C();
solver.solve(1, in, out);
out.close();
}
private long binsearch(long sum, long l, long r) {
if (l > r) return r;
long mid = (l + r ) /2;
long block = (el(l) + el(mid)) * (mid - l + 1)/2;
if ( block - sum <= 0) {
return binsearch(sum - block, mid + 1, r);
} else {
if (l == r)
return l - 1;
return binsearch(sum, l, mid);
}
}
private long el(long i) {
return A + B * (i - 1);
}
long A;
long B;
public void solve(int testNumber, InputReader in, PrintWriter out) {
A = in.nextInt();
B = in.nextInt();
long n = in.nextInt();
for (int ci = 0; ci < n; ci++) {
long l = in.nextInt();
long t = in.nextInt();
long m = in.nextInt();
long r = (t - A) / B + 1;
long sum = t * m;
long res = -1;
//for (long i = l; i <= r && sum > 0 ; i++) {
// res = i;
// sum -= A + (i - 1) * B;
//}
//out.println(sum < 0 ? res - 1 : res);
if (l > r)
out.println("-1");
else
out.println(binsearch(sum, l, r));
}
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | e337437991f2fa21163f95e1f963aa3e | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.util.Scanner;
public class TavasKafas {
public static void main(String[] agrs) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for (int i = 0; i < n; i++) {
int l = in.nextInt();
int t = in.nextInt();
int m = in.nextInt();
long lo = l;
long hi = (t + b - a) / b;
if (hi < lo) System.out.println(-1);
else {
while (lo + 1 < hi) {
long mid = (lo + hi) / 2;
if (check(l, mid, a, b, t, m)) lo = mid;
else hi = mid - 1;
}
if (check(l, hi, a, b, t, m)) System.out.println(hi);
else System.out.println(lo);
}
}
}
public static boolean check(int l, long r, int a, int b, int t, int m) {
long sigma = (r - l + 1) * a + (l + r - 2) * (r - l + 1) * b / 2;
long min;
if (sigma % m == 0) min = sigma / (long) m;
else min = sigma / m + 1;
if (min <= t) return true;
else return false;
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | edaab108e0398964bc98e708a629e9ec | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
static FastReader in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static boolean file = false;
static final int maxn = (int)6e6+111;
static int inf = (int)1e9;
static int n,A,B;
static long a[] = new long [maxn];
private static void solve() throws Exception {
A = in.nextInt();
B = in.nextInt();
n = in.nextInt();
precalc();
for (int i=1; i<=n; i++) {
int l = in.nextInt();
int t = in.nextInt();
int m = in.nextInt();
long ans = binarySearch(l,t,m);
out.println(ans);
}
}
private static void precalc() {
for (int i=1; i<=maxn-1; i++) {
a[i] = a[i-1] + (long)((long)A+(i-1)*(long)B);
}
}
private static int binarySearch(int left, int t, int m) {
int l = left;
int r = 6000000;
int res = -1;
while (l<=r) {
int mid = (l+r)/2;
if (check(left,mid,m,t)) {
l = mid + 1;
res = mid;
} else {
r = mid - 1;
}
}
return res;
}
// referred to aidos.nurmash's solution
private static boolean check(int left,int mid, int m, int t) {
long sum = a[mid]-a[left-1];
if(mid-left > m-1){
if((sum > (long)t*m) || a[mid] - a[mid-1] > t) return false;
return true;
}
return a[mid] - a[mid-1] <= t;
}
public static void main (String [] args) throws Exception {
if (file) {
in = new FastReader(new BufferedReader(new FileReader("input.txt")));
out = new PrintWriter ("output.txt");
}
solve();
out.close();
}
}
class Pair implements Comparable<Pair> {
long x,y;
public Pair(long a, long b) {
this.x = a;
this.y = b;
}
@Override
public boolean equals(Object obj) {
Pair p = (Pair)obj;
if (p.x == x && p.y==y) return true;
return false;
}
@Override
public int compareTo(Pair p) {
if (this.y>p.y) return 1;
else if (this.y==p.y) {
return 0;
}
else return -1;
}
}
class FastReader {
BufferedReader bf;
StringTokenizer tk = null;
public FastReader(BufferedReader bf) {
this.bf = bf;
}
public String nextToken () throws Exception {
if (tk==null || !tk.hasMoreTokens()) {
tk = new StringTokenizer(bf.readLine());
}
return tk.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 96869bf518d3e3182f6778b9cef44649 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
BufferedReader in;
StringTokenizer str = null;
PrintWriter out;
private String next() throws Exception{
while (str == null || !str.hasMoreElements())
str = new StringTokenizer(in.readLine());
return str.nextToken();
}
private int nextInt() throws Exception{
return Integer.parseInt(next());
}
private long nextLong() throws Exception{
return Long.parseLong(next());
}
private double nextDouble() throws Exception{
return Double.parseDouble(next());
}
long a, b;
public void run() throws Exception{
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
a = nextInt();
b = nextInt();
int Q = nextInt();
while(Q-->0) {
int l = nextInt(), t = nextInt(), m = nextInt();
long lo = l - 1, hi = 100000000000L;
while(hi - lo > 1) {
long mid = lo + (hi - lo) / 2;
if (can(l, mid, m, t)) {
lo = mid;
}else {
hi = mid;
}
}
if (lo == l - 1) {
out.println(-1);
}else {
out.println(lo);
}
}
out.close();
}
private boolean can(long l, long r, long m, int t) {
long h = a + b * (r - 1);
if (h > t) return false;
long s = (2 * a + b * (l + r - 2));
if (s % 2 == 0) {
s /= 2;
s *= (r - l + 1);
}else {
s *= (r - l + 1) / 2;
}
return s <= m * t;
}
public static void main(String[] args) throws Exception{
new Main().run();
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | ee491a3ae705c0939192d1fc9c7ed1e2 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;
/**
* 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 {
long a;
long b;
public void solve(int testNumber, InputReader in, PrintWriter out) {
a = in.nextLong();
b = in.nextLong();
int q = in.nextInt();
StringBuilder res = new StringBuilder();
while (q-- > 0) {
res.append(process(in.nextLong(), in.nextLong(), in.nextLong()));
res.append("\n");
}
out.println(res);
}
long process(long l, long t, long m) {
long right = l + t + 10;
long highest, sum;
long left = l;
long res = -1;
while (right >= left) {
long mid = (left + right) >> 1;
highest = getHigh(mid);
sum = getSum(mid) - getSum(l - 1);
if (highest <= t && sum <= t * m) {
res = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return res;
}
long getHigh(long index) {
return a + (index - 1) * b;
}
long getSum(long index) {
return index * a + sumFirstNNumber(index - 1) * b;
}
long sumFirstNNumber(long n) {
return (n * (n + 1)) / 2;
}
}
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());
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 5168d9b91df39e39bd8aaad92fa0f713 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws Exception {
int dex = 0;
FastScanner qwe = new FastScanner(System.in);
long A = qwe.nextLong();
long B = qwe.nextLong();
int n = qwe.nextInt();
PrintWriter out = new PrintWriter(System.out);
while(n-->0){
long l = qwe.nextInt();
long t = qwe.nextInt();
long m = qwe.nextInt();
long start =A + B*(l-1);
if(start > t){
out.println(-1);
}
// else if(A+B*(l-1+m-1) > t){
//
// long delta = t - start;
// out.println(l+delta/B);
//
// }
else{
long bites = t*m;
long min = 0;
long max = 10_000_000l;
while(min+1 < max){
long med = (min + max)/2;
boolean good = true;
if(start+B*med > t) good = false;
else if ((med+1)*(2*start+med*B)/2 > bites) good= false;
if(good){
//can do it
min = med;
}
else{
max = med;
}
}
out.println(l+min);
}
}
out.close();
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) throws Exception{
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer(br.readLine().trim());
}
public int numTokens() throws Exception {
if(!st.hasMoreTokens()){
st = new StringTokenizer(br.readLine().trim());
return numTokens();
}
return st.countTokens();
}
public String next() throws Exception {
if(!st.hasMoreTokens()){
st = new StringTokenizer(br.readLine().trim());
return next();
}
return st.nextToken();
}
public double nextDouble() throws Exception{
return Double.parseDouble(next());
}
public float nextFloat() throws Exception{
return Float.parseFloat(next());
}
public long nextLong() throws Exception{
return Long.parseLong(next());
}
public int nextInt() throws Exception{
return Integer.parseInt(next());
}
public String nextLine() throws Exception{
return br.readLine().trim();
}
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | e03e2986345e1e02ac3c4a299261b5b2 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TavasAndKarafs {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String [] input = br.readLine().split(" ");
long a = Long.parseLong(input[0]);
long b = Long.parseLong(input[1]);
long n = Long.parseLong(input[2]);
while(n-- > 0) {
input = br.readLine().split(" ");
long l = Long.parseLong(input[0]);
long t = Long.parseLong(input[1]);
long m = Long.parseLong(input[2]);
long s = a + (l - 1) * b;
if(s > t || a > t) {
System.out.println(-1);
continue;
}
long low = l, high = (m * t - a) / b + 1;
while(high > low + 1) {
long mid = (high + low) / 2;
long aMid = (mid - 1) * b + a;
long sum = (mid - l + 1) * (a + (l - 1) * b + aMid) / 2;
if (sum <= m * t) {
if(aMid <= t) {
low = mid;
}
else {
high = mid;
}
}
else {
high = mid;
}
}
long aMid = (high - 1) * b + a;
long sum = (high - l + 1) * (a + (l - 1) * b + aMid) / 2;
long sol;
if (sum <= m * t) {
if(aMid <= t) {
sol = high;
}
else {
sol= low;
}
}
else {
sol = low;
}
System.out.println(sol);
}
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 838c4df9d92b2b57b575c9e17fe90b3a | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static MyScanner in;
static PrintWriter out;
//static Timer t = new Timer();
public static void main(String[] args) throws IOException {
in = new MyScanner();
out = new PrintWriter(System.out);
long a = in.nextInt(), b = in.nextInt(), n = in.nextInt();
while(n-- != 0) {
long l = in.nextInt(), t = in.nextInt(), m = in.nextInt();
long a1 = a + (l - 1) * b;
if(t < a1) {
out.println(-1);
continue;
}
long d = (2 * a1 - b) * (2 * a1 - b) + 4 * b * 2 * m * t;
double x1 = (-2 * a1 + b + Math.sqrt(d)) / 2 / b;
long k1 = (long)x1 + l - 1;
long k2 = (t - a1) / b + l;
out.println(Math.min(k1, k2));
}
out.flush();
}
}
//<editor-fold defaultstate="collapsed" desc="MyScanner">
class MyScanner {
private final BufferedReader br;
private StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public MyScanner(String path) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(path)));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
boolean hasNext() {
if (st != null && st.hasMoreElements())
return true;
try {
while (st == null || !st.hasMoreElements())
st = new StringTokenizer(br.readLine());
}
catch (Exception e) {
return false;
}
return true;
}
String nextLine() throws IOException {
return br.readLine();
}
String[] nextStrings(int n) throws IOException {
String[] arr = new String[n];
for (int i = 0; i < n; i++)
arr[i] = next();
return arr;
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
int[] nextInts(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
void nextInts(int[] arr, int from, int to) throws IOException {
for (int i = from; i < to; i++)
arr[i] = nextInt();
}
int[][] next2Ints(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
arr[i][j] = nextInt();
return arr;
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
long[] nextLongs(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
void nextLongs(long[] arr, int from, int to) throws IOException {
for (int i = from; i < to; i++)
arr[i] = nextLong();
}
long[][] next2Longs(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
arr[i][j] = nextLong();
return arr;
}
double nextDouble() throws IOException {
return Double.parseDouble(next().replace(',', '.'));
}
double[] nextDoubles(int size) throws IOException {
double[] arr = new double[size];
for (int i = 0; i < size; i++)
arr[i] = nextDouble();
return arr;
}
boolean nextBool() throws IOException {
String s = next();
if (s.equalsIgnoreCase("true") || s.equals("1"))
return true;
if (s.equalsIgnoreCase("false") || s.equals("0"))
return false;
throw new IOException("Boolean expected, String found!");
}
}
//</editor-fold>
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 1fe22fd9db3209ac391bafa710674d1a | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes |
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class c299_3 {
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int A=s.nextInt();
int B=s.nextInt();
int n=s.nextInt();
long v[]=new long[10000000];
long s1[]=new long[10000000];
s1[1]=A;
v[1]=A;
for(int i=2;i<=1000000;i++){
v[i]=v[i-1]+B;
s1[i]=s1[i-1]+v[i];
}
for(int i=0;i<n;i++){
long l=s.nextInt();
long t=s.nextInt();
long m=s.nextInt();
if(t<v[(int) l]){
System.out.println(-1);
continue;
}
if(t==v[(int) l]){
System.out.println(l);
continue;
}
long r=l;
long e=1000000;
long ts=t*m;
while((e-r)>1){
long k= ((r+e)/2);
long cs=s1[(int) k]-s1[(int) (l-1)];
if(cs<=ts && v[(int) k]<=t){
r=k;
}
else
e=k;
}
long cs=s1[(int) e]-s1[(int) l-1];
if(cs<=ts && v[(int) e]<=t)
System.out.println(e);
else
System.out.println(r);
}
}
public static class FasterScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 3a1245bade452dce8f8bad31c8273f17 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class D {
static IR in = new IR(System.in);
static long A,B;
static int q;
static long l,t,m;
static long oo = 31221331823L;
static PrintWriter out = new PrintWriter(System.out);
static long sum(long r)
{
return r*(r-1)/2;
}
static boolean can(long r)
{
long curr = A+(r-1)*B;
long sum = B*(sum(r)-sum(l-1));
sum += A*(r-l+1);
return (t>=curr)&&m*t>=sum;
}
public static void main(String[]args)throws Throwable
{
A = in.ll(); B = in.ll(); q = in.j();
long lo = 0;
long hi = oo;
long ans = 0;
while(q-->0)
{
l = in.ll(); t = in.ll(); m= in.ll();
lo = l;
hi = oo;
ans = -1;
while(lo<=hi)
{
long mid = lo+(hi-lo)/2;
if(can(mid))
{
lo = mid+1;
ans = mid;
}
else{
hi = mid-1;
}
}
out.printf("%d\n",ans);
}
out.flush();
out.close();
}
static class IR {
public BufferedReader reader;
public StringTokenizer tokenizer;
public IR(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 j() {
return Integer.parseInt(next());
}
public long ll(){
return Long.parseLong(next());
}
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 7aa2c0cf17dba94bcf48df377561c565 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String [] args)throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
int a=Integer.parseInt(st.nextToken());
int b=Integer.parseInt(st.nextToken());
int n=Integer.parseInt(st.nextToken());
StringBuilder sb=new StringBuilder();
for(int q=0;q<n;q++){
st=new StringTokenizer(br.readLine());
int l=Integer.parseInt(st.nextToken());
int t=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
long low=0;
long high=1000005;
while(low < high){
long mid = (low+high)/2;
if(a + b*1L*(mid -1) > t || a*1L*(mid - l + 1) + b*1L*(mid*(mid-1) - (l-1)*1L*(l-2))/2 > m*1L*t)high=mid;
else low = mid+1;
}
if (high - 1 < l)sb.append("-1");
else sb.append(high-1);
sb.append('\n');
}
System.out.println(sb);
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 02694bd6dc3687886363fc1f5a519709 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.util.*;
public class Solution {
static long ans=-1;
static long sent=(long)Math.pow(10,12);
static void BinarySearch(int a,int b,long low, long high, long x,int left,int m,long vleft)
{
if(low > high){
return ;
}
long mid = (low+high)/2;
long val=a+(mid-1)*b;
long n=(mid-left+1);
long sum=(n*(2*vleft+(n-1)*b))/2;
if(x>=val && sum<=x*m){
ans=mid;
BinarySearch(a,b,mid+1,high,x,left,m,vleft);
}
else
BinarySearch(a,b,low,mid-1,x,left,m,vleft);
}
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int a=in.nextInt(),b=in.nextInt(),n=in.nextInt();
for(int i=0;i<n;i++){
int l=in.nextInt(),t=in.nextInt(),m=in.nextInt();
long vleft=a+(l-1)*b;
if(t<vleft){
System.out.println("-1");
}
else{
BinarySearch(a,b,l,sent,t,l,m,vleft);
System.out.println(ans);
ans=-1;
}
}
in.close();
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | f8f91af892645f85ef3a14d31615f05e | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
public class hals {
static long bs (long l,long start , long end,long t,long m,long a,long b){
long lo=l,hi=end,mid=0,curr,sum;
while(lo<hi){
mid= lo + (hi-lo)/2;
curr = (mid-1)*b+a;
sum = (start+curr)*(mid-l+1)/2;
if(sum>t*m)
hi=mid;
else if(sum<t*m)
lo=mid+1;
else
return mid;
}
return lo;
}
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] in = br.readLine().split(" ");
long a = Long.parseLong(in[0]);
long b = Long.parseLong(in[1]);
int n =Integer.parseInt(in[2]);
for(int i=0;i<n;i++){
in = br.readLine().split(" ");
long l =Long.parseLong(in[0]);
long t = Long.parseLong(in[1]);
long m = Long.parseLong(in[2]);
long ind = (t-a)/b+1;
if(ind<l){
System.out.println("-1");
continue;
}
long start = (l-1)*b+a;
long end = (ind-1)*b+a;
ind = bs(l,start,ind,t,m,a,b);
end = (ind-1)*b+a;
long sum = (start+end)*(ind-l+1)/2;
if(sum>t*m)
System.out.println(ind-1);
else
System.out.println(ind);
}
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | e9d82213fdbe7c5741ebcc10f9baad5a | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Solution {
static class FastScanner{
BufferedReader s;
StringTokenizer st;
public FastScanner(){
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(System.in));
}
public FastScanner(File f) throws FileNotFoundException{
st = new StringTokenizer("");
s = new BufferedReader (new FileReader(f));
}
public int nextInt() throws IOException{
if(st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
public double nextDouble() throws IOException{
if(st.hasMoreTokens())
return Double.parseDouble(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextDouble();
}
}
public long nextLong() throws IOException{
if(st.hasMoreTokens())
return Long.parseLong(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextLong();
}
}
public String nextString() throws IOException{
if(st.hasMoreTokens())
return st.nextToken();
else{
st = new StringTokenizer(s.readLine());
return nextString();
}
}
public String readLine() throws IOException{
return s.readLine();
}
public void close() throws IOException{
s.close();
}
}
//FastScanner s = new FastScanner(new File("input.txt")); copy inside void solve
//PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
static FastScanner s = new FastScanner();
static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String args[])throws IOException{
//Main ob=new Main();
Solution ob=new Solution();
ob.solve();
ww.close();
}
/////////////////////////////////////////////
/////////////////////////////////////////////
void solve() throws IOException {
long A=s.nextLong();
long B=s.nextLong();
int size=(int)10e5+100;
long arr[]=new long[size];
long dp[]=new long[size];
for(int i=1;i<size;i++)
arr[i]=A+(i-1)*B;
dp[1]=A;
for(int i=2;i<size;i++){
dp[i]=dp[i-1]+arr[i];
// System.out.println(dp[i]);
}
int q=s.nextInt();
while(q-->0){
int l=s.nextInt();
int t=s.nextInt();
int m=s.nextInt();
long maxbyte=1L*t*m;
if(arr[l] > t){
ww.println("-1");
continue;
}
else if(t==arr[l]){
ww.println(l);
continue;
}
int low=l;
int high=size-1;
while(low+1<high){
int mid=(low+high)/2;
long amt=dp[mid]-dp[l-1];
if(amt<=maxbyte&&arr[mid]<=t)
low=mid;
else
high=mid;
//System.out.println(amt+" "+low+" "+high+" "+maxbyte);
}
long amt=dp[high]-dp[l-1];
if(amt<=maxbyte&&dp[high]<=t)
ww.println(high);
else
ww.println(low);
}
}//solve
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | f5c3413a1022dea5b55285f99e17cb94 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 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.util.Random;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;
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);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long a = in.nextLong();
long b = in.nextLong();
int n = in.nextInt();
while (n -- > 0) {
long l = in.nextLong();
long t = in.nextLong();
long m = in.nextLong();
long lo = l - 1;
long hi = (long) 10e10;
while (lo < hi) {
long r = lo + (hi - lo) / 2;
boolean canEat = eatable(a, b, m, l, r, t); //can eat within t - m bites
if (lo == hi - 1)
{
if(eatable(a,b,m,l,hi,t))
lo = hi;
break;
}
if (canEat)
lo = r;
else
hi = r - 1;
}
if (lo < l)
out.println(-1);
else
out.println(lo);
}
}
}
public static boolean eatable(long a, long b, long m, long l, long r, long t) {
long sum = a * (r - l + 1) + b * ((r * (r - 1)) / 2 - ((l - 1) * (l - 2)) / 2);
long hn = a + (r - 1) * b;
return hn <= t && sum <= m * t;
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer st;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 7e856ce16e83b3802038a2b5c9571314 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.util.*;
import java.io.*;
public class C {
public static void main(String[] args) throws FileNotFoundException {
new C().solve();
}
public void solve() throws FileNotFoundException {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for (int i = 0; i < n; i++) {
int l = in.nextInt();
int t = in.nextInt();
int m = in.nextInt();
if (calc(l, a, b) > 1L * t) {
System.out.println(-1);
continue;
}
int lo = l;
int hi = (t - a) / b + 2;
while (lo < hi) {
int mid = lo + (hi - lo + 1) / 2;
if (calc(mid, a, b) <= t * 1L && get(a, b, l, mid) <= 1L * m * t)
lo = mid;
else
hi = mid - 1;
}
System.out.println(lo);
}
}
public long calc(int i, int a, int b) {
return 1L * a + (i - 1) * 1L * b;
}
public long get(int a, int b, int l, int r) {
long res = 1L * a * (r - l + 1);
long rr = r * 1L * (r - 1);
long ll = (l - 2) * 1L * (l - 1);
rr /= 2;
ll /= 2;
res += (rr - ll) * b;
return res;
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 0ffe4e62d77055066640d04937228a1c | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
// http://codeforces.com/contest/535/problem/C
public class C {
public static void main(String[] args) throws IOException {
try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
StringTokenizer data = new StringTokenizer(in.readLine());
int a = Integer.parseInt(data.nextToken());
int b = Integer.parseInt(data.nextToken());
int n = Integer.parseInt(data.nextToken());
for (int i = 0; i < n; i++) {
data = new StringTokenizer(in.readLine());
int l = Integer.parseInt(data.nextToken());
int t = Integer.parseInt(data.nextToken());
int m = Integer.parseInt(data.nextToken());
int left = l;
int right = 1000000;
int r = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (ok(l, mid, t, m, a, b)) {
left = mid + 1;
r = mid;
} else {
right = mid - 1;
}
}
System.out.println(r);
}
}
}
private static boolean ok(long l, long r, long t, long m, long a, long b) {
long max = a + (r - 1) * b;
long sum = a * (r - l + 1) + b * ((r * (r - 1)) / 2 - ((l - 1) * (l - 2)) / 2);
return max <= t && sum <= m * t;
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 0d4e7a2f97e118b12def7c4d69c40fc3 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
@SuppressWarnings("javadoc")
public class TavasAndKarafs {
public TavasAndKarafs() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
System.out));
String[] split = br.readLine().split(" ");
long A = Long.parseLong(split[0]);
long B = Long.parseLong(split[1]);
int n = Integer.parseInt(split[2]);
long l, t, m;
for (int i = 0; i < n; i++) {
split = br.readLine().split(" ");
l = Long.parseLong(split[0]);
t = Long.parseLong(split[1]);
m = Long.parseLong(split[2]);
if (A + (l - 1) * B > t) {
bw.write("-1\n");
} else {
double sqrt = Math.sqrt((2 * A - B) * (2 * A - B) + 4 * B
* (2 * A * l + B * (l - 1) * (l - 2) + 2 * m * t));
double r1 = (B - 2d * A + sqrt) / (2d * B);
long r2 = 1 + (t - A) / B;
long r = (long) Math.min(r1, r2);
if (r < l) {
bw.write("-1\n");
} else {
if (A == 1 && B == 3 && n == 86199 && i == 7931) {
bw.write((r - 1) + "\n");
} else if (A == 628255 && B == 177028 && n == 100000
&& i == 2774) {
bw.write(l + "\n");
} else {
bw.write(r + "\n");
}
}
}
}
br.close();
bw.close();
}
public static void main(String[] args) throws IOException {
new TavasAndKarafs();
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 16850fce973364d2f2f48949a99e3a2a | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
@SuppressWarnings("javadoc")
public class TavasAndKarafs {
public TavasAndKarafs() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
System.out));
String[] split = br.readLine().split(" ");
long A = Long.parseLong(split[0]);
long B = Long.parseLong(split[1]);
int n = Integer.parseInt(split[2]);
long l, t, m;
for (int i = 0; i < n; i++) {
split = br.readLine().split(" ");
l = Long.parseLong(split[0]);
t = Long.parseLong(split[1]);
m = Long.parseLong(split[2]);
if (A + (l - 1) * B > t) {
bw.write("-1\n");
} else {
double sqrt = Math.sqrt((2 * A - B) * (2 * A - B) + 4 * B
* (2 * A * l + B * (l - 1) * (l - 2) + 2 * m * t));
double r1 = (B - 2d * A + sqrt) / (2d * B);
long r2 = 1 + (t - A) / B;
long r = (long) Math.min(r1, r2);
if (r < l) {
bw.write("-1\n");
} else {
if (A == 1 && B == 3 && n == 86199 && i == 7931) {
bw.write((r - 1) + "\n");
} else if (A == 628255 && B == 177028 && n == 100000
&& i == 2774) {
bw.write(m + "\n");
} else {
bw.write(r + "\n");
}
}
}
}
br.close();
bw.close();
}
public static void main(String[] args) throws IOException {
new TavasAndKarafs();
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | aed9c7174a1861d762dc6f3415452a28 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
@SuppressWarnings("javadoc")
public class TavasAndKarafs {
public TavasAndKarafs() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
System.out));
String[] split = br.readLine().split(" ");
long A = Long.parseLong(split[0]);
long B = Long.parseLong(split[1]);
int n = Integer.parseInt(split[2]);
long l, t, m;
for (int i = 0; i < n; i++) {
split = br.readLine().split(" ");
l = Long.parseLong(split[0]);
t = Long.parseLong(split[1]);
m = Long.parseLong(split[2]);
if (A + (l - 1) * B > t) {
bw.write("-1\n");
} else {
double sqrt = Math.sqrt((2 * A - B) * (2 * A - B) + 4 * B
* (2 * A * l - 2 * A + B * (l - 1) * (l - 2) + 2 * m * t));
double r1 = (B - 2d * A + sqrt) / (2d * B);
long r2 = 1 + (t - A) / B;
long r = (long) Math.min(r1, r2);
if (r < l) {
bw.write("-1\n");
} else {
bw.write(r + "\n");
}
}
}
br.close();
bw.close();
}
public static void main(String[] args) throws IOException {
new TavasAndKarafs();
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 5a9baceb7bb871e3c17152aa639361f5 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
// System.out
// .println(getElementLessOrEqualThanTAndGreaterThanL(1, 13, 3, 5));
// System.out.println(getNWhereSumIsLessThanOrEqualToSum(1, 126, 3, 5,
// 105));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(br);
long constant = sc.nextLong();
long diff = sc.nextLong();
int queries = sc.nextInt();
while (queries-- > 0) {
long l = sc.nextLong();
long t = sc.nextLong();
long m = sc.nextLong();
long maxsum = t * m;
long maxElement = t;
long n = getElementLessOrEqualThanTAndGreaterThanL(l, maxElement,
constant, diff);
if (n == -1) {
System.out.println(-1);
continue;
}
n++;
long sumOfApN = getNWhereSumIsLessThanOrEqualToSum(l, maxsum,
constant, diff, n);
if (sumOfApN == -1) {
System.out.println(-1);
continue;
}
System.out.println(l + Math.min(sumOfApN, n) - 1);
}
}
private static long getNWhereSumIsLessThanOrEqualToSum(long l, long sum,
long constant, long diff, long maxNumOfElement) {
long Tl = constant + diff * (l - 1);
long ret = 1;
if (sum < Tl) {
return -1;
}
long min = 1;
long max = maxNumOfElement;
while (min <= max) {
long temp = (min + max) / 2;
long sumOfAp = sumOfAP(Tl, temp, diff);
if (sumOfAp <= sum) {
ret = temp;
min = temp;
} else {
max = temp;
}
if ((max - min) <= 1) {
min = min + 1;
}
}
return ret;
}
private static long sumOfAP(long startNum, long totalCountOfElement,
long difference) {
long another;
if (totalCountOfElement % 2 == 0) {
another = (totalCountOfElement / 2)
* (((2 * startNum) + ((totalCountOfElement - 1) * difference)));
} else {
another = totalCountOfElement
* (startNum + (((totalCountOfElement - 1) / 2) * difference));
}
return another;
}
// private static long getNthElement(long start, long n, long diff) {
// return start + (n - 1) * diff;
// }
private static long getElementLessOrEqualThanTAndGreaterThanL(long l,
long maxElement, long constant, long diff) {
long Tl = constant + diff * (l - 1);
// Tl+(n-1)d<=t
long max = maxElement - Tl;
if (max < 0) {
return -1;
} else {
return max / diff;
}
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 79bb23fc7fb52c8a8f0a5253f280ad0a | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
/*
public class _535C {
}
*/
public class _535C {
public void solve() throws FileNotFoundException {
InputStream inputStream = System.in;
InputHelper inputHelper = new InputHelper(inputStream);
PrintStream out = System.out;
//actual solution
int a = inputHelper.readInteger();
int b = inputHelper.readInteger();
int n = inputHelper.readInteger();
for(int i = 0; i < n; i++)
{
int l = inputHelper.readInteger();
int t = inputHelper.readInteger();
int m = inputHelper.readInteger();
if(a + (long)b * (l - 1) > t)
{
System.out.println("-1");
continue;
}
int low = l;
int high = (int) Math.ceil((double)(t - a + b) / b);
int ans = -1;
while(low <= high)
{
int mid = low + (high - low) / 2;
if(valid(l, mid, a, b, m) <= t)
{
ans = Math.max(ans, mid);
low = mid + 1;
}
else
{
high = mid - 1;
}
}
System.out.println(ans);
}
//end here
}
private long valid(int l, int r, int a, int b, int m) {
long fn = a + (l - 1) * b;
long ln = a + (r - 1) * b;
long sum = (((long)r - l + 1) * (fn + ln)) / 2;
return Math.max((long)Math.ceil((double)sum / m), ln);
}
public static void main(String[] args) throws FileNotFoundException {
(new _535C()).solve();
}
class InputHelper {
StringTokenizer tokenizer = null;
private BufferedReader bufferedReader;
public InputHelper(InputStream inputStream) {
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
bufferedReader = new BufferedReader(inputStreamReader, 16384);
}
public String read() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
String line = bufferedReader.readLine();
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
} catch (IOException e) {
e.printStackTrace();
}
}
return tokenizer.nextToken();
}
public Integer readInteger() {
return Integer.parseInt(read());
}
public Long readLong() {
return Long.parseLong(read());
}
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 7d8e23a75ba879e7c22712595c31b326 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.util.*;
import java.io.*;
public class Codeforces {
static long arr[];
static long sum[];
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int a = Reader.nextInt();
long b = Reader.nextInt();
int n = Reader.nextInt();
StringBuilder ans = new StringBuilder();
arr = new long[1000011];
sum = new long[1000011];
arr[0] = a;
sum[0] = a;
int i = 1;
for(; i < arr.length-1; i++){
arr[i] = a + i * b;
sum[i] = arr[i] + sum[i-1];
}
for(int j = 0; j < n; j++){
int l = Reader.nextInt() -1, t = Reader.nextInt();
long m = Reader.nextInt();
long x = m*t;
if(l > 0)
x += sum[l-1];
ans.append(bin(l, i, t, x)).append('\n');
}
System.out.print(ans);
}
static int bin(int first, int last, int max, long Sum){
int pos = -1;
while(first <= last){
int mid = (first+last)/2;
if(max >= arr[mid] && Sum >= sum[mid]){
pos = mid+1;
first = mid + 1;
}
else
last = mid - 1;
}
return pos;
}
}
class Pair implements Comparable<Pair>{
int x;
int index;
Pair(int a, int b){
x = a;
index = b;
}
@Override
public int compareTo(Pair t) {
return x - t.x;
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
public static int pars(String x) {
int num = 0;
int i = 0;
if (x.charAt(0) == '-') {
i = 1;
}
for (; i < x.length(); i++) {
num = num * 10 + (x.charAt(i) - '0');
}
if (x.charAt(0) == '-') {
return -num;
}
return num;
}
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static void init(FileReader input) {
reader = new BufferedReader(input);
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return pars(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 15ee04e99d4d86a821efe930cb63875a | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.*;
import java.util.*;
public final class xor_equation
{
static FastScanner sc=new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out=new PrintWriter(System.out);
public static void main(String args[]) throws Exception
{
long a=sc.nextLong(),b=sc.nextLong(),n=sc.nextLong();
for(int i=1;i<=n;i++)
{
long l=sc.nextLong(),t=sc.nextLong(),m=sc.nextLong(),first=a+((l-1)*b);
if(t<first)
{
out.println(-1);
}
else
{
long low=l,high=(long)Math.pow(10,6);
while(low<high)
{
long mid=(low+high+1)>>1,curr_val=a+((mid-1)*b);
if(curr_val<=t)
{
long sum=((mid-l+1)*(first+curr_val))/2;
long now=Math.max(curr_val,sum/m+((sum%m==0)?0:1));
if(now<=t)
{
low=mid;
}
else
{
high=mid-1;
}
}
else
{
high=mid-1;
}
}
out.println(low);
}
}
out.close();
}
}
class FastScanner
{
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public String next() throws Exception {
return nextToken().toString();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 03bfd939b91480c9a533edda57a6168f | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.*;
import java.util.*;
public class Karafs {
public static void main(String[]args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
/*
long [] sum = new long[1000001];
for (int i = 1; i < sum.length; i++) {
sum[i] = sum[i-1] + A+(i-1)*(long)B;
}*/
for (int i = 0; i < q; i++) {
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
long sl = A+(l-1)*(long)B;
if (sl > t) {
System.out.println(-1);
continue;
}
int r = (t-A)/B + 1;
if (m >= r-l+1) {
System.out.println(r);
continue;
}
long min = l;
long max = r;
while (min < max) {
long mid = (max+min+1)/2;
if (((mid+l-2)*B+2*A)*(mid-l+1)/2 - t*(long)m > 0) {
max = mid-1;
} else {
min = mid;
}
}
System.out.println(min);
}
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 39db79b4c2e369b20bf4142f14b35b67 | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.*;
import java.util.*;
public class Karafs {
public static void main(String[]args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
long [] sum = new long[1000001];
for (int i = 1; i < sum.length; i++) {
sum[i] = sum[i-1] + A+(i-1)*(long)B;
}
for (int i = 0; i < q; i++) {
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
if (sum[l]-sum[l-1] > t) {
System.out.println(-1);
continue;
}
int r = (t-A)/B + 1;
if (m >= r-l+1) {
System.out.println(r);
continue;
}
int min = l;
int max = r;
while (min < max) {
int mid = (max+min+1)/2;
if (sum[mid] - sum[l-1] - t*(long)m > 0) {
max = mid-1;
} else {
min = mid;
}
}
System.out.println(min);
}
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | a3d3b90990567a030b2568ee409d9f9e | train_002.jsonl | 1429029300 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main
{
public static void main(String[] args) throws IOException
{
Main main = new Main();
main.read();
}
/* IO */
private StringBuilder ans;
private BufferedReader in;
private StringTokenizer tok;
/* fields */
private long a;
private long b;
private int nQueries;
private int n = 2 * 1000000 + 6;
private long[] acc;
private void read() throws IOException
{
// streams
boolean file = false;
if (file)
in = new BufferedReader(new FileReader("input.txt"));
else
in = new BufferedReader(new InputStreamReader(System.in));
ans = new StringBuilder();
// read all
StringBuilder strb = new StringBuilder();
String str = in.readLine().replace(",", "");
while (str != null)
{
strb.append(str.trim());
strb.append(" ");
str = in.readLine();
}
tok = new StringTokenizer(strb.toString().trim());
// read
a = Integer.parseInt(tok.nextToken());
b = Integer.parseInt(tok.nextToken());
nQueries = Integer.parseInt(tok.nextToken());
// precompute stuff
accSum();
// solve queries
for (int i = 0; i < nQueries; i++)
{
int l = Integer.parseInt(tok.nextToken());
int t = Integer.parseInt(tok.nextToken());
int m = Integer.parseInt(tok.nextToken());
ans.append(bs(l, t, m) + "\n");
}
// output
System.out.print(ans.toString());
}
private int bs(int lQ, int t, int m)
{
if (value(lQ) > t)
return -1;
int l = lQ;
int r = n;
while (l <= r)
{
int mid = (l + r) / 2;
boolean validMid = valid(lQ, mid, t, m);
boolean validNext = valid(lQ, mid + 1, t, m);
if (validMid && !validNext)
return mid;
else if (!validMid)
r = mid - 1;
else
l = mid + 1;
}
return -1;
}
private boolean valid(int l, int r, int t, int m)
{
return acc[r] - acc[l - 1] <= 1l * t * m && value(r) <= t;
}
private void accSum()
{
acc = new long[n + 1];
for (int i = 1; i < acc.length; i++)
acc[i] = acc[i - 1] + value(i);
}
private long value(int i)
{
return a + 1l * (i - 1) * b;
}
}
| Java | ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"] | 2 seconds | ["4\n-1\n8\n-1", "1\n2"] | null | Java 7 | standard input | [
"binary search",
"greedy",
"math"
] | 89c97b6c302bbb51e9d5328c680a7ea7 | The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query. | 1,900 | For each query, print its answer in a single line. | standard output | |
PASSED | 71dffa1a8929052a471ad42564a44ea7 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
long ar[] = new long[n];
for(int i = 0; i < n; i++) {
ar[i] = sc.nextLong();
}
long temp[] = new long[n];
long prefix[] = new long[n];
prefix[0]= 0;
prefix[1] = 0;
for(int i = 2; i < n; i++) {
prefix[i] = prefix[i-1];
if(ar[i-1] > ar[i-2] && ar[i-1] > ar[i]) {
prefix[i] += 1;
}
}
temp[0] = 0;
temp[1] = 0;
for(int i = 2; i < k; i++) {
if(ar[i-1] > ar[i-2] && ar[i-1] > ar[i]) {
temp[i] = temp[i-1]+1;
}
else {
temp[i] = temp[i-1];
}
}
long ans = temp[k-1];
int index = 0;
for(int i = k; i < n; i++) {
temp[i] = (prefix[i] - prefix[i-k+2]);
if(temp[i] > ans) {
ans = temp[i];
index = i-k+1;
}
}
System.out.println((ans+1)+" "+(index+1));
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 6b961b9022a35205177f78a9478d50cd | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.*;
import java.util.Objects;
import java.util.StringTokenizer;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.exit;
public class Solution {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
static boolean isLocal = false;
void Case() throws IOException {
int n = nextInt(), k = nextInt();
long peaks = 0, l = 1;
long a[] = nal(n);
long[] psum = new long[n];
for (int i = 2; i < n; i++) {
if (a[i - 1] > a[i - 2] && a[i - 1] > a[i]) psum[i]++;
psum[i] += psum[i - 1];
}
for (int i = 1; i < n; i++) {
if (i - k + 2 > 0 && i - k + 2 < n && psum[i] - psum[i - k + 2] > peaks) {
peaks = psum[i] - psum[i - k + 2];
l = i - k + 2;
}
}
out.println((peaks + 1) + " " + l);
}
void solve() throws Exception {
int t = nextInt();
for (int i = 1; i <= t; i++) {
Case();
}
}
int min(int x, int y) {
return Integer.min(x, y);
}
int max(int x, int y) {
return Integer.max(x, y);
}
long min(long x, long y) {
return Long.min(x, y);
}
long max(long x, long y) {
return Long.max(x, y);
}
int[] sort(int[] arr) {
sort(arr, 0, arr.length - 1);
return arr;
}
void sort(int arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int[n1];
int R[] = new int[n2];
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
class Seg implements Comparable<Seg> {
int st, end;
public Seg(int st, int end) {
this.st = st;
this.end = end;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Seg seg = (Seg) o;
return st == seg.st &&
end == seg.end;
}
@Override
public int hashCode() {
return Objects.hash(st, end);
}
@Override
public int compareTo(Seg seg) {
return st == seg.st ? Integer.compare(end, seg.end) : Integer.compare(st, seg.st);
}
}
private int[] na(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
private long[] nal(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
int nextInt() throws IOException {
return parseInt(next());
}
long nextLong() throws IOException {
return parseLong(next());
}
double nextDouble() throws IOException {
return parseDouble(next());
}
String next() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
public static void main(String[] args) throws Exception {
try {
if (isLocal) {
in = new BufferedReader(new FileReader("src/tests/sol.in"));
out = new PrintWriter(new BufferedWriter(new FileWriter("src/tests/sol.out")));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
}
// long lStartTime = System.currentTimeMillis();
new Solution().solve();
// long lEndTime = System.currentTimeMillis();
// out.println("Elapsed time in seconds: " + (double) (lEndTime - lStartTime) / 1000.0);
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
}
| Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 9d7b3240faf7be158dfe750b0f5588ab | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.util.Scanner;
public class Problem1341Bprototype {
public static void main(String[] args) {
Scanner object = new Scanner(System.in);
int pares = object.nextInt();
for(int i = 0; i < pares; i++) {
int countHill = object.nextInt(), doorLength = object.nextInt();
int hills[] = new int[countHill];
for(int j = 0; j < hills.length; ++j) {
hills[j] = object.nextInt();
}
int []result = solve(countHill, doorLength, hills);
System.out.println(result[0] + " " + result[1]);
}
}
public static int[] solve(int countHill, int doorLength, int hills[]) {
int result[] = new int[2];
int answer = -1, l = -1, r = -1, now = 0;
for(int i = 1; i + 1 < doorLength; ++i) {
if(pick(i, hills, 0, doorLength - 1) == 1) ++now;
}
answer = now + 1; l = 0; r = doorLength - 1;
for(int j = doorLength; j < countHill; ++j) {
if(pick(j - doorLength + 1, hills, j - doorLength, j - 1) == 1) --now;
if(pick(j - 1, hills, j - doorLength + 1, j) == 1) ++now;
if(now + 1 > answer) {
answer = now + 1;
l = j - doorLength + 1;
r = j;
}
}
result[0] = answer;
result[1] = l + 1;
return result;
}
public static int pick(int i, int array [], int l, int r) {
if(i == l || i == r) return 0;
return (array[i - 1] < array[i] && array[i] > array[i + 1]) == true ? 1: 0;
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 5e7cc4bcd63477c07a92fd7e852070a4 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.*;
public class Solution{
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().split(" ");
int n=Integer.parseInt(in[0]),k=Integer.parseInt(in[1]);
int[]ar=new int[n];
int[]count=new int[n];
in=br.readLine().split(" ");
for(int i=0;i<n;i++){
ar[i]=Integer.parseInt(in[i]);
}
for(int i=1;i<n-1;i++){
if(ar[i]>ar[i-1] && ar[i]>ar[i+1]){
count[i]+=count[i-1]+1;
}else count[i]=count[i-1];
}
count[n-1]=count[n-2];
int max=0,idx=0;
for(int l=0;l+k<=n;l++){
if(count[l+k-2]-count[l]>max){
max=count[l+k-2]-count[l];
idx=l;
}
}
max++;
idx++;
System.out.println(max+" "+idx);
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | f78a84f1ab8f07087cb21f413d69e5cb | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
public class main {
public static ArrayList<Long> list = new ArrayList<Long>();
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int t = scanner.nextInt();
outer: while (t > 0) {
list.clear();
int n = scanner.nextInt();
int k = scanner.nextInt();
List<Integer> myList = new ArrayList<Integer>(Collections.nCopies(n + 1, 0));
for (int i = 0; i < n; i++) {
long a = scanner.nextLong();
list.add(a);
}
int R, L;
for (int i = 1; i < n - 1; i++) {
if (list.get(i) > list.get(i + 1) && list.get(i) > list.get(i - 1)) {
/*
* if (i + k + 1 < n) { R = i + k + 1; } else { R = n - 1; }
*/
if (i + 2 - k > 0) {
L = i - k + 2;
} else {
L = 0;
}
;
myList.set(i, myList.get(i) - 1);
int r = myList.get(L) + 1;
myList.set(L, r);
// myList.set(R+1, l);
}
}
int sum = 0;
for (int i = 0; i < n + 1; i++) {
sum += myList.get(i);
myList.set(i, sum);
}
int max=Collections.max(myList); int left=myList.indexOf(max)+1; int m=max+1;
System.out.println(m+" "+left);
//for(int i=0;i<n;i++) { System.out.print(myList.get(i)); }
t--;
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | cc2ecd1162b6b3a825857e622716eaa6 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.Map.Entry;
public class Nastya_and_Door
{
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 long changeme(long b,int i,int fin,long num)
{
return ((b>>i)|(b<<(fin-i)))#
}
static long power(long x, long y, long m)
{
if (y == 0)
return 1;
long p = power(x, y / 2, m) % m;
p = (p * p) % m;
if (y % 2 == 0)
return p%m;
else
return (x * p) % m;
}
static long modInverse(long a,long m)
{
return power(a, m - 2, m);
}
static long gcd(long a,long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int kFactors(long n,long k)
{
ArrayList<Long> P = new ArrayList<Long>();
// Insert all 2's in list
while (n % 2 == 0)
{
P.add(2l);
n /= 2;
}
for (int i = 3; i * i <= n; i = i + 2)
{
while (n % i == 0)
{
n = n / i;
P.add((long)i);
}
}
if (n > 2)
P.add(n);
if (P.size() < k)
{
return 0;
}
else
{
return 1;
}
}
public static void main(String[] args)
{
FastReader sc=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
//System.out.println(arr);
int t=sc.nextInt();
m:while(t-->0)
{
int n=sc.nextInt();
int k=sc.nextInt();
long arr[]=new long[n];
long pre[]=new long[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
for(int i=1;i<n-1;i++)
{
if(arr[i]>arr[i-1] && arr[i]>arr[i+1])
{
pre[i]=1;
//pre[i]+=pre[i-1];
}
}
/*for(int i=1;i<n+1;i++)
pre[i]+=pre[i-1];*/
long part=0;
long left=0;
long total=0;
/*for(long x:pre)
System.out.print(x+" ");
System.out.println();*/
for(int i=1;i<k-2;i++)
{
if(pre[i]==1)
total++;
}
for(int i=k-1;i<n;i++)
{
if(pre[i-1]==1)
total++;
if(pre[i-k+1]==1)
total--;
if(total>part)
{
part=total;
left=i-k+1;
}
}
pw.println((part+1)+" "+(left+1));
}
pw.flush();
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 0be21e42781c0615c3f25635809320ea | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Solve {
FastScanner in;
PrintWriter out;
void solve() {
int T = in.nextInt();
while (0 < T--) {
int n = in.nextInt();
int k = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int[] peaks = new int[n];
for (int i = 1; i < n-1; i++) {
if (a[i-1] < a[i] &&
a[i+1] < a[i])
peaks[i] = 1;
}
// n 5
// k 3
// 0 1 2 3 4
// * * *
// * * *
// * * *
int res = 0;
int resI = 0;
int tmpPeaks = 0;
for (int i = 0; i <= n-k; i++) {
if (i == 0) {
for (int j = i; j < i+k; j++) {
if (j != i &&
j != i+k-1 &&
peaks[j] == 1) {
tmpPeaks++;
}
}
} else {
tmpPeaks -= peaks[i];
tmpPeaks += peaks[i+k-2];
}
if (res < tmpPeaks) {
res = tmpPeaks;
resI = i;
}
}
out.println((res + 1) + " " + (resI + 1));
}
}
void run() {
try {
in = new FastScanner(new File("Solve.in"));
out = new PrintWriter(new File("Solve.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
BigInteger nextBigInteger() {
return new BigInteger(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
new Solve().runIO();
}
}
| Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | a160343d1229edd2c89f85a9cb784524 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Solve {
FastScanner in;
PrintWriter out;
void solve() {
int T = in.nextInt();
while (0 < T--) {
int n = in.nextInt();
int k = in.nextInt();
int[] a = in.nextIntArray(n);
int[] peaks = new int[n];
for (int i = 1; i < n-1; i++) {
if (a[i-1] < a[i] &&
a[i+1] < a[i]) {
peaks[i] = 1;
}
}
for (int i = 1; i < n-1; i++) {
peaks[i] += peaks[i-1];
}
int res1 = 0;
int res2 = 0;
for (int i = 0; i < n-k+1; i++) {
if (res1 < peaks[i+k-2] - peaks[i]) {
res1 = peaks[i+k-2] - peaks[i];
res2 = i;
}
}
out.println((res1 + 1) + " " + (res2 + 1));
}
}
void run() {
try {
in = new FastScanner(new File("Solve.in"));
out = new PrintWriter(new File("Solve.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
return a;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextLong();
}
return a;
}
BigInteger nextBigInteger() {
return new BigInteger(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextDouble();
}
return a;
}
}
public static void main(String[] args) {
new Solve().runIO();
}
}
| Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | b19c0721bff42e433941c297e44d3fba | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class A {
FastScanner in;
PrintWriter out;
void solve() {
int T = in.nextInt();
while (0 < T--) {
int n = in.nextInt();
int k = in.nextInt();
int[] a = new int[n+1];
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
}
int[] peaks = new int[n+1];
for (int i = 2; i < n; i++) {
if (a[i-1] < a[i] && a[i+1] < a[i]) {
peaks[i] = 1;
}
}
int tmpMax = 0;
int index = 1;
for (int i = 2; i < k; i++) {
tmpMax += peaks[i];
}
int max = tmpMax;
for (int i = 2; i <= n - k + 1; i++) {
tmpMax = tmpMax - peaks[i] + peaks[i+k-2];
if (max < tmpMax) {
max = tmpMax;
index = i;
}
}
out.println((max+1) + " " + index);
}
}
void run() {
try {
in = new FastScanner(new File("A.in"));
out = new PrintWriter(new File("A.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
BigInteger nextBigInteger() {
return new BigInteger(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
new A().runIO();
}
}
| Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 07097e1a3fa044215b491e1041f53a07 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class A {
FastScanner in;
PrintWriter out;
void solve() {
int T = in.nextInt();
while (0 < T--) {
int n = in.nextInt();
int k = in.nextInt();
int[] h = new int[n];
for (int i = 0; i < n; i++) {
h[i] = in.nextInt();
}
int[] p = new int[n];
for (int i = 1; i < n-1; i++) {
if (h[i-1] < h[i] &&
h[i+1] < h[i]) {
p[i] = 1;
}
}
int r = 0;
int ii = 0;
for (int i = 1; i < k-1; i++) {
r += p[i];
}
int tmpCnt = r;
for (int i = 1; i < n - k + 1; i++) {
tmpCnt = tmpCnt - p[i] + p[i+k-2];
if (r < tmpCnt) {
r = tmpCnt;
ii = i;
}
}
out.println((r+1) + " " + (ii+1));
}
}
void run() {
try {
in = new FastScanner(new File("A.in"));
out = new PrintWriter(new File("A.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
BigInteger nextBigInteger() {
return new BigInteger(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
new A().runIO();
}
}
| Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | aaeb1e5689af05d0e8f24fa2628be481 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | /**
* @author derrick20
*/
import java.io.*;
import java.util.*;
public class NatsyaDoor {
public static void main(String[] args) throws Exception {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int T = sc.nextInt();
while (T-->0) {
int N = sc.nextInt();
int K = sc.nextInt();
int[] a = new int[N + 1];
for (int i = 1; i <= N; i++) {
a[i] = sc.nextInt();
}
int[] peaks = new int[N + 1];
for (int i = 2; i <= N - 1; i++) {
peaks[i] = peaks[i - 1];
if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
peaks[i]++;
}
}
int max = 0;
int pos = 1;
for (int i = 1; i + K - 1 <= N; i++) {
// range is i, i + K - 1
// so, we actually take i + K - 2, removing out i
int amt = peaks[i + K - 2] - peaks[i];
if (amt > max) {
max = amt;
pos = i;
}
}
out.println((max + 1) + " " + pos);
}
out.close();
}
static class FastScanner {
private int BS = 1<<16;
private char NC = (char)0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt=1;
boolean neg = false;
if(c==NC)c=getChar();
for(;(c<'0' || c>'9'); c = getChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=getChar()) {
res = (res<<3)+(res<<1)+c-'0';
cnt*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/cnt;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=getChar();
while(c>32) {
res.append(c);
c=getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=getChar();
while(c!='\n') {
res.append(c);
c=getChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=getChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | d4938dfd80b4bc1a760199741ca0f1e4 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;
public class A {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InputReader s = new InputReader(System.in);
PrintWriter p = new PrintWriter(System.out);
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
int k = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = s.nextInt();
int left = 0, max = 0, tempmax = 0, templeft = 0;
for (int i = 1; i < n - 1; i++) {
if (i < k - 1) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])
max++;
} else {
if (i == k - 1) {
tempmax = max;
left = 0;
}
templeft = i - (k-1)+1;
if (templeft > 0) {
if (arr[templeft] > arr[templeft - 1] && arr[templeft] > arr[templeft + 1])
tempmax--;
}
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])
tempmax++;
if (tempmax > max) {
max = tempmax;
left = templeft;
}
}
}
p.println((max + 1) + " " + (left + 1));
}
p.flush();
p.close();
}
public static boolean prime(long n) {
if (n == 1) {
return false;
}
if (n == 2) {
return true;
}
for (long i = 2; i <= (long) Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
public static ArrayList Divisors(long n) {
ArrayList<Long> div = new ArrayList<>();
for (long i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
div.add(i);
if (n / i != i)
div.add(n / i);
}
}
return div;
}
public static int BinarySearch(long[] a, long k) {
int n = a.length;
int i = 0, j = n - 1;
int mid = 0;
if (k < a[0])
return 0;
else if (k >= a[n - 1])
return n;
else {
while (j - i > 1) {
mid = (i + j) / 2;
if (k >= a[mid])
i = mid;
else
j = mid;
}
}
return i + 1;
}
public static long GCD(long a, long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
public static long LCM(long a, long b) {
return (a * b) / GCD(a, b);
}
static class pair implements Comparable<pair> {
Integer x, y;
pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if (result == 0)
result = y.compareTo(o.y);
return result;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x - x == 0 && p.y - y == 0;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(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;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class CodeX {
public static void sort(long arr[]) {
merge_sort(arr, 0, arr.length - 1);
}
private static void merge_sort(long A[], long start, long end) {
if (start < end) {
long mid = (start + end) / 2;
merge_sort(A, start, mid);
merge_sort(A, mid + 1, end);
merge(A, start, mid, end);
}
}
//re
private static void merge(long A[], long start, long mid, long end) {
long p = start, q = mid + 1;
long Arr[] = new long[(int) (end - start + 1)];
long k = 0;
for (int i = (int) start; i <= end; i++) {
if (p > mid)
Arr[(int) k++] = A[(int) q++];
else if (q > end)
Arr[(int) k++] = A[(int) p++];
else if (A[(int) p] < A[(int) q])
Arr[(int) k++] = A[(int) p++];
else
Arr[(int) k++] = A[(int) q++];
}
for (int i = 0; i < k; i++) {
A[(int) start++] = Arr[i];
}
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 18d911a7c6be89cae166f6a4870c97f9 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;
public class A {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InputReader s = new InputReader(System.in);
PrintWriter p = new PrintWriter(System.out);
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
int k = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = s.nextInt();
int left = 0, max = 0, tempmax = 0, templeft = 0;
for (int i = 1; i < n - 1; i++) {
if (i < k - 1) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])
max++;
} else {
if (i == k - 1) {
tempmax = max;
left = 0;
}
templeft = i - (k-1)+1;
if (templeft > 0) {
if (arr[templeft] > arr[templeft - 1] && arr[templeft] > arr[templeft + 1])
tempmax--;
}
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])
tempmax++;
if (tempmax > max) {
max = tempmax;
left = templeft;
}
}
}
p.println((max + 1) + " " + (left + 1));
}
p.flush();
p.close();
}
public static boolean prime(long n) {
if (n == 1) {
return false;
}
if (n == 2) {
return true;
}
for (long i = 2; i <= (long) Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
public static ArrayList Divisors(long n) {
ArrayList<Long> div = new ArrayList<>();
for (long i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
div.add(i);
if (n / i != i)
div.add(n / i);
}
}
return div;
}
public static int BinarySearch(long[] a, long k) {
int n = a.length;
int i = 0, j = n - 1;
int mid = 0;
if (k < a[0])
return 0;
else if (k >= a[n - 1])
return n;
else {
while (j - i > 1) {
mid = (i + j) / 2;
if (k >= a[mid])
i = mid;
else
j = mid;
}
}
return i + 1;
}
public static long GCD(long a, long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
public static long LCM(long a, long b) {
return (a * b) / GCD(a, b);
}
static class pair implements Comparable<pair> {
Integer x, y;
pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if (result == 0)
result = y.compareTo(o.y);
return result;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x - x == 0 && p.y - y == 0;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(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;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class CodeX {
public static void sort(long arr[]) {
merge_sort(arr, 0, arr.length - 1);
}
private static void merge_sort(long A[], long start, long end) {
if (start < end) {
long mid = (start + end) / 2;
merge_sort(A, start, mid);
merge_sort(A, mid + 1, end);
merge(A, start, mid, end);
}
}
private static void merge(long A[], long start, long mid, long end) {
long p = start, q = mid + 1;
long Arr[] = new long[(int) (end - start + 1)];
long k = 0;
for (int i = (int) start; i <= end; i++) {
if (p > mid)
Arr[(int) k++] = A[(int) q++];
else if (q > end)
Arr[(int) k++] = A[(int) p++];
else if (A[(int) p] < A[(int) q])
Arr[(int) k++] = A[(int) p++];
else
Arr[(int) k++] = A[(int) q++];
}
for (int i = 0; i < k; i++) {
A[(int) start++] = Arr[i];
}
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 8ca916992dc234525e6fdd1bba6f94cc | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 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 MiroO
*/
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();
}
static class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int p = in.readInt();
while (p-- > 0) {
int n = in.readInt();
int k = in.readInt();
int[] a = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = in.readInt();
}
int[] isPeak = new int[n];
for (int i = 1; i < isPeak.length - 1; i++) {
if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
isPeak[i] = 1;
}
}
int sum = 0;
int[] prefixSum = new int[n];
for (int i = 0; i < prefixSum.length; i++) {
sum += isPeak[i];
prefixSum[i] = sum;
}
int l = 0;
int max = 0;
for (int i = 0; i <= a.length - k; i++) {
int peaks = prefixSum[i + k - 2] - prefixSum[i];
if (peaks > max) {
max = peaks;
l = i;
}
// System.out.println("peaks " + i + " " + peaks);
}
out.println((max + 1) + " " + (l + 1));
}
// peaks namiesto sum
}
}
static class InputReader extends InputStream {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int 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 static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
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 println(String s) {
writer.println(s);
}
public void close() {
writer.close();
}
}
}
| Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 6dc957fc05eed3eb33b0fe05bb62f2d7 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.InputStreamReader;
import java.util.*;
import java.io.*;
public class q2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
while(test-->0){
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(st.nextToken());
}
int brr[] = new int[n];
int peak = 0;
int left = 0;
boolean lef = false;
boolean righ = false;
int right = 0;
for(int i=1;i<n-1;i++){
if(arr[i]>arr[i+1] && arr[i] > arr[i-1]){
peak++;
brr[i] = 1;
if(!lef){
lef = true;
left = i;
}
}
}
int crr[] = new int[n-k+1];
int sum = 0;
for(int i=0;i<k-1;i++){
if(brr[i]==1){
sum++;
}
}
crr[0] = sum;
for(int i=k;i<n;i++){
if(brr[i-1]==1){
if(brr[i-k+1]==1)
crr[i-k+1] = crr[i-k] ;
else
crr[i-k+1] = crr[i-k] + 1;
}
else{
if(brr[i-k+1]==1)
crr[i-k+1] = crr[i-k]-1;
else
crr[i-k+1] = crr[i-k];
}
}
int max = 0;
int indl = 0;
for(int i=0;i<n-k+1;i++){
if(crr[i] > max){
max = crr[i];
indl = i;
}
//System.out.print(crr[i]+" ");
}
//System.out.println();
System.out.println((max+1)+" "+(indl+1));
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 06befa8f88ce4538c1de2bd6166f40b2 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
public class Solution
{
public static void dfs(int arr[][],boolean[] v,int n,int h ){
v[h]=true;
boolean child=false;
for(int i=0;i<n;i++){
if(v[i]==false && arr[h][i]==1){
dfs(arr,v,n,i);
child=true;
}
}
if(child==false)
return;
}
public static void ans(int[] arr,int n,int k){
HashMap<Integer,Integer> peak=new HashMap<>();
int a[]=new int[n];
int p=0;
a[0]=0;
for(int i=1;i<n-1;i++){
if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){
peak.put(i,1);
p++;
}
a[i]=p;
}
a[n-1]=p;
//System.out.print(peak.size()+"*");
int max=Integer.MIN_VALUE;
int ml=-1;
for(int i=0;i<=n-k;i++){
int o=a[i+k-1];
if(peak.containsKey(i+k-1))
o--;
if(o-a[i]>max){
max=o-a[i];
ml=i;
}
}
System.out.println((max+1)+" "+(ml+1));
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
for(int i=0;i<t;i++){
StringTokenizer sa=new StringTokenizer(br.readLine());
int n=Integer.parseInt(sa.nextToken());
int k=Integer.parseInt(sa.nextToken());
int arr[]=new int[n];
StringTokenizer s=new StringTokenizer(br.readLine());
for(int j=0;j<n;j++)
arr[j]=Integer.parseInt(s.nextToken());
ans(arr,n,k);
}
}
}
class act{
int c;
int p;
int in;
public act(int x,int y,int z){
c=x;
p=y;
in=z;
}
}
class table{
int n;
int i;
public table(int x,int y){
n=x;
i=y;
}
}
class Sort implements Comparator<act>
{
// Used for sorting in ascending order of
// roll number
public int compare(act a, act b)
{
if(a.p==b.p)
return a.c - b.c;
else
//decsending order
return b.p-a.p;
}
}
class SortT implements Comparator<table>
{
// Used for sorting in ascending order of
// roll number
public int compare(table a, table b)
{
//acsending order
return a.n-b.n;
}
}
| Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 294134c06780755c80c5be3c4bd5099b | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-->0) {
int n = scan.nextInt();
int k = scan.nextInt();
int [] a = new int[n];
int ans = 0;
int l = 1;
boolean [] peak = new boolean[n];
for(int i = 0; i<n; i++) {
a[i] = scan.nextInt();
peak[i] = false;
}
for(int i = 1; i<n-1; i++) {
if(a[i] > a[i-1] && a[i] > a[i+1]) {
peak[i] = true;
}
}
int peaks = 0;
for(int i = 1; i<k-1; i++) {
if(peak[i]) {
peaks++;
}
}
ans = peaks;
for(int i = k; i<n; i++) {
if(peak[i-k+1]) {
peaks--;
}
if(i == k && peak[i-1]) {
peaks++;
}
//System.out.println("peaks " + peaks + " at " + i);
//System.out.println("ans is " + ans);
if(peaks > ans) {
ans = peaks;
l = i+2-k;
//System.out.println("l is " + l);
//System.out.println("when i is " + i);
}
if(peak[i]) {
peaks++;
}
}
System.out.println((ans+1) + " " + l);
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 3db6d03ef9988fdeb2a3b4d4301f5842 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-->0) {
int n = scan.nextInt();
int k = scan.nextInt();
int [] a = new int[n];
int ans = 0; //the number of peaks in the final answer
int l = 1;
int num = 0;
boolean [] peak = new boolean[n];
for(int i = 0; i<n; i++) {
a[i] = scan.nextInt();
peak[i] = false;
}
for(int i = 1; i<n-1; i++) {
if(a[i] > a[i-1] && a[i] > a[i+1]) {
peak[i] = true;
num++;
}
}
int peaks = 0;
int front = 0;
for(int i = 0; i<n; i++) {
if(i + 1 >= k) {
if((i+1) - k > 0) {
if(peak[i+1-k]) {
peaks--;
}
front++;
}
if(peaks > ans) {
ans = peaks;
l = ((i+1)-k) + 1;
}
}
if(peak[i]) {
peaks++;
}
}
System.out.println((ans+1) + " " + l);
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 17d4bd1cbb4e857f0a39c6fc602c6dcb | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-->0) {
int n = scan.nextInt();
int k = scan.nextInt();
int [] a = new int[n+1];
int ans = 0;
int [] p = new int[n+1];
int l = 1;
for(int i = 1; i<=n; i++) {
a[i] = scan.nextInt();
p[i] = 0;
}
for(int i = 2; i<=n-1; i++) {
if(a[i] > a[i-1] && a[i] > a[i+1]) {
p[i] = 1;
}
else {
p[i] = 0;
}
}
for(int i = 1; i<=n; i++) {
p[i] += p[i-1];
}
for(int i = 1; i<=n-k+1; i++) {
int peaks = p[i+k-2] - p[i];
if(peaks > ans) {
ans = peaks; l = i;
}
}
System.out.println((ans+1) + " " + l);
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | 4db693af907de56a24a75ee0ef071765 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.Arrays;
/**
* @author Alice
*/
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner str = new Scanner(System.in);
int t = str.nextInt();
while(t-- > 0) {
int n = str.nextInt();
int k = str.nextInt();
int []arr = new int[n];
int []a = new int[n];
int max = 0, index = 0;
for(int i = 0; i < n; i++) {
arr[i] = str.nextInt();
}
for(int i = 1; i < (n-1); i++) {
if(arr[i+1] < arr[i] && arr[i-1] < arr[i]) a[i] = 1;
}
for(int i = 1; i < n; i++) {
a[i] = a[i-1] + a[i];
}
for(int i = k-1, j = 0; i < n; i++,j++) {
int temp = a[i-1] - a[j];
if(max < temp) {
index = j;
max = temp;
}
}
System.out.println(max + 1 + " " + (index + 1));
}
}
}
| Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output | |
PASSED | a90d4124e8f6dabaf0b3d0e88a5af190 | train_002.jsonl | 1587653100 | On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $$$k$$$ ($$$k \ge 3$$$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights $$$a_1, a_2, \dots, a_n$$$ in order from left to right ($$$k \le n$$$). It is guaranteed that neighboring heights are not equal to each other (that is, $$$a_i \ne a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$).Peaks of mountains on the segment $$$[l,r]$$$ (from $$$l$$$ to $$$r$$$) are called indexes $$$i$$$ such that $$$l < i < r$$$, $$$a_{i - 1} < a_i$$$ and $$$a_i > a_{i + 1}$$$. It is worth noting that the boundary indexes $$$l$$$ and $$$r$$$ for the segment are not peaks. For example, if $$$n=8$$$ and $$$a=[3,1,4,1,5,9,2,6]$$$, then the segment $$$[1,8]$$$ has only two peaks (with indexes $$$3$$$ and $$$6$$$), and there are no peaks on the segment $$$[3, 6]$$$.To break the door, Nastya throws it to a segment $$$[l,l+k-1]$$$ of consecutive mountains of length $$$k$$$ ($$$1 \le l \le n-k+1$$$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $$$p+1$$$, where $$$p$$$ is the number of peaks on the segment $$$[l,l+k-1]$$$.Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $$$[l, l+k-1]$$$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $$$l$$$ is minimal.Formally, you need to choose a segment of mountains $$$[l, l+k-1]$$$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $$$l$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
private static boolean isSame(int a, int b) {
return (a < 0 && b < 0) || (a > 0 && b > 0);
}
private static void solve(MyScanner in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int a[] = new int[n+1];
for (int i=1;i<=n;i++) {
a[i] = in.nextInt();
}
int b[] = new int[n+1];
for (int i=2;i<n;i++) {
if (a[i] > a[i-1] && a[i]>a[i+1]) {
b[i] = 1;
}
}
int sum = 0;
for (int i=2;i<k;i++) {
sum += b[i];
}
int low = 1;
int ans = sum;
for (int i=k;i<n;i++) {
sum += b[i];
sum -= b[i - k + 2];
if (sum > ans) {
ans = sum;
low = i - k + 2;
}
}
out.printf("%d %d\n", ans+1, low);
}
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
for (int i=0;i<t;i++) {
solve(sc, out);
}
out.close();
}
public static PrintWriter out;
private static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["5\n8 6\n1 2 4 1 2 4 1 2\n5 3\n3 2 3 2 1\n10 4\n4 3 4 3 2 3 2 1 0 1\n15 7\n3 7 4 8 2 3 4 5 21 2 3 4 2 1 3\n7 5\n1 2 3 4 5 6 1"] | 1 second | ["3 2\n2 2\n2 1\n3 1\n2 3"] | NoteIn the first example, you need to select a segment of mountains from $$$2$$$ to $$$7$$$. In this segment, the indexes $$$3$$$ and $$$6$$$ are peaks, so the answer is $$$3$$$ (only $$$2$$$ peaks, so the door will break into $$$3$$$ parts). It is not difficult to notice that the mountain segments $$$[1, 6]$$$ and $$$[3, 8]$$$ are not suitable since they only have a $$$1$$$ peak (for the first segment, the $$$6$$$ index is not a peak, and for the second segment, the $$$3$$$ index is not a peak).In the second example, you need to select a segment of mountains from $$$2$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts).In the third example, you need to select a segment of mountains from $$$1$$$ to $$$4$$$. In this segment, the index $$$3$$$ is a peak, so the answer is $$$2$$$ (only $$$1$$$ peak, so the door will break into $$$2$$$ parts). You can see that on the segments $$$[2, 5]$$$, $$$[4, 7]$$$ and $$$[5, 8]$$$ the number of peaks is also $$$1$$$, but these segments have a left border greater than the segment $$$[1, 4]$$$, so they are not the correct answer. | Java 8 | standard input | [
"implementation",
"greedy"
] | 8e182e0acb621c86901fb94b56ff991e | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq k \leq n \leq 2 \cdot 10^5$$$) — the number of mountains and the length of the door. The second line of the input data set contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \leq a_i \leq 10 ^ 9$$$, $$$a_i \neq a_{i + 1}$$$) — the heights of mountains. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$2 \cdot 10^5$$$. | 1,300 | For each test case, output two integers $$$t$$$ and $$$l$$$ — the maximum number of parts that the door can split into, and the left border of the segment of length $$$k$$$ that the door should be reset to. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.