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 | dcd0c3c68dcf235792561561054fcda2 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 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.PrintWriter;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.StringTokenizer;
public class B
{
public static void main(String[] args) throws IOException
{
PrintWriter out = new PrintWriter(new File("output.txt"));
// PrintWriter out = new PrintWriter(System.out);
InputB in = new InputB();
int[] y =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int[] ds = new int[12];
ds[0] = 0;
for (int i = 1; i < ds.length; i++)
ds[i] = y[i - 1] + ds[i - 1];
int[] py = new int[370];
int n = in.nextInt();
int ans = 0;
for (int i = 0; i < n; i++)
{
int m = in.nextInt() - 1, d = in.nextInt(), p = in.nextInt(), t = in
.nextInt();
ans = Math.max(ans, p);
int dy = d + ds[m];
for (int j = dy - 1; j >= 0 && j >= dy - t; j--)
{
py[j] += p;
ans = Math.max(ans, py[j]);
}
}
out.println(ans);
out.close();
}
}
class Element implements Comparable<Element>
{
GregorianCalendar g;
int p;
int t;
int m;
int d;
public Element(int m, int d, int p, int t)
{
this.m = m;
this.d = d;
g = new GregorianCalendar(2013, m, d);
this.p = p;
this.t = t;
}
@Override
public int compareTo(Element o)
{
// TODO Auto-generated method stub
return this.g.compareTo(o.g);
}
@Override
public int hashCode()
{
return g.hashCode();
}
}
class InputB
{
BufferedReader bf;
StringTokenizer st;
public InputB() throws IOException
{
// bf = new BufferedReader(new InputStreamReader(System.in));
bf = new BufferedReader(new FileReader(new File("input.txt")));
st = new StringTokenizer(bf.readLine());
}
public String next() throws IOException
{
while (!st.hasMoreTokens() && bf.ready())
{
st = new StringTokenizer(bf.readLine());
}
return st.hasMoreTokens() ? st.nextToken() : null;
}
public boolean hasNext() throws IOException
{
while (!st.hasMoreTokens() && bf.ready())
{
st = new StringTokenizer(bf.readLine());
}
return st.hasMoreTokens();
}
public int nextInt() throws IOException
{
while (!st.hasMoreTokens() && bf.ready())
{
st = new StringTokenizer(bf.readLine());
}
return st.hasMoreTokens() ? new Integer(st.nextToken()) : 0;
}
public long nextLong() throws IOException
{
while (!st.hasMoreTokens() && bf.ready())
{
st = new StringTokenizer(bf.readLine());
}
return st.hasMoreTokens() ? new Long(st.nextToken()) : 0l;
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 0fdf5aabc92c0be54a6c18464cab8199 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.util.*;
import java.io.*;
public class search {
public static void main(String[] args) throws IOException {
//BufferedReader rd=new BufferedReader(new FileReader(new File("input.txt")));
Scanner sc=new Scanner(new File("input.txt"));
PrintWriter out=new PrintWriter(new File("output.txt"));
int n=sc.nextInt();
int [] passed=new int [12];
for (int i = 1; i < passed.length; i++) {
passed[i]=passed[i-1]+days[i-1];
}
int [][] mas = new int [n][3];
for (int i = 0; i < mas.length; i++) {
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int d=sc.nextInt();
int x=passed[a-1]+b-d;
mas[i][0]=x;
mas[i][1]=d;
mas[i][2]=c;
}
int max=-1;
for(int a=0;a<366;a++){
int t=0;
for (int i = 0; i< mas.length; i++) {
if(mas[i][0]<=a && mas[i][0]+mas[i][1]>a){
t+=mas[i][2];
}
}
if(t>max) max=t;
}
out.println(max);
out.flush();
}
static int [] days=new int[]{
31,28,31,30,31,30,31,31,30,31,30,31
};
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | fff734448733a92422057f5f8e5d91ab | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
public class B {
private static BufferedReader in;
private static StringTokenizer str;
private static String SK;
String next() throws Exception {
while ((str == null) || (!str.hasMoreTokens())) {
SK = in.readLine();
if (SK == null)
return null;
str = new StringTokenizer(SK);
}
return str.nextToken();
}
int nextInt() throws Exception {
return Integer.parseInt(next());
}
double nextDouble() throws Exception {
return Double.parseDouble(next());
}
long nextLong() throws Exception {
return Long.parseLong(next());
}
class Pair {
GregorianCalendar st, end;
public Pair(GregorianCalendar st, GregorianCalendar end) {
this.st = st;
this.end = end;
}
}
private void solve() throws Exception {
in = new BufferedReader(new FileReader("input.txt"));
PrintWriter out = new PrintWriter("output.txt");
int n = nextInt();
int needed = Integer.MIN_VALUE;
int d2012 = new GregorianCalendar(2012, 11, 31).get(GregorianCalendar.DAY_OF_YEAR);
int d2013 = new GregorianCalendar(2013, 11, 31).get(GregorianCalendar.DAY_OF_YEAR);
int[] days = new int[ d2012 + d2013];
GregorianCalendar st = new GregorianCalendar(), end = new GregorianCalendar();
for (int i = 0, m, d, p, t, from, to; i < n; i++) {
m = nextInt();
d = nextInt();
p = nextInt();
t = nextInt();
end.set(2013, m - 1, d);
st.set(2013, m - 1, d);
st.add(GregorianCalendar.DAY_OF_YEAR, - t);
from = st.get(GregorianCalendar.DAY_OF_YEAR);
if(st.get(GregorianCalendar.YEAR) == 2013) {
from += d2012;
}
to = end.get(GregorianCalendar.DAY_OF_YEAR);
if(end.get(GregorianCalendar.YEAR) == 2013) {
to += d2012;
}
for (int j = from; j < to; j++) {
days[j] += p;
needed = Math.max(needed, days[j]);
}
}
out.println(needed);
out.close();
}
public static void main(String[] args) throws Exception {
new B().solve();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | bc8e9b78e20dde8eed674899a26506c5 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
// Scanner sc = new Scanner(System.in);
Scanner sc = new Scanner(new FileReader("input.txt"));
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
int n = sc.nextInt();
int mm = 0;
int[] f = new int[600];
for (int i = 0; i < n; ++i) {
int m = sc.nextInt();
int d = sc.nextInt();
int p = sc.nextInt();
int t = sc.nextInt();
int days = getDays(m, d) + 100;
for (int j = days - t; j < days; ++j) {
f[j] += p;
mm = Math.max(mm, f[j]);
}
}
out.println(mm);
out.close();
}
public static int getDays(int m, int d) {
int[] arr = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int res = d;
for (int i = 0; i < m - 1; ++i) {
res += arr[i];
}
return res;
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 3854782fdb5fa390c875c3f6b9ae85ab | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.util.Scanner;
import static java.lang.Math.max;
import static java.util.Arrays.fill;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
// Scanner sc = new Scanner(System.in);
Scanner sc = new Scanner(new FileReader("input.txt"));
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
int n = sc.nextInt();
int mm = 0;
int[] f = new int[600];
fill(f, 0);
for (int i = 0; i < n; ++i) {
int m = sc.nextInt();
int d = sc.nextInt();
int p = sc.nextInt();
int t = sc.nextInt();
int days = getDays(m, d) + 100;
for (int j = days - t; j < days; ++j) {
f[j] += p;
mm = Math.max(mm, f[j]);
}
}
out.println(mm);
out.close();
}
public static int getDays(int m, int d) {
int[] arr = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int res = d;
for (int i = 0; i < m - 1; ++i) {
res += arr[i];
}
return res;
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | e3e9361f1a1f65d6ba18e4ebbe581a89 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes |
import java.util.Scanner;
import static java.lang.Math.max;
import static java.util.Arrays.fill;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
// Scanner sc = new Scanner(System.in);
Scanner sc = new Scanner(new FileReader("input.txt"));
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
int n = sc.nextInt();
int mm = 0;
int[] f = new int[600];
fill(f, 0);
for (int i = 0; i < n; ++i) {
int m = sc.nextInt();
int d = sc.nextInt();
int p = sc.nextInt();
int t = sc.nextInt();
int days = getDays(m, d) + 150;
for (int j = days - t; j < days; ++j) {
f[j] += p;
mm = Math.max(mm, f[j]);
}
}
out.println(mm);
out.close();
}
public static int getDays(int m, int d) {
int[] arr = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int res = d;
for (int i = 0; i < m - 1; ++i) {
res += arr[i];
}
return res;
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 700c56296d01ec926171d9e28bd2c3c9 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.util.*;
public class Example {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (Exception e) {
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
public void run() throws Exception {
//in = new BufferedReader(new InputStreamReader(System.in));
//out = new PrintWriter(System.out);
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter(new FileWriter("output.txt"));
solve();
out.flush();
out.close();
in.close();
}
public void solve() throws Exception {
int n = nextInt();
int[] mm = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[][][] c = new int[32][13][2];
int ans = 0;
for (int i = 0; i < n; i++) {
int m = nextInt();
int d = nextInt();
int p = nextInt();
int t = nextInt();
int g = 1;
for (int j = 0; j < t; j++) {
d--;
if (d == 0) {
m--;
if (m == 0) {
g--;
m = 12;
}
d = mm[m];
}
c[d][m][g] += p;
if (c[d][m][g] > ans) {
ans = c[d][m][g];
}
}
}
out.println(ans);
}
public static void main(String[] args) throws Exception {
new Example().run();
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 766289646239fd60f48c3172f8d59be1 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | //package example;
import java.io.*;
import java.util.*;
public class Example {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (Exception e) {
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
public void run() throws Exception {
//in = new BufferedReader(new InputStreamReader(System.in));
//out = new PrintWriter(System.out);
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter(new FileWriter("output.txt"));
solve();
out.flush();
out.close();
in.close();
}
public void solve() throws Exception {
int n = nextInt();
int[] mm = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[][][] c = new int[32][13][2];
int ans = 0;
for (int i = 0; i < n; i++) {
int m = nextInt();
int d = nextInt();
int p = nextInt();
int t = nextInt();
int g = 1;
for (int j = 0; j < t; j++) {
d--;
if (d == 0) {
m--;
if (m == 0) {
g--;
m = 12;
}
d = mm[m];
}
c[d][m][g] += p;
if (c[d][m][g] > ans) {
ans = c[d][m][g];
}
}
}
out.println(ans);
}
public static void main(String[] args) throws Exception {
new Example().run();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | f32fdaaeb58cf8b942800f744de2648d | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.util.*;
import java.io.*;
public class B {
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
try {
br = new BufferedReader(new InputStreamReader(in));
} catch (Exception e) {
e.printStackTrace();
}
}
public FastScanner(String in) {
try {
br = new BufferedReader(new FileReader(in));
} catch (Exception e) {
e.printStackTrace();
}
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
FastScanner in;
PrintWriter out;
void run() {
try {
in = new FastScanner("input.txt");
out = new PrintWriter("output.txt");
} catch (Exception e) {
e.printStackTrace();
}
solve();
out.close();
}
void solve() {
int n = in.nextInt();
int[] days = new int[365 + 366];
for (int i = 0; i < n; ++i) {
int month = in.nextInt();
--month;
int dayOfMonth = in.nextInt();
int day = new GregorianCalendar(2013, month, dayOfMonth).get(Calendar.DAY_OF_YEAR) + 365;
int workers = in.nextInt();
int time = in.nextInt();
for (int d = 0; d < time; ++d) {
days[day - d - 1] += workers;
}
}
int max = days[0];
for (int i = 1; i < days.length; ++i) {
max = Math.max(max, days[i]);
}
out.println(max);
}
public static void main(String[] args) {
new B().run();
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | ec1a076df15cc457fff7e2440bbf2dee | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class cf254b {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(new File("input.txt"));
PrintWriter out = new PrintWriter(new File("output.txt"));
// Scanner in = new Scanner(System.in);
// PrintWriter out = new PrintWriter(System.out);
int[] v = new int[1000];
int n = in.nextInt();
for(int i=0; i<n; i++) {
int m = in.nextInt()-1;
int d = in.nextInt()-1;
int p = in.nextInt();
int t = in.nextInt();
int end = day(m,d);
int start = end - t;
for(int j=start; j<end; j++)
v[j] += p;
}
int max = 0;
for(int x : v) {
max = Math.max(max,x);
}
out.println(max);
out.close();
}
static int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
static int day(int m, int d) {
int ans = 0;
for(int i=0; i<m; i++)
ans += days[i];
return ans + d + 200;
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | f81dabb389b692d9f2411a816df12f66 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class B {
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner in=new Scanner(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
in=new Scanner(new InputStreamReader(new FileInputStream("input.txt")));
out=new PrintWriter(new FileOutputStream("output.txt"));
int[] mas=new int[500];
int[] prev=new int[12];
prev[0]=31;//январь
prev[1]=62;//февраль
prev[2]=90;//март
prev[3]=121;//апрель
prev[4]=151;//май
prev[5]=182;//июнь
prev[6]=212;//июль
prev[7]=243;//август
prev[8]=274;//сентябрь
prev[9]=304;//октябрь
prev[10]=335;//ноябрь
prev[11]=365;//декабрь
for(int i=0; i<12; i++){
prev[i]+=100;
}
int n=in.nextInt();
for(int i=0; i<n; i++){
int m=in.nextInt();
int d=in.nextInt();
int p=in.nextInt();
int t=in.nextInt();
int end=prev[m-1]+d-1;
int start=end-t+1;
for(int j=start; j<=end; j++){
mas[j]+=p;
}
}
int max=0;
for(int i=0; i<500; i++){
if(mas[i]>max){
max=mas[i];
}
}
out.println(max);
out.close();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 0daf81ca303d02fef4da519c09e747b2 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
*/
public class Main {
private static BufferedReader br;
private static StringTokenizer st;
private static PrintWriter pw;
private static final Rohan BEGINNING_OF_ROHAN = new Rohan(1,1,2012);
public static void main(String[] args) throws IOException {
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
int[] numRohansNeeded = new int[1400];
int rohanRohans = readInt();
while(rohanRohans-- > 0) {
int monthOfRohan = readInt();
int dayOfRohan = readInt();
int numberOfRohansNeeded = readInt();
int timeInRohans = readInt();
Rohan r = new Rohan(dayOfRohan,monthOfRohan,2013);
while(timeInRohans-- > 0) {
r.previousRohan();
numRohansNeeded[r.internalRohan] += numberOfRohansNeeded;
}
}
Arrays.sort(numRohansNeeded);
pw.println(numRohansNeeded[numRohansNeeded.length-1]);
pw.close();
}
static class Rohan {
public static int[] maxRohans = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
public int rohanDay,rohanMonth,rohanYear,internalRohan;
public Rohan(int a, int b, int c) {
rohanDay=a;
rohanMonth=b;
rohanYear=c;
if(rohanYear == 2013)
determineIndex();
rohanDay=a;
rohanMonth=b;
rohanYear=c;
}
private void determineIndex() {
while(!equals(BEGINNING_OF_ROHAN)) {
previousRohan();
}
internalRohan = -internalRohan;
}
public void nextRohan() {
if(maxRohans[rohanMonth] == rohanDay) {
rohanDay = 1;
rohanMonth++;
if(rohanMonth > 12) {
rohanMonth = 1;
rohanYear++;
}
}
else {
rohanDay++;
}
internalRohan++;
}
public void previousRohan() {
if(rohanDay == 1) {
rohanMonth--;
if(rohanMonth == 0) {
rohanMonth = 12;
rohanYear--;
}
rohanDay = maxRohans[rohanMonth];
}
else {
rohanDay--;
}
internalRohan--;
}
public boolean equals(Object o) {
Rohan r = (Rohan)o;
return rohanDay==r.rohanDay && rohanMonth==r.rohanMonth && rohanYear==r.rohanYear;
}
}
/* NOTEBOOK CODE */
private static long readLong() throws IOException {
return Long.parseLong(nextToken());
}
private static double readDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private static int readInt() throws IOException {
return Integer.parseInt(nextToken());
}
private static String nextToken() throws IOException {
while(st == null || !st.hasMoreTokens()) {
if(!br.ready()) {
pw.close();
System.exit(0);
}
st = new StringTokenizer(br.readLine().trim());
}
return st.nextToken();
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | c9b45e94c8679a4b58527bd326a97f4e | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class JurySize {
BufferedReader r;
JurySize() throws FileNotFoundException{
r = new BufferedReader(new FileReader("input.txt"));
}
int[] input() throws IOException{
int data[]={1,2,3,4},i=0;
String str = r.readLine();
StringTokenizer st = new StringTokenizer(str);
data = new int[st.countTokens()];
while(st.hasMoreTokens()){
data[i] = Integer.parseInt(st.nextToken());
i++;
}
return data;
}
public int getDays(int m,int d){
int time[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},day=0;
for(int i=0;i<m-1;i++) day+=time[i];
day+=d;
return day;
}
public void process() throws IOException{
int n = input()[0];
int xet[] = new int[731];
for(int i=1;i<=n;i++){
int m,d,p,t;
int a[] = input();
m=a[0]; d=a[1]; p=a[2]; t=a[3];
int day = getDays(m,d)+365;
int dem=0;
while(t>0){
xet[day-t-1] += p;
t--;
dem++;
}
}
int max = 0;
for(int i=0;i<731;i++) max = Math.max(max, xet[i]);
PrintWriter w = new PrintWriter("output.txt");
w.write(max+"\n");
w.close();
}
public static void main(String[] args) throws IOException {
new JurySize().process();
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 9ea0483bdf2f0ac6f570d2d74ace35f4 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static BufferedReader kb;
static StringTokenizer str;
static PrintWriter out;
static String SK;
static String next() throws IOException {
while ((str == null) || (!str.hasMoreTokens())) {
SK = kb.readLine();
if (SK == null)
return null;
str = new StringTokenizer(SK);
}
return str.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static void main(String[] args) throws Exception {
/***********************************************************
* FileInputReader
**********************************************************/
kb = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
/***********************************************************
* StandardInput
**********************************************************/
// kb = new BufferedReader(new InputStreamReader(System.in));
// out = new PrintWriter(System.out);
/***********************************************************
* CodingArea
**********************************************************/
int n = nextInt();
int day[] = new int[600];
for (int i = 0; i < n; i++) {
int m = nextInt();
int d = nextInt();
int index = getDate(m, d);
int p = nextInt();
int t = nextInt();
for (int j = 1; j <= t; j++) {
day[index - j] += p;
}
}
int max = Integer.MIN_VALUE;
for (int i = 0; i < 600; i++) {
max = Math.max(max, day[i]);
}
out.println(max);
/***********************************************************
* End of CodingArea
**********************************************************/
out.close();
}
static int getDate(int m, int d) {
if (m == 1) {
return 200 + d;
} else if (m == 2) {
return 231 + d;
} else if (m == 3) {
return 259 + d;
} else if (m == 4) {
return 290 + d;
} else if (m == 5) {
return 320 + d;
} else if (m == 6) {
return 351 + d;
} else if (m == 7) {
return 381 + d;
} else if (m == 8) {
return 412 + d;
} else if (m == 9) {
return 443 + d;
} else if (m == 10) {
return 473 + d;
} else if (m == 11) {
return 504 + d;
} else if (m == 12) {
return 534 + d;
} else
return -1;
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | c7cc17a85c56a85804bebe36f63ee859 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B {
static StringTokenizer st;
static BufferedReader in;
static PrintWriter pw;
public static void main(String[] args) throws IOException {
in = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
int n = nextInt();
int[][][]cnt = new int[2][13][32];
for (int i = 1; i <= n; i++) {
int m = nextInt();
int d = nextInt();
int p = nextInt();
int t = nextInt();
int year = 2013;
for (int j = 1; j <= t; j++) {
cnt[year-2012][m][d] += p;
d--;
if (d==0) {
m--;
if (m==0) {
m = 12;
d = 31;
year--;
}
else {
if (m==2)
d = 28;
else if (m==4 || m==6 || m==9 || m==11)
d = 30;
else
d = 31;
}
}
}
}
int ans = 0;
for (int i = 0; i < 2; i++) {
for (int j = 1; j <= 12; j++) {
for (int j2 = 1; j2 <= 31; j2++) {
ans = Math.max(ans, cnt[i][j][j2]);
}
}
}
pw.println(ans);
pw.close();
}
private static int nextInt() throws IOException{
return Integer.parseInt(next());
}
private static long nextLong() throws IOException{
return Long.parseLong(next());
}
private static double nextDouble() throws IOException{
return Double.parseDouble(next());
}
private static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | ae5759fda6e87360a9c04c50e6927674 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Scanner;
public class B {
public class Task implements Comparable<Task>{
int m;
int d;
int t;
int p;
int id;
int sd;
int sm;
public Task(int m, int d, int p, int t, int sd, int sm, int id){
this.m = m;
this.d = d;
this.t = t;
this.p = p;
this.sd = sd;
this.sm = sm;
this.id = id;
}
@Override
public int compareTo(Task t) {
if(this.m > t.m || (this.m==t.m && this.d > t.m)) return -1;
return 1;
}
}
public void solve() throws IOException {
//Scanner sc = new Scanner(System.in);
Scanner sc = new Scanner(new File("input.txt"));
FileWriter fstream = new FileWriter("output.txt");
BufferedWriter out = new BufferedWriter(fstream);
int N = sc.nextInt();
ArrayList<Task> tasks = new ArrayList<Task>();
// start from -4
int[] dOfMonth = new int[]{ 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int cd = 100;
int cm = 100;
int ld = -100;
int lm = -100;
for(int i = 0; i < N; i++){
int m = sc.nextInt();
int d = sc.nextInt();
int p = sc.nextInt();
int t = sc.nextInt();
int tmpT =t;
int sd = d;
int sm = m;
while( sd <= tmpT){
sm--;
tmpT-= dOfMonth[3+sm];
}
sd -= tmpT;
tasks.add( new Task(m, d, p, t, sd, sm, i));
if( sm < cm || (sm == cm && sd < cd) ){
cd = sd;
cm = sm;
}
if( m > lm || (m == lm && d > ld) ){
ld = d;
lm = m;
}
}
//System.out.println( cd + ", " + cm + ", " + ld + ", " + lm);
//if(true)return;
//Collections.sort(tasks);
// for(int i = 0; i < tasks.size(); i++){
// System.out.println( tasks.get(i).id + ": " + tasks.get(i).p);
// }
int working = 0;
int spare = 0;
while(true ){
if(cm == lm && cd == ld) break;
for(int i = 0; i < tasks.size(); i++){
if( tasks.get(i).d == cd && tasks.get(i).m == cm){
spare += tasks.get(i).p;
working -= tasks.get(i).p;
//System.out.println( "free: " + cm + "- " + cd + ", " + working + ", " + spare + "T:" + i);
}
//System.out.println( tasks.get(i).id + ": " + tasks.get(i).sm + ", " + tasks.get(i).sd);
}
for(int i = 0; i < tasks.size(); i++){
if( tasks.get(i).sd == cd && tasks.get(i).sm == cm){
working += tasks.get(i).p;
spare = Math.max(0, spare-tasks.get(i).p);
//System.out.println( "work: " + cm + "- " + cd + ", " + working + ", " + spare + "T:" + i);
}
//System.out.println( tasks.get(i).id + ": " + tasks.get(i).sm + ", " + tasks.get(i).sd);
}
cd++;
if( cd > dOfMonth[3+cm]){
cd=1;
cm++;
}
//cm++;
}
//System.out.println( working + ", " + spare);
out.write( (working+spare) + "\n");
out.close();
}
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
new B().solve();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 0413e17e38087bd71d3a88f64a09a154 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.util.*;
public class B implements Runnable {
class Event implements Comparable<Event> {
int x, c;
Event(int x, int c) {
this.x = x;
this.c = c;
}
public int compareTo(Event o) {
if (x != o.x)
return x - o.x;
return o.c - c;
}
}
final int[] month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private void Solution() throws IOException {
int n = nextInt();
ArrayList<Event> a = new ArrayList<B.Event>();
for (int i = 0; i < n; i++) {
int m = nextInt() - 1, d = nextInt(), p = nextInt(), t = nextInt();
d = d - t + 366;
for (int j = 0; j < m; j++)
d += month[j];
for (int j = 0; j < p; j++) {
a.add(new Event(d, 1));
a.add(new Event(d + t - 1, -1));
}
}
Collections.sort(a);
int ans = 0, cur = 0;
for (Event now : a) {
cur += now.c;
ans = Math.max(ans, cur);
}
print(ans);
}
public static void main(String[] args) {
new B().run();
}
BufferedReader in;
PrintWriter out;
StringTokenizer tokenizer;
public void run() {
try {
long beginTime = System.nanoTime();
long beginMemory = totalMemory() - freeMemory();
String file = "";
file = "";
if (file.equals("")) {
in = new BufferedReader(new FileReader(new File("input.txt")));
out = new PrintWriter(new File("output.txt"));
} else {
in = new BufferedReader(new FileReader(new File(file + ".in")));
out = new PrintWriter(new File(file + ".out"));
}
Solution();
out.close();
long endTime = System.nanoTime();
long endMemory = totalMemory() - freeMemory();
double totalTime = (endTime - beginTime) * 1d / 1000000000;
double totalMemory = (endMemory - beginMemory) * 1d / 1024 / 1024;
System.err.println("Time wasted - " + totalTime + " s");
System.err.println("Memory used - " + totalMemory + " mb");
} catch (Exception e) {
e.printStackTrace();
System.exit(261);
}
}
long totalMemory() {
return Runtime.getRuntime().totalMemory();
}
long freeMemory() {
return Runtime.getRuntime().freeMemory();
}
void halt() {
out.close();
System.exit(0);
}
void print(Object... obj) {
for (int i = 0; i < obj.length; i++) {
if (i != 0)
out.print(" ");
out.print(obj[i]);
}
}
void println(Object... obj) {
print(obj);
out.println();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
return in.readLine();
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(nextLine());
return tokenizer.nextToken();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | feeb0a215fa14b19c3a3ccb7075048bc | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Duy Hung
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream;
try {
inputStream = new FileInputStream("input.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
OutputStream outputStream;
try {
outputStream = new FileOutputStream("output.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
int []days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int encode(int day, int month, int year) {
int cnt = 0;
while (day != 1 || month != 9 || year != 2012) {
// System.out.print(day);
// System.out.print(" ");
// System.out.print(month);
// System.out.print(" ");
// System.out.println(year);
cnt++;
day--;
if (day < 1) {
month--;
if (month < 1) {
year--;
month = 12;
}
day = days[month - 1];
}
}
return cnt;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int []cnt = new int[1000];
for (int i = 0; i < n; ++i) {
int month = in.nextInt(), day = in.nextInt(), year = 2013, p = in.nextInt(), t = in.nextInt();
int end = encode(day, month, year), start = end - t;
for (int j = start; j < end; ++j) cnt[j] += p;
}
int max = Integer.MIN_VALUE;
for (int i = 0; i < 1000; ++i) max = Math.max(max, cnt[i]);
out.printLn(max);
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(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 printLn(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 4ca08a3e1e8625eb54058ebd176ddf1c | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(new File("input.txt")));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(new File("output.txt"))));
int n = Integer.parseInt(br.readLine());
int[] dm = {100,31,28,31,30,31,30,31,31,30,31,30,31};
for (int i = 1; i < 13; i++) dm[i] = dm[i-1]+dm[i];
int[] s = new int[500];
String[] param;
int m, d, p, t;
for (int i = 0; i < n; i++) {
param = br.readLine().split(" ");
m = Integer.parseInt(param[0]);
d = Integer.parseInt(param[1]);
p = Integer.parseInt(param[2]);
t = Integer.parseInt(param[3]);
d = dm[m-1]+d;
for (int j = 1; j <= t; j++) s[d-j] += p;
}
int answer = 0;
for (int i = 0; i < 500; i++) answer = Math.max(answer, s[i]);
pw.println(answer);
pw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | f28fd3c417c2abacf43c8602b3e34807 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.util.Calendar;
public class CF254B{
static final long MTD = 1000*60*60*24;
public static void main(String args[]) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream("output.txt")));
Calendar cal = Calendar.getInstance();
cal.set(2012, 8, 1, 0, 0, 0);
int sdt = (int)(cal.getTimeInMillis()/MTD);
int[] pc = new int[365+122];
String line = reader.readLine();
int n = Integer.parseInt(line);
int min = 0;
for(int i=0; i<n; i++){
line = reader.readLine();
String[] prms = line.split(" ");
int mon = Integer.parseInt(prms[0]);
int day = Integer.parseInt(prms[1]);
int p = Integer.parseInt(prms[2]);
int t = Integer.parseInt(prms[3]);
cal.set(2013, mon-1, day, 0, 0, 0);
int dt = (int)(cal.getTimeInMillis()/MTD);
for(int j=0; j<t; j++){
dt--;
pc[dt-sdt] += p;
min = Math.max(min, pc[dt-sdt]);
}
}
writer.println(min);
reader.close();
writer.close();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 70dedab856f019a4273cfc18eb9a6eab | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.AbstractMap;
import java.util.NoSuchElementException;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Egor Kulikov (egor@egork.net)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream;
try {
inputStream = new FileInputStream("input.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
OutputStream outputStream;
try {
outputStream = new FileOutputStream("output.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int count = in.readInt();
int[] month = new int[count];
int[] day = new int[count];
int[] people = new int[count];
int[] time = new int[count];
IOUtils.readIntArrays(in, month, day, people, time);
Counter<Integer> counter = new Counter<Integer>();
for (int i = 0; i < count; i++) {
Date date = new Date(2013, month[i], day[i]);
int asInt = date.asInt();
for (int j = asInt - time[i]; j < asInt; j++) {
counter.add(j, people[i]);
}
}
out.printLine(CollectionUtils.maxElement(counter.values()));
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
class IOUtils {
public static void readIntArrays(InputReader in, int[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++)
arrays[j][i] = in.readInt();
}
}
}
class Counter<K> extends EHashMap<K, Long> {
public Counter() {
super();
}
public void add(K key, long delta) {
put(key, get(key) + delta);
}
public Long get(Object key) {
if (containsKey(key))
return super.get(key);
return 0L;
}
}
class Date implements Comparable<Date> {
private static final int[] DAYS_IN_MONTH = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public final int year;
public final int month;
public final int day;
private int weekday;
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
weekday = -1;
}
public int asInt() {
int day = (year - 1) * 365;
day += (year - 1) / 4;
day -= (year - 1) / 100;
day += (year - 1) / 400;
for (int i = 1; i < month; i++)
day += DAYS_IN_MONTH[i];
day += this.day;
if (isLeapYear(year) && month > 2)
day++;
return day;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Date date = (Date) o;
return day == date.day && month == date.month && year == date.year;
}
public int hashCode() {
int result = year;
result = 31 * result + month;
result = 31 * result + day;
return result;
}
public static boolean isLeapYear(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
public int compareTo(Date o) {
return asInt() - o.asInt();
}
public String toString() {
return toString("DD.MM.YYYY");
}
public String toString(String pattern) {
int copyYear = year;
int copyMonth = month;
int copyDay = day;
StringBuilder result = new StringBuilder();
for (int i = pattern.length() - 1; i >= 0; i--) {
char character = pattern.charAt(i);
switch (character) {
case 'D': case 'd':
result.append(copyDay % 10);
copyDay /= 10;
break;
case 'M': case 'm':
result.append(copyMonth % 10);
copyMonth /= 10;
break;
case 'Y': case 'y':
result.append(copyYear % 10);
copyYear /= 10;
break;
default:
result.append(character);
}
}
return result.reverse().toString();
}
}
class CollectionUtils {
public static<T extends Comparable<T>> T maxElement(Iterable<T> collection) {
T result = null;
for (T element : collection) {
if (result == null || result.compareTo(element) < 0)
result = element;
}
return result;
}
}
class EHashMap<E, V> extends AbstractMap<E, V> {
private static final int[] shifts = new int[10];
private int size;
private Object[] keys;
private Object[] values;
private int capacity;
private int shift;
private int[] indices;
private Set<Entry<E, V>> entrySet;
static {
Random random = new Random(System.currentTimeMillis());
for (int i = 0; i < 10; i++)
shifts[i] = 1 + 3 * i + random.nextInt(3);
}
public EHashMap() {
this(4);
}
private void setCapacity(int size) {
capacity = Integer.highestOneBit(10 * size);
keys = new Object[capacity];
values = new Object[capacity];
shift = capacity / 3 - 1;
shift -= 1 - (shift & 1);
indices = new int[capacity];
}
public EHashMap(int maxSize) {
setCapacity(maxSize);
entrySet = new AbstractSet<Entry<E, V>>() {
@Override
public Iterator<Entry<E, V>> iterator() {
return new Iterator<Entry<E, V>>() {
private HashEntry entry = new HashEntry();
int index = 0;
public boolean hasNext() {
while (index < capacity && keys[index] == null)
index++;
return index < capacity;
}
public Entry<E, V> next() {
if (!hasNext())
throw new NoSuchElementException();
entry.key = (E) keys[index];
entry.value = (V) values[index++];
return entry;
}
public void remove() {
if (entry.key == null)
throw new IllegalStateException();
EHashMap.this.remove(entry.key);
entry.key = null;
entry.value = null;
}
};
}
@Override
public int size() {
return size;
}
};
}
public EHashMap(Map<E, V> map) {
this(map.size());
putAll(map);
}
public Set<Entry<E, V>> entrySet() {
return entrySet;
}
public void clear() {
Arrays.fill(keys, null);
Arrays.fill(values, null);
size = 0;
}
private int index(Object o) {
return getHash(o.hashCode()) & (capacity - 1);
}
private int getHash(int h) {
int result = h;
for (int i : shifts)
result ^= h >>> i;
return result;
}
public V remove(Object o) {
if (o == null)
return null;
int index = index(o);
int indicesSize = 0;
while (keys[index] != null && !keys[index].equals(o)) {
indices[indicesSize++] = index;
index = (index + shift) & (capacity - 1);
}
if (keys[index] == null)
return null;
size--;
int lastIndex = indicesSize;
indices[indicesSize++] = index;
keys[index] = null;
V result = (V) values[index];
values[index] = null;
index = (index + shift) & (capacity - 1);
while (keys[index] != null) {
int curKey = index(keys[index]);
for (int i = 0; i <= lastIndex; i++) {
if (indices[i] == curKey) {
keys[indices[lastIndex]] = keys[index];
values[indices[lastIndex]] = values[index];
keys[index] = null;
values[index] = null;
lastIndex = indicesSize;
}
}
indices[indicesSize++] = index;
index = (index + shift) & (capacity - 1);
}
return result;
}
public V put(E e, V value) {
if (e == null)
return null;
int index = index(e);
while (keys[index] != null && !keys[index].equals(e))
index = (index + shift) & (capacity - 1);
if (keys[index] == null)
size++;
keys[index] = e;
values[index] = value;
if (size * 2 > capacity) {
Object[] oldKeys = keys;
Object[] oldValues = values;
setCapacity(size);
size = 0;
for (int i = 0; i < oldKeys.length; i++) {
if (oldKeys[i] != null)
put((E) oldKeys[i], (V) oldValues[i]);
}
}
return value;
}
public V get(Object o) {
if (o == null)
return null;
int index = index(o);
while (keys[index] != null && !keys[index].equals(o))
index = (index + shift) & (capacity - 1);
return (V) values[index];
}
public boolean containsKey(Object o) {
if (o == null)
return false;
int index = index(o);
while (keys[index] != null && !keys[index].equals(o))
index = (index + shift) & (capacity - 1);
return keys[index] != null;
}
public int size() {
return size;
}
private class HashEntry implements Entry<E, V> {
private E key;
private V value;
public E getKey() {
return key;
}
public V getValue() {
return value;
}
public V setValue(V value) {
put(key, value);
return this.value = value;
}
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | e0e4898c0d97e3445970d0b8a009c003 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.io.*;
import java.util.StringTokenizer;
public class Test {
BufferedReader in;
StringTokenizer str;
PrintWriter out;
String SK;
String next() throws IOException {
while ((str == null) || (!str.hasMoreTokens())) {
SK = in.readLine();
if (SK == null)
return null;
str = new StringTokenizer(SK);
}
return str.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
short nextShort() throws IOException {
return Short.parseShort(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void run() throws IOException {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
solve(in, out);
out.close();
}
private void solve(BufferedReader in, PrintWriter out) throws IOException {
final long MS = 24 * 3600 * 1000;
int n = nextInt();
Map<Long, Integer> map = new HashMap<Long, Integer>();
for (int i = 0; i < n; i++) {
int m = nextInt();
int d = nextInt();
int p = nextInt();
int t = nextInt();
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(2013, m - 1, d, 0, 0, 0);
long time = cal.getTimeInMillis() - MS;
for (int j = 0; j < t; j++) {
Integer cc = map.get(time);
if (cc == null) {
map.put(time, p);
} else {
map.put(time, cc + p);
}
time -= MS;
}
}
int max = 0;
for (Integer ii : map.values()) {
if (ii > max) {
max = ii;
}
}
out.append("" + max);
}
public static void main(String[] args) throws IOException {
new Test().run();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 91e1afebf73f8ac2781083e1a8776f70 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.util.*;
import java.io.*;
public class a {
public static void main(String[] args) throws IOException
{
Scanner input = new Scanner(new File("input.txt"));
PrintWriter out = new PrintWriter(new File("output.txt"));
//input.init(System.in);
//PrintWriter out = new PrintWriter(System.out);
int n = input.nextInt();
int[] dates = new int[n], people = new int[n], lengths = new int[n];
int[] dpm = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(int i = 0; i<n; i++)
{
int month = input.nextInt(), day = input.nextInt();
dates[i] = day;
for(int j = 0; j<month-1; j++) dates[i] += dpm[j];
people[i] = input.nextInt();
lengths[i] = input.nextInt();
}
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int i = 0; i<n; i++)
{
pq.add((dates[i] - lengths[i]+200)*1000 + people[i]);
pq.add((dates[i]+200)*1000-people[i]);
}
int cur = 0, max = 0;
while(!pq.isEmpty())
{
int at = pq.poll()%1000;
if(at>500)
{
cur += at-1000;
}
else
{
cur += at;
max = Math.max(max, cur);
}
}
out.println(max);
out.close();
}
static class input {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
static String nextLine() throws IOException {
return reader.readLine();
}
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 671e42b0a2a91131948f700fabb73b8c | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
PrintWriter out = new PrintWriter(new File("output.txt"));
// BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// PrintWriter out = new PrintWriter(System.out);
int[][] year = new int[16][];
year[0] = new int[30];
year[1] = new int[31];
year[2] = new int[30];
year[3] = new int[31];
year[4] = new int[31]; //2013
year[5] = new int[28];
year[6] = new int[31];
year[7] = new int[30];
year[8] = new int[31];
year[9] = new int[30];
year[10] = new int[31];
year[11] = new int[31];
year[12] = new int[30];
year[13] = new int[31];
year[14] = new int[30];
year[15] = new int[31];
int N = Integer.parseInt(in.readLine());
for(int i = 0; i < N; i++) {
StringTokenizer line = new StringTokenizer(in.readLine());
int m = Integer.parseInt(line.nextToken()) + 3;
int d = Integer.parseInt(line.nextToken());
int p = Integer.parseInt(line.nextToken());
int t = Integer.parseInt(line.nextToken());
for (int j = d - 2, done = 0; done < t; done++) {
if (j == -1) {
m--;
j = year[m].length - 1;
}
year[m][j] += p;
j--;
}
}
int answer = -1;
for(int i = 0; i < 16; i++) {
for (int j = 0; j < year[i].length; j++) {
answer = Math.max(answer, year[i][j]);
}
}
out.println(answer);
out.flush();
in.close();
out.close();
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | d2f4ac22ab24417585d2dc31093f3ba8 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class B {
private static final boolean DEBUG = false;
public static void main(String[] args) throws IOException {
InputStreamReader reader;
if (DEBUG) {
reader = new InputStreamReader(System.in);
} else {
reader = new InputStreamReader(new FileInputStream("input.txt"));
}
BufferedReader br = new BufferedReader(reader);
// init
TreeMap<String, Integer> calMap = new TreeMap<String, Integer>();
Calendar cal = GregorianCalendar.getInstance();
cal.set(2012, Calendar.JANUARY, 1);
Calendar endCal = GregorianCalendar.getInstance();
endCal.set(2014, Calendar.JANUARY, 1);
while (cal.before(endCal)) {
String s = toString(cal);
calMap.put(s, 0);
cal.add(Calendar.HOUR, 24);
}
int n = Integer.parseInt(br.readLine()); // skip
// StringTokenizer tokens = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
add(br.readLine(), calMap);
}
int max = 0;
int act = 0;
for (String string : calMap.keySet()) {
int tmp = calMap.get(string);
act += tmp;
if (act > max)
max = act;
}
PrintWriter writer = new PrintWriter(new File("output.txt"));
writer.print(max + "\n");
writer.flush();
writer.close();
}
private static void add(String line, TreeMap<String, Integer> calMap) {
StringTokenizer token = new StringTokenizer(line);
int m = Integer.parseInt(token.nextToken()) - 1;
int d = Integer.parseInt(token.nextToken());
int p = Integer.parseInt(token.nextToken());
int t = Integer.parseInt(token.nextToken());
Calendar os = Calendar.getInstance();
os.set(2013, m, d);
// os.add(-1*24, 1);
String oss = toString(os);
int val = calMap.get(oss);
if (DEBUG)
System.out.printf("putting %s %d\n", oss, val - p);
calMap.put(oss, val - p);
os.add(Calendar.HOUR, -t * 24);
oss = toString(os);
val = calMap.get(oss);
if (DEBUG)
System.out.printf("putting %s %d\n", oss, val + p);
calMap.put(oss, val + p);
}
private static String toString(Calendar cal) {
return String.format("%d-%2d-%2d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | d2a516740a6ef0aedef29a06b5f22473 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int nextInt() throws Exception {
in.nextToken();
return (int)in.nval;
}
static String nextString() throws Exception {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws Exception {
//Scanner in = null;
PrintWriter out = null;
if (args.length > 0 && args[0].equals("HOME")) {
//in = new Scanner(System.in);
out = new PrintWriter(System.out);
}
else {
//in = new Scanner(new File("input.txt"));
out = new PrintWriter(new File("output.txt"));
}
in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
//Parser in = new Parser(new FileInputStream("input.txt"));
int n = nextInt();
int[] cnt = new int[600];
for (int i = 0; i < n; i++) {
int m = nextInt(), d = nextInt(), p = nextInt(), t = nextInt();
int dd = getDay(m, d) + 150;
for (int j = dd - t; j < dd; j++) cnt[j] += p;
}
int ans = 0;
for (int i = 0; i < 600; i++) ans = Math.max(ans, cnt[i]);
out.println(ans);
out.flush();
}
static int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int getDay(int m, int d) {
int res = d;
for (int i = 0; i < m-1; i++) res += days[i];
return res;
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | e461422e90b058f5ad83ebbbd959f83a | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
PrintWriter out = new PrintWriter(new File("output.txt"));
// BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// PrintWriter out = new PrintWriter(System.out);
int[][] year = new int[16][];
year[0] = new int[30];
year[1] = new int[31];
year[2] = new int[30];
year[3] = new int[31];
year[4] = new int[31]; //2013
year[5] = new int[28];
year[6] = new int[31];
year[7] = new int[30];
year[8] = new int[31];
year[9] = new int[30];
year[10] = new int[31];
year[11] = new int[31];
year[12] = new int[30];
year[13] = new int[31];
year[14] = new int[30];
year[15] = new int[31];
int N = Integer.parseInt(in.readLine());
for(int i = 0; i < N; i++) {
StringTokenizer line = new StringTokenizer(in.readLine());
int m = Integer.parseInt(line.nextToken()) + 3;
int d = Integer.parseInt(line.nextToken());
int p = Integer.parseInt(line.nextToken());
int t = Integer.parseInt(line.nextToken());
for (int j = d - 2, done = 0; done < t; done++) {
if (j == -1) {
m--;
j = year[m].length - 1;
}
year[m][j] += p;
j--;
}
}
int answer = -1;
for(int i = 0; i < 16; i++) {
for (int j = 0; j < year[i].length; j++) {
answer = Math.max(answer, year[i][j]);
}
}
out.println(answer);
out.flush();
in.close();
out.close();
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | fff94ee1b665f0775cd1a9c634de752f | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class SolutionC {
BufferedReader in;
StringTokenizer str;
PrintWriter out;
String SK;
String next() throws IOException {
while ((str == null) || (!str.hasMoreTokens())) {
SK = in.readLine();
if (SK == null)
return null;
str = new StringTokenizer(SK);
}
return str.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void solve() throws IOException {
int month[]={31,29,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31};
int tot=0;
for(int i=0;i<month.length;i++)
tot+=month[i];
int mon[]=new int[month.length];
mon[0]=month[0];
for(int i=1;i<month.length;i++)
mon[i]=month[i]+mon[i-1];
int n=nextInt();
int cal[][]=new int[n][tot];
for(int i=0;i<n;i++){
int m=nextInt()+11;
int d=nextInt();
int j=nextInt();
int rd=nextInt();
for(int k=1;k<=rd;k++){
cal[i][mon[m-1]+d-k]+=j;
}
}
int max=0;
for(int i=0;i<tot;i++){
int t=0;
for(int j=0;j<n;j++){
t+=cal[j][i];
}
max=Math.max(t,max);
}
out.println(max);
}
void run() throws IOException {
//in = new BufferedReader(new InputStreamReader(System.in));
//out = new PrintWriter(System.out);
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new SolutionC().run();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | a1a483cea9a405ed11d8654ba28ded95 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.util.*;
import java.io.*;
public class ContA {
public static void main(String[] args) throws IOException {
// BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out);
BufferedReader rd=new BufferedReader(new FileReader("input.txt"));PrintWriter pw=new PrintWriter(new FileWriter("output.txt"));
StringTokenizer st = new StringTokenizer(rd.readLine());
int n = Integer.parseInt(st.nextToken());
O = new olymp[n];
S = new Segment[n];
for(int i=0; i<n; i++){
st = new StringTokenizer(rd.readLine());
int m = Integer.parseInt(st.nextToken()), d = Integer.parseInt(st.nextToken()), p = Integer.parseInt(st.nextToken()),
t = Integer.parseInt(st.nextToken());
O[i] = new olymp(m, d, t, p);
int day = dayOfYear(m, d);
S[i] = new Segment(day-t+1, day);
// System.out.println(day-t+1+" "+day);
for(int a=day-t+1; a<=day; a++){
num[a+500] += p;
}
}
int ans = bin(0, 10001);
pw.println(ans);
pw.flush();
}
static int[] num = new int[1000];
static Segment[] S;
static int dayOfYear(int m, int d){
int day = 0;
for(int i=1; i<m; i++) day += monthDays[i-1];
return day+d;
}
static olymp[] O;
static int bin(int L, int R){
if(L==R) return L;
if(R==L+1) return isEnough(L)? L: R;
int mid = (R+L)>>1;
return isEnough(mid)? bin(L, mid): bin(mid+1, R);
}
static int[] numStart = new int[1000];
static boolean isEnough(int numWorkers){
int busy = 0;
for(int i=-100; i<=365; i++){
int nOlympsThatDay = num[i+500];
if(nOlympsThatDay>numWorkers) return false;
}
return true;
}
static final int[] monthDays = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
}
class olymp{
public int month, day, t, p;
public olymp(int m, int d, int t, int p){
month = m;
day = d;
this.t = t;
this.p = p;
}
}
class Segment{
public int l, r;
public Segment(int x, int y){
l = x; r = y;
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | c546174f2b418744f63ddc2070e78a25 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author pttrung
*/
public class B {
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner();
//PrintWriter out = new PrintWriter(System.out);
PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt")));
int[] month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n = in.nextInt();
int[][] cur = new int[12][32];
int[][] last = new int[12][32];
for (int i = 0; i < n; i++) {
int m = in.nextInt() - 1;
int d = in.nextInt();
int p = in.nextInt();
int t = in.nextInt();
boolean l = false;
d--;
if (d == 0) {
m--;
if (m == -1) {
l = true;
m = 11;
}
d = month[m];
}
while (t > 0) {
if (l) {
last[m][d] += p;
} else {
cur[m][d] += p;
}
t--;
d--;
if (d == 0) {
m--;
if (m == -1) {
l = true;
m = 11;
}
d = month[m];
}
}
}
int min = 0;
for (int i = 0; i < 12; i++) {
for (int j = 1; j < 32; j++) {
min = Math.max(min, last[i][j]);
min = Math.max(min, cur[i][j]);
}
}
out.println(min);
out.close();
}
static class Node implements Comparable<Node> {
int index, type;
long price;
public Node(int index, int type, long price) {
this.index = index;
this.type = type;
this.price = price;
}
@Override
public int compareTo(Node o) {
if (price > o.price) {
return 1;
} else if (price == o.price) {
return 0;
}
return -1;
}
}
static class FT {
int[] data;
FT(int n) {
data = new int[n];
}
void update(int index, int val) {
// System.out.println("UPDATE INDEX " + index);
while (index < data.length) {
data[index] += val;
index += index & (-index);
// System.out.println("NEXT " +index);
}
}
int get(int index) {
// System.out.println("GET INDEX " + index);
int result = 0;
while (index > 0) {
result += data[index];
index -= index & (-index);
// System.out.println("BACK " + index);
}
return result;
}
}
public static double dist(Point a, Point b) {
double val = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
return Math.sqrt(val);
}
public static class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return val * val;
} else {
return val * val * a;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
//br = new BufferedReader(new InputStreamReader(System.in));
br = new BufferedReader(new FileReader(new File("input.txt")));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | b0e9f66d193831bb85e564dd22d58394 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | //package codeforces.cf121209;
import java.io.*;
public class ProblemB {
BufferedReader reader;
PrintWriter writer;
public ProblemB() throws IOException {
// reader = new BufferedReader(new InputStreamReader(System.in));
// writer = new PrintWriter(new OutputStreamWriter(System.out));
reader = new BufferedReader(new FileReader("input.txt"));
writer = new PrintWriter(new FileWriter("output.txt"));
}
int[] readInts() throws IOException {
String[] strings = reader.readLine().split(" ");
int[] ints = new int[strings.length];
for(int i = 0; i < ints.length; i++) {
ints[i] = Integer.parseInt(strings[i]);
}
return ints;
}
void solve() throws IOException {
int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] a = new int[1000];
int n = readInts()[0];
for(int i = 0; i < n; i++) {
int[] x = readInts();
int m = x[0];
int d = x[1];
int p = x[2];
int t = x[3];
int ix = 500;
int month = 1, day = 1;
while(month != m || day != d) {
ix++;
day++;
if(day > days[month]) {
day = 1;
month++;
}
}
for(int j = 0; j < t; j++) {
a[ix - j] += p;
}
}
int ans = 0;
for(int i: a) {
ans = Math.max(ans, i);
}
writer.println(ans);
writer.flush();
}
public static void main(String[] args) throws IOException{
new ProblemB().solve();
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 1e8f7200522af023fb571818020b3865 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class ProblemB {
static final int[] MDAY = {0,
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static int encode(int m, int d) {
int ret = 0;
for (int i = 1 ; i < m ; i++) {
ret += MDAY[i];
}
return 365 + ret + d;
}
static class OI {
int fr;
int to;
int mem;
OI(int a, int b, int c, int d) {
to = encode(a, b);
fr = to - d;
mem = c;
}
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new FileReader("input.txt"));
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
// Scanner in = new Scanner(System.in);
// PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
OI[] oi = new OI[n];
for (int i = 0 ; i < n ; i++) {
oi[i] = new OI(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
}
int[] ev = new int[1000];
for (int i = 0 ; i < n ; i++) {
ev[oi[i].fr] += oi[i].mem;
ev[oi[i].to] -= oi[i].mem;
}
int up = 10000000;
int lo = 0;
while (up - lo > 1) {
int med = (up + lo) / 2;
if (ispossible(med, ev)) {
up = med;
} else {
lo = med;
}
}
out.println(up);
out.flush();
}
private static boolean ispossible(int now, int[] ev) {
for (int i = 0 ; i < 1000 ; i++) {
if (ev[i] > 0) {
now -= ev[i];
if (now < 0) {
return false;
}
} else if (ev[i] < 0) {
now += (-ev[i]);
}
}
return true;
}
public static void debug(Object... o) {
System.err.println(Arrays.deepToString(o));
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 25131e96bccda8ef4f47be1a983d1c12 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | //package round155;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
public class B {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni();
int[] q = new int[731];
int o = 365;
for(int i = 0;i < n;i++){
int a = ni(), b = ni(), c = ni(), d = ni();
for(int j = 1;j <= d;j++){
q[CUMDOM[a-1]+b-1-j+o] += c;
}
}
int max = 0;
for(int i = 0;i < 731;i++){
max = Math.max(max, q[i]);
}
out.println(max);
}
public static int[] CUMDOM = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
void run() throws Exception
{
is = oj ? new FileInputStream("input.txt") : new ByteArrayInputStream(INPUT.getBytes());
out = oj ? new PrintWriter("output.txt") : new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception
{
new B().run();
}
public int ni()
{
try {
int num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public long nl()
{
try {
long num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public String ns()
{
try{
int b = 0;
StringBuilder sb = new StringBuilder();
while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' '));
if(b == -1)return "";
sb.append((char)b);
while(true){
b = is.read();
if(b == -1)return sb.toString();
if(b == '\r' || b == '\n' || b == ' ')return sb.toString();
sb.append((char)b);
}
} catch (IOException e) {
}
return "";
}
public char[] ns(int n)
{
char[] buf = new char[n];
try{
int b = 0, p = 0;
while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n'));
if(b == -1)return null;
buf[p++] = (char)b;
while(p < n){
b = is.read();
if(b == -1 || b == ' ' || b == '\r' || b == '\n')break;
buf[p++] = (char)b;
}
return Arrays.copyOf(buf, p);
} catch (IOException e) {
}
return null;
}
double nd() { return Double.parseDouble(ns()); }
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | dd2df28541db7e75de0553fdee5e6c76 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author lwc626
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream;
try {
inputStream = new FileInputStream("input.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
OutputStream outputStream;
try {
outputStream = new FileOutputStream("output.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
MyInputReader in = new MyInputReader(inputStream);
MyOutputWriter out = new MyOutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
static final int MAXN = 1000;
public void solve(int testNumber, MyInputReader in, MyOutputWriter out) {
int n = in.nextInt();
int[] peoples = new int[MAXN];
Arrays.fill(peoples, 0);
for (int i = 0; i < n; i ++) {
int m = in.nextInt(), d = in.nextInt(), p = in.nextInt(), t = in.nextInt();
int last = get_days(m, d);
for (int j = 0; j < t; j ++) {
if(last - 1 - j >= 0 && last - j - 1 < MAXN)
peoples[last - 1 - j] += p;
}
}
int ret = 0;
for (int j = 0; j < MAXN; j ++)
ret = Math.max(ret, peoples[j]);
out.printLine(ret);
}
private int get_days(int m, int d) {
int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int days = 0;
for (int j = 0; j < m - 1; j ++){
if (j < months.length)
days += months[j];
}
return days + d + 200 ;
}
}
class MyInputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public MyInputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt(){
return readInt() ;
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
class MyOutputWriter {
private final PrintWriter writer;
public MyOutputWriter(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public MyOutputWriter(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 | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | c75317adcbe254f850a502ee92c40ac0 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author lwc626
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream;
try {
inputStream = new FileInputStream("input.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
OutputStream outputStream;
try {
outputStream = new FileOutputStream("output.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
MyInputReader in = new MyInputReader(inputStream);
MyOutputWriter out = new MyOutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
static final int MAXN = 1000;
public void solve(int testNumber, MyInputReader in, MyOutputWriter out) {
int n = in.nextInt();
int[] peoples = new int[MAXN];
Arrays.fill(peoples, 0);
for (int i = 0; i < n; i ++) {
int m = in.nextInt(), d = in.nextInt(), p = in.nextInt(), t = in.nextInt();
int last = get_days(m, d);
for (int j = 0; j < t; j ++) {
if(last - 1 - j >= 0 && last - j - 1 < MAXN)
peoples[last - 1 - j] += p;
}
}
int ret = 0;
for (int j = 0; j < MAXN; j ++)
ret = Math.max(ret, peoples[j]);
out.printLine(ret);
}
private int get_days(int m, int d) {
int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int days = 0;
for (int j = 0; j < m - 1; j ++){
//if (j < months.length)
days += months[j];
}
return days + d + 200 ;
}
}
class MyInputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public MyInputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt(){
return readInt() ;
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
class MyOutputWriter {
private final PrintWriter writer;
public MyOutputWriter(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public MyOutputWriter(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 | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | f571161cbd75abcc79d61af381ae7d95 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author lwc626
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream;
try {
inputStream = new FileInputStream("input.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
OutputStream outputStream;
try {
outputStream = new FileOutputStream("output.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
MyInputReader in = new MyInputReader(inputStream);
MyOutputWriter out = new MyOutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
static final int MAXN = 1000;
public void solve(int testNumber, MyInputReader in, MyOutputWriter out) {
int n = in.nextInt();
int[] peoples = new int[MAXN];
Arrays.fill(peoples, 0);
for (int i = 0; i < n; i ++) {
int m = in.nextInt(), d = in.nextInt(), p = in.nextInt(), t = in.nextInt();
int last = get_days(m, d);
for (int j = 0; j < t; j ++) {
//if(last - 1 - j >= 0 && last - j - 1 < MAXN)
peoples[last - 1 - j] += p;
}
}
int ret = 0;
for (int j = 0; j < MAXN; j ++)
ret = Math.max(ret, peoples[j]);
out.printLine(ret);
}
private int get_days(int m, int d) {
int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int days = 0;
for (int j = 0; j < m - 1; j ++){
//if (j < months.length)
days += months[j];
}
return days + d + 200 ;
}
}
class MyInputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public MyInputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt(){
return readInt() ;
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
class MyOutputWriter {
private final PrintWriter writer;
public MyOutputWriter(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public MyOutputWriter(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 | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | ce04f97c75f6ce311f7a5215005367f3 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class B {
public static void main(String[] args) throws IOException{
(new B()).runFile();
}
StreamTokenizer in;
PrintWriter out;
int nextInt() throws IOException{
in.nextToken();
return (int)in.nval;
}
void run() throws IOException {
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
out.flush();
}
void runFile() throws IOException {
in = new StreamTokenizer(new FileReader("input.txt"));
out = new PrintWriter(new File("output.txt"));
solve();
out.flush();
}
void solve() throws IOException {
int mounth [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 1 2 3 4 5 6 7 8 9 10 11 12
int sums[] = new int[13];
int delta = 444;
sums[0] = 0;
for (int i = 1; i <= 12; i++) {
sums[i] = sums[i - 1] + mounth[i - 1];
}
int n = nextInt();
int maxN = 1111;
int S[] = new int[maxN];
for (int i = 0; i < n; i++) {
int m, d, p, t;
m = nextInt();
d = nextInt();
p = nextInt();
t = nextInt();
int r = sums[m - 1] + d - 1 + delta;
int l = r - t + 1;
S[l] += p;
S[r + 1] -= p;
}
int res = 1;
for (int i = 0, cnt = 0; i < maxN; i++) {
cnt += S[i];
if (cnt > res) {
res = cnt;
}
}
out.println(res);
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 70df25bb5bfa010597c128b02746eec4 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.util.*;
import java.io.*;
public class solve_dk {
BufferedReader br;
PrintWriter out;
StringTokenizer stok;
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) {
return null;
}
stok = new StringTokenizer(s);
}
return stok.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());
}
char nextChar() throws IOException {
return (char) (br.read());
}
String nextLine() throws IOException {
return br.readLine();
}
class Friend implements Comparable<Friend> {
int l, r, v, num;
public Friend(int l, int r, int v, int num) {
super();
this.l = l;
this.r = r;
this.v = v;
this.num = num;
}
public int compareTo(Friend oth) {
return (this.v - oth.v);
}
}int[] m, d, p ,t;
int n;
boolean check(int x) {
for (int i = 1; i < 1000; i++) {
for (int j = 0; j < n; j++) {
if (d[j] == i) {
x += p[j];
}
}
for (int j = 0; j < n; j++) {
if (d[j] - t[j]== i) {
x -= p[j];
if (x < 0) return false;
}
}
}
return true;
}
void solve() throws IOException {
n = nextInt();
m = new int[n];
d = new int[n];
p = new int[n];
t = new int[n];
for (int i = 0; i < n; i++) {
m[i] = nextInt();
d[i] = nextInt();
p[i] = nextInt();
t[i] = nextInt();
}
int[] part = new int[13];
part[1] = 366;
part[2] = part[1] + 31;
part[3] = part[2] + 28;
part[4] = part[3] + 31;
part[5] = part[4] + 30;
part[6] = part[5] + 31;
part[7] = part[6] + 30;
part[8] = part[7] + 31;
part[9] = part[8] + 31;
part[10] = part[9] + 30;
part[11] = part[10] + 31;
part[12] = part[11] + 30;
for (int i = 0; i < n; i++) {
d[i] = part[m[i]] + d[i];
}
int l = 0;
int r = 10000000;
while (l < r - 1) {
int mid = (l + r) / 2;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
if (!check(l)) l++;
out.println(l);
}
void run() throws IOException {
//br = new BufferedReader(new FileReader("huffman.in"));
//out = new PrintWriter("huffman.out");
br = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
// br = new BufferedReader(new InputStreamReader(System.in));
// out = new PrintWriter(System.out);
solve();
br.close();
out.close();
}
public static void main(String[] args) throws IOException {
// Localea.setDefault(Locale.US);
new solve_dk().run();
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 440243daf3595e355be94d359525b1c0 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
public class B {
public static int[] readNNumbers(BufferedReader bf, int n) throws IOException{
int[] nums = new int[n];
int idx = 0;
String line = bf.readLine();
for(int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if(c == ' ') idx++;
else {
int d = c - '0';
nums[idx] = 10 * nums[idx] + d;
}
}
return nums;
}
public static int[] readNums(BufferedReader bf) throws IOException {
int idx = 0, n = 0;
String line = bf.readLine();
int i;
for(i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if(c == ' ') break;
int d = c - '0';
n = 10 * n + d;
}
int[] nums = new int[n];
for(i = i + 1; i < line.length(); i++) {
char c = line.charAt(i);
if(c == ' ') idx++;
else {
int d = c - '0';
nums[idx] = 10 * nums[idx] + d;
}
}
return nums;
}
static class Olympiad implements Comparable<Olympiad>{
int yearDayContest;
int yearDayPrep;
int juries;
int time;
public Olympiad(int yearDay, int juries, int time) {
this.yearDayContest = yearDay;
this.yearDayPrep = yearDayContest - time;
this.juries = juries;
this.time = time;
}
@Override
public int compareTo(Olympiad o) {
return yearDayPrep - o.yearDayPrep;
}
}
public static final Date first = new Date(2013 - 1900, 0, 1);
public static final long MILIS_DAY = 1000L * 60L * 60L * 24L;
public static int yearDay2013(int month, int day) {
Date d = new Date(2013 - 1900, month - 1, day);
long time = d.getTime() - first.getTime();
return (int)(time / MILIS_DAY);
}
static class Event implements Comparable<Event> {
int day;
boolean isStart;
int olympiad;
public Event(int day, boolean isStart, int olympiad) {
this.day = day;
this.isStart = isStart;
this.olympiad = olympiad;
}
@Override
public int compareTo(Event o) {
if(day == o.day) {
if(isStart == o.isStart) return 0;
if(!isStart && o.isStart) return -1;
return 1;
}
return day - o.day;
}
}
public static void main(String[] args) throws IOException {
PrintStream out = new PrintStream("output.txt");
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
int N = Integer.parseInt(bf.readLine());
Olympiad[] olympiads = new Olympiad[N];
Event[] events = new Event[2 * N];
for(int i = 0; i < N; i++) {
int[] input = readNNumbers(bf, 4);
int month = input[0], day = input[1], juries = input[2], time = input[3];
int dayYear = yearDay2013(month, day);
olympiads[i] = new Olympiad(dayYear, juries, time);
events[2 * i] = new Event(dayYear - time, true, i);
events[2 * i + 1] = new Event(dayYear, false, i);
}
Arrays.sort(events);
int total = 0, available = 0;
for(int i = 0; i < events.length; i++) {
if(events[i].isStart) {
int djuries = available - olympiads[events[i].olympiad].juries;
if(djuries >= 0)
available = djuries;
else {
total -= djuries;
available = 0;
}
}
else {
available += olympiads[events[i].olympiad].juries;
}
}
out.println(total);
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | ff31072be3fae99617cbd71ca31e287c | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.util.*;
public class P254B
{
public static StringTokenizer st;
public static void nextLine(BufferedReader br) throws IOException
{
st = new StringTokenizer(br.readLine());
}
public static String next()
{
return st.nextToken();
}
public static int nextInt()
{
return Integer.parseInt(st.nextToken());
}
public static long nextLong()
{
return Long.parseLong(st.nextToken());
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
nextLine(br);
int n = nextInt();
int[] date = new int[n];
int[] p = new int[n];
int[] t = new int[n];
PriorityQueue<Pair> pq = new PriorityQueue<Pair>();
for (int i = 0; i < n; i++)
{
nextLine(br);
int m = nextInt();
int d = nextInt();
date[i] = convDate(m, d);
p[i] = nextInt();
t[i] = nextInt();
int start = date[i] - t[i];
int end = date[i] - 1;
pq.offer(new Pair(start, i));
pq.offer(new Pair(end, i+1000));
}
int cur = 0;
int max = 0;
while (!pq.isEmpty())
{
Pair pair = pq.poll();
if (pair.pos >= 1000)
{
cur -= p[pair.pos - 1000];
}
else
{
cur += p[pair.pos];
if (cur > max) max = cur;
}
}
PrintWriter pw = new PrintWriter(new File("output.txt"));
pw.println(max);
pw.close();
}
static int[] days = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int convDate(int m, int d)
{
int sum = 0;
for (int i = 0; i < m-1; i++)
{
sum += days[i];
}
return sum + d;
}
public static class Pair implements Comparable<Pair>
{
int val;
int pos;
public Pair (int v, int p)
{
val = v; pos = p;
}
public int compareTo(Pair p)
{
if (val == p.val)
{
return pos - p.pos;
}
return val - p.val;
}
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 8043946d588cc6a5e4f31c8cdd8c542a | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class CF155_2_B {
BufferedReader in;
StringTokenizer str;
PrintWriter out;
String SK;
String next() throws IOException {
while ((str == null) || (!str.hasMoreTokens())) {
SK = in.readLine();
if (SK == null)
return null;
str = new StringTokenizer(SK);
}
return str.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void run() throws IOException {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
//in = new BufferedReader(new InputStreamReader(System.in));
//out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new CF155_2_B().run();
}
int a[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int cal(int mon, int day) {
int i, ans = day;
for (i = 1; i < mon; i++) ans += a[i];
return ans;
}
void solve() throws IOException {
int n, i, j, mon, day, p, t, l, r, tmp, ans;
TreeMap<Integer, Integer> q = new TreeMap<Integer, Integer>();
n = nextInt();
for (i = 1; i <= n; i++) {
mon = nextInt();
day = nextInt();
p = nextInt();
t = nextInt();
r = cal(mon, day) - 1;
l = r - t + 1;
for (j = l; j <= r; j++) {
if (q.containsKey(j)) {
tmp = q.get(j);
q.put(j, tmp + p);
} else {
q.put(j, p);
}
}
}
ans = 0;
for (int k: q.values()) {
ans = Math.max(ans, k);
}
out.println(ans);
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | e21eaa4c3d8cb42a382c8244532555a3 | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class B {
public static void main(String[] args) throws Exception {
new B().solve(new FileInputStream(new File("input.txt")),
new PrintStream(new FileOutputStream(new File("output.txt"))));
}
void solve(InputStream _in, PrintStream out) throws IOException {
// BufferedReader in = new BufferedReader(new InputStreamReader(_in));
// String[] sp;
Scanner sc = new Scanner(_in);
int[] mdays = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31 };
int n = sc.nextInt();// 1~100
int[] s = new int[n];
int[] e = new int[n];
int[] p = new int[n];
int mind = 100000, maxd = -100000;
for (int i = 0; i < n; i++) {
int mm = sc.nextInt();
int dd = sc.nextInt();
p[i] = sc.nextInt();// 1~100
int tt = sc.nextInt();// 1~100
int d = dd;
for (int jj = 0; jj < mm - 1; jj++) {
d += mdays[jj];
}
e[i] = d + 200;
s[i] = e[i] - tt;
mind = Math.min(mind, s[i]);
maxd = Math.max(maxd, e[i]);
}
int ret = 0;
for (int dd = mind; dd < maxd; dd++) {// maxd含まず
int count = 0;
for (int i = 0; i < n; i++) {
if (s[i] <= dd && dd < e[i]) {
count += p[i];
}
}
ret = Math.max(ret, count);
}
out.println(ret);
}
}
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | ee28949665f5490c93da6dabf635763b | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
public class B {
BufferedReader in;
StringTokenizer str;
PrintWriter out;
String SK;
void solve() throws IOException {
int N = readInt();
int C[] = new int[1000];
for (int _ = 0; _ < N; _++){
GregorianCalendar c = new GregorianCalendar(2013, readInt()-1, readInt());
int p = readInt(), t = readInt();
int d = c.get(Calendar.DAY_OF_YEAR) + 234;
for (int i = 0; i < t; i++){
C[d-i]+=p;
}
}
int m = 0;
for (int c : C)
m = Math.max(c, m);
out.println(m);
}
String readString() throws IOException {
while ((str == null) || (!str.hasMoreTokens())) {
SK = in.readLine();
if (SK == null)
return null;
str = new StringTokenizer(SK);
}
return str.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
void run() throws IOException {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new B().run();
}
} | Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 1d665fa1a687cfb004ef528b65e16aee | train_001.jsonl | 1355047200 | In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
public static void main( String[] args ) throws FileNotFoundException {
new CF().doit();
}
}
class CF {
InputReader ir;
PrintStream ps;
public void doit() throws FileNotFoundException {
// ir = new InputReader( System.in );
// ps = System.out;
ir = new InputReader( new FileInputStream("input.txt") );
ps = new PrintStream(new FileOutputStream("output.txt"));
int n = ir.nextInt();
int[] npeo = new int[500];
for( int i = 0; i < n; i++ ) {
int m = ir.nextInt();
int d = ir.nextInt();
int j = ir.nextInt();
int p = ir.nextInt();
int sd = dayInYear(m, d)+100-p;
for( int k = 0; k < p; k++ ) npeo[sd+k] += j;
}
int max = 0;
for( int i = 0; i < 500; i++ ) if( max < npeo[i] ){
max = npeo[i];
}
ps.println(max);
// int[] time = new int[5001];
// boolean[] ok = new boolean[5001];
// boolean flag = true;
// for( int i = 1; i <= 2*n; i++ ) {
// int num = ir.nextInt();
// time[num]++;
// if( time[num] % 2 == 1 ) ok[num] = true;
// else ok[num] = false;
// a[i] = new Card(num, i);
// }
//
// for(int i = 1; i < 5001; i++) if(ok[i]) flag = false;
// if( !flag ) ps.println(-1);
// else {
// Arrays.sort(a, 1, 2*n+1);
// for( int i = 1; i <= 2*n; i += 2 ) {
// ps.println(a[i].ord + " " + a[i+1].ord);
// }
// }
}
public int dayInYear(int m, int d) {
int res = d;
switch(m) {
case 12:
res += 30;
case 11:
res += 31;
case 10:
res += 30;
case 9:
res += 31;
case 8:
res += 31;
case 7:
res += 30;
case 6:
res += 31;
case 5:
res += 30;
case 4:
res += 31;
case 3:
res += 28;
case 2:
res += 31;
}
return res;
}
}
class Card implements Comparable<Card> {
int num, ord;
public Card(int n, int o) {
num = n;
ord = o;
}
@Override
public int compareTo(Card arg0) {
return num - arg0.num;
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong( next() );
}
public double nextDouble() {
return Double.parseDouble( next() );
}
}
class CucumberMarket
{
public long countColors(String[] clipboard, int T)
{
long ans = 0;
int add = 0;
int row = clipboard.length;
int col = clipboard[0].length();
boolean[][] vist = new boolean[row][col];
for(int i = 0; i < col; i++ ) {
if( clipboard[row-1].charAt(i) == 'B' ) {
add++;
vist[row-1][i] = true;
} else {
int ii = i;
for(int j = row-1; ii>= 0 && j >= 0; j--,ii-- ) if(clipboard[j].charAt(ii) == 'B') {
add++;
vist[j][ii] = true;
break;
}
}
}
for( int i = 0; i < row; i++ ) if(!vist[i][col-1]) {
if(clipboard[i].charAt(col-1) == 'B') {
add++;
vist[i][col-1] = true;
} else {
int ii = i;
for( int j = col-1; ii >= 0 && j >= 0; ii--,j-- ) if(clipboard[ii].charAt(j) == 'B') {
if(vist[ii][j]) break;
add++;
vist[ii][j] = true;
break;
}
}
}
ans += ((long)T) * add;
for( int i = 0; i < row; i++ ) {
for( int j = 0; j < col; j++ ) if(clipboard[i].charAt(j) == 'B' && !vist[i][j]) {
int a = 1;
for(int ii = i+1, jj = j+1; ii < row && jj < col; ii++,jj++) {
if(clipboard[ii].charAt(jj) != 'B') {
a++;
} else break;
}
ans += Math.min(a, T);
}
}
return ans;
}
}
class Scan {
InputStream inp;
public Scan(InputStream in) {
inp = in;
}
public String next() {
StringBuffer res = new StringBuffer();
try {
int t = inp.read();
while ( (t < 'a' || t > 'z') && (t < 'A' || t > 'Z') && ( t < '1' || t < '9') ) t = inp.read();
while ( ('a' <= t && t <= 'z') || ('A' <= t && t <= 'Z') || ('1' <= t && t <= '9') ) {
res.append( ((char)t) );
t = inp.read();
}
} catch (IOException e) {
e.printStackTrace();
}
return res.toString();
}
public int nextInt() {
boolean isN = false;
int res = 0;
int t;
try {
t = inp.read();
if (t == -1) return -1;
while (t != -1 && t != '-' && (t > '9' || t < '0')) t = inp.read();
if ( t == '-' ) {
isN = true;
t = inp.read();
}
while (t != -1 && t >= '0' && t <= '9') {
res = res * 10 + (t - '0');
t = inp.read();
}
} catch (IOException e) {
e.printStackTrace();
}
if ( isN ) res = -res;
return res;
}
public double nextDouble() {
double res = 0, Dec = 0.1;
boolean isN = false, isD = false;
int t;
try {
t = inp.read();
if ( t == -1 ) return -1;
while ( t != '-' && t != '.' && (t < '0' || t > '9') ) t = inp.read();
if ( t == '-' ) {
isN = true;
res = 0;
} else if ( t == '.' ) {
isD = true;
res = 0;
} else res = (double) t - '0';
if ( !isD ) {
t = inp.read();
while ( t >= '0' && t <= '9' ) {
res = res * 10 + (t - '0');
t = inp.read();
}
}
if ( t != '.' ) {
if ( isN ) res = -res;
return res;
} else {
t = inp.read();
while ( t >= '0' && t <= '9' ) {
res += Dec * (t - '0');
Dec *= 0.1;
t= inp.read();
}
}
if ( isN ) res = -res;
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
}
| Java | ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"] | 1 second | ["2", "3", "1"] | null | Java 6 | input.txt | [
"implementation",
"brute force"
] | 34655e8de9f1020677c5681d9d217d75 | The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | 1,500 | Print a single number — the minimum jury size. | output.txt | |
PASSED | 427c5eb2b0b1b4abca65339b632d6855 | train_001.jsonl | 1541860500 | You are given a connected undirected graph without cycles (that is, a tree) of $$$n$$$ vertices, moreover, there is a non-negative integer written on every edge.Consider all pairs of vertices $$$(v, u)$$$ (that is, there are exactly $$$n^2$$$ such pairs) and for each pair calculate the bitwise exclusive or (xor) of all integers on edges of the simple path between $$$v$$$ and $$$u$$$. If the path consists of one vertex only, then xor of all integers on edges of this path is equal to $$$0$$$.Suppose we sorted the resulting $$$n^2$$$ values in non-decreasing order. You need to find the $$$k$$$-th of them.The definition of xor is as follows.Given two integers $$$x$$$ and $$$y$$$, consider their binary representations (possibly with leading zeros): $$$x_k \dots x_2 x_1 x_0$$$ and $$$y_k \dots y_2 y_1 y_0$$$ (where $$$k$$$ is any number so that all bits of $$$x$$$ and $$$y$$$ can be represented). Here, $$$x_i$$$ is the $$$i$$$-th bit of the number $$$x$$$ and $$$y_i$$$ is the $$$i$$$-th bit of the number $$$y$$$. Let $$$r = x \oplus y$$$ be the result of the xor operation of $$$x$$$ and $$$y$$$. Then $$$r$$$ is defined as $$$r_k \dots r_2 r_1 r_0$$$ where:$$$$$$ r_i = \left\{ \begin{aligned} 1, ~ \text{if} ~ x_i \ne y_i \\ 0, ~ \text{if} ~ x_i = y_i \end{aligned} \right. $$$$$$ | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
FastScanner in;
PrintWriter out;
void solve() {
int n = in.nextInt();
long k = in.nextLong() - 1;
int[] p = new int[n];
long[] w = new long[n];
for (int i = 1; i < n; i++) {
p[i] = in.nextInt() - 1;
w[i] = in.nextLong() ^ w[p[i]];
}
Arrays.sort(w);
// System.err.println(Arrays.toString(w));
final int MAX = n * 2 + 10;
int[] firstLeft = new int[MAX];
int[] firstRight = new int[MAX];
int[] secondLeft = new int[MAX];
int[] secondRight = new int[MAX];
int[] nfirstLeft = new int[MAX];
int[] nfirstRight = new int[MAX];
int[] nsecondLeft = new int[MAX];
int[] nsecondRight = new int[MAX];
int sz = 1;
long result = 0;
firstLeft[0] = 0;
firstRight[0] = n;
secondLeft[0] = 0;
secondRight[0] = n;
final int BIT = 62;
int[] firstMid = new int[MAX];
int[] secondMid = new int[MAX];
for (int bit = BIT - 1; bit >= 0; bit--) {
// System.err.println("bit = " + bit);
// for (int i = 0; i < sz; i++) {
// System.err.println(firstLeft[i] + " " + firstRight[i] + " " + secondLeft[i] + " " + secondRight[i]);
// }
long CURRENT = 1L << bit;
for (int i = 0; i < sz; i++) {
firstMid[i] = firstLeft[i];
while (firstMid[i] != firstRight[i] && (w[firstMid[i]] & CURRENT) != CURRENT) {
firstMid[i]++;
}
}
for (int i = 0; i < sz; i++) {
secondMid[i] = secondLeft[i];
while (secondMid[i] != secondRight[i] && (w[secondMid[i]] & CURRENT) != CURRENT) {
secondMid[i]++;
}
}
long sum = 0;
for (int i = 0; i < sz; i++) {
sum += (firstMid[i] - firstLeft[i]) * 1L * (secondMid[i] - secondLeft[i]);
sum += (firstRight[i] - firstMid[i]) * 1L * (secondRight[i] - secondMid[i]);
}
// System.err.println("sum = " + sum);
if (sum > k) {
// use zero
int nsz = 0;
for (int i = 0; i < sz; i++) {
if ((firstMid[i] - firstLeft[i]) * 1L * (secondMid[i] - secondLeft[i]) > 0) {
nfirstLeft[nsz] = firstLeft[i];
nfirstRight[nsz] = firstMid[i];
nsecondLeft[nsz] = secondLeft[i];
nsecondRight[nsz] = secondMid[i];
nsz++;
}
if ((firstRight[i] - firstMid[i]) * 1L * (secondRight[i] - secondMid[i]) > 0) {
nfirstLeft[nsz] = firstMid[i];
nfirstRight[nsz] = firstRight[i];
nsecondLeft[nsz] = secondMid[i];
nsecondRight[nsz] = secondRight[i];
nsz++;
}
}
sz = nsz;
} else {
int nsz = 0;
for (int i = 0; i < sz; i++) {
if ((firstMid[i] - firstLeft[i]) * 1L * (secondRight[i] - secondMid[i]) > 0) {
nfirstLeft[nsz] = firstLeft[i];
nfirstRight[nsz] = firstMid[i];
nsecondLeft[nsz] = secondMid[i];
nsecondRight[nsz] = secondRight[i];
nsz++;
}
if ((firstRight[i] - firstMid[i]) * 1L * (secondMid[i] - secondLeft[i]) > 0) {
nfirstLeft[nsz] = firstMid[i];
nfirstRight[nsz] = firstRight[i];
nsecondLeft[nsz] = secondLeft[i];
nsecondRight[nsz] = secondMid[i];
nsz++;
}
}
sz = nsz;
result |= 1L << bit;
k -= sum;
}
{
int[] tmp = nfirstLeft;
nfirstLeft = firstLeft;
firstLeft = tmp;
}
{
int[] tmp = nfirstRight;
nfirstRight = firstRight;
firstRight = tmp;
}
{
int[] tmp = nsecondLeft;
nsecondLeft = secondLeft;
secondLeft = tmp;
}
{
int[] tmp = nsecondRight;
nsecondRight = secondRight;
secondRight = tmp;
}
}
out.println(result);
}
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());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
new A().runIO();
}
} | Java | ["2 1\n1 3", "3 6\n1 2\n1 3"] | 4 seconds | ["0", "2"] | NoteThe tree in the second sample test looks like this:For such a tree in total $$$9$$$ paths exist: $$$1 \to 1$$$ of value $$$0$$$ $$$2 \to 2$$$ of value $$$0$$$ $$$3 \to 3$$$ of value $$$0$$$ $$$2 \to 3$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$3 \to 2$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$1 \to 2$$$ of value $$$2$$$ $$$2 \to 1$$$ of value $$$2$$$ $$$1 \to 3$$$ of value $$$3$$$ $$$3 \to 1$$$ of value $$$3$$$ | Java 8 | standard input | [
"trees",
"strings"
] | 4a6ec68740e0a99e3907ed133b36956d | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^6$$$, $$$1 \le k \le n^2$$$) — the number of vertices in the tree and the number of path in the list with non-decreasing order. Each of the following $$$n - 1$$$ lines contains two integers $$$p_i$$$ and $$$w_i$$$ ($$$1 \le p_i \le i$$$, $$$0 \le w_i < 2^{62}$$$) — the ancestor of vertex $$$i + 1$$$ and the weight of the corresponding edge. | 2,900 | Print one integer: $$$k$$$-th smallest xor of a path in the tree. | standard output | |
PASSED | f590462616cbffc3bdd24e107b9eb817 | train_001.jsonl | 1541860500 | You are given a connected undirected graph without cycles (that is, a tree) of $$$n$$$ vertices, moreover, there is a non-negative integer written on every edge.Consider all pairs of vertices $$$(v, u)$$$ (that is, there are exactly $$$n^2$$$ such pairs) and for each pair calculate the bitwise exclusive or (xor) of all integers on edges of the simple path between $$$v$$$ and $$$u$$$. If the path consists of one vertex only, then xor of all integers on edges of this path is equal to $$$0$$$.Suppose we sorted the resulting $$$n^2$$$ values in non-decreasing order. You need to find the $$$k$$$-th of them.The definition of xor is as follows.Given two integers $$$x$$$ and $$$y$$$, consider their binary representations (possibly with leading zeros): $$$x_k \dots x_2 x_1 x_0$$$ and $$$y_k \dots y_2 y_1 y_0$$$ (where $$$k$$$ is any number so that all bits of $$$x$$$ and $$$y$$$ can be represented). Here, $$$x_i$$$ is the $$$i$$$-th bit of the number $$$x$$$ and $$$y_i$$$ is the $$$i$$$-th bit of the number $$$y$$$. Let $$$r = x \oplus y$$$ be the result of the xor operation of $$$x$$$ and $$$y$$$. Then $$$r$$$ is defined as $$$r_k \dots r_2 r_1 r_0$$$ where:$$$$$$ r_i = \left\{ \begin{aligned} 1, ~ \text{if} ~ x_i \ne y_i \\ 0, ~ \text{if} ~ x_i = y_i \end{aligned} \right. $$$$$$ | 256 megabytes | import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.arraycopy;
import static java.lang.System.exit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class F {
static void sort(long a[]) {
int n = a.length;
if (n == 0) {
return;
}
for (int i = 1; i < n; i++) {
int j = i;
long ca = a[i];
do {
int nj = (j - 1) >> 1;
long na = a[nj];
if (ca <= na) {
break;
}
a[j] = na;
j = nj;
} while (j != 0);
a[j] = ca;
}
long ca = a[0];
for (int i = n - 1; i > 0; i--) {
int j = 0;
while ((j << 1) + 2 + Integer.MIN_VALUE < i + Integer.MIN_VALUE) {
j <<= 1;
j += (a[j + 2] > a[j + 1]) ? 2 : 1;
}
if ((j << 1) + 2 == i) {
j = (j << 1) + 1;
}
long na = a[i];
a[i] = ca;
ca = na;
while (j != 0 && a[j] < ca) {
j = (j - 1) >> 1;
}
while (j != 0) {
na = a[j];
a[j] = ca;
ca = na;
j = (j - 1) >> 1;
}
}
a[0] = ca;
}
static void solve() throws Exception {
int n = scanInt();
long k = scanLong() - 1;
long a[] = new long[n];
for (int i = 1; i < n; i++) {
a[i] = a[scanInt() - 1] ^ scanLong();
}
sort(a);
long a2[] = a.clone();
long tmp[] = new long[n];
long prefix = 0;
for (int bit = 61; bit >= 0; bit--) {
long mask = (1L << 62) - (1L << bit);
long cnt = 0;
for (int i = 0, j, pos = 0; i < n; i = j) {
long creq = a2[i] & mask;
for (j = i + 1; j < n && (a2[j] & mask) == creq; j++) { }
creq ^= prefix;
long ccnt = j - i;
for (; pos < n && a[pos] < creq; pos++) { }
for (; pos < n && (a[pos] & mask) == creq; pos++) {
cnt += ccnt;
}
}
if (k >= cnt) {
k -= cnt;
long bb = 1L << bit;
prefix |= bb;
for (int i = 0, i2, i3; i < n;) {
long v = a2[i] & mask;
if ((v & bb) != 0) {
++i;
continue;
}
for (i2 = i + 1; i2 < n && (a2[i2] & mask) == v; i2++) { }
long vv = v ^ bb;
for (i3 = i2; i3 < n && (a2[i3] & mask) == vv; i3++) { }
if (i3 == i2) {
i = i2;
continue;
}
arraycopy(a2, i, tmp, 0, i2 - i);
arraycopy(a2, i2, a2, i, i3 - i2);
arraycopy(tmp, 0, a2, i + i3 - i2, i2 - i);
i = i3;
}
}
}
out.print(prefix);
}
static int scanInt() throws IOException {
return parseInt(scanString());
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static String scanString() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
} | Java | ["2 1\n1 3", "3 6\n1 2\n1 3"] | 4 seconds | ["0", "2"] | NoteThe tree in the second sample test looks like this:For such a tree in total $$$9$$$ paths exist: $$$1 \to 1$$$ of value $$$0$$$ $$$2 \to 2$$$ of value $$$0$$$ $$$3 \to 3$$$ of value $$$0$$$ $$$2 \to 3$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$3 \to 2$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$1 \to 2$$$ of value $$$2$$$ $$$2 \to 1$$$ of value $$$2$$$ $$$1 \to 3$$$ of value $$$3$$$ $$$3 \to 1$$$ of value $$$3$$$ | Java 8 | standard input | [
"trees",
"strings"
] | 4a6ec68740e0a99e3907ed133b36956d | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^6$$$, $$$1 \le k \le n^2$$$) — the number of vertices in the tree and the number of path in the list with non-decreasing order. Each of the following $$$n - 1$$$ lines contains two integers $$$p_i$$$ and $$$w_i$$$ ($$$1 \le p_i \le i$$$, $$$0 \le w_i < 2^{62}$$$) — the ancestor of vertex $$$i + 1$$$ and the weight of the corresponding edge. | 2,900 | Print one integer: $$$k$$$-th smallest xor of a path in the tree. | standard output | |
PASSED | d4f02690a6c6baa038ce65026a1b9c16 | train_001.jsonl | 1541860500 | You are given a connected undirected graph without cycles (that is, a tree) of $$$n$$$ vertices, moreover, there is a non-negative integer written on every edge.Consider all pairs of vertices $$$(v, u)$$$ (that is, there are exactly $$$n^2$$$ such pairs) and for each pair calculate the bitwise exclusive or (xor) of all integers on edges of the simple path between $$$v$$$ and $$$u$$$. If the path consists of one vertex only, then xor of all integers on edges of this path is equal to $$$0$$$.Suppose we sorted the resulting $$$n^2$$$ values in non-decreasing order. You need to find the $$$k$$$-th of them.The definition of xor is as follows.Given two integers $$$x$$$ and $$$y$$$, consider their binary representations (possibly with leading zeros): $$$x_k \dots x_2 x_1 x_0$$$ and $$$y_k \dots y_2 y_1 y_0$$$ (where $$$k$$$ is any number so that all bits of $$$x$$$ and $$$y$$$ can be represented). Here, $$$x_i$$$ is the $$$i$$$-th bit of the number $$$x$$$ and $$$y_i$$$ is the $$$i$$$-th bit of the number $$$y$$$. Let $$$r = x \oplus y$$$ be the result of the xor operation of $$$x$$$ and $$$y$$$. Then $$$r$$$ is defined as $$$r_k \dots r_2 r_1 r_0$$$ where:$$$$$$ r_i = \left\{ \begin{aligned} 1, ~ \text{if} ~ x_i \ne y_i \\ 0, ~ \text{if} ~ x_i = y_i \end{aligned} \right. $$$$$$ | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.io.IOException;
import java.util.Random;
import java.util.Deque;
import java.util.function.Supplier;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.util.function.Consumer;
import java.util.List;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
FTreeAndXOR solver = new FTreeAndXOR();
solver.solve(1, in, out);
out.close();
}
}
static class FTreeAndXOR {
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.readInt();
long k = in.readLong();
long[] weights = new long[n];
for (int i = 1; i < n; i++) {
int p = in.readInt() - 1;
long w = in.readLong();
weights[i] = weights[p] ^ w;
}
long mask = KthXorTwoElement.solve(weights, k);
out.println(mask);
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public long readLong() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
long val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class Randomized {
static Random random = new Random();
public static void randomizedArray(long[] data) {
randomizedArray(data, 0, data.length - 1);
}
public static void randomizedArray(long[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
long tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static int nextInt(int l, int r) {
return random.nextInt(r - l + 1) + l;
}
}
static class Bits {
private Bits() {
}
public static int bitAt(long x, int i) {
return (int) ((x >> i) & 1);
}
public static long setBit(long x, int i, boolean v) {
if (v) {
x |= 1L << i;
} else {
x &= ~(1L << i);
}
return x;
}
}
static class FastOutput implements AutoCloseable, Closeable {
private StringBuilder cache = new StringBuilder(10 << 20);
private final Writer os;
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput println(long c) {
cache.append(c).append('\n');
return this;
}
public FastOutput flush() {
try {
os.append(cache);
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class CachedLog2 {
private static final int BITS = 16;
private static final int LIMIT = 1 << BITS;
private static final byte[] CACHE = new byte[LIMIT];
static {
int b = 0;
for (int i = 0; i < LIMIT; i++) {
while ((1 << (b + 1)) <= i) {
b++;
}
CACHE[i] = (byte) b;
}
}
public static int floorLog(long x) {
int ans = 0;
while (x >= LIMIT) {
ans += BITS;
x >>>= BITS;
}
return ans + CACHE[(int) x];
}
}
static class Buffer<T> {
private Deque<T> deque;
private Supplier<T> supplier;
private Consumer<T> cleaner;
public Buffer(Supplier<T> supplier) {
this(supplier, (x) -> {
});
}
public Buffer(Supplier<T> supplier, Consumer<T> cleaner) {
this(supplier, cleaner, 0);
}
public Buffer(Supplier<T> supplier, Consumer<T> cleaner, int exp) {
this.supplier = supplier;
this.cleaner = cleaner;
deque = new ArrayDeque<>(exp);
}
public T alloc() {
return deque.isEmpty() ? supplier.get() : deque.removeFirst();
}
public void release(T e) {
cleaner.accept(e);
deque.addLast(e);
}
}
static class KthXorTwoElement {
public static long solve(long[] data, long k) {
int n = data.length;
Buffer<KthXorTwoElement.Interval> buffer = new Buffer<>(KthXorTwoElement.Interval::new, x -> {
}, n * 2);
Randomized.randomizedArray(data);
Arrays.sort(data);
List<KthXorTwoElement.Interval> lastLevel = new ArrayList<>(n);
List<KthXorTwoElement.Interval> curLevel = new ArrayList<>(n);
lastLevel.add(newInterval(buffer, 0, n - 1));
int level = CachedLog2.floorLog(data[n - 1]);
long mask = 0;
for (; level >= 0; level--) {
curLevel.clear();
for (KthXorTwoElement.Interval interval : lastLevel) {
int l = interval.l;
int r = interval.r;
int m = r;
while (m >= l && Bits.bitAt(data[m], level) == 1) {
m--;
}
interval.m = m;
}
long total = 0;
for (KthXorTwoElement.Interval interval : lastLevel) {
total += (long) (interval.m - interval.l + 1) * (interval.relative.m - interval.relative.l + 1);
total += (long) (interval.r - interval.m) * (interval.relative.r - interval.relative.m);
}
if (total < k) {
k -= total;
mask = Bits.setBit(mask, level, true);
for (KthXorTwoElement.Interval interval : lastLevel) {
if (interval.relative == interval) {
if (interval.l <= interval.m && interval.m < interval.r) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.l, interval.m);
KthXorTwoElement.Interval b = newInterval(buffer, interval.m + 1, interval.r);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
} else if (interval.r >= interval.relative.r) {
if (interval.l <= interval.m && interval.relative.r > interval.relative.m) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.l, interval.m);
KthXorTwoElement.Interval b = newInterval(buffer, interval.relative.m + 1, interval.relative.r);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
if (interval.m < interval.r && interval.relative.m >= interval.relative.l) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.m + 1, interval.r);
KthXorTwoElement.Interval b = newInterval(buffer, interval.relative.l, interval.relative.m);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
}
}
} else {
for (KthXorTwoElement.Interval interval : lastLevel) {
if (interval.relative == interval) {
if (interval.l <= interval.m) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.l, interval.m);
a.relative = a;
curLevel.add(a);
}
if (interval.m < interval.r) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.m + 1, interval.r);
a.relative = a;
curLevel.add(a);
}
} else if (interval.r >= interval.relative.r) {
if (interval.l <= interval.m && interval.relative.l <= interval.relative.m) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.l, interval.m);
KthXorTwoElement.Interval b = newInterval(buffer, interval.relative.l, interval.relative.m);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
if (interval.m < interval.r && interval.relative.m < interval.relative.r) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.m + 1, interval.r);
KthXorTwoElement.Interval b = newInterval(buffer, interval.relative.m + 1, interval.relative.r);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
}
}
}
for (KthXorTwoElement.Interval interval : lastLevel) {
buffer.release(interval);
}
List<KthXorTwoElement.Interval> tmp = curLevel;
curLevel = lastLevel;
lastLevel = tmp;
}
return mask;
}
private static KthXorTwoElement.Interval newInterval(Buffer<KthXorTwoElement.Interval> buffer, int l, int r) {
KthXorTwoElement.Interval ans = buffer.alloc();
ans.l = l;
ans.r = r;
return ans;
}
static class Interval {
int l;
int r;
int m;
KthXorTwoElement.Interval relative = this;
}
}
}
| Java | ["2 1\n1 3", "3 6\n1 2\n1 3"] | 4 seconds | ["0", "2"] | NoteThe tree in the second sample test looks like this:For such a tree in total $$$9$$$ paths exist: $$$1 \to 1$$$ of value $$$0$$$ $$$2 \to 2$$$ of value $$$0$$$ $$$3 \to 3$$$ of value $$$0$$$ $$$2 \to 3$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$3 \to 2$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$1 \to 2$$$ of value $$$2$$$ $$$2 \to 1$$$ of value $$$2$$$ $$$1 \to 3$$$ of value $$$3$$$ $$$3 \to 1$$$ of value $$$3$$$ | Java 8 | standard input | [
"trees",
"strings"
] | 4a6ec68740e0a99e3907ed133b36956d | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^6$$$, $$$1 \le k \le n^2$$$) — the number of vertices in the tree and the number of path in the list with non-decreasing order. Each of the following $$$n - 1$$$ lines contains two integers $$$p_i$$$ and $$$w_i$$$ ($$$1 \le p_i \le i$$$, $$$0 \le w_i < 2^{62}$$$) — the ancestor of vertex $$$i + 1$$$ and the weight of the corresponding edge. | 2,900 | Print one integer: $$$k$$$-th smallest xor of a path in the tree. | standard output | |
PASSED | c4b260976a3f16bb03a1df50046f2eba | train_001.jsonl | 1541860500 | You are given a connected undirected graph without cycles (that is, a tree) of $$$n$$$ vertices, moreover, there is a non-negative integer written on every edge.Consider all pairs of vertices $$$(v, u)$$$ (that is, there are exactly $$$n^2$$$ such pairs) and for each pair calculate the bitwise exclusive or (xor) of all integers on edges of the simple path between $$$v$$$ and $$$u$$$. If the path consists of one vertex only, then xor of all integers on edges of this path is equal to $$$0$$$.Suppose we sorted the resulting $$$n^2$$$ values in non-decreasing order. You need to find the $$$k$$$-th of them.The definition of xor is as follows.Given two integers $$$x$$$ and $$$y$$$, consider their binary representations (possibly with leading zeros): $$$x_k \dots x_2 x_1 x_0$$$ and $$$y_k \dots y_2 y_1 y_0$$$ (where $$$k$$$ is any number so that all bits of $$$x$$$ and $$$y$$$ can be represented). Here, $$$x_i$$$ is the $$$i$$$-th bit of the number $$$x$$$ and $$$y_i$$$ is the $$$i$$$-th bit of the number $$$y$$$. Let $$$r = x \oplus y$$$ be the result of the xor operation of $$$x$$$ and $$$y$$$. Then $$$r$$$ is defined as $$$r_k \dots r_2 r_1 r_0$$$ where:$$$$$$ r_i = \left\{ \begin{aligned} 1, ~ \text{if} ~ x_i \ne y_i \\ 0, ~ \text{if} ~ x_i = y_i \end{aligned} \right. $$$$$$ | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.io.IOException;
import java.util.Random;
import java.util.Deque;
import java.util.function.Supplier;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.util.function.Consumer;
import java.util.List;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
FTreeAndXOR solver = new FTreeAndXOR();
solver.solve(1, in, out);
out.close();
}
}
static class FTreeAndXOR {
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.readInt();
long k = in.readLong();
long[] weights = new long[n];
for (int i = 1; i < n; i++) {
int p = in.readInt() - 1;
long w = in.readLong();
weights[i] = weights[p] ^ w;
}
long mask = KthXorTwoElement.solve(weights, k);
out.println(mask);
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public long readLong() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
long val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class Randomized {
static Random random = new Random();
public static void randomizedArray(long[] data) {
randomizedArray(data, 0, data.length - 1);
}
public static void randomizedArray(long[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
long tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static int nextInt(int l, int r) {
return random.nextInt(r - l + 1) + l;
}
}
static class Bits {
private Bits() {
}
public static int bitAt(long x, int i) {
return (int) ((x >> i) & 1);
}
public static long setBit(long x, int i, boolean v) {
if (v) {
x |= 1L << i;
} else {
x &= ~(1L << i);
}
return x;
}
}
static class FastOutput implements AutoCloseable, Closeable {
private StringBuilder cache = new StringBuilder(10 << 20);
private final Writer os;
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput println(long c) {
cache.append(c).append('\n');
return this;
}
public FastOutput flush() {
try {
os.append(cache);
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class Buffer<T> {
private Deque<T> deque;
private Supplier<T> supplier;
private Consumer<T> cleaner;
public Buffer(Supplier<T> supplier) {
this(supplier, (x) -> {
});
}
public Buffer(Supplier<T> supplier, Consumer<T> cleaner) {
this(supplier, cleaner, 0);
}
public Buffer(Supplier<T> supplier, Consumer<T> cleaner, int exp) {
this.supplier = supplier;
this.cleaner = cleaner;
deque = new ArrayDeque<>(exp);
}
public T alloc() {
return deque.isEmpty() ? supplier.get() : deque.removeFirst();
}
public void release(T e) {
cleaner.accept(e);
deque.addLast(e);
}
}
static class KthXorTwoElement {
public static long solve(long[] data, long k) {
int n = data.length;
Buffer<KthXorTwoElement.Interval> buffer = new Buffer<>(KthXorTwoElement.Interval::new, x -> {
}, n * 2);
Randomized.randomizedArray(data);
Arrays.sort(data);
List<KthXorTwoElement.Interval> lastLevel = new ArrayList<>(n);
List<KthXorTwoElement.Interval> curLevel = new ArrayList<>(n);
lastLevel.add(newInterval(buffer, 0, n - 1));
int level = 62;
long mask = 0;
for (; level >= 0; level--) {
curLevel.clear();
for (KthXorTwoElement.Interval interval : lastLevel) {
int l = interval.l;
int r = interval.r;
int m = r;
while (m >= l && Bits.bitAt(data[m], level) == 1) {
m--;
}
interval.m = m;
}
long total = 0;
for (KthXorTwoElement.Interval interval : lastLevel) {
total += (long) (interval.m - interval.l + 1) * (interval.relative.m - interval.relative.l + 1);
total += (long) (interval.r - interval.m) * (interval.relative.r - interval.relative.m);
}
if (total < k) {
k -= total;
mask = Bits.setBit(mask, level, true);
for (KthXorTwoElement.Interval interval : lastLevel) {
if (interval.relative == interval) {
if (interval.l <= interval.m && interval.m < interval.r) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.l, interval.m);
KthXorTwoElement.Interval b = newInterval(buffer, interval.m + 1, interval.r);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
} else if (interval.r >= interval.relative.r) {
if (interval.l <= interval.m && interval.relative.r > interval.relative.m) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.l, interval.m);
KthXorTwoElement.Interval b = newInterval(buffer, interval.relative.m + 1, interval.relative.r);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
if (interval.m < interval.r && interval.relative.m >= interval.relative.l) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.m + 1, interval.r);
KthXorTwoElement.Interval b = newInterval(buffer, interval.relative.l, interval.relative.m);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
}
}
} else {
for (KthXorTwoElement.Interval interval : lastLevel) {
if (interval.relative == interval) {
if (interval.l <= interval.m) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.l, interval.m);
a.relative = a;
curLevel.add(a);
}
if (interval.m < interval.r) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.m + 1, interval.r);
a.relative = a;
curLevel.add(a);
}
} else if (interval.r >= interval.relative.r) {
if (interval.l <= interval.m && interval.relative.l <= interval.relative.m) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.l, interval.m);
KthXorTwoElement.Interval b = newInterval(buffer, interval.relative.l, interval.relative.m);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
if (interval.m < interval.r && interval.relative.m < interval.relative.r) {
KthXorTwoElement.Interval a = newInterval(buffer, interval.m + 1, interval.r);
KthXorTwoElement.Interval b = newInterval(buffer, interval.relative.m + 1, interval.relative.r);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
}
}
}
for (KthXorTwoElement.Interval interval : lastLevel) {
buffer.release(interval);
}
List<KthXorTwoElement.Interval> tmp = curLevel;
curLevel = lastLevel;
lastLevel = tmp;
}
return mask;
}
private static KthXorTwoElement.Interval newInterval(Buffer<KthXorTwoElement.Interval> buffer, int l, int r) {
KthXorTwoElement.Interval ans = buffer.alloc();
ans.l = l;
ans.r = r;
return ans;
}
static class Interval {
int l;
int r;
int m;
KthXorTwoElement.Interval relative = this;
}
}
}
| Java | ["2 1\n1 3", "3 6\n1 2\n1 3"] | 4 seconds | ["0", "2"] | NoteThe tree in the second sample test looks like this:For such a tree in total $$$9$$$ paths exist: $$$1 \to 1$$$ of value $$$0$$$ $$$2 \to 2$$$ of value $$$0$$$ $$$3 \to 3$$$ of value $$$0$$$ $$$2 \to 3$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$3 \to 2$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$1 \to 2$$$ of value $$$2$$$ $$$2 \to 1$$$ of value $$$2$$$ $$$1 \to 3$$$ of value $$$3$$$ $$$3 \to 1$$$ of value $$$3$$$ | Java 8 | standard input | [
"trees",
"strings"
] | 4a6ec68740e0a99e3907ed133b36956d | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^6$$$, $$$1 \le k \le n^2$$$) — the number of vertices in the tree and the number of path in the list with non-decreasing order. Each of the following $$$n - 1$$$ lines contains two integers $$$p_i$$$ and $$$w_i$$$ ($$$1 \le p_i \le i$$$, $$$0 \le w_i < 2^{62}$$$) — the ancestor of vertex $$$i + 1$$$ and the weight of the corresponding edge. | 2,900 | Print one integer: $$$k$$$-th smallest xor of a path in the tree. | standard output | |
PASSED | 4a5b8e71e9ca14d62918d4e445e7d843 | train_001.jsonl | 1541860500 | You are given a connected undirected graph without cycles (that is, a tree) of $$$n$$$ vertices, moreover, there is a non-negative integer written on every edge.Consider all pairs of vertices $$$(v, u)$$$ (that is, there are exactly $$$n^2$$$ such pairs) and for each pair calculate the bitwise exclusive or (xor) of all integers on edges of the simple path between $$$v$$$ and $$$u$$$. If the path consists of one vertex only, then xor of all integers on edges of this path is equal to $$$0$$$.Suppose we sorted the resulting $$$n^2$$$ values in non-decreasing order. You need to find the $$$k$$$-th of them.The definition of xor is as follows.Given two integers $$$x$$$ and $$$y$$$, consider their binary representations (possibly with leading zeros): $$$x_k \dots x_2 x_1 x_0$$$ and $$$y_k \dots y_2 y_1 y_0$$$ (where $$$k$$$ is any number so that all bits of $$$x$$$ and $$$y$$$ can be represented). Here, $$$x_i$$$ is the $$$i$$$-th bit of the number $$$x$$$ and $$$y_i$$$ is the $$$i$$$-th bit of the number $$$y$$$. Let $$$r = x \oplus y$$$ be the result of the xor operation of $$$x$$$ and $$$y$$$. Then $$$r$$$ is defined as $$$r_k \dots r_2 r_1 r_0$$$ where:$$$$$$ r_i = \left\{ \begin{aligned} 1, ~ \text{if} ~ x_i \ne y_i \\ 0, ~ \text{if} ~ x_i = y_i \end{aligned} \right. $$$$$$ | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.io.IOException;
import java.util.Random;
import java.util.Deque;
import java.util.function.Supplier;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.util.function.Consumer;
import java.util.List;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
FTreeAndXOR solver = new FTreeAndXOR();
solver.solve(1, in, out);
out.close();
}
}
static class FTreeAndXOR {
Buffer<Interval> buffer = new Buffer<>(Interval::new, x -> {
x.l = x.r = x.m = 0;
x.relative = x;
}, 1000000 * 2);
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.readInt();
long k = in.readLong();
long[] weights = new long[n];
for (int i = 1; i < n; i++) {
int p = in.readInt() - 1;
long w = in.readLong();
weights[i] = weights[p] ^ w;
}
Randomized.randomizedArray(weights);
Arrays.sort(weights);
List<Interval> lastLevel = new ArrayList<>(n);
List<Interval> curLevel = new ArrayList<>(n);
lastLevel.add(newInterval(0, n - 1));
int level = 62;
long mask = 0;
for (; level >= 0; level--) {
curLevel.clear();
for (Interval interval : lastLevel) {
int l = interval.l;
int r = interval.r;
int m = r;
while (m >= l && Bits.bitAt(weights[m], level) == 1) {
m--;
}
interval.m = m;
}
long total = 0;
for (Interval interval : lastLevel) {
total += (long) (interval.m - interval.l + 1) * (interval.relative.m - interval.relative.l + 1);
total += (long) (interval.r - interval.m) * (interval.relative.r - interval.relative.m);
}
if (total < k) {
k -= total;
mask = Bits.setBit(mask, level, true);
for (Interval interval : lastLevel) {
if (interval.relative == interval) {
if (interval.l <= interval.m && interval.m < interval.r) {
Interval a = newInterval(interval.l, interval.m);
Interval b = newInterval(interval.m + 1, interval.r);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
} else if (interval.r >= interval.relative.r) {
if (interval.l <= interval.m && interval.relative.r > interval.relative.m) {
Interval a = newInterval(interval.l, interval.m);
Interval b = newInterval(interval.relative.m + 1, interval.relative.r);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
if (interval.m < interval.r && interval.relative.m >= interval.relative.l) {
Interval a = newInterval(interval.m + 1, interval.r);
Interval b = newInterval(interval.relative.l, interval.relative.m);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
}
}
} else {
for (Interval interval : lastLevel) {
if (interval.relative == interval) {
if (interval.l <= interval.m) {
Interval a = newInterval(interval.l, interval.m);
a.relative = a;
curLevel.add(a);
}
if (interval.m < interval.r) {
Interval a = newInterval(interval.m + 1, interval.r);
a.relative = a;
curLevel.add(a);
}
} else if (interval.r >= interval.relative.r) {
if (interval.l <= interval.m && interval.relative.l <= interval.relative.m) {
Interval a = newInterval(interval.l, interval.m);
Interval b = newInterval(interval.relative.l, interval.relative.m);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
if (interval.m < interval.r && interval.relative.m < interval.relative.r) {
Interval a = newInterval(interval.m + 1, interval.r);
Interval b = newInterval(interval.relative.m + 1, interval.relative.r);
a.relative = b;
b.relative = a;
curLevel.add(a);
curLevel.add(b);
}
}
}
}
for (Interval interval : lastLevel) {
buffer.release(interval);
}
List<Interval> tmp = curLevel;
curLevel = lastLevel;
lastLevel = tmp;
}
out.println(mask);
}
public Interval newInterval(int l, int r) {
Interval ans = buffer.alloc();
ans.l = l;
ans.r = r;
return ans;
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public long readLong() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
long val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class Randomized {
static Random random = new Random();
public static void randomizedArray(long[] data) {
randomizedArray(data, 0, data.length - 1);
}
public static void randomizedArray(long[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
long tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static int nextInt(int l, int r) {
return random.nextInt(r - l + 1) + l;
}
}
static class Bits {
private Bits() {
}
public static int bitAt(long x, int i) {
return (int) ((x >> i) & 1);
}
public static long setBit(long x, int i, boolean v) {
if (v) {
x |= 1L << i;
} else {
x &= ~(1L << i);
}
return x;
}
}
static class FastOutput implements AutoCloseable, Closeable {
private StringBuilder cache = new StringBuilder(10 << 20);
private final Writer os;
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput println(long c) {
cache.append(c).append('\n');
return this;
}
public FastOutput flush() {
try {
os.append(cache);
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class Buffer<T> {
private Deque<T> deque;
private Supplier<T> supplier;
private Consumer<T> cleaner;
public Buffer(Supplier<T> supplier) {
this(supplier, (x) -> {
});
}
public Buffer(Supplier<T> supplier, Consumer<T> cleaner) {
this(supplier, cleaner, 0);
}
public Buffer(Supplier<T> supplier, Consumer<T> cleaner, int exp) {
this.supplier = supplier;
this.cleaner = cleaner;
deque = new ArrayDeque<>(exp);
}
public T alloc() {
return deque.isEmpty() ? supplier.get() : deque.removeFirst();
}
public void release(T e) {
cleaner.accept(e);
deque.addLast(e);
}
}
static class Interval {
int l;
int r;
int m;
Interval relative = this;
}
}
| Java | ["2 1\n1 3", "3 6\n1 2\n1 3"] | 4 seconds | ["0", "2"] | NoteThe tree in the second sample test looks like this:For such a tree in total $$$9$$$ paths exist: $$$1 \to 1$$$ of value $$$0$$$ $$$2 \to 2$$$ of value $$$0$$$ $$$3 \to 3$$$ of value $$$0$$$ $$$2 \to 3$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$3 \to 2$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$1 \to 2$$$ of value $$$2$$$ $$$2 \to 1$$$ of value $$$2$$$ $$$1 \to 3$$$ of value $$$3$$$ $$$3 \to 1$$$ of value $$$3$$$ | Java 8 | standard input | [
"trees",
"strings"
] | 4a6ec68740e0a99e3907ed133b36956d | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^6$$$, $$$1 \le k \le n^2$$$) — the number of vertices in the tree and the number of path in the list with non-decreasing order. Each of the following $$$n - 1$$$ lines contains two integers $$$p_i$$$ and $$$w_i$$$ ($$$1 \le p_i \le i$$$, $$$0 \le w_i < 2^{62}$$$) — the ancestor of vertex $$$i + 1$$$ and the weight of the corresponding edge. | 2,900 | Print one integer: $$$k$$$-th smallest xor of a path in the tree. | standard output | |
PASSED | fdfddbbce7fd302770851caec8fa998a | train_001.jsonl | 1541860500 | You are given a connected undirected graph without cycles (that is, a tree) of $$$n$$$ vertices, moreover, there is a non-negative integer written on every edge.Consider all pairs of vertices $$$(v, u)$$$ (that is, there are exactly $$$n^2$$$ such pairs) and for each pair calculate the bitwise exclusive or (xor) of all integers on edges of the simple path between $$$v$$$ and $$$u$$$. If the path consists of one vertex only, then xor of all integers on edges of this path is equal to $$$0$$$.Suppose we sorted the resulting $$$n^2$$$ values in non-decreasing order. You need to find the $$$k$$$-th of them.The definition of xor is as follows.Given two integers $$$x$$$ and $$$y$$$, consider their binary representations (possibly with leading zeros): $$$x_k \dots x_2 x_1 x_0$$$ and $$$y_k \dots y_2 y_1 y_0$$$ (where $$$k$$$ is any number so that all bits of $$$x$$$ and $$$y$$$ can be represented). Here, $$$x_i$$$ is the $$$i$$$-th bit of the number $$$x$$$ and $$$y_i$$$ is the $$$i$$$-th bit of the number $$$y$$$. Let $$$r = x \oplus y$$$ be the result of the xor operation of $$$x$$$ and $$$y$$$. Then $$$r$$$ is defined as $$$r_k \dots r_2 r_1 r_0$$$ where:$$$$$$ r_i = \left\{ \begin{aligned} 1, ~ \text{if} ~ x_i \ne y_i \\ 0, ~ \text{if} ~ x_i = y_i \end{aligned} \right. $$$$$$ | 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.Random;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskF solver = new TaskF();
solver.solve(1, in, out);
out.close();
}
static class TaskF {
long[] a;
long[] b;
long[] c;
long[] d;
int n;
public void solve(int testNumber, InputReader in, PrintWriter out) {
n = in.nextInt();
long k = in.nextLong() - 1;
a = new long[n];
for (int i = 1; i < n; ++i) {
int p = in.nextInt() - 1;
a[i] = a[p] ^ in.nextLong();
}
Random random = new Random(534515151L + System.currentTimeMillis());
for (int i = 1; i < n; ++i) {
int j = random.nextInt(i + 1);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
b = a.clone();
c = new long[n];
d = new long[n];
long pmask = 0;
for (int bit = 62; bit >= 0; --bit) {
pmask <<= 1;
long am = countCommon(bit);
/*if (n < 1000 && am != countCommonStupid(bit)) {
throw new RuntimeException();
}*/
if (am <= k) {
k -= am;
pmask ^= 1;
flipBit(bit);
}
}
// if (k != 0) throw new RuntimeException();
out.println(pmask);
}
private void flipBit(int bit) {
/*for (int i = 0; i < n; ++i) b[i] ^= (1L << bit);
Arrays.sort(b);*/
int pos = 0;
int pc = 0;
int pd = 0;
while (pos < n) {
int spos = pos;
long val = b[pos] ^ (1L << bit);
long v = val >> bit;
long vv = v >> 1;
while (true) {
if ((v & 1) == 0) c[pc++] = val;
else d[pd++] = val;
++pos;
if (pos >= n) break;
val = b[pos] ^ (1L << bit);
v = val >> bit;
if ((v >> 1) != vv) break;
}
for (int i = 0; i < pc; ++i) b[spos++] = c[i];
for (int i = 0; i < pd; ++i) b[spos++] = d[i];
pc = 0;
pd = 0;
if (spos != pos) throw new RuntimeException();
}
}
private long countCommon(int bit) {
int pa = 0;
int pb = 0;
long va = a[pa] >> bit;
long vb = b[pb] >> bit;
long res = 0;
while (true) {
if (va < vb) {
++pa;
if (pa >= n) break;
va = a[pa] >> bit;
} else if (va > vb) {
++pb;
if (pb >= n) break;
vb = b[pb] >> bit;
} else {
int sa = pa++;
int sb = pb++;
long sv = va;
while (pa < n) {
va = a[pa] >> bit;
if (va != sv) break;
++pa;
}
while (pb < n) {
vb = b[pb] >> bit;
if (vb != sv) break;
++pb;
}
res += (pa - sa) * (long) (pb - sb);
if (pa >= n || pb >= n) break;
}
}
return res;
}
}
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 1\n1 3", "3 6\n1 2\n1 3"] | 4 seconds | ["0", "2"] | NoteThe tree in the second sample test looks like this:For such a tree in total $$$9$$$ paths exist: $$$1 \to 1$$$ of value $$$0$$$ $$$2 \to 2$$$ of value $$$0$$$ $$$3 \to 3$$$ of value $$$0$$$ $$$2 \to 3$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$3 \to 2$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$1 \to 2$$$ of value $$$2$$$ $$$2 \to 1$$$ of value $$$2$$$ $$$1 \to 3$$$ of value $$$3$$$ $$$3 \to 1$$$ of value $$$3$$$ | Java 8 | standard input | [
"trees",
"strings"
] | 4a6ec68740e0a99e3907ed133b36956d | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^6$$$, $$$1 \le k \le n^2$$$) — the number of vertices in the tree and the number of path in the list with non-decreasing order. Each of the following $$$n - 1$$$ lines contains two integers $$$p_i$$$ and $$$w_i$$$ ($$$1 \le p_i \le i$$$, $$$0 \le w_i < 2^{62}$$$) — the ancestor of vertex $$$i + 1$$$ and the weight of the corresponding edge. | 2,900 | Print one integer: $$$k$$$-th smallest xor of a path in the tree. | standard output | |
PASSED | c0ef16dda624a5881b67dfd700b055f9 | train_001.jsonl | 1541860500 | You are given a connected undirected graph without cycles (that is, a tree) of $$$n$$$ vertices, moreover, there is a non-negative integer written on every edge.Consider all pairs of vertices $$$(v, u)$$$ (that is, there are exactly $$$n^2$$$ such pairs) and for each pair calculate the bitwise exclusive or (xor) of all integers on edges of the simple path between $$$v$$$ and $$$u$$$. If the path consists of one vertex only, then xor of all integers on edges of this path is equal to $$$0$$$.Suppose we sorted the resulting $$$n^2$$$ values in non-decreasing order. You need to find the $$$k$$$-th of them.The definition of xor is as follows.Given two integers $$$x$$$ and $$$y$$$, consider their binary representations (possibly with leading zeros): $$$x_k \dots x_2 x_1 x_0$$$ and $$$y_k \dots y_2 y_1 y_0$$$ (where $$$k$$$ is any number so that all bits of $$$x$$$ and $$$y$$$ can be represented). Here, $$$x_i$$$ is the $$$i$$$-th bit of the number $$$x$$$ and $$$y_i$$$ is the $$$i$$$-th bit of the number $$$y$$$. Let $$$r = x \oplus y$$$ be the result of the xor operation of $$$x$$$ and $$$y$$$. Then $$$r$$$ is defined as $$$r_k \dots r_2 r_1 r_0$$$ where:$$$$$$ r_i = \left\{ \begin{aligned} 1, ~ \text{if} ~ x_i \ne y_i \\ 0, ~ \text{if} ~ x_i = y_i \end{aligned} \right. $$$$$$ | 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.Random;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskF solver = new TaskF();
solver.solve(1, in, out);
out.close();
}
static class TaskF {
long[] a;
long[] b;
long[] c;
long[] d;
int n;
public void solve(int testNumber, InputReader in, PrintWriter out) {
n = in.nextInt();
long k = in.nextLong() - 1;
a = new long[n];
for (int i = 1; i < n; ++i) {
int p = in.nextInt() - 1;
a[i] = a[p] ^ in.nextLong();
}
Random random = new Random(534515151L + System.currentTimeMillis());
for (int i = 1; i < n; ++i) {
int j = random.nextInt(i + 1);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
b = a.clone();
c = new long[n];
d = new long[n];
long pmask = 0;
for (int bit = 62; bit >= 0; --bit) {
pmask <<= 1;
long am = countCommon(bit);
/*if (n < 1000 && am != countCommonStupid(bit)) {
throw new RuntimeException();
}*/
if (am <= k) {
k -= am;
pmask ^= 1;
flipBit(bit);
}
}
// if (k != 0) throw new RuntimeException();
out.println(pmask);
}
private void flipBit(int bit) {
/*for (int i = 0; i < n; ++i) b[i] ^= (1L << bit);
Arrays.sort(b);*/
int pos = 0;
int pc = 0;
int pd = 0;
while (pos < n) {
int spos = pos;
long val = b[pos] ^ (1L << bit);
long v = val >> bit;
long vv = v >> 1;
while (true) {
if ((v & 1) == 0) c[pc++] = val;
else d[pd++] = val;
++pos;
if (pos >= n) break;
val = b[pos] ^ (1L << bit);
v = val >> bit;
if ((v >> 1) != vv) break;
}
for (int i = 0; i < pc; ++i) b[spos++] = c[i];
for (int i = 0; i < pd; ++i) b[spos++] = d[i];
pc = 0;
pd = 0;
if (spos != pos) throw new RuntimeException();
}
}
private long countCommon(int bit) {
int pa = 0;
int pb = 0;
long va = a[pa] >> bit;
long vb = b[pb] >> bit;
long res = 0;
while (true) {
if (va < vb) {
++pa;
if (pa >= n) break;
va = a[pa] >> bit;
} else if (va > vb) {
++pb;
if (pb >= n) break;
vb = b[pb] >> bit;
} else {
int sa = pa++;
int sb = pb++;
long sv = va;
while (pa < n) {
va = a[pa] >> bit;
if (va != sv) break;
++pa;
}
while (pb < n) {
vb = b[pb] >> bit;
if (vb != sv) break;
++pb;
}
res += (pa - sa) * (long) (pb - sb);
if (pa >= n || pb >= n) break;
}
}
return res;
}
}
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 1\n1 3", "3 6\n1 2\n1 3"] | 4 seconds | ["0", "2"] | NoteThe tree in the second sample test looks like this:For such a tree in total $$$9$$$ paths exist: $$$1 \to 1$$$ of value $$$0$$$ $$$2 \to 2$$$ of value $$$0$$$ $$$3 \to 3$$$ of value $$$0$$$ $$$2 \to 3$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$3 \to 2$$$ (goes through $$$1$$$) of value $$$1 = 2 \oplus 3$$$ $$$1 \to 2$$$ of value $$$2$$$ $$$2 \to 1$$$ of value $$$2$$$ $$$1 \to 3$$$ of value $$$3$$$ $$$3 \to 1$$$ of value $$$3$$$ | Java 8 | standard input | [
"trees",
"strings"
] | 4a6ec68740e0a99e3907ed133b36956d | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^6$$$, $$$1 \le k \le n^2$$$) — the number of vertices in the tree and the number of path in the list with non-decreasing order. Each of the following $$$n - 1$$$ lines contains two integers $$$p_i$$$ and $$$w_i$$$ ($$$1 \le p_i \le i$$$, $$$0 \le w_i < 2^{62}$$$) — the ancestor of vertex $$$i + 1$$$ and the weight of the corresponding edge. | 2,900 | Print one integer: $$$k$$$-th smallest xor of a path in the tree. | standard output | |
PASSED | 972286d6daa6f5e5692c767bffa438b1 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.awt.event.MouseAdapter;
import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
String s[]=bu.readLine().split(" ");
int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]);
int i,max=Integer.MIN_VALUE,min=Integer.MAX_VALUE,a[]=new int[n];
s=bu.readLine().split(" ");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(s[i]);
min=Math.min(min,a[i]);
max=Math.max(max,a[i]);
}
if(k==1) sb.append(min);
else if(k>2) sb.append(max);
else
{
int minl[]=new int[n],minr[]=new int[n];
minl[0]=a[0];
minr[n-1]=a[n-1];
for(i=1;i<n;i++)
minl[i]=Math.min(a[i],minl[i-1]);
for(i=n-2;i>=0;i--)
minr[i]=Math.min(a[i],minr[i+1]);
max=Integer.MIN_VALUE;
for(i=1;i<n;i++)
max=Math.max(max,Math.max(minl[i-1],minr[i]));
sb.append(max);
}
System.out.print(sb);
}
} | Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 2071f4ebe96b51be8cfd3e3633b37e80 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class MaximumofMaximumsofMinimums {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int k=in.nextInt();
int result = 0;
int i;
ArrayList<Integer> num=new ArrayList<>();
for(i=0;i<n;i++){
num.add(in.nextInt());
}
if(k==1){
result=Collections.min(num);
}else if(k==2){
if(num.get(0)>num.get(num.size()-1)){
result=num.get(0);
}else {
result=num.get(num.size()-1);
}
}else if(k>=3){
result=Collections.max(num);
}
System.out.println(result);
}
}
| Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 4e43c80c8e0f41d3bec162cdc4174667 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.io.*;
import java.util.*;
public class epicCode {
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Long> daList = new ArrayList<>();
StringTokenizer line = new StringTokenizer(br.readLine());
int n = Integer.parseInt(line.nextToken());
long k = Long.parseLong(line.nextToken());
line = new StringTokenizer(br.readLine());
boolean isT10 = false;
for(int i = 0; i < n; i++){
daList.add(Long.parseLong(line.nextToken()));
}
if(daList.get(0) == -451199021){
isT10 = true;
}
if(k==1){
Collections.sort(daList);
System.out.print(daList.get(0));
}
else if (k == 2){
long templast = daList.get(n-1);
int i = n-1;
while(i>0 && templast<daList.get(i-1)){
i--;
}
long tempfirst = daList.get(0);
i = 0;
while(i<n -1 && tempfirst<daList.get(i+1)){
i++;
}
if(tempfirst>templast){
templast = tempfirst;
}
Collections.sort(daList);
if(daList.get(0)==templast){
System.out.print(daList.get(1));
}
else {
System.out.print(templast);
}
}
else{
Collections.sort(daList);
System.out.print(daList.get(n-1));
}
/*
if(isT10) {
System.out.println("K: " + k + " N: " + n + "\n");
for (int i = n-1; i > 0; i--) {
System.out.print("\n" + daList.get(i) + "\n");
}
}
*/
daList.clear();
}
}
| Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 3fe53ae5694b50003efff726506518b1 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.io.*;
import java.util.Scanner;
public class Max {
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
int m = input.nextInt();
int k = input.nextInt();
int [] arr = new int[m];
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int i = 0; i < m; i++) {
arr[i] = input.nextInt();
if(arr[i] < min) {
min = arr[i];
}
if(arr[i] > max) {
max = arr[i];
}
}
if(k == 1) {
System.out.println(min);
}
else if(k == 2) {
if(arr[0] > arr[arr.length - 1]) {
System.out.println(arr[0]);
}
else {
System.out.println(arr[arr.length - 1]);
}
}
else {
System.out.println(max);
}
}
}
| Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 8729531b2a92e38b560a9163074125f2 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class MaxMin
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int arrlen = sc.nextInt();
int subsets = sc.nextInt();
int [] arr = new int[arrlen];
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < arrlen; i++)
{
arr[i] = sc.nextInt();
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
if (subsets == 1)
System.out.println(min);
else if (subsets == 2)
{
System.out.println(Math.max(arr[0],arr[arr.length - 1]));
}
else
{
System.out.println(max);
}
}
}
| Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 7466989fcc0b3359cec284668a6d37c4 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main{
static long mod=998244353;
public static void main(String[] args) throws Exception{
PrintWriter pw=new PrintWriter(System.out);
MScanner sc = new MScanner(System.in);
int n=sc.nextInt(),k=sc.nextInt();
long min=(long)1e10,max=-min;
int[]in=new int[n];
long[]prefMin=new long[n],sufMin=new long[n];
for(int i=0;i<n;i++) {
in[i]=sc.nextInt();
min=Math.min(min, in[i]);
max=Math.max(max, in[i]);
prefMin[i]=(i==0?in[i]:Math.min(prefMin[i-1], in[i]));
}
for(int i=n-1;i>=0;i--) {
sufMin[i]=(i==n-1?in[i]:Math.min(sufMin[i+1], in[i]));
}
if(k==1) {
pw.println(min);
pw.flush();
return;
}
if(k>=3) {
pw.println(max);
pw.flush();
return;
}
long ans=-(long)1e10;
for(int i=0;i<n-1;i++) {
ans=Math.max(ans, Math.max(prefMin[i], sufMin[i+1]));
}
pw.println(ans);
pw.flush();
}
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int[] intArr(int n) throws IOException {
int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
return in;
}
public long[] longArr(int n) throws IOException {
long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
return in;
}
public int[] intSortedArr(int n) throws IOException {
int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
shuffle(in);
Arrays.sort(in);
return in;
}
public long[] longSortedArr(int n) throws IOException {
long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
shuffle(in);
Arrays.sort(in);
return in;
}
static void shuffle(int[]in) {
for(int i=0;i<in.length;i++) {
int idx=(int)(Math.random()*in.length);
int tmp=in[i];
in[i]=in[idx];
in[idx]=tmp;
}
}
static void shuffle(long[]in) {
for(int i=0;i<in.length;i++) {
int idx=(int)(Math.random()*in.length);
long tmp=in[i];
in[i]=in[idx];
in[idx]=tmp;
}
}
public Integer[] IntegerArr(int n) throws IOException {
Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt();
return in;
}
public Long[] LongArr(int n) throws IOException {
Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong();
return in;
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
static void addX(int[]in,int x) {
for(int i=0;i<in.length;i++)in[i]+=x;
}
static void addX(long[]in,int x) {
for(int i=0;i<in.length;i++)in[i]+=x;
}
} | Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 0603546c5546dd7536474ae3e38e8443 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main (String []args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] params = br.readLine().split(" ");
int count = Integer.parseInt(params[0]);
int numGroups = Integer.parseInt(params[1]);
params = br.readLine().split(" ");
List<Integer> l = new ArrayList<>();
for (int i = 0; i < count; ++i) {
l.add(Integer.parseInt(params[i]));
}
br.close();
if (numGroups == 1) {
Collections.sort(l);
System.out.println(l.get(0));
} else if (numGroups >= 3) {
Collections.sort(l);
System.out.println(l.get(l.size()-1));
} else {
System.out.println(Math.max((int)(l.get(l.size()-1)), (int)(l.get(0))));
}
}
} | Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 7f9ff4b0b8de6340e0f5013974167b0c | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.io.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int k = sc.nextInt();
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i<n; i++)
arr.add(sc.nextInt());
if (k == 1)
{
int min = Integer.MAX_VALUE;
for (Integer o : arr)
if (min > o)
min = o;
System.out.println (min);
}
else if (k == 2)
System.out.println (Math.max(arr.get(0), arr.get(arr.size() - 1)));
else
{
int max = Integer.MIN_VALUE;
for (Integer t : arr)
if (max < t)
max = t;
System.out.println (max);
}
}
}
| Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | a7724680bbd655899add341708f87e85 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.util.*;
public class MaximumofMaximumsofMinimums {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int num = kb.nextInt();
int k = kb.nextInt();
long min = Long.MAX_VALUE;
long max = Long.MIN_VALUE;
int first = 0;
int current = 0;
for(int i = 0; i < num; i++) {
current = kb.nextInt();
if(i == 0)
first = current;
if(current > max) {
max = current;
}
if (current < min) {
min = current;
}
}
int last = current;
if(k == 1) {
if(min == Long.MAX_VALUE)
min = max;
System.out.println(min);
}
else {
if(k == 2)
System.out.println(Math.max(first, last));
else
System.out.println(max);
}
}
} | Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | f74eee72a7d5889a63e6d5f9c397adcb | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solutionc {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bufferedReader.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
st = new StringTokenizer(bufferedReader.readLine());
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int first = 0;
int last = 0;
for (int i = 0; i < n; i++) {
int a = Integer.parseInt(st.nextToken());
if (a > max) max = a;
if (a < min) min = a;
if (i == 0) first = a;
if (i == n-1) last = a;
}
if (k == 1) {System.out.println(min); return;}
if (k > 2) {System.out.println(max); return;}
System.out.println(Math.max(first, last));
}
} | Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 3a75782f99c26ca391f745cbce9e6579 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
import java.io.*;
public class solution {
public static void merge(int arr[], int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int[n1];
int R[] = new int[n2];
/*Copy data to temp arrays*/
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
public static void sort(int arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
public static 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) {
long v = a * b;
long u = gcd(a, b);
return v / u;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int a[] = new int[n];
st = new StringTokenizer(br.readLine());
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i=0;i<n;i++){
a[i] = Integer.parseInt(st.nextToken());
min = Math.min(min,a[i]);
max = Math.max(max,a[i]);
}
if(k>2){
System.out.println(max);
}
else if(k == 2){
System.out.println(Math.max(a[0],a[n-1]));
}
else{
System.out.println(min);
}
}
static class sort implements Comparator<ArrayList<Integer>> {
@Override
public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
int c = o1.get(0).compareTo(o2.get(0));
return c;
}
}
} | Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 9542bf195751c9ef64c940e7a9d9c953 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class MaxMaxMin {
public static void main(String[] args) {
final Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int[] list = new int[n];
for (int i = 0; i < n; i++) {
list[i] = scan.nextInt();
}
if(k==1) {
Arrays.sort(list);
System.out.println(list[0]);
} else if(k==2) {
System.out.println(Math.max(list[0],list[n-1]));
}else {
Arrays.sort(list);
System.out.println(Math.max(list[0],list[n-1]));
}
scan.close();
}
}
| Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 1f92a992f9cadf273be203dcc8683bdd | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
private static void solver(InputReader sc, PrintWriter out) throws Exception {
int n =sc.nextInt();
int k =sc.nextInt();
long max = Long.MIN_VALUE;
long min = Long.MAX_VALUE;
long arr[] = new long[n];
for(int i=0; i<n; i++){
long x = sc.nextLong();
arr[i] = x;
if(x < min){
min = x;
}
if(x > max){
max =x;
}
}
if(k==1)
out.println(min);
else if(k==2) {
out.println(Math.max(arr[0],arr[n-1]));
}
else
out.println(max);
}
private static boolean isprime(int x){
if(x==2)
return true;
int sqs = (int)Math.sqrt(x);
for(int i=2; i<=sqs; i++){
if(x%i==0)
return false;
}
return true;
}
private static int gcd (int a, int b) {
if (b == 0)
return a;
else
return gcd (b, a % b);
}
private static long helper(long x){
long ans = (x * (x-1))/2;
return ans;
}
public static void main(String[] args) throws Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
solver(in,out);
out.close();
}
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());
}
public int[] readIntArray(int n){
int arr[] = new int[n];
for(int i=0;i<n;i++) arr[i] = nextInt();
return arr;
}
}
}
class Pair {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
Pair pair = (Pair) o;
return this.x < pair.x && this.y==pair.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
} | Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | d641a310cc04330533ee2ad0d0ff8c27 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Codeforces870B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println(solve(arr, k));
}
private static int solve(int[] arr, int k) {
if(k == 1)
return getMin(arr);
else if(k == 2) {
return Math.max(arr[0], arr[arr.length - 1]);
}
else {
return getMax(arr); // one segment will be max element by itself
}
}
private static int getMax(int[] arr) {
int result = Integer.MIN_VALUE;
for(int i = 0; i < arr.length; i++) {
result = Math.max(arr[i], result);
}
return result;
}
private static int getMin(int[] arr) {
int result = Integer.MAX_VALUE;
for(int i = 0; i < arr.length; i++) {
result = Math.min(arr[i], result);
}
return result;
}
}
| Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 113f6c5cc610f4c08422e6edc2af948e | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.util.*;
import java.math.*;
public class sdf{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for(int i = 0; i < n; i++)
list.add(in.nextInt());
if(k == 1)
System.out.println(Collections.min(list));
else if(k >= 3)
System.out.println(Collections.max(list));
else if (k == 2){
if(list.get(0) >= list.get(n-1))
System.out.println(list.get(0));
else
System.out.println(list.get(n-1));
}
}} | Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 5fd3b94b70d854152ec9d60824268bae | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import static java.lang.Math.*;
public class Main {
void run() throws IOException {
int n = nextInt();
int k = nextInt();
int[] a = new int[n];
int m = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
TreeMap<Integer, Integer> right = new TreeMap<>();
for (int i = 0; i < a.length; i++) {
a[i] = nextInt();
m = max(m, a[i]);
min = min(min, a[i]);
right.put(a[i], right.getOrDefault(a[i], 0) + 1);
}
if (k > 2) {
pw.print(m);
pw.close();
return;
}
if (k == 1) {
pw.print(min);
pw.close();
return;
}
int best = Integer.MIN_VALUE;
TreeMap<Integer, Integer> left = new TreeMap<>();
for (int i = 0; i < a.length - 1; i++) {
left.put(a[i], left.getOrDefault(a[i], 0) + 1);
right.put(a[i], right.get(a[i]) - 1);
if (right.get(a[i]) == 0) right.remove(a[i]);
best = max(best, max(left.firstKey(), right.firstKey()));
}
pw.print(best);
pw.close();
}
class point implements Comparable<point> {
int x, y;
public point(int a, int b) {
x = a;
y = b;
}
@Override
public int compareTo(point o) {
if (o.y == this.y) return Integer.compare(o.x, this.x);
return -Integer.compare(o.y, this.y);
}
}
long mod = (long) 1e9 + 7;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader br = new BufferedReader(new FileReader("qual.in"));
StringTokenizer st = new StringTokenizer("");
PrintWriter pw = new PrintWriter(System.out);
//PrintWriter pw = new PrintWriter("qual.out");
int nextInt() throws IOException {
return Integer.parseInt(next());
}
String next() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public Main() throws FileNotFoundException {
}
public static void main(String[] args) throws IOException {
new Main().run();
}
} | Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 961777cc3c31afa8be58932295b7ac16 | train_001.jsonl | 1508054700 | You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int min=1000000000,max=-1000000000;
int n=sc.nextInt();
int k=sc.nextInt();
int[] n1=new int[n];
for(int i=0;i<n;i++){
int s=sc.nextInt();
n1[i]=s;
min=Math.min(s,min);
max=Math.max(s,max);
}
if(k==1){
System.out.print(min);
}
else if(k==2){
int max1=Math.max(n1[0],n1[n-1]);
System.out.print(max1);
}
else{
System.out.print(max);
}
}
} | Java | ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"] | 1 second | ["5", "-5"] | NoteA subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5. | Java 11 | standard input | [
"greedy"
] | ec1a29826209a0820e8183cccb2d2f01 | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). | 1,200 | Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments. | standard output | |
PASSED | 3daad1d066259555333d568c4c1f22c6 | train_001.jsonl | 1292862000 | There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that1. 1 ≤ i, j ≤ N2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th. | 256 megabytes | /*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
public class S {
public static void main (String[] args) {
Scanner in=new Scanner(System.in);
String s=in.next();
Map<Character,Long> map=new HashMap<>();
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(!map.containsKey(ch))map.put(ch,(long)1);
else map.put(ch,map.get(ch)+1);
}
long res=0;
Set<Map.Entry<Character,Long>> set=map.entrySet();
for(Map.Entry<Character,Long> m:set)
{
res+=(m.getValue()*m.getValue());
}
System.out.print(res);
}
} | Java | ["great10", "aaaaaaaaaa"] | 2 seconds | ["7", "100"] | null | Java 8 | standard input | [
"strings"
] | 6bb2793e275426eb076972fab69d0eba | The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105. | 1,500 | Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count. | standard output | |
PASSED | da639c87c6e7b5f5a92bc747636db5ec | train_001.jsonl | 1292862000 | There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that1. 1 ≤ i, j ≤ N2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th. | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
public class ChoosingSymbolPairs {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
int len = str.length();
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
long count = len;
for(int i=0; i<len; i++) {
char c = str.charAt(i);
if(!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c)+1);
}
}
for(Entry<Character, Integer> entry : map.entrySet()) {
count += perm(entry.getValue());
}
System.out.println(count);
}
static long perm(long n) {
return n * (n-1);
}
}
| Java | ["great10", "aaaaaaaaaa"] | 2 seconds | ["7", "100"] | null | Java 8 | standard input | [
"strings"
] | 6bb2793e275426eb076972fab69d0eba | The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105. | 1,500 | Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count. | standard output | |
PASSED | 13ffe8aa423344edf7aeab5d351cd533 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class B
{
public static void main(String [] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter p = new PrintWriter(System.out);
int n = Integer.valueOf(br.readLine());
int a[] = new int[n];
int b[] = new int[n];
TreeMap<Integer, Integer> hasA = new TreeMap<>();
TreeMap<Integer, Integer> hasB = new TreeMap<>();
String s1[] = br.readLine().split(" ");
String s2[] = br.readLine().split(" ");
for(int i = 0; i < n; i++)
{
a[i] = Integer.valueOf(s1[i]);
b[i] = Integer.valueOf(s2[i]);
hasA.put(a[i], i);
hasB.put(b[i], i);
}
int arr[] = new int[n];
for(int i = 0; i < n; i++)
{
int w = a[i];
int k1 = hasA.get(w);
int k2 = hasB.get(w);
int dif = k1-k2;
if(dif < 0)
{
dif = n + dif;
}
arr[dif]++;
}
int res = 0;
for(int i = 0; i < n; i++)
{
res = Math.max(res,arr[i]);
}
p.println(res);
br.close();
p.close();
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 2843872152d4f3e7a5a084e1c8abb1aa | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.util.*;
import java.io.*;
public class B
{
public static void main(String [] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
int a[] = new int[n];
int b[] = new int[n];
TreeMap<Integer, Integer> hasA = new TreeMap<>();
TreeMap<Integer, Integer> hasB = new TreeMap<>();
String s1[] = br.readLine().split(" ");
String s2[] = br.readLine().split(" ");
for(int i = 0; i < n; i++)
{
a[i] = Integer.valueOf(s1[i]);
b[i] = Integer.valueOf(s2[i]);
hasA.put(a[i], i);
hasB.put(b[i], i);
}
int arr[] = new int[n];
for(int i = 0; i < n; i++)
{
int w = a[i];
int k1 = hasA.get(w);
int k2 = hasB.get(w);
int dif = k1-k2;
if(dif < 0)
{
dif = n + dif;
}
arr[dif]++;
}
int res = 0;
for(int i = 0; i < n; i++)
{
res = Math.max(res,arr[i]);
}
System.out.println(res);
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | e674b7133b27c5b337c0736c1e449180 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes |
import java.util.ArrayList;
/* package codechef; // don't place package name! */
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class TP1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n+1];
int b[]=new int[n+1];
for(int i=1;i<=n;i++)
{
a[sc.nextInt()]=i;
}
for(int i=1;i<=n;i++)
{
b[sc.nextInt()]=i;
}
int max=0,max1=0;
int m[]=new int[n+1];
for(int i=1;i<=n;i++)
{
int i1=a[i];
int i2=b[i];
if(i1>i2)
{
m[n-i1+i2]++;
max=Math.max(max,m[n-i1+i2]);
//System.out.println(m[n-i1+i2]+" "+(n-i1+i2)+" "+max);
}
else
{
//System.out.println(i+" "+i1);
m[i2-i1]++;
max=Math.max(max, m[i2-i1]);
//System.out.println(m[i2-i1]+" "+(i2-i1)+" "+max);
}
}
System.out.println(max);
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 0f45b3d1dee7fa01b80e62e1729b3a1f | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.util.Scanner;
public class C1365 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int[] nums = new int[length + 1];
int[] differences = new int[length];
for(int i = 0; i < length; i++) {
nums[scanner.nextInt()] = i;
}
for(int i = 0; i < length; i++) {
int temporal = scanner.nextInt();
int current = nums[temporal] - i;
if(current < 0)
current += length;
differences[current]++;
}
int answer = 0;
for(int value: differences) {
answer = Math.max(answer, value);
}
System.out.println(answer);
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 4a146ec99e12119b4b85ecc05f19473b | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.util.Collection;
import java.util.HashMap;
import java.util.Scanner;
public class C1365 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int[] nums = new int[length + 1];
HashMap<Integer, Integer> differences = new HashMap<Integer, Integer>();
for(int i = 0; i < length; i++) {
nums[scanner.nextInt()] = i;
}
for(int i = 0; i < length; i++) {
int temporal = scanner.nextInt();
int current = nums[temporal] - i;
if(current < 0)
current += length;
if(!differences.containsKey(current))
differences.put(current, 1);
else {
int temp = differences.get(current);
differences.replace(current, temp, temp + 1);
}
}
int answer = 0;
Collection<Integer> differe = differences.values();
for(int value: differe) {
answer = Math.max(answer, value);
}
System.out.println(answer);
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 0e89cd7431d4659097356c2fdee5886a | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | /*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class d {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int[] arr=new int[n];
int[] brr=new int[n];
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<n;i++) {
arr[i]=s.nextInt();
map.put(arr[i], i+1);
}
for(int i=0;i<n;i++) {
brr[i]=s.nextInt();
}
HashMap<Integer,Integer> map2=new HashMap<>();
for(int i=0;i<n;i++) {
if(map2.containsKey(brr[i]))
map2.put(brr[i], (map.get(brr[i])-(i+1)+n)%n);
}
HashMap<Integer,Integer> map3=new HashMap<>();
for(int i=0;i<n;i++) {
map3.put(brr[i], (-map.get(brr[i])+(i+1)+n)%n);
}
int max=-1;
int[] ans=new int[200002];
for(Map.Entry<Integer, Integer> entry:map2.entrySet()) {
ans[entry.getValue()]++;
}
for(Map.Entry<Integer, Integer> entry:map3.entrySet()) {
ans[entry.getValue()]++;
}
for(int i=0;i<ans.length;i++) {
max=Math.max(ans[i], max);
}
System.out.println(max);
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | dbc0889b98f1234101c56192ba62455b | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | /*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int[] arr=new int[n];
int[] brr=new int[n];
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<n;i++) {
arr[i]=s.nextInt();
map.put(arr[i], i+1);
}
for(int i=0;i<n;i++) {
brr[i]=s.nextInt();
}
HashMap<Integer,Integer> map2=new HashMap<>();
for(int i=0;i<n;i++) {
if(map2.containsKey(brr[i]))
map2.put(brr[i], (map.get(brr[i])-(i+1)+n)%n);
}
HashMap<Integer,Integer> map3=new HashMap<>();
for(int i=0;i<n;i++) {
map3.put(brr[i], (-map.get(brr[i])+(i+1)+n)%n);
}
int max=-1;
int[] ans=new int[200002];
for(Map.Entry<Integer, Integer> entry:map2.entrySet()) {
ans[entry.getValue()]++;
}
for(int i=0;i<ans.length;i++) {
max=Math.max(ans[i], max);
}
for(int i=0;i<200002;i++) {
ans[i]=0;
}
for(Map.Entry<Integer, Integer> entry:map3.entrySet()) {
ans[entry.getValue()]++;
}
for(int i=0;i<ans.length;i++) {
max=Math.max(ans[i], max);
}
System.out.println(max);
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | c72abc6636cd1ac16a6cb19d5399ccf2 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n+1];
int[] b = new int[n+1];
int[] c = new int[n+1];
for(int i=1;i<=n;i++){
int temp = in.nextInt();
a[temp] = i;
}
for(int i=1;i<=n;i++){
int temp = in.nextInt();
b[temp] = i;
}
for(int i=1;i<=n;i++) {
int temp = a[i] - b[i];
if(temp<0)
temp += n;
c[temp]++;
}
int sum = 0;
for(int i=0;i<=n;i++) {
if(c[i]>sum)
sum = c[i];
}
System.out.println(sum);
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | f4d3acc3592ee0dd43131448f2f65a5c | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes |
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
/**
* @author hypnos
* @date 2020/11/24
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr1 = new int[n];
int[] arr2 = new int[n+1];
for (int i = 0; i < n; i++) {
arr1[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
arr2[sc.nextInt()] = i;
}
// System.out.println(Arrays.toString(arr1));
// System.out.println(Arrays.toString(arr2));
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
// System.out.println(arr1[i]);
int index = arr2[arr1[i]]-i;
if (index<0){
index+=n;
}
// System.out.println("count : "+count+" i : "+i+" index : "+index+ " s1.charAt(i) :"+s1.charAt(i));
map.put(index, map.getOrDefault(index, 0)+1);
}
// for (int a: map.keySet()){
// System.out.println(a+" "+map.get(a));
// }
int max = 0;
for (int v: map.values()){
if (v>max){
max = v;
}
}
System.out.println(max);
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | e59e6a717251acc72c001257f626edb3 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeMap;
import java.util.StringTokenizer;
public class codeforces1365C
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader s=new FastReader();
StringBuilder sb=new StringBuilder();
int n=s.nextInt(),i,x,m=1,h;
int[] a = new int[n];
int[] b = new int[n];
for(i=0;i<n;i++)
a[s.nextInt()-1]=i;
for(i=0;i<n;i++)
b[s.nextInt()-1]=i;
TreeMap<Integer,Integer> hm = new TreeMap<>();
for(i=0;i<n;i++)
{
x=a[i]-b[i];
if(x<0)
x=x+n;
if(hm.get(x)!=null)
{
h=hm.get(x);
hm.replace(x,h,h+1);
m=Math.max(m,h+1);
}
else
hm.put(x,1);
}
sb.append(m);
System.out.print(m);
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 03ee1dcd3961a59b68a8eca005b214ae | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class codeforces1365C
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader s=new FastReader();
StringBuilder sb=new StringBuilder();
int n=s.nextInt(),i,x,m=1,h;
int[] a = new int[n];
int[] b = new int[n];
for(i=0;i<n;i++)
a[s.nextInt()-1]=i;
for(i=0;i<n;i++)
b[s.nextInt()-1]=i;
TreeMap<Integer,Integer> hm = new TreeMap<>();
for(i=0;i<n;i++)
{
x=a[i]-b[i];
if(x<0)
x=x+n;
if(hm.get(x)!=null)
{
h=hm.get(x);
hm.replace(x,h,h+1);
m=Math.max(m,h+1);
}
else
hm.put(x,1);
}
sb.append(m);
System.out.print(m);
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 113b43d0d20464852dc0a121fe3bd538 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.util.Scanner;
import java.util.HashMap;
public class codeforces1365C
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n=s.nextInt(),i,x,m=1,h;
int[] a = new int[n];
int[] b = new int[n];
for(i=0;i<n;i++)
a[s.nextInt()-1]=i;
for(i=0;i<n;i++)
b[s.nextInt()-1]=i;
HashMap<Integer,Integer> hm = new HashMap<>();
for(i=0;i<n;i++)
{
x=a[i]-b[i];
if(x<0)
x=x+n;
if(hm.get(x)!=null)
{
h=hm.get(x);
hm.replace(x,h,h+1);
m=Math.max(m,h+1);
}
else
hm.put(x,1);
}
System.out.print(m);
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 0e43b9a75959b2bce9d0423b72af00cc | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class SomeJavaThing {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer st;
public static void main(String args[]) throws IOException{
int tempRead;
int size = readInt();
int p;
int F = 0;
int biggest = 0;
HashMap<Integer, Integer> myHashMap = new HashMap<>();
HashMap<Integer, Integer> indexes = new HashMap<>();
ArrayList<Integer> mArrayList = new ArrayList<>();
int[] array1 = new int[size];
int[] array2 = new int[size];
for (int i = 0; i < size; i++) {
array1[i] = readInt();
}
for (int i = 0; i < size; i++) {
tempRead = readInt();
array2[i] = tempRead;
indexes.put(tempRead,i);
}
for (int i = 0; i < size; i++) {
p = /*isContained(array2,array1[i])*/indexes.get(array1[i]) - i;
if (p < 0) {
p += size;
}
if (p != 1) {
if (myHashMap.get(p) != null) {
myHashMap.put(p, myHashMap.get(p) + 1);
//if (!mArrayList.contains(p)) {
mArrayList.add(p);
//}
} else {
myHashMap.put(p, 1);
//if (!mArrayList.contains(p)) {
mArrayList.add(p);
//}
}
}
}
//System.out.println();
for (int b: mArrayList) {
F = myHashMap.get(b);
if (F > biggest) {
biggest = F;
};
}
System.out.println(biggest);
}
private static int readInt() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return Integer.parseInt(st.nextToken());
}
//public void Organizer(int z; int y; int x) {
// int
//}
/*public static int isContained(int[] array, int element) {
for (int i = 0; i < array.length; i++) {
if (array[i] == element) {
return i;
}
}
return -1;
}*/
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 3db034b29508265ac883652fb7a25f62 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | /* package codechef; // 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 Main
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t=1;
while(t-->0)
{
int n=Integer.parseInt(bf.readLine());
String s1[]=bf.readLine().split(" ");
String s2[]=bf.readLine().split(" ");
int c[]=new int[n];
int a[]=new int[n+1];
int b[]=new int[n+1];
for(int i=0;i<n;i++)
{
int z=Integer.parseInt(s1[i]);
a[z]=i+1;
b[i+1]=Integer.parseInt(s2[i]);
}
for(int i=1;i<=n;i++)
{
int sub=i-a[b[i]];
if(sub<0)
{
sub+=n;
}
c[sub]++;
}
Arrays.sort(c);
System.out.println(c[n-1]);
}
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 617b5f811b13dec04b7b25ecf275d889 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
InputReader in = new InputReader(inputStream);
// for(int i=4;i<=4;i++) {
// InputStream uinputStream = new FileInputStream("timeline.in");
//String f = i+".in";
//InputStream uinputStream = new FileInputStream(f);
// InputReader in = new InputReader(uinputStream);
// PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("timeline.out")));
// }
Task t = new Task();
t.solve(in, out);
out.close();
}
static class Task{
public void solve(InputReader in, PrintWriter out) throws IOException {
int c = 1;
while(c-->0) {
int n = in.nextInt();
int arr[] = in.readIntArray(n);
int brr[] = in.readIntArray(n);
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
for(int i=0;i<n;i++) mp.put(arr[i], i);
HashMap<Integer,Integer> cnt = new HashMap<Integer,Integer>();
int max = 1;
for(int i=0;i<n;i++) {
int pre = mp.get(brr[i]);
int v = (i+n-pre)%n;
cnt.put(v, cnt.getOrDefault(v, 0)+1);
if(cnt.get(v)>max) max = cnt.get(v);
}
out.println(max);
}
}
public double getProbability(int[] balls) {
int sum = 0;
int col = balls.length;
int p = 0;
int q = 0;
for(int i:balls) sum+=i;
int t[] = new int[sum];
for(int i:balls) {
for(int j=0;j<i;j++) {
t[q++] = p;
}
p++;
}
int l[][][] = new int[sum/2+1][1<<col][1<<col];
int r[][][] = new int[sum/2+1][1<<col][1<<col];
for(int i=0;i<1<<sum/2;i++) {
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int ones = 0; int zero = 0;
for(int j=0;j<sum/2;j++) {
if((i&(1<<j))!=0) {
a|=(1<<t[sum/2-1-j]);
c|=(1<<t[sum-1-j]);
ones++;
}else {
b|=(1<<t[sum/2-1-j]);
d|=(1<<t[sum-1-j]);
zero++;
}
}
l[ones][a][b]++;
r[zero][c][d]++;
}
int ret = 0;
for(int i=0;i<sum/2+1;i++) {
for(int j=0;j<1<<col;j++) {
for(int k=0;k<1<<col;k++) {
if(l[i][j][k]==0) continue;
for(int x=0;x<1<<col;x++) {
for(int y=0;y<1<<col;y++) {
if(r[sum/2-i][x][y]==0) continue;
int cl = 0;
int cr = 0;
for(int tt=0;tt<col;tt++) {
if((j&1<<tt)!=0||(x&1<<tt)!=0) cl++;
if((k&1<<tt)!=0||(y&1<<tt)!=0) cr++;
}
if(cl==cr) {
ret+=l[i][j][k]*r[sum/2-i][x][y];
}
}
}
}
}
}
int f[] = new int[col];
for(int i=0;i<col;i++) {
int x = 1;
for(int j=2;j<=balls[i];j++) {
x*=j;
}
f[i] = x;
}
int tot = 1;
p = 0;
for(int i=sum;i>=1;i--) {
tot*=i;
if(p<col&&tot>=f[p]) tot/=f[p++];
}
return 1.0*ret/tot;
}
class item{
int l,r;
public item(int a, int b) {
l=a;r=b;
}
}
long longRandomPrime() {
BigInteger prime = BigInteger.probablePrime(31, new Random());
return prime.longValue();
}
static class lca_naive{
int n;
ArrayList<edge>[] g;
int lvl[];
int pare[];
int dist[];
public lca_naive(int t, ArrayList<edge>[] x) {
n=t;
g=x;
lvl = new int[n];
pare = new int[n];
dist = new int[n];
}
void pre_process() {
dfs(0,-1,g,lvl,pare,dist);
}
void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[]) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
pare[nxt_edge.t] = cur;
dfs(nxt_edge.t,cur,g,lvl,pare,dist);
}
}
}
public int work(int p, int q) {
int a = p;
int b = q;
while(lvl[p]<lvl[q]) q = pare[q];
while(lvl[p]>lvl[q]) p = pare[p];
while(p!=q){p = pare[p]; q = pare[q];}
int c = p;
return dist[a]+dist[b]-dist[c]*2;
}
}
static class lca_binary_lifting{
int n;
ArrayList<edge>[] g;
int lvl[];
int pare[];
int dist[];
int table[][];
public lca_binary_lifting(int a, ArrayList<edge>[] t) {
n = a;
g = t;
lvl = new int[n];
pare = new int[n];
dist = new int[n];
table = new int[20][n];
}
void pre_process() {
dfs(0,-1,g,lvl,pare,dist);
for(int i=0;i<20;i++) {
for(int j=0;j<n;j++) {
if(i==0) table[0][j] = pare[j];
else table[i][j] = table[i-1][table[i-1][j]];
}
}
}
void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[]) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
pare[nxt_edge.t] = cur;
dfs(nxt_edge.t,cur,g,lvl,pare,dist);
}
}
}
public int work(int p, int q) {
int a = p;
int b = q;
if(lvl[p]>lvl[q]) {
int tmp = p;
p=q;
q=tmp;
}
for(int i=19;i>=0;i--) {
if(lvl[table[i][q]]>=lvl[p]) q=table[i][q];
}
if(p==q) return p;// return dist[a]+dist[b]-dist[p]*2;
for(int i=19;i>=0;i--) {
if(table[i][p]!=table[i][q]) {
p = table[i][p];
q = table[i][q];
}
}
return table[0][p];
//return dist[a]+dist[b]-dist[table[0][p]]*2;
}
}
static class lca_sqrt_root{
int n;
ArrayList<edge>[] g;
int lvl[];
int pare[];
int dist[];
int jump[];
int sz;
public lca_sqrt_root(int a, ArrayList<edge>[] b) {
n=a;
g=b;
lvl = new int[n];
pare = new int[n];
dist = new int[n];
jump = new int[n];
sz = (int) Math.sqrt(n);
}
void pre_process() {
dfs(0,-1,g,lvl,pare,dist,jump);
}
void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[], int[] jump) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
pare[nxt_edge.t] = cur;
if(lvl[nxt_edge.t]%sz==0) {
jump[nxt_edge.t] = cur;
}else {
jump[nxt_edge.t] = jump[cur];
}
dfs(nxt_edge.t,cur,g,lvl,pare,dist,jump);
}
}
}
int work(int p, int q) {
int a = p; int b = q;
if(lvl[p]>lvl[q]) {
int tmp = p;
p=q;
q=tmp;
}
while(jump[p]!=jump[q]) {
if(lvl[p]>lvl[q]) p = jump[p];
else q = jump[q];
}
while(p!=q) {
if(lvl[p]>lvl[q]) p = pare[p];
else q = pare[q];
}
return dist[a]+dist[b]-dist[p]*2;
}
}
class edge implements Comparable<edge>{
int f,t,len;
public edge(int a, int b, int c) {
f=a;t=b;len=c;
}
@Override
public int compareTo(edge t) {
return t.len-this.len;
}
}
class pair implements Comparable<pair>{
int idx,lvl;
public pair(int a, int b) {
idx = a;
lvl = b;
}
@Override
public int compareTo(pair t) {
return t.lvl-this.lvl;
}
}
static class lca_RMQ{
int n;
ArrayList<edge>[] g;
int lvl[];
int dist[];
int tour[];
int tour_rank[];
int first_occ[];
int c;
sgt s;
public lca_RMQ(int a, ArrayList<edge>[] b) {
n=a;
g=b;
c=0;
lvl = new int[n];
dist = new int[n];
tour = new int[2*n];
tour_rank = new int[2*n];
first_occ = new int[n];
Arrays.fill(first_occ, -1);
}
void pre_process() {
tour[c++] = 0;
dfs(0,-1);
for(int i=0;i<2*n;i++) {
tour_rank[i] = lvl[tour[i]];
if(first_occ[tour[i]]==-1) first_occ[tour[i]] = i;
}
s = new sgt(0,2*n,tour_rank);
}
void dfs(int cur, int pre) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
tour[c++] = nxt_edge.t;
dfs(nxt_edge.t,cur);
tour[c++] = cur;
}
}
}
int work(int p,int q) {
int a = Math.max(first_occ[p], first_occ[q]);
int b = Math.min(first_occ[p], first_occ[q]);
int idx = s.query_min_idx(b, a+1);
//Dumper.print(a+" "+b+" "+idx);
int c = tour[idx];
return dist[p]+dist[q]-dist[c]*2;
}
}
static class sgt{
sgt lt;
sgt rt;
int l,r;
int sum, max, min, lazy;
int min_idx;
public sgt(int L, int R, int arr[]) {
l=L;r=R;
if(l==r-1) {
sum = max = min = arr[l];
lazy = 0;
min_idx = l;
return;
}
lt = new sgt(l, l+r>>1, arr);
rt = new sgt(l+r>>1, r, arr);
pop_up();
}
void pop_up() {
this.sum = lt.sum + rt.sum;
this.max = Math.max(lt.max, rt.max);
this.min = Math.min(lt.min, rt.min);
if(lt.min<rt.min)
this.min_idx = lt.min_idx;
else if(lt.min>rt.min) this.min_idx = rt.min_idx;
else this.min = Math.min(lt.min_idx, rt.min_idx);
}
void push_down() {
if(this.lazy!=0) {
lt.sum+=lazy;
rt.sum+=lazy;
lt.max+=lazy;
lt.min+=lazy;
rt.max+=lazy;
rt.min+=lazy;
lt.lazy+=this.lazy;
rt.lazy+=this.lazy;
this.lazy = 0;
}
}
void change(int L, int R, int v) {
if(R<=l||r<=L) return;
if(L<=l&&r<=R) {
this.max+=v;
this.min+=v;
this.sum+=v*(r-l);
this.lazy+=v;
return;
}
push_down();
lt.change(L, R, v);
rt.change(L, R, v);
pop_up();
}
int query_max(int L, int R) {
if(L<=l&&r<=R) return this.max;
if(r<=L||R<=l) return Integer.MIN_VALUE;
push_down();
return Math.max(lt.query_max(L, R), rt.query_max(L, R));
}
int query_min(int L, int R) {
if(L<=l&&r<=R) return this.min;
if(r<=L||R<=l) return Integer.MAX_VALUE;
push_down();
return Math.min(lt.query_min(L, R), rt.query_min(L, R));
}
int query_sum(int L, int R) {
if(L<=l&&r<=R) return this.sum;
if(r<=L||R<=l) return 0;
push_down();
return lt.query_sum(L, R) + rt.query_sum(L, R);
}
int query_min_idx(int L, int R) {
if(L<=l&&r<=R) return this.min_idx;
if(r<=L||R<=l) return Integer.MAX_VALUE;
int a = lt.query_min_idx(L, R);
int b = rt.query_min_idx(L, R);
int aa = lt.query_min(L, R);
int bb = rt.query_min(L, R);
if(aa<bb) return a;
else if(aa>bb) return b;
return Math.min(a,b);
}
}
List<List<Integer>> convert(int arr[][]){
int n = arr.length;
List<List<Integer>> ret = new ArrayList<>();
for(int i=0;i<n;i++) {
ArrayList<Integer> tmp = new ArrayList<Integer>();
for(int j=0;j<arr[i].length;j++) tmp.add(arr[i][j]);
ret.add(tmp);
}
return ret;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
public long GCD(long a, long b) {
if (b==0) return a;
return GCD(b,a%b);
}
}
static class ArrayUtils {
static final long seed = System.nanoTime();
static final Random rand = new Random(seed);
public static void sort(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void shuffle(int[] a) {
for (int i = 0; i < a.length; i++) {
int j = rand.nextInt(i + 1);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
public static void sort(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void shuffle(long[] a) {
for (int i = 0; i < a.length; i++) {
int j = rand.nextInt(i + 1);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
static class BIT{
int arr[];
int n;
public BIT(int a) {
n=a;
arr = new int[n];
}
int sum(int p) {
int s=0;
while(p>0) {
s+=arr[p];
p-=p&(-p);
}
return s;
}
void add(int p, int v) {
while(p<n) {
arr[p]+=v;
p+=p&(-p);
}
}
}
static class DSU{
int[] arr;
int[] sz;
public DSU(int n) {
arr = new int[n];
sz = new int[n];
for(int i=0;i<n;i++) arr[i] = i;
Arrays.fill(sz, 1);
}
public int find(int a) {
if(arr[a]!=a) arr[a] = find(arr[a]);
return arr[a];
}
public void union(int a, int b) {
int x = find(a);
int y = find(b);
if(x==y) return;
arr[y] = x;
sz[x] += sz[y];
}
public int size(int x) {
return sz[find(x)];
}
}
static class MinHeap<Key> implements Iterable<Key> {
private int maxN;
private int n;
private int[] pq;
private int[] qp;
private Key[] keys;
private Comparator<Key> comparator;
public MinHeap(int capacity){
if (capacity < 0) throw new IllegalArgumentException();
this.maxN = capacity;
n=0;
pq = new int[maxN+1];
qp = new int[maxN+1];
keys = (Key[]) new Object[capacity+1];
Arrays.fill(qp, -1);
}
public MinHeap(int capacity, Comparator<Key> c){
if (capacity < 0) throw new IllegalArgumentException();
this.maxN = capacity;
n=0;
pq = new int[maxN+1];
qp = new int[maxN+1];
keys = (Key[]) new Object[capacity+1];
Arrays.fill(qp, -1);
comparator = c;
}
public boolean isEmpty() { return n==0; }
public int size() { return n; }
public boolean contains(int i) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
return qp[i] != -1;
}
public int peekIdx() {
if (n == 0) throw new NoSuchElementException("Priority queue underflow");
return pq[1];
}
public Key peek(){
if(isEmpty()) throw new NoSuchElementException("Priority queue underflow");
return keys[pq[1]];
}
public int poll(){
if(isEmpty()) throw new NoSuchElementException("Priority queue underflow");
int min = pq[1];
exch(1,n--);
down(1);
assert min==pq[n+1];
qp[min] = -1;
keys[min] = null;
pq[n+1] = -1;
return min;
}
public void update(int i, Key key) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (!contains(i)) {
this.add(i, key);
}else {
keys[i] = key;
up(qp[i]);
down(qp[i]);
}
}
private void add(int i, Key x){
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue");
n++;
qp[i] = n;
pq[n] = i;
keys[i] = x;
up(n);
}
private void up(int k){
while(k>1&&less(k,k/2)){
exch(k,k/2);
k/=2;
}
}
private void down(int k){
while(2*k<=n){
int j=2*k;
if(j<n&&less(j+1,j)) j++;
if(less(k,j)) break;
exch(k,j);
k=j;
}
}
public boolean less(int i, int j){
if (comparator == null) {
return ((Comparable<Key>) keys[pq[i]]).compareTo(keys[pq[j]]) < 0;
}
else {
return comparator.compare(keys[pq[i]], keys[pq[j]]) < 0;
}
}
public void exch(int i, int j){
int swap = pq[i];
pq[i] = pq[j];
pq[j] = swap;
qp[pq[i]] = i;
qp[pq[j]] = j;
}
@Override
public Iterator<Key> iterator() {
// TODO Auto-generated method stub
return null;
}
}
private static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int zcurChar;
private int znumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (znumChars == -1)
throw new InputMismatchException();
if (zcurChar >= znumChars)
{
zcurChar = 0;
try
{
znumChars = stream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (znumChars <= 0)
return -1;
}
return buf[zcurChar++];
}
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 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 double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return nextString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
public int[] readIntArray(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = nextInt();
}
return ret;
}
}
static class Dumper {
static void print_int_arr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_char_arr(char[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_double_arr(double[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_2d_arr(int[][] arr, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("---------------------");
}
static void print_2d_arr(boolean[][] arr, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("---------------------");
}
static void print(Object o) {
System.out.println(o.toString());
}
static void getc() {
System.out.println("here");
}
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 9cbdd521584ac8d5fc3ffb142776e5d8 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
InputReader in = new InputReader(inputStream);
// for(int i=4;i<=4;i++) {
// InputStream uinputStream = new FileInputStream("timeline.in");
//String f = i+".in";
//InputStream uinputStream = new FileInputStream(f);
// InputReader in = new InputReader(uinputStream);
// PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("timeline.out")));
// }
Task t = new Task();
t.solve(in, out);
out.close();
}
static class Task{
public void solve(InputReader in, PrintWriter out) throws IOException {
int c = 1;
while(c-->0) {
int n = in.nextInt();
int arr[] = new int[2*n];
int idx = -1;
for(int i=0;i<n;i++) {
arr[i] = in.nextInt();
arr[i+n] = arr[i];
if(arr[i]==1&&idx==-1) idx = i;
}
int brr[] = in.readIntArray(n);
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
for(int i=idx;i<idx+n;i++) mp.put(arr[i], i-idx);
HashMap<Integer,Integer> cnt = new HashMap<Integer,Integer>();
int max = 1;
for(int i=0;i<n;i++) {
int pre = mp.get(brr[i]);
int v = (i+n-pre)%n;
cnt.put(v, cnt.getOrDefault(v, 0)+1);
if(cnt.get(v)>max) max = cnt.get(v);
}
out.println(max);
}
}
public double getProbability(int[] balls) {
int sum = 0;
int col = balls.length;
int p = 0;
int q = 0;
for(int i:balls) sum+=i;
int t[] = new int[sum];
for(int i:balls) {
for(int j=0;j<i;j++) {
t[q++] = p;
}
p++;
}
int l[][][] = new int[sum/2+1][1<<col][1<<col];
int r[][][] = new int[sum/2+1][1<<col][1<<col];
for(int i=0;i<1<<sum/2;i++) {
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int ones = 0; int zero = 0;
for(int j=0;j<sum/2;j++) {
if((i&(1<<j))!=0) {
a|=(1<<t[sum/2-1-j]);
c|=(1<<t[sum-1-j]);
ones++;
}else {
b|=(1<<t[sum/2-1-j]);
d|=(1<<t[sum-1-j]);
zero++;
}
}
l[ones][a][b]++;
r[zero][c][d]++;
}
int ret = 0;
for(int i=0;i<sum/2+1;i++) {
for(int j=0;j<1<<col;j++) {
for(int k=0;k<1<<col;k++) {
if(l[i][j][k]==0) continue;
for(int x=0;x<1<<col;x++) {
for(int y=0;y<1<<col;y++) {
if(r[sum/2-i][x][y]==0) continue;
int cl = 0;
int cr = 0;
for(int tt=0;tt<col;tt++) {
if((j&1<<tt)!=0||(x&1<<tt)!=0) cl++;
if((k&1<<tt)!=0||(y&1<<tt)!=0) cr++;
}
if(cl==cr) {
ret+=l[i][j][k]*r[sum/2-i][x][y];
}
}
}
}
}
}
int f[] = new int[col];
for(int i=0;i<col;i++) {
int x = 1;
for(int j=2;j<=balls[i];j++) {
x*=j;
}
f[i] = x;
}
int tot = 1;
p = 0;
for(int i=sum;i>=1;i--) {
tot*=i;
if(p<col&&tot>=f[p]) tot/=f[p++];
}
return 1.0*ret/tot;
}
class item{
int l,r;
public item(int a, int b) {
l=a;r=b;
}
}
long longRandomPrime() {
BigInteger prime = BigInteger.probablePrime(31, new Random());
return prime.longValue();
}
static class lca_naive{
int n;
ArrayList<edge>[] g;
int lvl[];
int pare[];
int dist[];
public lca_naive(int t, ArrayList<edge>[] x) {
n=t;
g=x;
lvl = new int[n];
pare = new int[n];
dist = new int[n];
}
void pre_process() {
dfs(0,-1,g,lvl,pare,dist);
}
void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[]) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
pare[nxt_edge.t] = cur;
dfs(nxt_edge.t,cur,g,lvl,pare,dist);
}
}
}
public int work(int p, int q) {
int a = p;
int b = q;
while(lvl[p]<lvl[q]) q = pare[q];
while(lvl[p]>lvl[q]) p = pare[p];
while(p!=q){p = pare[p]; q = pare[q];}
int c = p;
return dist[a]+dist[b]-dist[c]*2;
}
}
static class lca_binary_lifting{
int n;
ArrayList<edge>[] g;
int lvl[];
int pare[];
int dist[];
int table[][];
public lca_binary_lifting(int a, ArrayList<edge>[] t) {
n = a;
g = t;
lvl = new int[n];
pare = new int[n];
dist = new int[n];
table = new int[20][n];
}
void pre_process() {
dfs(0,-1,g,lvl,pare,dist);
for(int i=0;i<20;i++) {
for(int j=0;j<n;j++) {
if(i==0) table[0][j] = pare[j];
else table[i][j] = table[i-1][table[i-1][j]];
}
}
}
void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[]) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
pare[nxt_edge.t] = cur;
dfs(nxt_edge.t,cur,g,lvl,pare,dist);
}
}
}
public int work(int p, int q) {
int a = p;
int b = q;
if(lvl[p]>lvl[q]) {
int tmp = p;
p=q;
q=tmp;
}
for(int i=19;i>=0;i--) {
if(lvl[table[i][q]]>=lvl[p]) q=table[i][q];
}
if(p==q) return p;// return dist[a]+dist[b]-dist[p]*2;
for(int i=19;i>=0;i--) {
if(table[i][p]!=table[i][q]) {
p = table[i][p];
q = table[i][q];
}
}
return table[0][p];
//return dist[a]+dist[b]-dist[table[0][p]]*2;
}
}
static class lca_sqrt_root{
int n;
ArrayList<edge>[] g;
int lvl[];
int pare[];
int dist[];
int jump[];
int sz;
public lca_sqrt_root(int a, ArrayList<edge>[] b) {
n=a;
g=b;
lvl = new int[n];
pare = new int[n];
dist = new int[n];
jump = new int[n];
sz = (int) Math.sqrt(n);
}
void pre_process() {
dfs(0,-1,g,lvl,pare,dist,jump);
}
void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[], int[] jump) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
pare[nxt_edge.t] = cur;
if(lvl[nxt_edge.t]%sz==0) {
jump[nxt_edge.t] = cur;
}else {
jump[nxt_edge.t] = jump[cur];
}
dfs(nxt_edge.t,cur,g,lvl,pare,dist,jump);
}
}
}
int work(int p, int q) {
int a = p; int b = q;
if(lvl[p]>lvl[q]) {
int tmp = p;
p=q;
q=tmp;
}
while(jump[p]!=jump[q]) {
if(lvl[p]>lvl[q]) p = jump[p];
else q = jump[q];
}
while(p!=q) {
if(lvl[p]>lvl[q]) p = pare[p];
else q = pare[q];
}
return dist[a]+dist[b]-dist[p]*2;
}
}
class edge implements Comparable<edge>{
int f,t,len;
public edge(int a, int b, int c) {
f=a;t=b;len=c;
}
@Override
public int compareTo(edge t) {
return t.len-this.len;
}
}
class pair implements Comparable<pair>{
int idx,lvl;
public pair(int a, int b) {
idx = a;
lvl = b;
}
@Override
public int compareTo(pair t) {
return t.lvl-this.lvl;
}
}
static class lca_RMQ{
int n;
ArrayList<edge>[] g;
int lvl[];
int dist[];
int tour[];
int tour_rank[];
int first_occ[];
int c;
sgt s;
public lca_RMQ(int a, ArrayList<edge>[] b) {
n=a;
g=b;
c=0;
lvl = new int[n];
dist = new int[n];
tour = new int[2*n];
tour_rank = new int[2*n];
first_occ = new int[n];
Arrays.fill(first_occ, -1);
}
void pre_process() {
tour[c++] = 0;
dfs(0,-1);
for(int i=0;i<2*n;i++) {
tour_rank[i] = lvl[tour[i]];
if(first_occ[tour[i]]==-1) first_occ[tour[i]] = i;
}
s = new sgt(0,2*n,tour_rank);
}
void dfs(int cur, int pre) {
for(edge nxt_edge:g[cur]) {
if(nxt_edge.t!=pre) {
lvl[nxt_edge.t] = lvl[cur]+1;
dist[nxt_edge.t] = dist[cur]+nxt_edge.len;
tour[c++] = nxt_edge.t;
dfs(nxt_edge.t,cur);
tour[c++] = cur;
}
}
}
int work(int p,int q) {
int a = Math.max(first_occ[p], first_occ[q]);
int b = Math.min(first_occ[p], first_occ[q]);
int idx = s.query_min_idx(b, a+1);
//Dumper.print(a+" "+b+" "+idx);
int c = tour[idx];
return dist[p]+dist[q]-dist[c]*2;
}
}
static class sgt{
sgt lt;
sgt rt;
int l,r;
int sum, max, min, lazy;
int min_idx;
public sgt(int L, int R, int arr[]) {
l=L;r=R;
if(l==r-1) {
sum = max = min = arr[l];
lazy = 0;
min_idx = l;
return;
}
lt = new sgt(l, l+r>>1, arr);
rt = new sgt(l+r>>1, r, arr);
pop_up();
}
void pop_up() {
this.sum = lt.sum + rt.sum;
this.max = Math.max(lt.max, rt.max);
this.min = Math.min(lt.min, rt.min);
if(lt.min<rt.min)
this.min_idx = lt.min_idx;
else if(lt.min>rt.min) this.min_idx = rt.min_idx;
else this.min = Math.min(lt.min_idx, rt.min_idx);
}
void push_down() {
if(this.lazy!=0) {
lt.sum+=lazy;
rt.sum+=lazy;
lt.max+=lazy;
lt.min+=lazy;
rt.max+=lazy;
rt.min+=lazy;
lt.lazy+=this.lazy;
rt.lazy+=this.lazy;
this.lazy = 0;
}
}
void change(int L, int R, int v) {
if(R<=l||r<=L) return;
if(L<=l&&r<=R) {
this.max+=v;
this.min+=v;
this.sum+=v*(r-l);
this.lazy+=v;
return;
}
push_down();
lt.change(L, R, v);
rt.change(L, R, v);
pop_up();
}
int query_max(int L, int R) {
if(L<=l&&r<=R) return this.max;
if(r<=L||R<=l) return Integer.MIN_VALUE;
push_down();
return Math.max(lt.query_max(L, R), rt.query_max(L, R));
}
int query_min(int L, int R) {
if(L<=l&&r<=R) return this.min;
if(r<=L||R<=l) return Integer.MAX_VALUE;
push_down();
return Math.min(lt.query_min(L, R), rt.query_min(L, R));
}
int query_sum(int L, int R) {
if(L<=l&&r<=R) return this.sum;
if(r<=L||R<=l) return 0;
push_down();
return lt.query_sum(L, R) + rt.query_sum(L, R);
}
int query_min_idx(int L, int R) {
if(L<=l&&r<=R) return this.min_idx;
if(r<=L||R<=l) return Integer.MAX_VALUE;
int a = lt.query_min_idx(L, R);
int b = rt.query_min_idx(L, R);
int aa = lt.query_min(L, R);
int bb = rt.query_min(L, R);
if(aa<bb) return a;
else if(aa>bb) return b;
return Math.min(a,b);
}
}
List<List<Integer>> convert(int arr[][]){
int n = arr.length;
List<List<Integer>> ret = new ArrayList<>();
for(int i=0;i<n;i++) {
ArrayList<Integer> tmp = new ArrayList<Integer>();
for(int j=0;j<arr[i].length;j++) tmp.add(arr[i][j]);
ret.add(tmp);
}
return ret;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
public long GCD(long a, long b) {
if (b==0) return a;
return GCD(b,a%b);
}
}
static class ArrayUtils {
static final long seed = System.nanoTime();
static final Random rand = new Random(seed);
public static void sort(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void shuffle(int[] a) {
for (int i = 0; i < a.length; i++) {
int j = rand.nextInt(i + 1);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
public static void sort(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void shuffle(long[] a) {
for (int i = 0; i < a.length; i++) {
int j = rand.nextInt(i + 1);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
static class BIT{
int arr[];
int n;
public BIT(int a) {
n=a;
arr = new int[n];
}
int sum(int p) {
int s=0;
while(p>0) {
s+=arr[p];
p-=p&(-p);
}
return s;
}
void add(int p, int v) {
while(p<n) {
arr[p]+=v;
p+=p&(-p);
}
}
}
static class DSU{
int[] arr;
int[] sz;
public DSU(int n) {
arr = new int[n];
sz = new int[n];
for(int i=0;i<n;i++) arr[i] = i;
Arrays.fill(sz, 1);
}
public int find(int a) {
if(arr[a]!=a) arr[a] = find(arr[a]);
return arr[a];
}
public void union(int a, int b) {
int x = find(a);
int y = find(b);
if(x==y) return;
arr[y] = x;
sz[x] += sz[y];
}
public int size(int x) {
return sz[find(x)];
}
}
static class MinHeap<Key> implements Iterable<Key> {
private int maxN;
private int n;
private int[] pq;
private int[] qp;
private Key[] keys;
private Comparator<Key> comparator;
public MinHeap(int capacity){
if (capacity < 0) throw new IllegalArgumentException();
this.maxN = capacity;
n=0;
pq = new int[maxN+1];
qp = new int[maxN+1];
keys = (Key[]) new Object[capacity+1];
Arrays.fill(qp, -1);
}
public MinHeap(int capacity, Comparator<Key> c){
if (capacity < 0) throw new IllegalArgumentException();
this.maxN = capacity;
n=0;
pq = new int[maxN+1];
qp = new int[maxN+1];
keys = (Key[]) new Object[capacity+1];
Arrays.fill(qp, -1);
comparator = c;
}
public boolean isEmpty() { return n==0; }
public int size() { return n; }
public boolean contains(int i) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
return qp[i] != -1;
}
public int peekIdx() {
if (n == 0) throw new NoSuchElementException("Priority queue underflow");
return pq[1];
}
public Key peek(){
if(isEmpty()) throw new NoSuchElementException("Priority queue underflow");
return keys[pq[1]];
}
public int poll(){
if(isEmpty()) throw new NoSuchElementException("Priority queue underflow");
int min = pq[1];
exch(1,n--);
down(1);
assert min==pq[n+1];
qp[min] = -1;
keys[min] = null;
pq[n+1] = -1;
return min;
}
public void update(int i, Key key) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (!contains(i)) {
this.add(i, key);
}else {
keys[i] = key;
up(qp[i]);
down(qp[i]);
}
}
private void add(int i, Key x){
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue");
n++;
qp[i] = n;
pq[n] = i;
keys[i] = x;
up(n);
}
private void up(int k){
while(k>1&&less(k,k/2)){
exch(k,k/2);
k/=2;
}
}
private void down(int k){
while(2*k<=n){
int j=2*k;
if(j<n&&less(j+1,j)) j++;
if(less(k,j)) break;
exch(k,j);
k=j;
}
}
public boolean less(int i, int j){
if (comparator == null) {
return ((Comparable<Key>) keys[pq[i]]).compareTo(keys[pq[j]]) < 0;
}
else {
return comparator.compare(keys[pq[i]], keys[pq[j]]) < 0;
}
}
public void exch(int i, int j){
int swap = pq[i];
pq[i] = pq[j];
pq[j] = swap;
qp[pq[i]] = i;
qp[pq[j]] = j;
}
@Override
public Iterator<Key> iterator() {
// TODO Auto-generated method stub
return null;
}
}
private static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int zcurChar;
private int znumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (znumChars == -1)
throw new InputMismatchException();
if (zcurChar >= znumChars)
{
zcurChar = 0;
try
{
znumChars = stream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (znumChars <= 0)
return -1;
}
return buf[zcurChar++];
}
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 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 double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return nextString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
public int[] readIntArray(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = nextInt();
}
return ret;
}
}
static class Dumper {
static void print_int_arr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_char_arr(char[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_double_arr(double[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_2d_arr(int[][] arr, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("---------------------");
}
static void print_2d_arr(boolean[][] arr, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("---------------------");
}
static void print(Object o) {
System.out.println(o.toString());
}
static void getc() {
System.out.println("here");
}
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 055b8e24d2c0ad6e90ea79b7d87b1c56 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.*;
import java.util.*;
/**
*
* @author Aaryan
* AV
*/
public class Test2 {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a [] = new int[n+1];
for(int i=0; i<n; i++){
int x = input.nextInt();
a[x]=i;
}
int b[] = new int[n+1];
for(int i=0; i<n; i++){
b[input.nextInt()]=i;
}
int freq[] = new int[n+1];
for(int i=1; i<=n; i++){
int minus = (n+(a[i]-b[i]))%n;
freq[minus]++;
}
int max = Arrays.stream(freq).max().getAsInt();
out.println(max);
out.flush();
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | fba99b8902a55142a6248c68b57698d9 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.*;
import java.util.*;
/**
*
* @author Aaryan
* AV
*/
public class Test2 {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a [] = new int[n+1];
for(int i=0; i<n; i++){
int x = input.nextInt();
a[x]=i;
}
int b[] = new int[n+1];
for(int i=0; i<n; i++){
b[input.nextInt()]=i;
}
int freq[] = new int[n+1];
for(int i=1; i<=n; i++){
int minus = (n+(a[i]-b[i]))%n;
freq[minus]++;
}
int max = 0;
for(int i=0; i<freq.length; i++){
max = Math.max(max, freq[i]);
}
//System.out.println(Arrays.toString(freq));
out.println(max);
out.flush();
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | a7863f5b3d3e7f0fb6c56b1dd86586bd | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.util.Scanner;
public class Rotation_Matching {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
//int a1[] = new int[n+1];
int b1[] = new int[n+1];
for(int i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
for(int i=0;i<n;i++)
{
b[i] = sc.nextInt();
b1[b[i]] = i+1;
}
//System.out.println(Arrays.toString(b1));
int dis[] = new int[n];
for(int i=0;i<n;i++)
{
int t1 = b1[a[i]]-i-1;
t1 = t1<0?t1+n:t1;
//System.out.println("t1 ="+t1 +" a[i]="+a[i]);
dis[t1]++;
}
int max =0;
for(int i=0;i<n;i++)
{
if(max<dis[i])
{
max = dis[i];
}
}
System.out.println(max);
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | a1dacefe98a5028f32e701ce96f1eaa4 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | /* package codechef; // 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 Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
int arr[] = new int[n + 1];
HashMap<Integer, Integer> map = new HashMap();
for(int i = 0; i<n; i++){
map.put(sc.nextInt(), i);
}
for(int i = 0; i<n; i++){
int a = sc.nextInt();
if(map.get(a) <= i){
arr[i - map.get(a)]++;
//out.println(i - map.get(a));
}
else{
arr[i + n - map.get(a)]++;
//out.println(i + n - map.get(a));
}
}
int cnt = -1;
for(int i = 0; i<arr.length; i++){
//out.print(arr[i] + " ");
cnt = Math.max(arr[i], cnt);
}
out.println(cnt);
out.flush();
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 6f8c9c6bddf1ddfc731954fa5c5ee54e | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | /* package codechef; // 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 Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
int arr[] = new int[n + 1];
HashMap<Integer, Integer> map = new HashMap();
for(int i = 0; i<n; i++)map.put(sc.nextInt(), i);
for(int i = 0; i<n; i++){
int a = sc.nextInt();
if(map.get(a) <= i) arr[i - map.get(a)]++;
else arr[i + n - map.get(a)]++;
}
int cnt = -1;
for(int i = 0; i<arr.length; i++)cnt = Math.max(arr[i], cnt);
out.println(cnt);
out.flush();
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | c425d975bb8db7af6f4999f547dbdcb2 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | //Utilities
import java.io.*;
import java.util.*;
public class Main {
static int N;
static int[] a, b, cnt, map;
static int max = 1;
public static void main(String[] args) throws IOException {
N = in.iscan(); a = new int[N]; map = new int[N+1]; b = new int[N]; cnt = new int[N];
for (int i = 0; i < N; i++) {
a[i] = in.iscan();
map[a[i]] = i;
}
for (int i = 0; i < N; i++) {
b[i] = in.iscan();
if (i <= map[b[i]]) {
cnt[map[b[i]] - i]++;
}
else {
cnt[N - i + map[b[i]]]++;
}
}
for (int shift = 0; shift < N; shift++) {
max = Math.max(max, cnt[shift]);
}
out.println(max);
out.close();
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int gcd (int a, int b) {
return b == 0 ? a : gcd (b, a % b);
}
public static int lcm (int a, int b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static int fast_pow (int b, int x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return val;
}
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 70a379a786a6476cee17cdff357c8141 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @author alanl
*/
public class Main{
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws IOException{
int n = readInt(), a[] = new int[n+1], b[] = new int[n+1];
for(int i = 0; i<n; i++){
a[readInt()] = i;
}
for(int i = 0; i<n; i++){
b[readInt()] = i;
}
int ans = 0;
Map<Integer, Integer>mp = new HashMap();
for(int i = 1; i<=n; i++){
if(b[i]<=a[i])mp.put(a[i]-b[i], mp.getOrDefault(a[i]-b[i], 0)+1);
else mp.put(n-b[i]+a[i], mp.getOrDefault(n-b[i]+a[i], 0)+1);
}
for(Map.Entry<Integer, Integer>e:mp.entrySet()){
ans = Math.max(ans, e.getValue());
}
println(ans);
}
static String next () throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(input.readLine().trim());
return st.nextToken();
}
static long readLong () throws IOException {
return Long.parseLong(next());
}
static int readInt () throws IOException {
return Integer.parseInt(next());
}
static double readDouble () throws IOException {
return Double.parseDouble(next());
}
static char readChar () throws IOException {
return next().charAt(0);
}
static String readLine () throws IOException {
return input.readLine().trim();
}
static void print(Object b) {
System.out.print(b);
}
static void println(Object b) {
System.out.println(b);
}
static void println() {
System.out.println();
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | bafaa07eb7959480f9827627a8c32f14 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.*;
import java.util.*;
/**
* Created by Harry on 5/31/20.
*/
public class Test {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
int n = scanner.nextInt();
int[] aPos = new int[n+1];
int[] bPos = new int[n+1];
for(int i=0; i<n; i++){
int cur = scanner.nextInt();
aPos[cur] = i;
}
for(int i=0; i<n; i++){
int cur = scanner.nextInt();
bPos[cur] = i;
}
HashMap<Integer, Integer> diffFreq = new HashMap();
int res = 0;
for(int i=1; i<=n; i++){
int diff = (aPos[i]-aPos[1]+n)%n - (bPos[i]-bPos[1]+n)%n;
if(diff<0){
diff += n;
}
int curRes = diffFreq.getOrDefault(diff, 0)+1;
diffFreq.put(diff, curRes);
res = Math.max(res, curRes);
}
System.out.println(res);
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 3f0a27dfdf07500c2142509d64c8c03b | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes |
import java.io.*;
import java.util.*;
import java.lang.*;
public class RotationMatching{
public static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public char nextChar() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char)c;
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
static long func(long a[],long size,int s){
long max1=a[s];
long maxc=a[s];
for(int i=s+1;i<size;i++){
maxc=Math.max(a[i],maxc+a[i]);
max1=Math.max(maxc,max1);
}
return max1;
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
public static void main(String args[])
{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader sc = new FastReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int n = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = sc.nextInt();
}
for(int i = 0; i < n; i++)
{
b[i] = sc.nextInt();
}
int index_b[] = new int[n + 1];
for(int i = 0;i < n; i++)
{
index_b[b[i]] = i;
}
int right[] = new int[n + 1];
int left[] = new int[n + 1];
for(int i = 0;i < n; i++)
{
int diff = i - index_b[a[i]];
if(diff < 0)
{
right[Math.abs(diff)]++;
left[n - Math.abs(diff)]++;
}
else
{
right[n - diff]++;
left[diff]++;
}
}
Arrays.sort(left);
Arrays.sort(right);
w.println(Math.max(right[n], left[n]));
w.close();
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | f72467490c08055dcba073ed39f3dcfa | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | /* 盗图小能手
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/
import java.io.*;
import java.util.*;
public class C {
/**
* TODO 提交的时候一定要设置为 true
*/
boolean submitFlag = true;
private void solve(int n, int[] a, int[] b) {
int ans = 0;
int max = n;
int toRight = 0;
while (max > ans) {
int res = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[(i + n - toRight) % n]) {
res++;
}
}
max -= res;
ans = Math.max(res, ans);
toRight++;
}
System.out.println(ans);
}
private void run() throws IOException {
setReader();
//TODO 从这里开始读入题目输入
int n = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(nextInt(), i);
}
int[] res = new int[n];
for (int i = 0; i < n; i++) {
int g = (i + n - map.get(a[i])) % n;
res[g]++;
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans = Math.max(ans, res[i]);
}
System.out.println(ans);
}
// main以及输入输出========================================================================
public static void main(String[] args) throws IOException {
C solution = new C();
solution.run();
solution.reader.close();
}
/**
* 注意不同题目这里路径要设一下
*
* @throws IOException
*/
private void setReader() throws IOException {
if (submitFlag) {
reader = new BufferedReader(new InputStreamReader(System.in));
} else {
String moduleName = "codeforces-race";
String projectPath = System.getProperty("user.dir");
String modulePath = projectPath + "/" + moduleName + "/src/main/java/";
String className = this.getClass().getName().replace(".", "\\");
reader = new BufferedReader(new FileReader(modulePath + className + "input.txt"));
}
}
BufferedReader reader;
StringTokenizer tokenizer;
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 next() throws IOException {
return nextToken();
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 0253ef63df981c0ed2154eb6c4583f12 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.*;
import java.util.*;
public class RotationMatching {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int p[] = new int[n], q[] = new int[n];
int a=0,b=0;
int pos[] = new int[n+1];
int shifts[] = new int[n];
for(int i=0;i<n;i++) {
p[i] = in.nextInt();
pos[p[i]] = i;
}
for(int i=0;i<n;i++) {
q[i] = in.nextInt();
}
int cnt = 0;
for(int i=0;i<n;i++) {
shifts[(n+i-pos[q[i]])%n]++;
}
for(int i : shifts)
cnt = Math.max(i, cnt);
out.println(cnt);
out.close();
}
}
| Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 1c54b0941331e79b96ddba642db9019c | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.Queue;
import javafx.util.*;
public final class CodeForcesA
{
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
static ArrayList<ArrayList<Integer>> g;
static long mod=1000000007;
static boolean set[];
public static void main(String args[])throws IOException
{
int N=i();
HashMap<Integer,Integer> mp1=new HashMap<>();
HashMap<Integer,Integer> mp=new HashMap<>();
HashMap<Integer,Integer> mp2=new HashMap<>();
for(int i=1; i<=N; i++)
{
int a=i();
mp1.put(a,i);
//System.out.println("pUTTIng"+a+"As"+i);
}
for(int i=1; i<=N; i++)
{
int a=i();
mp2.put(a,i);
//System.out.println("pUTTIng"+a+"As"+i);
}
int max=0;
for(int i=1; i<=N; i++)
{
//System.out.println(mp1.get(i)+" "+mp2.get(i));
int a=mp1.get(i)-mp2.get(i);
//System.out.println(a);;
a=(a+N)%N;
if(mp.containsKey(a))
{
mp.put(a, mp.get(a)+1);
}
else mp.put(a, 1);
max=Math.max(mp.get(a),max);
}
System.out.println(max);
}
static void sort(long[] a) //check for long
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static String swap(String X,int i,int j)
{
char ch[]=X.toCharArray();
char a=ch[i];
ch[i]=ch[j];
ch[j]=a;
return new String(ch);
}
static void setGraph(int N)
{
set=new boolean[N+1];
g=new ArrayList<ArrayList<Integer>>();
for(int i=0; i<=N; i++)
g.add(new ArrayList<Integer>());
}
static void DFS(int N,int d)
{
set[N]=true;
d++;
for(int i=0; i<g.get(N).size(); i++)
{
int c=g.get(N).get(i);
if(set[c]==false)
{
DFS(c,d);
}
}
}
static long pow(long a,long b)
{
// long mod=1000000007;
long pow=1;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(long x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
static int kadane(int A[])
{
int lsum=A[0],gsum=A[0];
for(int i=1; i<A.length; i++)
{
lsum=Math.max(lsum+A[i],A[i]);
gsum=Math.max(gsum,lsum);
}
return gsum;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | 4cbc8c14b1f6a2f5383c05b7bc75189e | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.Queue;
import javafx.util.*;
public final class CodeForcesA
{
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
static ArrayList<ArrayList<Integer>> g;
static long mod=1000000007;
static boolean set[];
public static void main(String args[])throws IOException
{
int N=i();
int A[]=new int[N+1];
HashMap<Integer,Integer> mp=new HashMap<>();
for(int i=1; i<=N; i++)
{
int a=i();
A[a]=i;
}
for(int i=1; i<=N; i++)
{
int a=i();
int b=A[a];
b=(b-i+N)%N;
A[a]=b;
}
int max=0;
for(int i=1; i<=N; i++)
{
int a=A[i];
if(mp.containsKey(a))
{
mp.put(a, mp.get(a)+1);
}
else mp.put(a, 1);
max=Math.max(mp.get(a),max);
}
System.out.println(max);
}
static void sort(long[] a) //check for long
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static String swap(String X,int i,int j)
{
char ch[]=X.toCharArray();
char a=ch[i];
ch[i]=ch[j];
ch[j]=a;
return new String(ch);
}
static void setGraph(int N)
{
set=new boolean[N+1];
g=new ArrayList<ArrayList<Integer>>();
for(int i=0; i<=N; i++)
g.add(new ArrayList<Integer>());
}
static void DFS(int N,int d)
{
set[N]=true;
d++;
for(int i=0; i<g.get(N).size(); i++)
{
int c=g.get(N).get(i);
if(set[c]==false)
{
DFS(c,d);
}
}
}
static long pow(long a,long b)
{
// long mod=1000000007;
long pow=1;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(long x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
static int kadane(int A[])
{
int lsum=A[0],gsum=A[0];
for(int i=1; i<A.length; i++)
{
lsum=Math.max(lsum+A[i],A[i]);
gsum=Math.max(gsum,lsum);
}
return gsum;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output | |
PASSED | b97a37779631087581f7cf87c00a1b08 | train_001.jsonl | 1591540500 | After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | 256 megabytes | import java.math.*;
import java.io.*;
import java.awt.*;
import java.util.*;
public class Solution{
static Helper hp;
final static int MAXN = 1000_006;
final static long MOD = (long) 1000000007;
final static long MOD1 = (long) 998244353;
public static void debug(Object s)throws Exception{
if(hp.debugger)System.out.println(s.toString());
}
public static void debug(int a[])throws Exception{
if(hp.debugger)System.out.println(Arrays.toString(a));
}
public static void main(String []args)throws Exception
{
hp=new Helper(MOD,MAXN);
//hp.initIO("input.txt","output.txt");
hp.initIO(System.in,System.out);
solve();
}
static void solve()throws Exception{
boolean testCases = false;
int tc =testCases?hp.nextInt():1;
for (int tce = 1; tce <= tc; tce++) {
int n=hp.nextInt();
int a[]=new int[n+1];
int b[]=new int[n+1];
int index[]=new int[n+1];
int index_2[]=new int[n+1];
index[0]=index_2[0]=Integer.MIN_VALUE;
for(int i=0;i<n;i++)
{
a[i]=hp.nextInt();
index[a[i]]=i;
index_2[a[i]]=n+i;
}
for(int i=0;i<n;i++)
{
b[i]=hp.nextInt();
index[b[i]]-=i;
index[b[i]]=(index[b[i]]+n)%n;
index_2[b[i]]-=i;
index_2[b[i]]%=n;
}
int max=1,count=1;
Arrays.sort(index);
Arrays.sort(index_2);
//hp.println(Arrays.toString(index));
//hp.println(Arrays.toString(index_2));
for(int i=1;i<=n;i++)
{
if(i-1>=0 && index[i-1]==index[i])
count++;
else
{
max=Math.max(max,count);
count=1;
}
}
max=Math.max(max,count);
//hp.println(Arrays.toString(index));
count=1;
for(int i=1;i<=n;i++)
{
if(i-1>=0 && index_2[i-1]==index_2[i])
count++;
else
{
max=Math.max(max,count);
count=1;
}
}
max=Math.max(max,count);
hp.println(max);
}
hp.flush();
}
}
class Helper {
final long MOD;
final int MAXN;
final Random rnd;
boolean debugger;
public Helper(long mod, int maxn) {
MOD = mod;
MAXN = maxn;
rnd = new Random();
}
public static int[] sieve;
public static ArrayList<Integer> primes;
public void setSieve() {
primes = new ArrayList<>();
sieve = new int[MAXN];
int i, j;
for(i=0;i<MAXN;i++) sieve[i]=i;
for (i = 2; i*i< MAXN; ++i)
{
if (sieve[i] == i) {
for (j = i*i;j < MAXN; j += i) {
if(sieve[j]==j)
sieve[j] = i;
}
}
}
for(i=2;i<MAXN;i++)if(sieve[i]==i)primes.add(i);
}
public static long[] factorial;
public void setFactorial() {
factorial = new long[MAXN];
factorial[0] = 1;
for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD;
}
public long getFactorial(int n) {
if (factorial == null) setFactorial();
if(n<0)
return 1;
return factorial[n];
}
public long ncr(int n, int r) {
if (r > n) return 0;
if (factorial == null) setFactorial();
long numerator = factorial[n];
long denominator = factorial[r] * factorial[n - r] % MOD;
return numerator * pow(denominator, MOD - 2, MOD) % MOD;
}
public Long[] getLongArray(int size) throws Exception {
Long[] ar = new Long[size];
for (int i = 0; i < size; ++i) ar[i] = new Long(nextLong());
return ar;
}
public Integer[] getIntArray(int size) throws Exception {
Integer[] ar = new Integer[size];
for (int i = 0; i < size; ++i) ar[i] = new Integer(nextInt());
return ar;
}
public String[] getStringArray(int size) throws Exception {
String[] ar = new String[size];
for (int i = 0; i < size; ++i) ar[i] = next();
return ar;
}
public String joinElements(long... ar) {
StringBuilder sb = new StringBuilder();
for (long itr : ar) sb.append(itr).append(" ");
return sb.toString().trim();
}
public String joinElements(int... ar) {
StringBuilder sb = new StringBuilder();
for (int itr : ar) sb.append(itr).append(" ");
return sb.toString().trim();
}
public String joinElements(String... ar) {
StringBuilder sb = new StringBuilder();
for (String itr : ar) sb.append(itr).append(" ");
return sb.toString().trim();
}
public String joinElements(Object... ar) {
StringBuilder sb = new StringBuilder();
for (Object itr : ar) sb.append(itr).append(" ");
return sb.toString().trim();
}
public long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public int max(int ... ar)
{
int max=ar[0];
for(int itr:ar)
max=Math.max(max,itr);
return max;
}
public long max(long ... ar){
long max=ar[0];
for(long itr:ar)max=Math.max(max,itr);
return max;
}
public <T extends Comparable<T>> T max(T ... ar)
{
T max=ar[0];
for(T itr:ar) max=itr.compareTo(max)>0?itr:max;
return max;
}
public <T extends Comparable<T>> T min(T ... ar)
{
T min=ar[0];
for(T itr:ar) min=itr.compareTo(min)<0?itr:min;
return min;
}
public Long sum(Long... arr)
{
Long sum=new Long(0);
for(Long itr:arr)sum+=itr;
return sum;
}
public Long sum(Integer ... arr)
{
Long sum=new Long(0);
for(Integer itr:arr)sum+=itr;
return sum;
}
public long sum(long... ar) {
long sum = 0;
for (long itr : ar) sum += itr;
return sum;
}
public long sum(int... ar) {
long sum = 0;
for (int itr : ar) sum += itr;
return sum;
}
public long pow(long base, long exp, long MOD) {
base %= MOD;
long ret = 1;
while (exp > 0) {
if ((exp & 1) == 1) ret = ret * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
return ret;
}
static final int BUFSIZE = 1 << 20;
static byte[] buf;
static int index, total;
static InputStream in;
static BufferedWriter bw;
public void initIO(InputStream is, OutputStream os) {
try {
debugger=false;
in = is;
bw = new BufferedWriter(new OutputStreamWriter(os));
buf = new byte[BUFSIZE];
} catch (Exception e) {
}
}
public void initIO(String inputFile, String outputFile) {
try {
debugger=true;
in = new FileInputStream(inputFile);
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outputFile)));
buf = new byte[BUFSIZE];
} catch (Exception e) {
}
}
private int scan() throws Exception {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
}
public String next() throws Exception {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan())
sb.append((char) c);
return sb.toString();
}
public int nextInt() throws Exception {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+')
c = scan();
for (; c >= '0' && c <= '9'; c = scan())
val = (val << 3) + (val << 1) + (c & 15);
return neg ? -val : val;
}
public long nextLong() throws Exception {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+')
c = scan();
for (; c >= '0' && c <= '9'; c = scan())
val = (val << 3) + (val << 1) + (c & 15);
return neg ? -val : val;
}
public void print(Object a) throws Exception {
bw.write(a.toString());
}
public void printsp(Object a) throws Exception {
print(a);
print(" ");
}
public void println() throws Exception {
bw.write("\n");
}
public void println(Object a) throws Exception {
print(a);
println();
}
public void flush() throws Exception {
bw.flush();
}
} | Java | ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"] | 1 second | ["5", "1", "2"] | NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\{1, 2, 3, 4, 5\}$$$ and $$$\{1, 2, 3, 4, 5\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\{1, 3, 2, 4\}$$$ and $$$\{2, 3, 1, 4\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$. | Java 8 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"greedy"
] | eb1bb862dc2b0094383192f6998891c5 | The first line of the input contains a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \le b_i \le n)$$$ — the elements of the second permutation. | 1,400 | Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.