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
1d36a3058a8cbf293eb811c8721c1e7c
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package farrelativesparty; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; /** * * @author PrudhviNIT */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { // TODO code application logic here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()),i,j,daysM[],daysF[]; daysM = new int[370]; daysF = new int[370]; String str[]; for(i=0;i<n;i++){ str = br.readLine().trim().split(" "); if(str[0].charAt(0)=='M'){ daysM[Integer.parseInt(str[1])]+=1; daysM[Integer.parseInt(str[2])+1]+=-1; //System.out.println(Integer.parseInt(str[1])+" "+Integer.parseInt(str[2])); } else{ daysF[Integer.parseInt(str[1])]+=1; daysF[Integer.parseInt(str[2])+1]+=-1; //System.out.println(Integer.parseInt(str[1])+" "+Integer.parseInt(str[2])); } } for(j=1;j<=366;j++){ daysM[j] += daysM[j-1]; daysF[j] += daysF[j-1]; //System.out.println(); } //System.out.println(Arrays.toString(daysM)); //System.out.println(Arrays.toString(daysF)); int ans = -1; for(i=1;i<=366;i++){ ans = Math.max(ans,2*Math.min(daysM[i],daysF[i])); } System.out.println(ans); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
b34e461dd35f0cf632ad97c729ca95f2
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class FarRelativeProblem { public static void main(String[] args) { InputReader r = new InputReader(System.in); int n = r.nextInt(); int[][] available = new int[400][2]; for (int i = 0; i < n; i++) { char c = r.next().charAt(0); int index = c=='M'?0:1; int x = r.nextInt(); int y=r.nextInt(); for(int j=x;j<=y;j++){ available[j][index]++; } } int res = 0; for (int i = 0; i < available.length; i++) { res = Math.max(res, Math.min(available[i][0], available[i][1]) * 2); } System.out.println(res); } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public InputReader(FileReader stream) { reader = new BufferedReader(stream); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
715b5cb57abb889f1bf0a4e7a240f855
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Ivan */ public class secondProblem { static int count(ArrayList<Interval> friends,int day){ int res=0; for(Interval i : friends) res+=i.checkIfIniside(day)?1:0; return res; } public static void main(String[] args) throws IOException { FastReader sc=new FastReader(System.in); int n=sc.nextInt(); ArrayList<Interval> femaleFriends=new ArrayList<>(); ArrayList<Interval> maleFriends=new ArrayList<>(); String line; int from,to; for(int i=0;i<n;i++){ line=sc.next(); from=sc.nextInt(); to=sc.nextInt(); if(line.charAt(0)=='F') femaleFriends.add(new Interval(from-1,to-1)); else maleFriends.add(new Interval(from-1,to-1)); } int[] days=new int[366]; Arrays.fill(days,0); for(int i=0;i<366;i++) days[i]=2*Math.min(count(femaleFriends,i),count(maleFriends,i)); int res=0; for(int i=0;i<366;i++) res=Math.max(res,days[i]); System.out.println(res); } } class Interval{ public int from; public int to; public Interval(int from,int to){ this.from=from; this.to=to; } public boolean checkIfIniside(int t){ return t>=from&&t<=to; } } class FastReader { public BufferedReader reader; public StringTokenizer tokenizer; public FastReader(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() throws IOException{ return Integer.parseInt(next()); } public long nextL() throws IOException{ return Long.parseLong(next()); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
9563422a909c699afc9cfe56a7b923d6
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.*; import java.math.*; public class Main { public static void main(String[] args)throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int arrayMen[]=new int[366]; int arrayWomen[]=new int[366]; for(int i=0; i<n;++i) { String input[] = br.readLine().split(" "); int start = Integer.parseInt(input[1]); int end = Integer.parseInt(input[2]); //System.out.println(start+" "+ end); if(input[0].equals("M")) { //System.out.println("Men found"); for(int j=start; j<=end; ++j) ++arrayMen[j-1]; } else { //System.out.println("Women found"); for(int j=start; j<=end; ++j) ++arrayWomen[j-1]; } //System.out.print(arrayMen[i]); } int ind=0, max=0; for(int i=0; i<366; ++i) { arrayMen[i]=Math.min(arrayMen[i], arrayWomen[i]); if(arrayMen[i]>max) { max = arrayMen[i]; //ind=i; } //System.out.print(arrayMen[i]); } //System.out.println(); System.out.print(2*max); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
9d97a6108b6b2ad9b0c2851f0c1ca9a8
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.*; import java.io.*; public class BoysGirls { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); String s = in.nextLine(); int[] boy = new int[367]; int[] girl = new int[367]; for (int i = 0; i < n; i++) { s = in.nextLine(); StringTokenizer st = new StringTokenizer(s); String gender = st.nextToken(); int start = Integer.parseInt(st.nextToken()); int end = Integer.parseInt(st.nextToken()); for (int k = start; k <= end; k++) { if (gender.equals("M")) { boy[k]++; } else { girl[k]++; } } } int ans = 0; for (int i = 1; i < 367; i++) { ans = Math.max(ans, Math.min(boy[i], girl[i])); } System.out.println(ans*2); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
ac0202e5bacd8dfaa70fcbb2e83e2072
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.util.StringTokenizer; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); char[] gender = new char[n]; int[][]days = new int[n][2]; for (int i = 0; i < n; i++) { StringTokenizer st = new StringTokenizer(reader.readLine()); gender[i]=st.nextToken().charAt(0); days[i][0]=Integer.parseInt(st.nextToken()); days[i][1]=Integer.parseInt(st.nextToken()); } int max=0; for (int i = 1; i <= 366; i++) { int f =0; int m=0; for (int j = 0; j < days.length; j++) { int s = days[j][0]; int e = days[j][1]; if (i>= s && i<=e) { if (gender[j]=='M') { m++; } else{ f++; } } } if ((Math.min(f, m)*2)>max) { max = (Math.min(f, m)*2); } } System.out.println(max); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
d0920f2a8cc473015f596fbeb115b195
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package far.relative.problem; import java.util.Scanner; /** * * @author Himanshu Khare */ public class FarRelativeProblem { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input=new Scanner(System.in); int n,i,j,m,f,max; int[] a= new int[5001]; int[] b= new int[5001]; char[] c= new char[5001]; n=input.nextInt(); for(i=0;i<n;i++) { c[i]=input.next().charAt(0); a[i]=input.nextInt(); b[i]=input.nextInt(); } max=0; for(j=1;j<=366;j++) { m=f=0; for(i=0;i<n;i++) { if(c[i]=='M') { if(j>=a[i]&&j<=b[i]) { m++; } } else { if(j>=a[i]&&j<=b[i]) { f++; } } } if(m>f) { if(f>max) max=f; } else { if(m>max) max=m; } } System.out.print(2*max); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
63a1d04891cc58e524807cd3db06549c
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by MohannadHassanPersonal on 2/20/16. */ public class FarRelativeProblem { public static void main (String [] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); final int DAYS = 367; int [] males = new int[DAYS]; int [] females = new int[DAYS]; for (int i = 0; i < n; i++) { String [] input = br.readLine().split(" "); int start = Integer.parseInt(input[1]); int end = Integer.parseInt(input[2]); int [] array; if (input[0].charAt(0) == 'M') { array = males; } else { array = females; } for (int j = start; j <= end; j++) { array[j] ++; } } int max = 0; for (int i = 1; i < DAYS; i++) { int temp; if (males[i] > females[i] ) { temp = females[i]; } else { temp = males[i]; } if (temp > max) { max = temp; } } System.out.println(max * 2); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
90929292d40ba573338e3dabe7633701
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class B629 { BufferedReader br; StringTokenizer in; PrintWriter out; public String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public void solve() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(); int max = 0; int [][] friends = new int[n][3]; for(int i = 0 ; i < n ; i++) { String m = nextToken(); if(m.equals("M")) friends[i][0] = 1; else friends[i][0] = 0; friends[i][1] = nextInt(); friends[i][2] = nextInt(); } for(int i = 1; i <= 366 ; i++) { int males = 0; int females = 0; for(int j = 0 ; j < n ; j++) { if(friends[j][1] <= i && friends[j][2] >= i) { if(friends[j][0] == 1) males++; else females++; } } max = Math.max(max, Math.min(males,females)*2); } out.println(max); out.close(); } public int f(int m, int n) { if(m == 1 || n == 1) return 1; else return f(m-1,n)+f(m,n-1); } public int gcd(int m, int n){ if(m == 0) return n; return gcd(n % m, m); } public static void main (String [] args) throws IOException { new B629().solve(); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
b10cdad970c8dc83941cf142db6edd3d
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static Scanner in= new Scanner(System.in); public static void main (String[] args) throws java.lang.Exception { int n; n=in.nextInt(); int i; int malesEntry[]= new int[370]; int malesExit[]= new int[370]; int femalesEntry[]= new int[370]; int femalesExit[]= new int[370]; String shit=in.nextLine(); for(i=0;i<n;i++){ String nextLine= in.nextLine(); //System.out.print("-"+nextLine+"-"); char gender= nextLine.charAt(0); nextLine=nextLine.substring(2); String ints[]=nextLine.split(" "); int low= Integer.valueOf(ints[0]); int high=Integer.valueOf(ints[1]); if(gender=='M'){ malesEntry[low]++; malesExit[high+1]++; }else{ femalesEntry[low]++; femalesExit[high+1]++; } } int malesCount[]= new int[370]; int femalesCount[]= new int[370]; malesCount[0]=femalesCount[0]=0; int malesLeft[]= new int[370]; int femalesLeft[]= new int[370]; malesLeft[0]=femalesLeft[0]=0; for(int days=1;days<=366;days++){ malesCount[days]=malesCount[days-1]+malesEntry[days]; } //System.out.println("Hekllo"); for(int days=1;days<=366;days++){ malesLeft[days]=malesLeft[days-1]+malesExit[days]; malesCount[days]=malesCount[days]-malesLeft[days]; // System.out.print(malesCount[days]+","); } for(int days=1;days<=366;days++){ femalesCount[days]=femalesCount[days-1]+femalesEntry[days]; } for(int days=1;days<=366;days++){ femalesLeft[days]=femalesLeft[days-1]+femalesExit[days]; femalesCount[days]=femalesCount[days]-femalesLeft[days]; // System.out.print(femalesCount[days]+","); } int ans=0; for(int days=1;days<=366;days++){ ans= max(ans, 2*min(femalesCount[days], malesCount[days])); } System.out.println(ans); } static int min(int a, int b){ return a<b? a:b; } static int max(int a, int b){ return a>=b? a:b; } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
d73c8301b5e992aeb7f7d411391589c8
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.*; import java.util.*; public class Solution { private static void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); int[] m, f; m = new int[368]; f = new int[368]; String gender; int left, right; for (int i = 0; i < n; i++) { gender = in.next(); left = in.nextInt(); right = in.nextInt() + 1; if (gender.equals("M")) { m[left]++; m[right]--; } else { f[left]++; f[right]--; } } int curm, curf, cur, max; curm = curf = max = 0; for (int i = 1; i < m.length - 1; i++) { curm += m[i]; curf += f[i]; cur = Math.min(curm, curf); if (cur > max) max = cur; } out.print(max * 2); } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); solve(in, out); in.close(); out.close(); } private static class InputReader { private BufferedReader br; private StringTokenizer st; InputReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String nextLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } String next() { while (st == null || !st.hasMoreTokens()) { String line = nextLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } private static class OutputWriter { BufferedWriter bw; OutputWriter(OutputStream os) { bw = new BufferedWriter(new OutputStreamWriter(os)); } void print(int i) { print(Integer.toString(i)); } void println(int i) { println(Integer.toString(i)); } void print(long l) { print(Long.toString(l)); } void println(long l) { println(Long.toString(l)); } void print(double d) { print(Double.toString(d)); } void println(double d) { println(Double.toString(d)); } void print(boolean b) { print(Boolean.toString(b)); } void println(boolean b) { println(Boolean.toString(b)); } void print(char c) { try { bw.write(c); } catch (IOException e) { e.printStackTrace(); } } void println(char c) { println(Character.toString(c)); } void print(String s) { try { bw.write(s); } catch (IOException e) { e.printStackTrace(); } } void println(String s) { print(s); print('\n'); } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
4c97d58e9dd1873da58fb2c21d7be786
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String [] args)throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st; int maledays[] = new int[400]; int femdays[] = new int[400]; for(int i = 0;i < n;i++){ st = new StringTokenizer(br.readLine()); int a = st.nextToken().charAt(0) - 'M'; int b = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); if(a == 0){ maledays[b]++; maledays[c + 1]--; } else{ femdays[b]++; femdays[c + 1]--; } } for(int i = 1;i < maledays.length;i++){ maledays[i] += maledays[i - 1]; femdays[i] += femdays[i - 1]; } int ans = 0; for(int i = 1;i < maledays.length;i++) ans = Math.max(ans,Math.min(maledays[i],femdays[i])*2); System.out.print(ans); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
6fe4174a0345792e1639d341480deef5
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner lee=new Scanner(System.in); int n=lee.nextInt(); String []v=new String[n]; int []M=new int[n]; int []F=new int[n]; for (int i = 0; i <n; i++) { v[i]=lee.next(); M[i]=lee.nextInt(); F[i]=lee.nextInt(); } int resp=0; for (int i = 1; i <= 366; i++) { int male=0,female=0; for (int j = 0; j < n; j++) { if(v[j].equals("M")&&M[j]<=i&&i<=F[j]) male++; if(v[j].equals("F")&&M[j]<=i&&i<=F[j]) female++; } resp=Math.max(resp,(Math.min(male,female)*2)); } System.out.println(resp); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
93c5520837613b242b10d3ab7f30985e
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.*; import java.io.*; public class farfar{ public static void main(String args[]) throws IOException{ BufferedReader lector = new BufferedReader(new InputStreamReader(System.in)); int tmp1 = Integer.parseInt(lector.readLine()); int tal[][] = new int[tmp1][3]; for(int n =0;n<tmp1;n++){ String t[] = lector.readLine().split(" "); tal[n][0]=t[0].charAt(0)=='F'?1:0; tal[n][1]=Integer.parseInt(t[1]); tal[n][2]=Integer.parseInt(t[2]); } int max = 0; for(int n = 1;n<367;n++){ int tmp = 0,tmpf=0; for(int m = 0;m<tmp1;m++) if(tal[m][1]<=n && tal[m][2]>=n){ if(tal[m][0]==1)tmpf++; tmp++; } if(2*Math.min(tmpf,tmp-tmpf)>max)max=2*Math.min(tmpf,tmp-tmpf); } System.out.println(max); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
059cc8c6fa9e0b57c172d9ffa8cf9a23
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.*; public class B629 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] ms = new int[400], fs = new int[400]; for(int i = 0; i<n; i++) { char c= input.next().charAt(0); int[] a = c == 'M' ? ms : fs; int s = input.nextInt(), b = input.nextInt(); for(int j = s; j<=b; j++) a[j]++; } int res = 0; for(int i = 0; i<400; i++) res = Math.max(res, Math.min(ms[i], fs[i])*2); System.out.println(res); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
f788eb157bc181363e9af0d6cb815f64
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; import java.util.HashSet; import java.util.Iterator; //package codeforce; /* @author Kbk*/ public class Abc { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int N=sc.nextInt(); String gender[]=new String[N]; int startday[]=new int[N]; int endday[]=new int[N]; HashSet tm=new HashSet(); for(int i=0;i<N;i++){ gender[i]=sc.next(); startday[i]=sc.nextInt(); endday[i]=sc.nextInt(); } if(N==1) System.out.println(0); else{ for(int i=1;i<=366;i++){ int MC=0,FC=0; for(int j=0;j<N;j++){ for(int k=startday[j];k<=endday[j];k++){ if(i==k){ if(gender[j].equals("M")) MC++; else if(gender[j].equals("F")) FC++; break; } } } if(MC>FC && MC!=0 && FC!=0){ int temp=MC-FC; MC=MC-temp; tm.add(FC+MC); } else if(FC>MC && MC!=0 && FC!=0){ int temp=FC-MC; FC=FC-temp; tm.add(FC+MC); } else tm.add(FC+MC); } Iterator itr=tm.iterator(); int max=0; while(itr.hasNext()){ int I=(int)itr.next(); if(I>max) max=I; } if(max>1) System.out.println(max); else if(max<=1) System.out.println(0); } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
b27c2f9f2c0e5c8a6a07ed2b247155ae
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); Friend[] frd = new Friend[n]; stdin.nextLine(); for (int i = 0; i < n; i++) { String s = stdin.nextLine(); String[] ss = s.split(" "); char sex = ss[0].charAt(0); int a = Integer.parseInt(ss[1]); int b = Integer.parseInt(ss[2]); // System.out.println(sex +" " + a +" " + b); frd[i] = new Friend(sex, a, b); } int ans = 0; for (int i = 1; i <= 366; i++) { int m = 0; int f = 0; for (int j = 0; j < n; j++) { if (frd[j].sex == 'F' && i <= frd[j].b && i >= frd[j].a) f++; else if (frd[j].sex == 'M' && i <= frd[j].b && i >= frd[j].a) m++; } ans = Math.max(ans, Math.min(m, f) * 2); } System.out.println(ans); } } class Friend { char sex; int a; int b; Friend(char sex, int a, int b) { this.sex = sex; this.a = a; this.b = b; } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
ceee154225e9879e4fba28da3df0b6a3
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class P629B { InputStream is; PrintWriter out; String INPUT = "6\n" + "M 128 130\n" + "F 128 131\n" + "F 131 140\n" + "F 131 141\n" + "M 131 200\n" + "M 140 200"; void solve() { int n = ni(); int[][] map = new int[2][367]; for (int i = 0; i < n; i++) { int male = ns().equals("M") ? 0 : 1; int start = ni(); int end = ni(); for (int j = start; j <= end; j++) { map[male][j]++; } } int res = 0; for (int i = 0; i < map[0].length; i++) { int max = Math.min(map[0][i], map[1][i]); res = Math.max(max * 2, res); } System.out.println(res); } public static void main(String[] args) throws Exception { new P629B().run(); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis() - s + "ms"); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
ac075590e21383fa40d44ee629a83ca1
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; /** * Feb 20, 2016 | 6:08:03 PM * <pre> * <u>Description</u> * * </pre> * * @author Essiennta Emmanuel (colourfulemmanuel@gmail.com) */ public class ProblemB{ int solve(char[] sex, int[] be, int[] en){ int[] nf = new int[400]; int[] nm = new int[400]; for (int i = 0; i < sex.length; i++) for (int j = be[i]; j <= en[i]; j++) if (sex[i] == 'M') nm[j]++; else nf[j]++; int max = Integer.MIN_VALUE; for (int i = 1; i <= 366; i++) max = Math.max(max, Math.min(nm[i], nf[i])); return max * 2; } public static void main(String[] args){ try (Scanner sc = new Scanner(System.in)) { int n = sc.nextInt(); char[] sex = new char[n]; int[] be = new int[n]; int[] en = new int[n]; for (int i = 0; i < n; i++) { sex[i] = sc.next().charAt(0); be[i] = sc.nextInt(); en[i] = sc.nextInt(); } System.out.println(new ProblemB().solve(sex, be, en)); } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
334318c1bad2d485adeafa3ac968945e
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { public static void main(String[] args) throws IOException { Reader.init(System.in); int m[] = new int[370]; int f[] = new int[370]; int n = Reader.nextInt(); char g; int from, to; for (int i = 0; i < n; i++) { g = Reader.next().charAt(0); from = Reader.nextInt(); to = Reader.nextInt(); if (g == 'M') for (int k = from; k <= to; k++) m[k]++; else for (int k = from; k <= to; k++) f[k]++; } int max = 0; for (int i = 0; i < 370; i++) { if (Math.min(m[i], f[i]) > max) max = Math.min(m[i], f[i]); } System.out.println(max*2); } } class Reader { 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 String nextLine() throws IOException { return reader.readLine(); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
9a124cb4bc66e2c617b8cf80078e7166
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { static Scanner in = new Scanner(System.in); static int f[]; static int m[]; static int n; public static void main(String[] args) { f = new int[380]; m = new int[380]; n =in.nextInt(); for(int i=0;i<n;i++) { int a,b; String string = in.next(); a = in.nextInt(); b = in.nextInt(); if(string.charAt(0)=='M') { for(int j=a;j<=b;j++) m[j]++; } else{ for(int j=a;j<=b;j++) f[j]++; } //System.out.println(string+" "+a+" "+b); } int num = 0; for(int i=1;i<=366;i++) { num = Math.max(num, Math.min(f[i], m[i])); } System.out.println(num*2); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
1cf0a1cb5726b9bba54e010e6f2fc49c
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
//package com.company.codeforces.div2.train; import java.io.IOException; import java.util.Scanner; /** * Created by Daniil on 5/19/2016. */ public class Task2 { public static void main(String[] args) throws IOException { //Scanner scanner = new Scanner(new FileInputStream("file.in")); Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[][] mas = new int[n][3]; for (int i= 0 ;i < n; ++ i){ mas[i][0] = (scanner.next().equals("F"))? 0 : 1; mas[i][1] = scanner.nextInt(); mas[i][2] = scanner.nextInt(); } int best = 0; for (int d = 1; d <= 366; ++ d){ int f = 0; int m = 0; for (int i = 0; i < n; ++ i){ if (mas[i][1] <= d && mas[i][2] >= d){ if (mas[i][0] == 0){ f++; }else { m++; } } } best = Math.max(best, Math.min(m,f) * 2); } System.out.println(best); scanner.close(); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
b245cc0a3bcb79fb5045cc0c6f8daab7
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.util.Set; import java.util.Map.Entry; import java.util.HashMap; import java.util.Iterator; import java.math.BigDecimal; public class test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] M = new int[367]; int[] F = new int[367]; for(int i = 0 ; i < n ; i++){ String temp = input.next(); int start = input.nextInt(); int end = input.nextInt(); if(temp.equals("M")){ //System.out.println("**m**"); for(int j = start ; j <= end ; j++){ M[j]++; } } else if(temp.equals("F")) { //System.out.println("**m**"); for (int j = start; j <= end; j++) { F[j]++; } } } int[] total = new int[367]; int max = Integer.MIN_VALUE; /*for(int i = 1 ; i <= 366 ;i++){ System.out.println("M "+ M[i] +" F "+F[i]); }*/ for(int i = 1 ; i <= 366 ; i++){ int min = M[i] > F[i] ? F[i] : M[i]; total[i] = min; max = max > total[i] ? max : total[i]; } System.out.println(max*2); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
cecfbce2587460bd978014fd3bdaf0bc
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; public class FarRelativeProblem { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] ar = new int[367][2]; int[] gender = new int[n]; int[][] time = new int[n][2]; for (int i = 0; i < n; i++) { gender[i] = in.next().charAt(0); time[i][0] = in.nextInt(); time[i][1] = in.nextInt(); } for (int i = 1; i <= 366; i++) { for (int j = 0; j < n; j++) { if (time[j][0] <= i && i <= time[j][1]) { if (gender[j] == 'M') { ar[i][0]++; } else { ar[i][1]++; } } } } for (int i = 1; i <= 366; i++) { if (ar[i][0] < ar[i][1]) { ar[i][1] = ar[i][0]; } else { ar[i][0] = ar[i][1]; } } int max = ar[1][0] + ar[1][1]; for (int i = 2; i <= 366; i++) { if (ar[i][0] + ar[i][1] > max) { max = ar[i][0] + ar[i][1]; } } System.out.println(max); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
146e69940162c066997b775d88cf602f
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.*; public class Main { static class Friend{ public char sex; public int start; public int end; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); Friend[] friends = new Friend[n]; scanner.nextLine(); for(int i =0;i<n ; i++) { String s = scanner.nextLine(); String[] temp = s.split(" "); Friend friend = new Friend(); friend.sex = temp[0].charAt(0); friend.start = Integer.valueOf(temp[1]); friend.end = Integer.valueOf(temp[2]); friends[i] = friend; } int[][] yearCounter = new int[367][2]; for(int i =0;i<friends.length ; i++) { if(friends[i].sex == 'M' ) { for(int j =friends[i].start ; j<=friends[i].end;j++) { //System.out.println("here"); yearCounter[j][0] += 1; } } else { for(int j =friends[i].start ; j<=friends[i].end ; j++) { //System.out.println("here"); yearCounter[j][1] += 1; } } } int maxVal = 0; for(int i =1;i<=366;i++) { if(maxVal < Math.min(yearCounter[i][0],yearCounter[i][1])) { maxVal = Math.min(yearCounter[i][0],yearCounter[i][1]); } } System.out.println(maxVal*2); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
137ac8c40dee00aa2dae6a9de6e72cb6
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
//package Codeforces.Div2B_343.Code1; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /* * some cheeky quote */ public class Main { FastScanner in; PrintWriter out; public void solve() throws IOException { int male[] = new int[367]; int female[] = new int[367]; int n = in.nextInt(); for (int i = 0; i < n; i++) { String type = in.next(); int from = in.nextInt(); int to = in.nextInt(); if (type.equals("M")) { fill(from, to, male); } else { fill(from, to, female); } } int max = 0; for (int i = 0; i < male.length; i++) { max = Math.max(max, 2 * Math.min(male[i], female[i])); } System.out.println(max); } public void fill(int from, int to, int a[]) { for (int i = from; i <= to; i++) { a[i]++; } } public void run() { try { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } public static void main(String[] arg) { new Main().run(); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
b228c61da6ae84bfcea4784fb1ae0c59
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solve(in, out); out.close(); } private static void solve(InputReader in, PrintWriter out) { int N = in.nextInt(); int MAXX = 380; int[][] tab = new int[MAXX][2]; for(int i = 1; i < MAXX; i++) { tab[i] = new int[2]; } for(int i = 0; i < N; i++) { String S = in.next(); int gender = S.equals("M") ? 0 : 1; int FROM = in.nextInt(); int TO = in.nextInt(); for(int j = FROM; j <= TO; j++) { tab[j][gender]++; } } int best = 0; for(int i = 1; i < MAXX; i++) { int countPairs = 2 * Math.min(tab[i][0], tab[i][1]); best = Math.max(best, countPairs); } out.println(best); } /* * */ // -------------------------------------------------------- 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 double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
30802d0316782b463bcce9021b7b7b76
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; import java.util.InputMismatchException; public class Main { public static void main(String [] args) { Scanner in = new Scanner(System.in); Task task = new Task(); task.solve(in); } } class Task { final int MAXN = 5005; final int MAXRANGE = 370; // day, M[0], F[1] int [][] days = new int[MAXRANGE][2]; public void printf(String s) { System.out.printf("%s", s); } public void printd(int d) { System.out.printf("%d", d); } public void solve(Scanner in) { int N = in.nextInt(); in.nextLine(); for (int i = 0; i < N; i++) { String friend = in.next(); int a = in.nextInt(); int b = in.nextInt(); for (int d = a; d <= b; d++) { if (friend.equals("M")) { days[d][0] += 1; } else if (friend.equals("F")) { days[d][1] += 1; } else { throw new InputMismatchException("Unrecognized gender"); } } } int ans = 0; for (int d = 1; d < MAXRANGE; d++) { int canGoM = days[d][0]; int canGoF = days[d][1]; int canGo = 2 * Math.min(canGoM, canGoF); if (canGo > ans) ans = canGo; } printd(ans); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
85837b48e4fff10bbfce5a150895dbf7
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; /** * @author sadasidha * @since 2/10/16 */ public class Main { public static Scanner sc = new Scanner(System.in); static int[] m = new int[370]; static int[] f = new int[370]; public static void main(String[] args) { int n = sc.nextInt(); while (n-- > 0) { char gender = sc.next().charAt(0); int s = sc.nextInt(); int e = sc.nextInt(); if (gender == 'M') { for (int i = s; i <= e; i++) { m[i]++; } } if (gender == 'F') { for (int i = s; i <= e; i++) { f[i]++; } } } int count = 0; for (int i = 1; i <= 366; i++) { if (count < Math.min(m[i], f[i])) { count = Math.min(m[i], f[i]); } } System.out.println(count*2); } private static void func(int[] num) { int l = sc.nextInt(); int r = sc.nextInt(); int x = sc.nextInt(); for (int p = l; p <= r; p++) { if (num[p-1] != x) { System.out.println(p); return; } } System.out.println(-1); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
5d516506d67759bffe7aa5fdf2d38713
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class B { public static void main(String[] args) { Scanner in = new Scanner(System.in); Plan[] ar = new Plan[in.nextInt()*2]; for(int i =0; i<ar.length/2; i++){ char c = in.next().charAt(0); int start = in.nextInt(); int end = in.nextInt(); ar[i*2] = new Plan(true,start,c); ar[i*2+1] = new Plan(false, end, c); } Arrays.sort(ar); int m = 0; int f = 0; int best = 0; for(Plan p : ar){ if(p.male){ if(p.start){ m++; } else{ m--; } } else{ if(p.start){ f++; } else{ f--; } } best = Math.max(best, Math.min(m,f)); } System.out.println((best*2)); } public static class Plan implements Comparable<Plan>{ public boolean start; public int time; public boolean male; public Plan(boolean s, int t, char c){ start = s; time = t; male = c=='M'?true:false; } public int compareTo(Plan o){ if(time!=o.time){ return time-o.time; } if(o.start!=start){ return start?-1:1; } return 0; } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
b3c3a34b4eedb588afc1d75b1efca266
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; import java.util.StringTokenizer; public class problema2 { static class Triplet { char gender; int a; int b; Triplet(char gender, int a, int b) { this.gender = gender; this.a = a; this.b = b; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Triplet tt[] = new Triplet[n]; for(int i = 0; i < n; i++) { char gender = sc.next().charAt(0); int a = Integer.parseInt(sc.next()); int b = Integer.parseInt(sc.next()); tt[i] = new Triplet(gender, a, b); } int ans = 0; for(int i = 1; i <= 366; i++) { int M = 0, F = 0; for(int j = 0; j < n; j++) { if(tt[j].gender == 'M' && (tt[j].a <= i && i <= tt[j].b)) M++; if(tt[j].gender == 'F' && (tt[j].a <= i && i <= tt[j].b)) F++; } ans = Math.max(ans,2 * Math.min(F, M)); } System.out.println(ans); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
94eb5a7432ffbfbaa8b8419692159c09
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Codeforces { public static void main(String[] args) { int[] shabab = new int[500]; int[] halageem = new int[500]; Scanner in = new Scanner(System.in); int n = in.nextInt(); for (int i = 0; i < n; i++) { char g = in.next().charAt(0); if (g == 'M') { int s = in.nextInt(); int f = in.nextInt(); for (int j = s; j <= f; j++) { shabab[j]++; } } else { int s = in.nextInt(); int f = in.nextInt(); for (int j = s; j <= f; j++) { halageem[j]++; } } } ArrayList<Integer> ls = new ArrayList<>(); for (int i = 0; i < halageem.length; i++) { ls.add(Math.min(halageem[i], shabab[i])*2); } System.out.println(Collections.max(ls)); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
649913223ff81bd6b5f70fcf02918ab6
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader;import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class Friends { static StringTokenizer st; static BufferedReader sc; static PrintWriter pw; public static void main(String args[]) throws Exception { sc = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter( System.out))); int n = nextInt(); String [] a = new String[n]; long sum=0; boolean[] male = new boolean[n]; boolean[] female = new boolean[n]; int mc=0, fc=0, max=0; for (int i=0;i<n;i++) { a[i] = sc.readLine(); } //int [] days = new int[367]; for (int i=1;i<=366;i++) { mc=0; fc=0; for (int j=0;j<n;j++) { String [] t = a[j].split("\\s++"); int r1= Integer.parseInt(t[1]); int r2= Integer.parseInt(t[2]); if (i>=r1 && i<=r2) { if (t[0].equals("M")) mc++; if (t[0].equals("F")) fc++; } } if (max<2*Math.min(mc, fc)) max = 2*Math.min(mc, fc); } pw.println(max); pw.close(); } public static boolean istPalindrom(char[] word){ int i1 = 0; int i2 = word.length - 1; while (i2 > i1) { if (word[i1] != word[i2]) { return false; } ++i1; --i2; } return true; } 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(sc.readLine()); return st.nextToken(); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
090d501df0fc0b040f0d75cc88944ab4
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
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.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * @author pttrung */ public class B_Round_343_Div2 { public static long MOD = 1000000007; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); int[][] data = new int[n][3]; for (int i = 0; i < n; i++) { String v = in.next(); data[i][1] = in.nextInt(); data[i][2] = in.nextInt(); if ("M".equals(v)) { data[i][0] = 0; } else { data[i][0] = 1; } } int result = 0; for (int i = 1; i <= 366; i++) { int a = 0; int b = 0; for (int j = 0; j < n; j++) { if (data[j][1] <= i && data[j][2] >= i) { if (data[j][0] == 0) { a++; } else { b++; } } } result = Math.max(result, Math.min(a, b) * 2); } out.println(result); out.close(); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } 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 int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return x - o.x; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b, long MOD) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2, MOD); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } 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 InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
36f8ac00dd3bd1dec0d0c255307bc78b
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.*; import java.io.*; public class FarProblem { /************************ SOLUTION STARTS HERE ************************/ static class Pair { char gender; int l,r; Pair(char gender,int l,int r) { this.gender = gender; this.l = l; this.r = r; } @Override public String toString() { return gender + " " + l+","+r; } } private static void solve(FastScanner s1, FastWriter out){ int N = s1.nextInt(); Pair arr[] = new Pair[N]; for(int i=0;i<N;i++) { char g = s1.next().charAt(0); arr[i] = new Pair(g, s1.nextInt(), s1.nextInt()); } int M[] = new int[367]; int F[] = new int[367]; for(int day = 1;day<=366;day++) { for(int i = 0;i<N;i++) { if(day >= arr[i].l && day <= arr[i].r) { if(arr[i].gender == 'M') M[day]++; else F[day]++; } } } int max = Integer.MIN_VALUE; for(int i=1;i<=366;i++) max = Math.max(max, 2 * Math.min(M[i], F[i])); out.print(max); } /************************ SOLUTION ENDS HERE ************************/ /************************ TEMPLATE STARTS HERE ************************/ public static void main(String []args) throws IOException { FastScanner in = new FastScanner(System.in); FastWriter out = new FastWriter(System.out); solve(in, out); in.close(); out.close(); } static class FastScanner{ public BufferedReader reader; public StringTokenizer st; public FastScanner(InputStream stream){ reader = new BufferedReader(new InputStreamReader(stream)); st = null; } public String next(){ while(st == null || !st.hasMoreTokens()){ try{ String line = reader.readLine(); if(line == null) return null; st = new StringTokenizer(line); }catch (Exception e){ throw (new RuntimeException()); } } return st.nextToken(); } public String nextLine(){ String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public void close(){ try{ reader.close(); } catch(IOException e){e.printStackTrace();} } } static class FastWriter{ BufferedWriter writer; public FastWriter(OutputStream stream){ writer = new BufferedWriter(new OutputStreamWriter(stream)); } public void print(int i) { print(Integer.toString(i)); } public void println(int i) { print(Integer.toString(i)); print('\n'); } public void print(long i) { print(Long.toString(i)); } public void println(long i) { print(Long.toString(i)); print('\n'); } public void print(double i) { print(Double.toString(i)); } public void print(boolean i) { print(Boolean.toString(i)); } public void print(Object o){ print(o.toString()); } public void println(Object o){ print(o.toString()); print('\n'); } public void print(char i) { try{writer.write(Character.toString(i));} catch(IOException e){e.printStackTrace();} } public void print(String s){ try{writer.write(s);} catch(IOException e){e.printStackTrace();} } public void println(String s){ try{writer.write(s);writer.write('\n');} catch(IOException e){e.printStackTrace();} } public void println(){ try{writer.write('\n');} catch(IOException e){e.printStackTrace();} } public void print(int arr[]) { for (int i = 0; i < arr.length; i++) { print(arr[i]); print(' '); } } public void close(){ try{writer.close();} catch(IOException e){e.printStackTrace();} } } /************************ TEMPLATE ENDS HERE ************************/ }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
3535b38fc699c7c1fed45d35c4458601
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.text.*; public class Problem { public static int solve(ArrayList<Integer[]> m, ArrayList<Integer[]> f) { int best = 0; for (int day = 1; day <= 366; day++) { int nm = 0; int nf = 0; for (int mi = 0; mi < m.size(); mi++) if (m.get(mi)[0] <= day && m.get(mi)[1] >= day) nm++; for (int fi = 0; fi < f.size(); fi++) if (f.get(fi)[0] <= day && f.get(fi)[1] >= day) nf++; int pairs = Math.min(nm, nf); best = Math.max(best, 2*pairs); } return best; } public static void main(String[] args) { try { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); int n = Integer.parseInt(br.readLine()); ArrayList<Integer[]> m = new ArrayList<>(); ArrayList<Integer[]> f = new ArrayList<>(); for (int i = 0; i < n; i++) { String[] st = br.readLine().split(" "); Integer[] range = new Integer[2]; range[0] = Integer.parseInt(st[1]); range[1] = Integer.parseInt(st[2]); if (st[0].equals("M")) { m.add(range); } else if (st[0].equals("F")) { f.add(range); } } System.out.println(solve(m, f)); } catch (Exception e) {e.printStackTrace();} } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
c0aee694f4fe93a6dd249583c40fb98f
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; public class Attempt { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); char[] D = new char[n]; int[] l = new int[n]; int[] r = new int[n]; for (int i=0; i<n; i++) { D[i] = sc.next().charAt(0); l[i] = sc.nextInt(); r[i] = sc.nextInt(); } int ans=0; for (int i=1; i<=366; i++) { int x=0, y=0; for (int j=0; j<n; j++) { if (D[j]=='F' && l[j]<=i && r[j]>=i) { x++; } if (D[j]=='M' && l[j]<=i && r[j]>=i) { y++; } } ans=Math.max(ans, Math.min(x, y)*2); } System.out.println(ans); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
4e4a51be466f4c29d3e2511c2b261c7e
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.IOException; import java.util.*; public class Problem { public static void main(String[] args) { FasterScanner sc = new FasterScanner(); int n = sc.nextInt(); int c[][] = new int[2][370]; for(int i = 1; i <= n; i++) { char s[] = new char[3]; s[0] = sc.nextString().charAt(0); int a = sc.nextInt(); int b = sc.nextInt(); s[1] = (char)a; s[2] = (char)b; if(s[0] == 'M') { for(int j = a; j <= b; j++) c[0][j]++; } else { for(int j = a; j<= b; j++) c[1][j]++; } } int ans = 0; for(int i = 0; i <= 366; i++) ans = Math.max(ans, 2 * Math.min(c[0][i], c[1][i])); System.out.println(ans); } public static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public String[] nextStringArray(int n) { String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = nextString(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
7ae516802d2590c98b6044a7ffd8c052
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.*; public class Lab12 { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner sin = new Scanner(System.in); int n = sin.nextInt(); int arr[] = new int[367]; int brr[] = new int[367]; for (int i = 0; i < n; i++) { char ch = sin.next().charAt(0); int a = sin.nextInt(), b = sin.nextInt(); int x[]; if(ch == 'M') x = arr; else x = brr; for (int j = a; j <= b; j++) { x[j]++; } } int max = 0; for (int i = 0; i < arr.length; i++) { max = Math.max(max, Math.min(arr[i], brr[i]) * 2); } System.out.println(max); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
516d87c536ead53c52c53a199af410ce
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.IOException; import java.io.InputStream; public class B629 { public static void main(String[] args) throws IOException { InputReader reader = new InputReader(System.in); int N = reader.readInt(); int[] M = new int[367]; int[] W = new int[367]; for (int n=0; n<N; n++) { int type = reader.readInt(); int A = reader.readInt(); int B = reader.readInt(); if (type == 'M'-'0') { for (int i=A; i<=B; i++) M[i]++; } else { for (int i=A; i<=B; i++) W[i]++; } } int max = 0; for (int i=1; i<=366; i++) { max = Math.max(max, Math.min(M[i], W[i])); } System.out.println(2*max); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final int readInt() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return negative ? -res : res; } public final long readLong() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return negative ? -res : res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
fc056c9c71e9fcfbe0a8892c856d682f
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; public class CodeforcesD { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); char c; int a, b; int[] m = new int[367]; int[] f = new int[367]; for(int i=0; i<n; i++){ if(in.next().equals("M")){ m[in.nextInt()-1]++; m[in.nextInt()]--; } else{ f[in.nextInt()-1]++; f[in.nextInt()]--; } } int max=Math.min(m[0], f[0]); for(int i=1; i<366; i++){ m[i]+=m[i-1]; f[i]+=f[i-1]; if(Math.min(m[i], f[i])>max){ max=Math.min(m[i], f[i]); } } System.out.println(2*max); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
9c364e424538f2c75579ec9b90782160
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class B343 { public void solve() throws IOException { int n = nextInt(); int[] male = new int[366]; int[] female = new int[366]; for (int i = 0; i < n; i++) { boolean isMale = nextToken().equals("M"); int start = nextInt(); int end = nextInt(); for (int j = start; j <= end; j++) { if (isMale) { male[j - 1]++; } else { female[j - 1]++; } } } int max = 0; for (int i = 0; i < 366; i++) { max = Math.max(max, Math.min(female[i], male[i])); } out.println(max * 2); } public BufferedReader br; public StringTokenizer st; public PrintWriter out; public String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public void run() throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; oj = true; br = new BufferedReader( new InputStreamReader(oj ? System.in : new FileInputStream("input.txt"))); out = new PrintWriter(oj ? System.out : new FileOutputStream("output.txt")); solve(); out.close(); } public static void main(String[] args) throws IOException { new B343().run(); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
532391144c82be9cb6388ba76124f68e
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] males = new int[367]; int[] females = new int[367]; for (int i = 0; i < n; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); boolean gender = "M".equals(st.nextToken()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); for (int j = a; j <= b; j++) { if (gender) { males[j]++; } else { females[j]++; } } } int ret = Integer.MIN_VALUE; for (int i = 1; i <= 366; i++) { ret = Math.max(ret, Math.min(males[i], females[i])); } System.out.println(2 * ret); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
c2a6771800c8b8c541d455d33f490937
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; public class Test{ class Pair{ int malec; int femalec; Pair(){ malec=0; femalec=0; } } public static Scanner sb; public static void main(String[] args){ sb=new Scanner(System.in); int n=sb.nextInt();int i,j,k,day1,day2,max=0;char a; String input[][]=new String[n][3]; for(i=0;i<n;i++){ for(j=0;j<3;j++){ input[i][j]=sb.next(); } } Test t=new Test(); Pair count[]=new Pair[367]; count[0]=t.new Pair(); for(i=1;i<367;i++){ count[i]=t.new Pair(); for(j=0;j<n;j++){ day1=Integer.parseInt(input[j][1]); day2=Integer.parseInt(input[j][2]); a=input[j][0].charAt(0); if(a=='M'){ if(i>=day1&&i<=day2) count[i].malec++; } else{ if(i>=day1&&i<=day2) count[i].femalec++; } } } for(i=1;i<367;i++){ k=min(count[i].malec,count[i].femalec)*2; if(k>max) max=k; } System.out.println(max); } public static int min(int a,int b){ if(a<=b) return a; return b; } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
23e1e9bf7b7592ba050e9ebbaa95c47e
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.*; import java.util.*; public final class fd2 { static FastScanner sc=new FastScanner(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws Exception { int n=sc.nextInt(); Node[] a=new Node[n]; long[] m1=new long[367],m2=new long[367]; for(int i=0;i<n;i++) { a[i]=new Node(sc.next().charAt(0),sc.nextInt(),sc.nextInt()); if(a[i].a=='M') { for(int j=a[i].l;j<=a[i].r;j++) { m1[j]++; } } else { for(int j=a[i].l;j<=a[i].r;j++) { m2[j]++; } } } long ans=0; for(int i=1;i<=366;i++) { if(m1[i]>0 && m2[i]>0) { long min=Math.min(m1[i],m2[i]); ans=Math.max(ans,min<<1); } } out.println(ans); out.close(); } } class Node implements Comparable<Node> { char a; int l,r; public Node(char a,int l,int r) { this.a=a; this.l=l; this.r=r; } public int compareTo(Node x) { return Integer.compare(this.l,x.l); } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public String next() throws Exception { return nextToken().toString(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
c0994f348e884b331dcfef1b793eda2e
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B { static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer tok = new StringTokenizer(""); static int[][] cnt; public static void main(String[] args) throws IOException { int N = readInt(); cnt = new int[367][2]; for (int i=0; i<N; i++) { String str = readString(); int start = readInt(); int end = readInt(); int g = str.equals("M")? 0:1; for (int j=start; j<=end; j++) { cnt[j][g]++; } } int ans = 0; for (int i=1; i<=366; i++) { ans = Math.max(ans, Math.min(cnt[i][0], cnt[i][1])); } System.out.println(ans*2); } static String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine(), " ."); } return tok.nextToken(); } static int readInt() throws IOException { return Integer.parseInt(readString()); } static long readLong() throws IOException { return Long.parseLong(readString()); } static double readDouble() throws IOException { return Double.parseDouble(readString()); } static int[] readArr(int n) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = readInt(); } return res; } static long[] readArrL(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = readLong(); } return res; } static void readArr2(int[] A, int[] B) throws IOException { int n = A.length; for (int i = 0; i < n; i++) { A[i] = readInt(); B[i] = readInt(); } } static void readArrL2(long[] A, long[] B) throws IOException { int n = A.length; for (int i = 0; i < n; i++) { A[i] = readLong(); B[i] = readLong(); } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
d75eafd450fcf5354f4bd740c71a1db0
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.io.*; import java.util.*; public class C343B { private static StringTokenizer st; public static void nextLine(BufferedReader br) throws IOException { st = new StringTokenizer(br.readLine()); } public static int nextInt() { return Integer.parseInt(st.nextToken()); } public static String next() { return st.nextToken(); } public static long nextLong() { return Long.parseLong(st.nextToken()); } public static double nextDouble() { return Double.parseDouble(st.nextToken()); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); nextLine(br); int n = nextInt(); int[] f = new int[367]; int[] m = new int[367]; for (int i = 0; i < n; i++) { nextLine(br); String s = next(); int a = nextInt(); int b = nextInt(); if (s.equals("M")) { for (int j = a; j <= b; j++) { m[j]++; } } else { for (int j = a; j <= b; j++) { f[j]++; } } } int best = 0; for (int i = 0; i <= 366; i++) { best = Math.max(best, Math.min(f[i], m[i]) * 2); } System.out.println(best); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
536d495c6cd99c03cbb0b654b576080f
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class ProblemB { public static void main(String[] args) { InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); new ProblemB().solve(in, out); out.close(); } public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int d = 366; int[] mCount = new int[d]; int[] fCount = new int[d]; for (int i = 0; i < n; i++) { String s = in.next(); int a = in.nextInt() - 1; int b = in.nextInt() - 1; if ("M".equals(s)) { for (int j = a; j <= b; j++) { mCount[j]++; } } else { for (int j = a; j <= b; j++) { fCount[j]++; } } } int res = 0; for (int i = 0; i < d; i++) { res = Math.max(res, Math.min(mCount[i], fCount[i])); } out.println(res * 2); } static class InputReader { public BufferedReader br; public StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
22b1fb6a684dd0b236283ce47c639c70
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class test_b { public static void main(String[] args) { MyScanner sc = new MyScanner(); int n = sc.nextInt(); int[]a[] = new int[n][]; for(int i=0;i<n; i++){ a[i] = new int[]{sc.next().trim().equals("M")?1:0, sc.nextInt(), sc.nextInt()}; } int o = 0; for(int d=1;d<=366; d++){ int m=0, f=0; for(int i=0;i<n; i++){ if (a[i][1]<=d&&d<=a[i][2]){ if(a[i][0]==1) m++; else f++; } } o = Math.max(2*Math.min(m, f), o); } System.out.println(o); } // -----------PrintWriter for faster output--------------------------------- public static PrintWriter out; // -----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // -------------------------------------------------------- }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
d8f4e2dbec387a31bda747c4a935f88b
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class Test1{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); boolean[] mrr = new boolean[n]; int[] a = new int[n]; int[] b = new int[n]; for(int i=0;i<n;i++){ mrr[i] = sc.next().equals("M"); a[i] = sc.nextInt(); b[i] = sc.nextInt(); } int res =0; for(int i=1;i<=366;i++){ int c1=0,c2=0; for(int j=0;j<n;j++){ if(i>=a[j] && i<=b[j]){ if(mrr[j]== true)c1++; else c2++; } } res = Math.max(res,Math.min(c1,c2)*2); } System.out.print(res); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
0af27763544c831519ffa76643bd4f16
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; public class FarRelativesProblem { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] boys=new int[368]; int[] girls=new int[368]; for(int i=1;i<=n;i++) { if(sc.next().equals("M")) { boys[sc.nextInt()]++; boys[sc.nextInt()+1]--; } else { girls[sc.nextInt()]++; girls[sc.nextInt()+1]--; } } int answer=0; for(int i=1;i<=366;i++) { boys[i]+=boys[i-1]; girls[i]+=girls[i-1]; answer=Math.max(answer, 2*Math.min(boys[i], girls[i])); } System.out.println(answer); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
d4b46748093d94f989f1e3b90c6bfdc0
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String[]args){ Scanner ss=new Scanner(System.in); int n=ss.nextInt(),ans=0,temp=0,m,f; String input[]; ss.nextLine(); Range list[]=new Range[n]; for(int i=0;i<n;i++){ input=ss.nextLine().split(" "); list[i]=new Range(Integer.parseInt(input[1]),Integer.parseInt(input[2]),input[0]); } Arrays.sort(list); for(int i=list[0].lower;i<=366;i++){ m=0;f=0; for(int j=0;j<n;j++){ if(list[j].isItInRange(i)){ if(list[j].gender.equals("F")) f++; else m++; } } temp=(m>f)?f:m; ans=(ans>temp)?ans:temp; } System.out.println(ans*2); } } class Range implements Comparable<Range>{ int lower; int upper; String gender; public Range(int x,int y,String z){ lower=x; upper=y; gender=z; } public int compareTo(Range o){ return (this.lower-o.lower>0)?1:(this.lower==o.lower)?0:-1; } public boolean isItInRange(int x){ if(x<=this.upper&&x>=this.lower) return true; return false; } public String toString(){ return this.lower+" "+this.upper; } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
bb1781e97c99155aa44aadde4e54f0c3
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
//package srm343; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class B { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); FarFriend [] friends = new FarFriend[n]; for (int i=0;i<n;i++){ String [] parts = br.readLine().split(" "); friends[i] = new FarFriend(parts[0], Integer.parseInt(parts[1]),Integer.parseInt(parts[2])); } int max = 0; for (int i=1;i<=366;i++){ int numMales = 0; int numFemales = 0; for (int j=0;j<n;j++){ FarFriend f = friends[j]; if (i>=f.from && i<=f.to){ if (f.isMale()){ numMales++; }else{ numFemales++; } } } max = Math.max(max, Math.min(numMales, numFemales)*2); } System.out.println(max); } static class FarFriend{ String sex; int from; int to; public FarFriend(String sex, int from, int to){ this.sex=sex; this.from=from; this.to=to; } public boolean isMale(){ return sex.equals("M"); } } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
24b671ad634155e1c48051ecd4c68fca
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Scanner; public class FarRelative { public static void main(String ... arg){ Scanner s = new Scanner(System.in); int n = s.nextInt(); long countM[] = new long[370]; long countF[] = new long[370]; ArrayList sum = new ArrayList(); String[] gender = new String[n]; List<int[]> dayRangeF = new ArrayList<int[]>(); List<int[]> dayRangeM = new ArrayList<int[]>(); for(int i=0;i<n;i++){ gender[i] = s.next(); int [] dayRange = new int[2]; for(int j=0;j<2;j++){ dayRange[j] = s.nextInt(); } if(gender[i].equals("F")){ dayRangeF.add(dayRange);//for females } else{ dayRangeM.add(dayRange);//for males } } for(int i=0,k=0,l=0;i<n;i++){ int days[] = new int[2]; if(gender[i].equals("F")){ days = dayRangeF.get(k); for(int j = days[0];j<=days[1];j++){ countF[j]++;// for the days in the range updating the count by 1 for females } k++; } else{ days = dayRangeM.get(l); for(int j = days[0];j<=days[1];j++){ countM[j]++;/// for the days in the range updating the count by 1 for males } l++; } } int max = 0, cur = 0; for(int i = 0; i < 370; i ++){ cur = (int) (Math.min(countM[i],countF[i])*2); if(cur > max){ max = cur; } } System.out.println(max); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
a188ce076ce6a638f78c48602672ec78
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class B { public static int guestsCanCome(int day, int guests[][], int i, int length) { if (i >= length) { return 0; } int countGuest = 0; if (day >= guests[i][0] && day <= guests[i][1]) { countGuest = 1; } return countGuest + guestsCanCome(day, guests, i+1, length); } public static void main(String[] args) throws FileNotFoundException { //File inFile = new File("./src/b.txt"); Scanner in; in = new Scanner(System.in); int n; n = in.nextInt(); int maleGuests[][] = new int[n][2], femaleGuests[][] = new int[n][2], mCount = 0, fCount = 0; in.nextLine(); for (int i = 0; i < n; i++) { String guest = in.nextLine(); String temp[] = guest.split(" "); char g = temp[0].charAt(0); int start = Integer.parseInt(temp[1]); int end = Integer.parseInt(temp[2]); if (g == 'M') { maleGuests[mCount][0] = start; maleGuests[mCount][1] = end; mCount++; } else { femaleGuests[fCount][0] = start; femaleGuests[fCount][1] = end; fCount++; } } int maxPossible = 0; for (int i = 1; i <= 366; i++) { int curMaxPossible = Math.min(guestsCanCome(i, maleGuests, 0, mCount), guestsCanCome(i, femaleGuests, 0, fCount)); maxPossible = Math.max(maxPossible, curMaxPossible); } System.out.println(maxPossible*2); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
1495a3bcc40b23b03596cea47d041721
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; public class B { public static void main(String [] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer tokenizer; int N = Integer.parseInt(br.readLine()); int [] counterM = new int[380]; int [] counterF = new int[380]; for(int i=0; i<N; i++) { tokenizer = new StringTokenizer(br.readLine()); char ch = tokenizer.nextToken().charAt(0); int fr = Integer.parseInt(tokenizer.nextToken()); int to = Integer.parseInt(tokenizer.nextToken()); for(int it = fr; it<=to; it++) { if(ch == 'M') counterM[it]++; else counterF[it]++; } } int max = -1; for(int i=1; i<=366; i++) { int pairs = Math.min(counterM[i], counterF[i]); if(pairs > max) max = pairs; } System.out.println(2 * max); bw.flush(); bw.close(); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
a42007a13677bd1ec77fdc271165a17a
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import java.util.StringTokenizer; public class B { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); scan.nextLine(); ArrayList<Res> males = new ArrayList<Res>(); ArrayList<Res> females = new ArrayList<Res>(); ArrayList<Res> everyone = new ArrayList<Res>(); ArrayList<Res> leave = new ArrayList<Res>(); for (int i=0; i<n; i++) { StringTokenizer tokens = new StringTokenizer(scan.nextLine()); Res temp = new Res(tokens.nextToken().charAt(0), Integer.parseInt(tokens.nextToken()), Integer.parseInt(tokens.nextToken())); everyone.add(temp); //leave.add(temp); } Collections.sort(everyone); Res.sort = false; //Collections.sort(leave); //Res.sort = true; int answer = 0; for (int i=0; i<n; i++) { Res temp = everyone.get(0); everyone.remove(0); while (!males.isEmpty() && temp.start > males.get(0).end) { males.remove(0); } while (!females.isEmpty() && temp.start > females.get(0).end) { females.remove(0); } if (temp.gender) { males.add(temp); Collections.sort(males); } else { females.add(temp); Collections.sort(females); } //System.out.println(temp.start + " " + males.size() + " " + females.size()); int maybeAnswer = Math.min(males.size(), females.size()); answer = Math.max(answer, maybeAnswer); } System.out.println(answer*2); } } class Res implements Comparable<Res> { boolean gender; int start; int end; public static boolean sort = true; public Res(char gender, int start, int end) { this.gender = (gender == 'M'); this.start = start; this.end = end; } @Override public int compareTo(Res o) { if (sort) return start - o.start; return end - o.end; } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
83befa44efbf031c47fc8e75f2ef9e54
train_003.jsonl
1455986100
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
256 megabytes
import java.util.Scanner; public class Relative { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); int[] m = new int[368]; int[] f = new int[368]; while(n-- > 0) { String s = sc.nextLine(); String[] line = s.split(" "); int from = Integer.valueOf(line[1]); int to = Integer.valueOf(line[2]); int[] gender = "M".equals(line[0]) ? m : f; gender[from]++; gender[to+1]--; } int men = 0; int women = 0; int max = 0; for(int i = 0; i < 367; i++) { men += m[i]; women += f[i]; max = Math.max(max, Math.min(men, women)); } System.out.println(2*max); } }
Java
["4\nM 151 307\nF 343 352\nF 117 145\nM 24 128", "6\nM 128 130\nF 128 131\nF 131 140\nF 131 141\nM 131 200\nM 140 200"]
2 seconds
["2", "4"]
NoteIn the first sample, friends 3 and 4 can come on any day in range [117, 128].In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Java 7
standard input
[ "brute force" ]
75d500bad37fbd2c5456a942bde09cd3
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door's friends. Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
1,100
Print the maximum number of people that may come to Famil Door's party.
standard output
PASSED
bb85413de5bdd5403598708a468d87f8
train_003.jsonl
1474119900
ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a &gt; c, then everything on the screen disappears and after that the word you have typed appears on the screen.For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); int n, c, count = 1; n = inp.nextInt(); c = inp.nextInt(); int[] arr = new int[n]; for (int i=0; i<n; i++) { arr[i] = inp.nextInt(); } for (int i=0; i<n-1; i++) { if (arr[i+1] - arr[i] <= c) { count++; } else { count = 1; } } System.out.println(count); } }
Java
["6 5\n1 3 8 14 19 20", "6 1\n1 3 5 7 9 10"]
2 seconds
["3", "2"]
NoteThe first sample is already explained in the problem statement.For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 &gt; 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1.
Java 11
standard input
[ "implementation" ]
fb58bc3be4a7a78bdc001298d35c6b21
The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 &lt; t2 &lt; ... &lt; tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word.
800
Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn.
standard output
PASSED
5a1eb22c8eb0f1cc929d5d580352ba78
train_003.jsonl
1474119900
ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a &gt; c, then everything on the screen disappears and after that the word you have typed appears on the screen.For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.Collections; import java.util.StringTokenizer; public class Codeforces716A { public static void main(String[] args) { FastScanner sc=new FastScanner(); int n = sc.nextInt() , c = sc.nextInt(); int a[] = sc.readArray(n); int ans = 1; for(int i = n-1 ; i > 0 ; i--) { if(a[i] - a[i -1] <= c) { ans++; }else break; } System.out.println(ans); } public 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 class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["6 5\n1 3 8 14 19 20", "6 1\n1 3 5 7 9 10"]
2 seconds
["3", "2"]
NoteThe first sample is already explained in the problem statement.For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 &gt; 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1.
Java 11
standard input
[ "implementation" ]
fb58bc3be4a7a78bdc001298d35c6b21
The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 &lt; t2 &lt; ... &lt; tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word.
800
Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn.
standard output
PASSED
09c965703c0b3954266b59cb87559469
train_003.jsonl
1474119900
ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a &gt; c, then everything on the screen disappears and after that the word you have typed appears on the screen.For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.math.BigDecimal; import java.math.BigInteger; public class JavaApplication9 { //*************************************************************************************** static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } //**************************************************************************************** /*static boolean isp(long n) { if(n==3||n==2) return true; if(n%2==0||n%3==0||n==1) 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 factorial(int n) { if (n == 0) return 1; return n*factorial(n-1); } */ /* public int BinaryToDecimal(int binaryNumber){ int decimal = 0; int p = 0; while(true){ if(binaryNumber == 0){ break; } else { int temp = binaryNumber%10; decimal += temp*Math.pow(2, p); binaryNumber = binaryNumber/10; p++; } } return decimal; }*/ /*-********************** Start *********************************-*/ public static void main(String[] args) throws IOException{ //----------------------- Input ---------------------------------------// Reader st=new Reader(); Scanner in=new Scanner(System.in); //HashMap dp = new HashMap(); //String s; int n=st.nextInt(),c=st.nextInt(); int a,s=1,k=0,b; a=st.nextInt(); for(int i=0;i<n-1;i++) { b=st.nextInt(); if(b-a<=c) {s++; a=b;} else{a=b;s=1;} } System.out.print(s); }}
Java
["6 5\n1 3 8 14 19 20", "6 1\n1 3 5 7 9 10"]
2 seconds
["3", "2"]
NoteThe first sample is already explained in the problem statement.For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 &gt; 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1.
Java 11
standard input
[ "implementation" ]
fb58bc3be4a7a78bdc001298d35c6b21
The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 &lt; t2 &lt; ... &lt; tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word.
800
Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn.
standard output
PASSED
153e2c8e422360c8474de0d3761485f0
train_003.jsonl
1474119900
ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a &gt; c, then everything on the screen disappears and after that the word you have typed appears on the screen.For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.
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 ob=new Scanner(System.in); int n=ob.nextInt(); int c=ob.nextInt(); long a[]=new long[n]; int max=1; int k=1; for(int i=0;i<n;i++) a[i]=ob.nextInt(); Arrays.sort(a); for(int i=1;i<n;i++) { if((a[i]-a[i-1])<=c) k++; else k=1; } System.out.println(k); } }
Java
["6 5\n1 3 8 14 19 20", "6 1\n1 3 5 7 9 10"]
2 seconds
["3", "2"]
NoteThe first sample is already explained in the problem statement.For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 &gt; 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1.
Java 11
standard input
[ "implementation" ]
fb58bc3be4a7a78bdc001298d35c6b21
The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 &lt; t2 &lt; ... &lt; tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word.
800
Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn.
standard output
PASSED
843ba6583ffcb81cd1563b290a4052ad
train_003.jsonl
1582473900
VK news recommendation system daily selects interesting publications of one of $$$n$$$ disjoint categories for each user. Each publication belongs to exactly one category. For each category $$$i$$$ batch algorithm selects $$$a_i$$$ publications.The latest A/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of $$$i$$$-th category within $$$t_i$$$ seconds. What is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can't remove publications recommended by the batch algorithm.
512 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.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; import java.util.stream.Collectors; import java.util.stream.IntStream; //import net.leksi.cf.SegTreeEx1; public class A5 { public static void main(String[] args) throws IOException { new A5().run(); } private void load_inputs(final BufferedReader br) throws IOException { try ( PrintWriter pw = select_output(); ) { int t = 1;//Integer.valueOf(br.readLine().trim()); while(t-- > 0) { int[] n = Arrays.stream(br.readLine().trim().split("\\s+")).mapToInt(v -> Integer.valueOf(v)).toArray(); int[] a = Arrays.stream(br.readLine().trim().split("\\s+")).mapToInt(v -> Integer.valueOf(v)).toArray(); int[] tm = Arrays.stream(br.readLine().trim().split("\\s+")).mapToInt(v -> Integer.valueOf(v)).toArray(); solve(n[0], a, tm, pw); } } } private Comparator<int[]> cmp = (x, y) -> { return x[0] != y[0] ? x[0] - y[0] : y[1] - x[1]; }; private void solve(final int n, final int[] a, final int[] t, final PrintWriter pw) { List<int[]> aa = IntStream.range(0, n + 1).mapToObj(i -> i < n ? new int[]{a[i], t[i]} : new int[]{Integer.MAX_VALUE, 0}).sorted((x, y) -> x[0] - y[0]).collect(Collectors.toList()); PriorityQueue<Integer> t_sorted = new PriorityQueue<>((x, y) -> y - x); // System.out.println(aa.stream().map(v -> "{" + v[0] + ", " + v[1] + "}").collect(Collectors.joining(", ", "[", "]"))); // System.out.println(a_uniq.stream().map(v -> "{" + v[0] + ", " + v[1] + "}").collect(Collectors.joining(", ", "[", "]"))); long res = 0; long s = 0; // System.out.println(aa.stream().map(v -> "{" + v[0] + ", " + v[1] + "}").collect(Collectors.joining(", ", "[", "]"))); t_sorted.add(aa.get(0)[1]); s += aa.get(0)[1]; for(int i = 1; i < aa.size(); i++) { int c = aa.get(i)[0] - aa.get(i - 1)[0]; while(!t_sorted.isEmpty() && c > 0) { s -= t_sorted.poll(); res += s; c--; } t_sorted.add(aa.get(i)[1]); s += aa.get(i)[1]; } // System.out.println(aa.stream().map(v -> "{" + v[0] + ", " + v[1] + "}").collect(Collectors.joining(", ", "[", "]"))); pw.println(res); } String nameIn = "A.in"; String nameOut = null; private void run() throws IOException { File input = null; if (nameIn != null && (input = new File(nameIn)).exists()) { try ( FileReader fr = new FileReader(input); BufferedReader br = new BufferedReader(fr);) { load_inputs(br); } } else { try ( InputStreamReader fr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(fr);) { load_inputs(br); } } } PrintWriter select_output() throws FileNotFoundException { if (nameOut != null) { return new PrintWriter(nameOut); } return new PrintWriter(System.out); } }
Java
["5\n3 7 9 7 8\n5 2 5 7 5", "5\n1 2 3 4 5\n1 1 1 1 1"]
2 seconds
["6", "0"]
NoteIn the first example, it is possible to find three publications of the second type, which will take 6 seconds.In the second example, all news categories contain a different number of publications.
Java 8
standard input
[ "data structures", "sortings", "greedy" ]
6bceaf308c38d234ba931c13e4f70929
The first line of input consists of single integer $$$n$$$ — the number of news categories ($$$1 \le n \le 200\,000$$$). The second line of input consists of $$$n$$$ integers $$$a_i$$$ — the number of publications of $$$i$$$-th category selected by the batch algorithm ($$$1 \le a_i \le 10^9$$$). The third line of input consists of $$$n$$$ integers $$$t_i$$$ — time it takes for targeted algorithm to find one new publication of category $$$i$$$ ($$$1 \le t_i \le 10^5)$$$.
1,700
Print one integer — the minimal required time for the targeted algorithm to get rid of categories with the same size.
standard output
PASSED
d5c2a11e6bd4197930dadd7d4f58a092
train_003.jsonl
1582473900
VK news recommendation system daily selects interesting publications of one of $$$n$$$ disjoint categories for each user. Each publication belongs to exactly one category. For each category $$$i$$$ batch algorithm selects $$$a_i$$$ publications.The latest A/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of $$$i$$$-th category within $$$t_i$$$ seconds. What is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can't remove publications recommended by the batch algorithm.
512 megabytes
import java.io.*; import java.util.*; import java.math.BigInteger; import java.util.Map.Entry; import static java.lang.Math.*; public class A extends PrintWriter { void run() { int n = nextInt(); int[] a = nextArray(n); int[] t = nextArray(n); Integer[] order = new Integer[n]; for (int i = 0; i < n; i++) { order[i] = i; } Arrays.sort(order, Comparator.comparingInt(i -> a[i])); int[] b = new int[n]; b[0] = a[order[0]]; for (int i = 1; i < n; i++) { b[i] = max(a[order[i]], b[i - 1] + 1); } TreeSet<Integer> h = new TreeSet<>(); for (int val : b) { h.add(val); } Arrays.sort(order, Comparator.comparingInt(i -> t[i])); long ans = 0; for (int i = n - 1; i >= 0; i--) { Integer x = h.ceiling(a[order[i]]); h.remove(x); ans += ((long) x - a[order[i]]) * t[order[i]]; } println(ans); } boolean skip() { while (hasNext()) { next(); } return true; } int[][] nextMatrix(int n, int m) { int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) matrix[i][j] = nextInt(); return matrix; } String next() { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(nextLine()); return tokenizer.nextToken(); } boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String line = nextLine(); if (line == null) { return false; } tokenizer = new StringTokenizer(line); } return true; } int[] nextArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { try { return reader.readLine(); } catch (IOException err) { return null; } } public A(OutputStream outputStream) { super(outputStream); } static BufferedReader reader; static StringTokenizer tokenizer = new StringTokenizer(""); static Random rnd = new Random(); static boolean OJ; public static void main(String[] args) throws IOException { OJ = System.getProperty("ONLINE_JUDGE") != null; A solution = new A(System.out); if (OJ) { reader = new BufferedReader(new InputStreamReader(System.in)); solution.run(); } else { reader = new BufferedReader(new FileReader(new File(A.class.getName() + ".txt"))); long timeout = System.currentTimeMillis(); while (solution.hasNext()) { solution.run(); solution.println(); solution.println("----------------------------------"); } solution.println("time: " + (System.currentTimeMillis() - timeout)); } solution.close(); reader.close(); } }
Java
["5\n3 7 9 7 8\n5 2 5 7 5", "5\n1 2 3 4 5\n1 1 1 1 1"]
2 seconds
["6", "0"]
NoteIn the first example, it is possible to find three publications of the second type, which will take 6 seconds.In the second example, all news categories contain a different number of publications.
Java 8
standard input
[ "data structures", "sortings", "greedy" ]
6bceaf308c38d234ba931c13e4f70929
The first line of input consists of single integer $$$n$$$ — the number of news categories ($$$1 \le n \le 200\,000$$$). The second line of input consists of $$$n$$$ integers $$$a_i$$$ — the number of publications of $$$i$$$-th category selected by the batch algorithm ($$$1 \le a_i \le 10^9$$$). The third line of input consists of $$$n$$$ integers $$$t_i$$$ — time it takes for targeted algorithm to find one new publication of category $$$i$$$ ($$$1 \le t_i \le 10^5)$$$.
1,700
Print one integer — the minimal required time for the targeted algorithm to get rid of categories with the same size.
standard output
PASSED
32fc476bf94fab56fad4ba94424303af
train_003.jsonl
1582473900
VK news recommendation system daily selects interesting publications of one of $$$n$$$ disjoint categories for each user. Each publication belongs to exactly one category. For each category $$$i$$$ batch algorithm selects $$$a_i$$$ publications.The latest A/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of $$$i$$$-th category within $$$t_i$$$ seconds. What is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can't remove publications recommended by the batch algorithm.
512 megabytes
import java.util.*; public class reccommendations { public static void main(String[] args) { Scanner x = new Scanner(System.in); int n = x.nextInt(); int[] arr = new int[n]; News[] newsarr = new News[n]; for(int i = 0; i < n; i++) { arr[i] = x.nextInt(); } for(int i = 0; i < n; i++) { newsarr[i] = new News(arr[i], x.nextInt()); } Arrays.sort(newsarr); long total = 0; PriorityQueue<Integer> values = new PriorityQueue<Integer>(); int val = 0; long sum = 0; for(int i = 0; i <= n; i++) { while (!values.isEmpty() && (i == n || newsarr[i].num > val)) { int max = -values.poll(); total -= max; sum += total; val++; } if (i == n)break; val = newsarr[i].num; values.add(-newsarr[i].cost); total += newsarr[i].cost; } System.out.println(sum); } } class News implements Comparable<News>{ public int num; public int cost; public News(int a, int b) { num = a; cost = b; } public int compareTo(News other) { return this.num - other.num; } }
Java
["5\n3 7 9 7 8\n5 2 5 7 5", "5\n1 2 3 4 5\n1 1 1 1 1"]
2 seconds
["6", "0"]
NoteIn the first example, it is possible to find three publications of the second type, which will take 6 seconds.In the second example, all news categories contain a different number of publications.
Java 8
standard input
[ "data structures", "sortings", "greedy" ]
6bceaf308c38d234ba931c13e4f70929
The first line of input consists of single integer $$$n$$$ — the number of news categories ($$$1 \le n \le 200\,000$$$). The second line of input consists of $$$n$$$ integers $$$a_i$$$ — the number of publications of $$$i$$$-th category selected by the batch algorithm ($$$1 \le a_i \le 10^9$$$). The third line of input consists of $$$n$$$ integers $$$t_i$$$ — time it takes for targeted algorithm to find one new publication of category $$$i$$$ ($$$1 \le t_i \le 10^5)$$$.
1,700
Print one integer — the minimal required time for the targeted algorithm to get rid of categories with the same size.
standard output
PASSED
e5cb647f0672844ecaa645c231c0a442
train_003.jsonl
1422705600
Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students:You are given a tree T with n vertices, specified by its adjacency matrix a[1... n, 1... n]. What is the output of the following pseudocode?used[1 ... n] = {0, ..., 0};procedure dfs(v): print v; used[v] = 1; for i = 1, 2, ..., n: if (a[v][i] == 1 and used[i] == 0): dfs(i);dfs(1);In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree T such that the result is his favorite sequence b. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees T such that the result of running the above pseudocode with T as input is exactly the sequence b. Can you help him?Two trees with n vertices are called different if their adjacency matrices a1 and a2 are different, i. e. there exists a pair (i, j), such that 1 ≤ i, j ≤ n and a1[i][j] ≠ a2[i][j].
256 megabytes
//package codeforces.cfr289div2; import java.util.Arrays; import java.util.Scanner; /** * Created by raggzy on 01-Feb-15. */ public class F { static int MODULO = 1000000007; int n; int[] a; int[][] g; // i - root, next childs int f(int i, int j) { if (i == j) { return 1; } else { return g(i+1, j); } } // all possible children trees count int g(int i, int j) { if (g[i][j] == -1) { int res = 0; for (int k = i; k <=j; k++) { if (k == j) { res += f(i, k); } else { if (a[k+1] > a[i]) { res += ((long) f(i, k) * g(k+1, j)) % MODULO; } } res %= MODULO; } g[i][j] = res; } return g[i][j]; } void solve() { Scanner in = new Scanner(System.in); n = in.nextInt(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } g = new int[n][n]; for (int i = 0; i < n; i++) { Arrays.fill(g[i], -1); } System.out.println(f(0, n-1)); } public static void main(String[] args) { new F().solve(); } }
Java
["3\n1 2 3", "3\n1 3 2"]
1 second
["2", "1"]
null
Java 7
standard input
[ "dp", "trees" ]
a93294df0a596850d5080cc024ffddcd
The first line contains the positive integer n (1 ≤ n ≤ 500) — the length of sequence b. The second line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ n). It is guaranteed that b is a permutation, or in other words, each of the numbers 1, 2, ..., n appears exactly once in the sequence b. Also it is guaranteed that b1 = 1.
2,300
Output the number of trees satisfying the conditions above modulo 109 + 7.
standard output
PASSED
b38b92641c3e6876289ce0a1dc1d3c75
train_003.jsonl
1422705600
Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students:You are given a tree T with n vertices, specified by its adjacency matrix a[1... n, 1... n]. What is the output of the following pseudocode?used[1 ... n] = {0, ..., 0};procedure dfs(v): print v; used[v] = 1; for i = 1, 2, ..., n: if (a[v][i] == 1 and used[i] == 0): dfs(i);dfs(1);In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree T such that the result is his favorite sequence b. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees T such that the result of running the above pseudocode with T as input is exactly the sequence b. Can you help him?Two trees with n vertices are called different if their adjacency matrices a1 and a2 are different, i. e. there exists a pair (i, j), such that 1 ≤ i, j ≤ n and a1[i][j] ≠ a2[i][j].
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Locale; import java.util.NavigableSet; import java.util.Random; import java.util.SortedSet; import java.util.StringTokenizer; import java.util.TreeSet; public final class CF_ProgressMonitoring_v2 { void log(int[] X){ int L=X.length; for (int i=0;i<L;i++){ logWln(X[i]+" "); } log(""); } void log(long[] X){ int L=X.length; for (int i=0;i<L;i++){ logWln(X[i]+" "); } log(""); } void log(Object[] X){ int L=X.length; for (int i=0;i<L;i++){ logWln(X[i]+" "); } log(""); } void log(Object o){ logWln(o+"\n"); } void logWln(Object o){ System.out.print(o); //outputWln(o); } void info(Object o){ System.out.println(o); //output(o); } void output(Object o){ outputWln(""+o+"\n"); } void outputWln(Object o){ // System.out.print(o); try { out.write(""+ o); } catch (Exception e) { } } String next(){ while (st== null|| !st.hasMoreTokens()) { try { st=new StringTokenizer(in.readLine()); } catch (Exception e) { } } return st.nextToken(); } String nextLine(){ try { return in.readLine(); } catch (Exception e) { } return null; } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } int[] used; int[][] a; int N; int[] b; void dfs(int v){ log(v); used[v] = 1; for (int i=0;i<N;i++){ if (a[v][i] == 1 && used[i] == 0) dfs(i); } } long[][] cacheH; long[][] cacheV; long computeHorizontal(String s,int l,int r){ // on commence à l // on va jusqu'à r // on segment en vertical ou horizontal // mais pour segmenter en horizontal il ne faut pas être inférieur à b[l] log(s+" l:"+l+" r:"+r); long res=0; if (r<l) { log(s+" "+0); return 0; } if (cacheH[l][r]>0) return cacheH[l][r]-1; if (r==l) { log(s+" "+1); return 1; } if (r==l+1){ if (b[r]>b[l]) res=2; else res=1; cacheH[l][r]=res+1; log(s+" "+res); return res; } long t1=0,t2=0; for (int x=l+1;x<=r;x++){ if (b[x]>b[l]){ if (x==l+1) t1=1; else t1=computeHorizontal(s+"-",l+1,x-1); t2=computeHorizontal(s+"-",x,r); res+=t1*t2; } } res+=computeHorizontal(s+"-",l+1,r); cacheH[l][r]=res+1; log(s+" "+res); return res; } long computeHorizontal(int l,int r){ // on commence à l // on va jusqu'à r // on segment en vertical ou horizontal // mais pour segmenter en horizontal il ne faut pas être inférieur à b[l] long res=0; if (r<l) { return 0; } if (cacheH[l][r]>0) return cacheH[l][r]-1; if (r==l) { cacheH[l][r]=1+1; return 1; } if (r==l+1){ if (b[r]>b[l]) res=2; else res=1; cacheH[l][r]=res+1; return res; } long t1=0,t2=0; for (int x=l+1;x<=r;x++){ if (b[x]>b[l]){ if (x==l+1) t1=1; else t1=computeHorizontal(l+1,x-1); t2=computeHorizontal(x,r); long v=t1*t2; if (v>=MOD) v%=MOD; res+=v; if (res>=MOD) res%=MOD; } } res+=computeHorizontal(l+1,r); if (res>=MOD) res%=MOD; cacheH[l][r]=res+1; return res; } void solve(){ cacheH=new long[N+1][N+1]; if (N==1) output("1"); else output(computeHorizontal(1,N-1)); } // Global vars StringTokenizer st; BufferedReader in; BufferedWriter out; static long MOD=1000000007; void process() throws Exception { Locale.setDefault(Locale.US); out = new BufferedWriter(new OutputStreamWriter(System.out)); in=new BufferedReader(new InputStreamReader(System.in)); N=nextInt(); b=new int[N]; for (int i=0;i<N;i++) b[i]=nextInt()-1; solve(); try { out.close(); } catch (Exception e){} } public static void main(String[] args) throws Exception { CF_ProgressMonitoring_v2 J=new CF_ProgressMonitoring_v2(); J.process(); } }
Java
["3\n1 2 3", "3\n1 3 2"]
1 second
["2", "1"]
null
Java 7
standard input
[ "dp", "trees" ]
a93294df0a596850d5080cc024ffddcd
The first line contains the positive integer n (1 ≤ n ≤ 500) — the length of sequence b. The second line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ n). It is guaranteed that b is a permutation, or in other words, each of the numbers 1, 2, ..., n appears exactly once in the sequence b. Also it is guaranteed that b1 = 1.
2,300
Output the number of trees satisfying the conditions above modulo 109 + 7.
standard output
PASSED
d02340ff14ecfac362235f2b3ad28f70
train_003.jsonl
1422705600
Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students:You are given a tree T with n vertices, specified by its adjacency matrix a[1... n, 1... n]. What is the output of the following pseudocode?used[1 ... n] = {0, ..., 0};procedure dfs(v): print v; used[v] = 1; for i = 1, 2, ..., n: if (a[v][i] == 1 and used[i] == 0): dfs(i);dfs(1);In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree T such that the result is his favorite sequence b. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees T such that the result of running the above pseudocode with T as input is exactly the sequence b. Can you help him?Two trees with n vertices are called different if their adjacency matrices a1 and a2 are different, i. e. there exists a pair (i, j), such that 1 ≤ i, j ≤ n and a1[i][j] ≠ a2[i][j].
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Locale; import java.util.NavigableSet; import java.util.Random; import java.util.SortedSet; import java.util.StringTokenizer; import java.util.TreeSet; public final class CF_ProgressMonitoring_v2 { void log(int[] X){ int L=X.length; for (int i=0;i<L;i++){ logWln(X[i]+" "); } log(""); } void log(long[] X){ int L=X.length; for (int i=0;i<L;i++){ logWln(X[i]+" "); } log(""); } void log(Object[] X){ int L=X.length; for (int i=0;i<L;i++){ logWln(X[i]+" "); } log(""); } void log(Object o){ logWln(o+"\n"); } void logWln(Object o){ System.out.print(o); //outputWln(o); } void info(Object o){ System.out.println(o); //output(o); } void output(Object o){ outputWln(""+o+"\n"); } void outputWln(Object o){ // System.out.print(o); try { out.write(""+ o); } catch (Exception e) { } } String next(){ while (st== null|| !st.hasMoreTokens()) { try { st=new StringTokenizer(in.readLine()); } catch (Exception e) { } } return st.nextToken(); } String nextLine(){ try { return in.readLine(); } catch (Exception e) { } return null; } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } int[] used; int[][] a; int N; int[] b; void dfs(int v){ log(v); used[v] = 1; for (int i=0;i<N;i++){ if (a[v][i] == 1 && used[i] == 0) dfs(i); } } long[][] cacheH; long computeHorizontal(int l,int r){ // on commence à l // on va jusqu'à r // on segment en vertical ou horizontal // mais pour segmenter en horizontal il ne faut pas être inférieur à b[l] long res=0; if (r<l) { return 0; } if (cacheH[l][r]>0) return cacheH[l][r]-1; if (r==l) { cacheH[l][r]=1+1; return 1; } if (r==l+1){ if (b[r]>b[l]) res=2; else res=1; cacheH[l][r]=res+1; return res; } long t1=0,t2=0; for (int x=l+1;x<=r;x++){ if (b[x]>b[l]){ if (x==l+1) t1=1; else t1=computeHorizontal(l+1,x-1); t2=computeHorizontal(x,r); long v=t1*t2; if (v>=MOD) v%=MOD; res+=v; if (res>=MOD) res%=MOD; } } res+=computeHorizontal(l+1,r); if (res>=MOD) res%=MOD; cacheH[l][r]=res+1; return res; } void solve(){ cacheH=new long[N+1][N+1]; if (N==1) output("1"); else output(computeHorizontal(1,N-1)); } // Global vars StringTokenizer st; BufferedReader in; BufferedWriter out; static long MOD=1000000007; void process() throws Exception { Locale.setDefault(Locale.US); out = new BufferedWriter(new OutputStreamWriter(System.out)); in=new BufferedReader(new InputStreamReader(System.in)); N=nextInt(); b=new int[N]; for (int i=0;i<N;i++) b[i]=nextInt()-1; solve(); try { out.close(); } catch (Exception e){} } public static void main(String[] args) throws Exception { CF_ProgressMonitoring_v2 J=new CF_ProgressMonitoring_v2(); J.process(); } }
Java
["3\n1 2 3", "3\n1 3 2"]
1 second
["2", "1"]
null
Java 7
standard input
[ "dp", "trees" ]
a93294df0a596850d5080cc024ffddcd
The first line contains the positive integer n (1 ≤ n ≤ 500) — the length of sequence b. The second line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ n). It is guaranteed that b is a permutation, or in other words, each of the numbers 1, 2, ..., n appears exactly once in the sequence b. Also it is guaranteed that b1 = 1.
2,300
Output the number of trees satisfying the conditions above modulo 109 + 7.
standard output
PASSED
3a40a73f73770e159c0b7e6f5c8671dd
train_003.jsonl
1422705600
Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students:You are given a tree T with n vertices, specified by its adjacency matrix a[1... n, 1... n]. What is the output of the following pseudocode?used[1 ... n] = {0, ..., 0};procedure dfs(v): print v; used[v] = 1; for i = 1, 2, ..., n: if (a[v][i] == 1 and used[i] == 0): dfs(i);dfs(1);In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree T such that the result is his favorite sequence b. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees T such that the result of running the above pseudocode with T as input is exactly the sequence b. Can you help him?Two trees with n vertices are called different if their adjacency matrices a1 and a2 are different, i. e. there exists a pair (i, j), such that 1 ≤ i, j ≤ n and a1[i][j] ≠ a2[i][j].
256 megabytes
import java.io.*; import java.util.*; public class e { static int n; static int[] p; static long mod = (long)1e9+7; public static void main(String[] args) throws IOException { input.init(System.in); PrintWriter out = new PrintWriter(System.out); n = input.nextInt(); p = new int[n]; for(int i = 0; i<n; i++) p[i] = input.nextInt(); long[][] dp = new long[n+1][(n+1)<<1]; for(int i = 0; i<=n; i++) for(int j = i; j>=0; j--) { dp[i][j<<1] = dp[i][(j<<1)+1] = 1; } for(int d = 1; d<n; d++) for(int k = 0; k+d<n; k++) for(int root = 1; root>=0; root--) { int a = k, b = k+d; long res = 0; //System.out.println(a+" "+b+" "+root); for(int i = a; i<=b; i++) { if(i != b && p[i+1] < (root == 0 ? p[a] : p[a+1])) continue; if(root == 1 && i!=a) res = (res + dp[a+1][(i<<1) + 1] * dp[i+1][b<<1]); else if(root == 0) res = (res + dp[a][(i<<1) + 1] * dp[i+1][b<<1]); if(res > 5e18) res %= mod; } dp[a][(b<<1)+root] = res%mod; } //System.out.println(dp[0].length+" "+(n-1)); out.println(dp[0][((n-1)<<1) + 1]); out.close(); } public static class input { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } }
Java
["3\n1 2 3", "3\n1 3 2"]
1 second
["2", "1"]
null
Java 7
standard input
[ "dp", "trees" ]
a93294df0a596850d5080cc024ffddcd
The first line contains the positive integer n (1 ≤ n ≤ 500) — the length of sequence b. The second line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ n). It is guaranteed that b is a permutation, or in other words, each of the numbers 1, 2, ..., n appears exactly once in the sequence b. Also it is guaranteed that b1 = 1.
2,300
Output the number of trees satisfying the conditions above modulo 109 + 7.
standard output
PASSED
3f5efb23ba77ad7a15d5343bca5474ff
train_003.jsonl
1422705600
Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students:You are given a tree T with n vertices, specified by its adjacency matrix a[1... n, 1... n]. What is the output of the following pseudocode?used[1 ... n] = {0, ..., 0};procedure dfs(v): print v; used[v] = 1; for i = 1, 2, ..., n: if (a[v][i] == 1 and used[i] == 0): dfs(i);dfs(1);In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree T such that the result is his favorite sequence b. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees T such that the result of running the above pseudocode with T as input is exactly the sequence b. Can you help him?Two trees with n vertices are called different if their adjacency matrices a1 and a2 are different, i. e. there exists a pair (i, j), such that 1 ≤ i, j ≤ n and a1[i][j] ≠ a2[i][j].
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class CF289F { static int[] a; static int[][] dp; static int MOD = 1_000_000_007; static int f(int l, int r) { if (l >= r) return 1; if (dp[l][r] != -1) return dp[l][r]; long answer = 0; for (int k = l + 1; k <= r + 1; k++) if (k == r + 1 || a[k] >= a[l]) answer = (answer + f(l + 1, k - 1) * 1L * f(k, r) % MOD) % MOD; return dp[l][r] = (int) answer; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); dp = new int[n][n]; for (int[] a : dp) Arrays.fill(a, -1); a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); System.out.println(f(1, n - 1)); } }
Java
["3\n1 2 3", "3\n1 3 2"]
1 second
["2", "1"]
null
Java 7
standard input
[ "dp", "trees" ]
a93294df0a596850d5080cc024ffddcd
The first line contains the positive integer n (1 ≤ n ≤ 500) — the length of sequence b. The second line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ n). It is guaranteed that b is a permutation, or in other words, each of the numbers 1, 2, ..., n appears exactly once in the sequence b. Also it is guaranteed that b1 = 1.
2,300
Output the number of trees satisfying the conditions above modulo 109 + 7.
standard output
PASSED
2b7e1b7ad76fedc64dedf1c313a27c03
train_003.jsonl
1422705600
Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students:You are given a tree T with n vertices, specified by its adjacency matrix a[1... n, 1... n]. What is the output of the following pseudocode?used[1 ... n] = {0, ..., 0};procedure dfs(v): print v; used[v] = 1; for i = 1, 2, ..., n: if (a[v][i] == 1 and used[i] == 0): dfs(i);dfs(1);In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree T such that the result is his favorite sequence b. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees T such that the result of running the above pseudocode with T as input is exactly the sequence b. Can you help him?Two trees with n vertices are called different if their adjacency matrices a1 and a2 are different, i. e. there exists a pair (i, j), such that 1 ≤ i, j ≤ n and a1[i][j] ≠ a2[i][j].
256 megabytes
//package codeforces; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class F { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer stringTokenizer; String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } int MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { a += b; return a >= MOD ? a - MOD : a; } int product(int a, int b) { return (int) (1l * a * b % MOD); } void solve() throws IOException { final int n = nextInt(); final int[] b = new int[n]; for(int i = 0; i < n; i++) { b[i] = nextInt(); } class Utils { int countTrees(int from, int to) { if(from == to) return 1; if(from + 1 == to) return 1; return countKidsTrees(from + 1, to); } int countKidsTrees(int from, int to) { if(from == to) return 1; if(from + 1 == to) return 1; int result = countTrees(from, to); for(int i = from + 1; i < to; i++) { if(b[i] > b[from]) { result = sum(result, product(countTrees(from, i), countKidsTrees(i, to))); } } return result; } } class CachedUtils extends Utils { boolean[][] hit = new boolean[n + 1][n + 1]; int[][] cache = new int[n + 1][n + 1]; @Override int countKidsTrees(int from, int to) { if(!hit[from][to]) { hit[from][to] = true; cache[from][to] = super.countKidsTrees(from, to); } return cache[from][to]; } } writer.println(new CachedUtils().countTrees(0, n)); writer.close(); } public static void main(String[] args) throws IOException { new F().solve(); } }
Java
["3\n1 2 3", "3\n1 3 2"]
1 second
["2", "1"]
null
Java 7
standard input
[ "dp", "trees" ]
a93294df0a596850d5080cc024ffddcd
The first line contains the positive integer n (1 ≤ n ≤ 500) — the length of sequence b. The second line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ n). It is guaranteed that b is a permutation, or in other words, each of the numbers 1, 2, ..., n appears exactly once in the sequence b. Also it is guaranteed that b1 = 1.
2,300
Output the number of trees satisfying the conditions above modulo 109 + 7.
standard output
PASSED
dcb7b31bb21bfe5fe6318e0bdc2f9585
train_003.jsonl
1422705600
Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students:You are given a tree T with n vertices, specified by its adjacency matrix a[1... n, 1... n]. What is the output of the following pseudocode?used[1 ... n] = {0, ..., 0};procedure dfs(v): print v; used[v] = 1; for i = 1, 2, ..., n: if (a[v][i] == 1 and used[i] == 0): dfs(i);dfs(1);In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree T such that the result is his favorite sequence b. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees T such that the result of running the above pseudocode with T as input is exactly the sequence b. Can you help him?Two trees with n vertices are called different if their adjacency matrices a1 and a2 are different, i. e. there exists a pair (i, j), such that 1 ≤ i, j ≤ n and a1[i][j] ≠ a2[i][j].
256 megabytes
import java.io.*; import java.util.*; public class progressmonitor { private static InputReader in; private static PrintWriter out; public static int mod = 1000000007; public static void main(String[] args) throws IOException { in = new InputReader(System.in); out = new PrintWriter(System.out, true); int N = in.nextInt(); arr = new int[N]; for (int i = 0; i < N; i++) arr[i] = in.nextInt(); if (arr[0] != 1) { out.println(0); } else { dp = new long[N+1][N+1]; for (int i = 0; i <= N; i++) Arrays.fill(dp[i], -1); out.println(dfs1(1, N-1)); } out.close(); System.exit(0); } public static int[] arr; public static long[][] dp; public static long dfs1(int s, int e) { if (s > e) return 1; if (dp[s][e] != -1) return dp[s][e]; long res = dfs1(s+1, e); for (int split = s+1; split <= e; split++) { if (arr[split] > arr[s]) { res = (res + dfs1(s+1, split-1) * dfs1(split, e)) % mod; } } return dp[s][e] = 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()); } } }
Java
["3\n1 2 3", "3\n1 3 2"]
1 second
["2", "1"]
null
Java 7
standard input
[ "dp", "trees" ]
a93294df0a596850d5080cc024ffddcd
The first line contains the positive integer n (1 ≤ n ≤ 500) — the length of sequence b. The second line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ n). It is guaranteed that b is a permutation, or in other words, each of the numbers 1, 2, ..., n appears exactly once in the sequence b. Also it is guaranteed that b1 = 1.
2,300
Output the number of trees satisfying the conditions above modulo 109 + 7.
standard output
PASSED
4098382fb388d70856c6b432c2cc0a8d
train_003.jsonl
1422705600
Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students:You are given a tree T with n vertices, specified by its adjacency matrix a[1... n, 1... n]. What is the output of the following pseudocode?used[1 ... n] = {0, ..., 0};procedure dfs(v): print v; used[v] = 1; for i = 1, 2, ..., n: if (a[v][i] == 1 and used[i] == 0): dfs(i);dfs(1);In order to simplify the test results checking procedure, Dmitry Olegovich decided to create a tree T such that the result is his favorite sequence b. On the other hand, Dmitry Olegovich doesn't want to provide students with same trees as input, otherwise they might cheat. That's why Dmitry Olegovich is trying to find out the number of different trees T such that the result of running the above pseudocode with T as input is exactly the sequence b. Can you help him?Two trees with n vertices are called different if their adjacency matrices a1 and a2 are different, i. e. there exists a pair (i, j), such that 1 ≤ i, j ≤ n and a1[i][j] ≠ a2[i][j].
256 megabytes
import java.io.*; import java.util.*; public class F { public static void main(String[] args) throws IOException { new F().solve(); } void solve() throws IOException { FastScanner in = new FastScanner(System.in); long mod = (long) (1e+9) + 7; int n = in.nextInt(); int[] p = new int[n]; for (int i = 0; i < n; i++) { p[i] = in.nextInt(); } long[][] dp = new long[n][n + 1]; for (int i = 0; i < n; i++) { dp[i][i] = 1; dp[i][i + 1] = 1; } for (int df = 2; df <= n; df++) { for (int a = 0; a + df < n + 1; a++) { int b = a + df; dp[a][b] += dp[a + 1][b]; for (int c = a + 1; c < b; c++) { if (p[a] <= p[c]) { dp[a][b] += dp[a + 1][c] * dp[c][b]; dp[a][b] %= mod; } } } } if (n == 1) { System.out.println(1); } else { System.out.println(dp[1][n]); } } class FastScanner { private InputStream _stream; private byte[] _buf = new byte[1024]; private int _curChar; private int _numChars; private StringBuilder _sb = new StringBuilder(); FastScanner(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 next() { int c = read(); while (isWhitespace(c)) { c = read(); } _sb.setLength(0); do { _sb.appendCodePoint(c); c = read(); } while (!isWhitespace(c)); return _sb.toString(); } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = read(); while (isWhitespace(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 (!isWhitespace(c)); return res * sgn; } public boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } } // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
Java
["3\n1 2 3", "3\n1 3 2"]
1 second
["2", "1"]
null
Java 7
standard input
[ "dp", "trees" ]
a93294df0a596850d5080cc024ffddcd
The first line contains the positive integer n (1 ≤ n ≤ 500) — the length of sequence b. The second line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ n). It is guaranteed that b is a permutation, or in other words, each of the numbers 1, 2, ..., n appears exactly once in the sequence b. Also it is guaranteed that b1 = 1.
2,300
Output the number of trees satisfying the conditions above modulo 109 + 7.
standard output
PASSED
e8ed0b736e2d1f1b9faf35f930254c4f
train_003.jsonl
1557671700
Let $$$s$$$ be some string consisting of symbols "0" or "1". Let's call a string $$$t$$$ a substring of string $$$s$$$, if there exists such number $$$1 \leq l \leq |s| - |t| + 1$$$ that $$$t = s_l s_{l+1} \ldots s_{l + |t| - 1}$$$. Let's call a substring $$$t$$$ of string $$$s$$$ unique, if there exist only one such $$$l$$$. For example, let $$$s = $$$"1010111". A string $$$t = $$$"010" is an unique substring of $$$s$$$, because $$$l = 2$$$ is the only one suitable number. But, for example $$$t = $$$"10" isn't a unique substring of $$$s$$$, because $$$l = 1$$$ and $$$l = 3$$$ are suitable. And for example $$$t =$$$"00" at all isn't a substring of $$$s$$$, because there is no suitable $$$l$$$.Today Vasya solved the following problem at the informatics lesson: given a string consisting of symbols "0" and "1", the task is to find the length of its minimal unique substring. He has written a solution to this problem and wants to test it. He is asking you to help him.You are given $$$2$$$ positive integers $$$n$$$ and $$$k$$$, such that $$$(n \bmod 2) = (k \bmod 2)$$$, where $$$(x \bmod 2)$$$ is operation of taking remainder of $$$x$$$ by dividing on $$$2$$$. Find any string $$$s$$$ consisting of $$$n$$$ symbols "0" or "1", such that the length of its minimal unique substring is equal to $$$k$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DTheMinimalUniqueSubstring solver = new DTheMinimalUniqueSubstring(); solver.solve(1, in, out); out.close(); } static class DTheMinimalUniqueSubstring { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int K = in.nextInt(); char[] answer = new char[n]; ArrayUtils.fill(answer, '0'); int period = (n - K) / 2 + 1; for (int i = period - 1; i < n; i += period) { answer[i] = '1'; } out.println(new String(answer)); } } static class ArrayUtils { public static void fill(char[] array, char value) { Arrays.fill(array, value); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["4 4", "5 3", "7 3"]
1 second
["1111", "01010", "1011011"]
NoteIn the first test, it's easy to see, that the only unique substring of string $$$s = $$$"1111" is all string $$$s$$$, which has length $$$4$$$.In the second test a string $$$s = $$$"01010" has minimal unique substring $$$t =$$$"101", which has length $$$3$$$.In the third test a string $$$s = $$$"1011011" has minimal unique substring $$$t =$$$"110", which has length $$$3$$$.
Java 8
standard input
[ "greedy", "constructive algorithms", "math", "brute force", "strings" ]
7e8baa4fb780f11e66bb2b7078e34c04
The first line contains two integers $$$n$$$ and $$$k$$$, separated by spaces ($$$1 \leq k \leq n \leq 100\,000$$$, $$$(k \bmod 2) = (n \bmod 2)$$$).
2,200
Print a string $$$s$$$ of length $$$n$$$, consisting of symbols "0" and "1". Minimal length of the unique substring of $$$s$$$ should be equal to $$$k$$$. You can find any suitable string. It is guaranteed, that there exists at least one such string.
standard output
PASSED
2ef9f9b2958ef362e510a4dce9e163b6
train_003.jsonl
1557671700
Let $$$s$$$ be some string consisting of symbols "0" or "1". Let's call a string $$$t$$$ a substring of string $$$s$$$, if there exists such number $$$1 \leq l \leq |s| - |t| + 1$$$ that $$$t = s_l s_{l+1} \ldots s_{l + |t| - 1}$$$. Let's call a substring $$$t$$$ of string $$$s$$$ unique, if there exist only one such $$$l$$$. For example, let $$$s = $$$"1010111". A string $$$t = $$$"010" is an unique substring of $$$s$$$, because $$$l = 2$$$ is the only one suitable number. But, for example $$$t = $$$"10" isn't a unique substring of $$$s$$$, because $$$l = 1$$$ and $$$l = 3$$$ are suitable. And for example $$$t =$$$"00" at all isn't a substring of $$$s$$$, because there is no suitable $$$l$$$.Today Vasya solved the following problem at the informatics lesson: given a string consisting of symbols "0" and "1", the task is to find the length of its minimal unique substring. He has written a solution to this problem and wants to test it. He is asking you to help him.You are given $$$2$$$ positive integers $$$n$$$ and $$$k$$$, such that $$$(n \bmod 2) = (k \bmod 2)$$$, where $$$(x \bmod 2)$$$ is operation of taking remainder of $$$x$$$ by dividing on $$$2$$$. Find any string $$$s$$$ consisting of $$$n$$$ symbols "0" or "1", such that the length of its minimal unique substring is equal to $$$k$$$.
256 megabytes
//package com.krakn.CF.D1159; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n, k; n = sc.nextInt(); k = sc.nextInt(); int a = (n - k) / 2; StringBuilder s = new StringBuilder(); int i; while (s.length() < n) { i = 0; while (i < a && s.length() < n) { s.append("0"); i++; } if (s.length() < n) s.append("1"); } System.out.println(s); } }
Java
["4 4", "5 3", "7 3"]
1 second
["1111", "01010", "1011011"]
NoteIn the first test, it's easy to see, that the only unique substring of string $$$s = $$$"1111" is all string $$$s$$$, which has length $$$4$$$.In the second test a string $$$s = $$$"01010" has minimal unique substring $$$t =$$$"101", which has length $$$3$$$.In the third test a string $$$s = $$$"1011011" has minimal unique substring $$$t =$$$"110", which has length $$$3$$$.
Java 8
standard input
[ "greedy", "constructive algorithms", "math", "brute force", "strings" ]
7e8baa4fb780f11e66bb2b7078e34c04
The first line contains two integers $$$n$$$ and $$$k$$$, separated by spaces ($$$1 \leq k \leq n \leq 100\,000$$$, $$$(k \bmod 2) = (n \bmod 2)$$$).
2,200
Print a string $$$s$$$ of length $$$n$$$, consisting of symbols "0" and "1". Minimal length of the unique substring of $$$s$$$ should be equal to $$$k$$$. You can find any suitable string. It is guaranteed, that there exists at least one such string.
standard output
PASSED
b00caac13450273d1d20e29421d9ca2e
train_003.jsonl
1557671700
Let $$$s$$$ be some string consisting of symbols "0" or "1". Let's call a string $$$t$$$ a substring of string $$$s$$$, if there exists such number $$$1 \leq l \leq |s| - |t| + 1$$$ that $$$t = s_l s_{l+1} \ldots s_{l + |t| - 1}$$$. Let's call a substring $$$t$$$ of string $$$s$$$ unique, if there exist only one such $$$l$$$. For example, let $$$s = $$$"1010111". A string $$$t = $$$"010" is an unique substring of $$$s$$$, because $$$l = 2$$$ is the only one suitable number. But, for example $$$t = $$$"10" isn't a unique substring of $$$s$$$, because $$$l = 1$$$ and $$$l = 3$$$ are suitable. And for example $$$t =$$$"00" at all isn't a substring of $$$s$$$, because there is no suitable $$$l$$$.Today Vasya solved the following problem at the informatics lesson: given a string consisting of symbols "0" and "1", the task is to find the length of its minimal unique substring. He has written a solution to this problem and wants to test it. He is asking you to help him.You are given $$$2$$$ positive integers $$$n$$$ and $$$k$$$, such that $$$(n \bmod 2) = (k \bmod 2)$$$, where $$$(x \bmod 2)$$$ is operation of taking remainder of $$$x$$$ by dividing on $$$2$$$. Find any string $$$s$$$ consisting of $$$n$$$ symbols "0" or "1", such that the length of its minimal unique substring is equal to $$$k$$$.
256 megabytes
import static java.lang.Math.*; import static java.util.Arrays.*; import java.util.*; import java.io.*; public class A { ArrayList<String> arr = new ArrayList<>() ; int n , k ; void brute(String s , int idx) { if(idx == n) { int min = 1 << 30 ; for(int i = 0 ; i < n ;i++) for(int j = i + 1; j <= n ; j++) { String sub = s.substring(i , j) ; int len = sub.length(); int cnt = 0 ; for(int l = 0 ; l + len <= n ; l++) if(s.substring(l , l + len).equals(sub)) cnt ++ ; if(cnt == 1) min = min(min, len) ; } if(min == k) arr.add(s) ; return; } brute(s+ "0" , idx + 1); brute(s+ "1" , idx + 1); } void main() throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out) ; n = sc.nextInt() ; k = sc.nextInt() ; // brute("" , 0); // System.out.println(arr); if(k == 1 || k == 2) { for(int i = 0 ; i < n - k; i++) out.print(0) ; while(k -->0) out.print(1) ; } else { int zeros = 0 ; for(int i = n - 2 ; i >= k ; i -= 2) zeros ++ ; StringBuilder sb = new StringBuilder() ; while(zeros -->0) sb.append(0) ; sb.append(1) ; int sz = sb.length() ; char [] c = sb.toString().toCharArray() ; for(int i = 0 ;i < n ; i++) out.print(c[i % sz]) ; } out.flush(); out.close(); } class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String path) throws Exception{ br = new BufferedReader((new FileReader(path))) ; } String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next());} } public static void main (String [] args) throws Exception {(new A()).main();} }
Java
["4 4", "5 3", "7 3"]
1 second
["1111", "01010", "1011011"]
NoteIn the first test, it's easy to see, that the only unique substring of string $$$s = $$$"1111" is all string $$$s$$$, which has length $$$4$$$.In the second test a string $$$s = $$$"01010" has minimal unique substring $$$t =$$$"101", which has length $$$3$$$.In the third test a string $$$s = $$$"1011011" has minimal unique substring $$$t =$$$"110", which has length $$$3$$$.
Java 8
standard input
[ "greedy", "constructive algorithms", "math", "brute force", "strings" ]
7e8baa4fb780f11e66bb2b7078e34c04
The first line contains two integers $$$n$$$ and $$$k$$$, separated by spaces ($$$1 \leq k \leq n \leq 100\,000$$$, $$$(k \bmod 2) = (n \bmod 2)$$$).
2,200
Print a string $$$s$$$ of length $$$n$$$, consisting of symbols "0" and "1". Minimal length of the unique substring of $$$s$$$ should be equal to $$$k$$$. You can find any suitable string. It is guaranteed, that there exists at least one such string.
standard output
PASSED
300a3e118b622ccc4ec704545d1c3371
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import static java.lang.Math.*; import java.util.*; public class E { public E () { sc.nextInt(); int K = sc.nextInt(); int [] A = sc.nextInts(); TreeMap<Integer, Integer> L = new TreeMap<>(); for (int k : req(K)) for (int a : A) { int x = k*a, y = INF; if (L.containsKey(x)) y = L.get(x); y = min(y, k); L.put(x, y); } int Q = sc.nextInt(); for (@SuppressWarnings("unused") int q : rep(Q)) { int T = sc.nextInt(); int res = INF; for (int x : L.keySet()) { int y = T - x; if (L.containsKey(y)) { int a = L.get(x); int b = L.get(y); res = min(res, a+b); } } if (res > K) res = -1; print(res); } } private static final int INF = (int) 1e9; private static int [] rep(int N) { return rep(0, N); } private static int [] rep(int S, int T) { if (T <= S) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; } private static int [] req(int N) { return req(0, N); } private static int [] req(int S, int T) { return rep(S, T+1); } //////////////////////////////////////////////////////////////////////////////////// private final static IOUtils.MyScanner sc = new IOUtils.MyScanner(); private static void print (Object o, Object ... A) { IOUtils.print(o, A); } private static class IOUtils { public static class MyScanner { public String next() { newLine(); return line[index++]; } public int nextInt() { return Integer.parseInt(next()); } public String nextLine() { line = null; return readLine(); } public String [] nextStrings() { return split(nextLine()); } public int [] nextInts() { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } ////////////////////////////////////////////// private boolean eol() { return index == line.length; } private String readLine() { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } private MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine() { if (line == null || eol()) { line = split(readLine()); index = 0; } } private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; } } private static String build(Object o, Object ... A) { return buildDelim(" ", o, A); } private static String buildDelim(String delim, Object o, Object ... A) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : A) append(b, p, delim); return b.substring(delim.length()); } ////////////////////////////////////////////////////////////////////////////////// private static void start() { if (t == 0) t = millis(); } private static void append(StringBuilder b, Object o, String delim) { if (o.getClass().isArray()) { int len = java.lang.reflect.Array.getLength(o); for (int i = 0; i < len; ++i) append(b, java.lang.reflect.Array.get(o, i), delim); } else if (o instanceof Iterable<?>) for (Object p : (Iterable<?>) o) append(b, p, delim); else { if (o instanceof Double) o = new java.text.DecimalFormat("#.############").format(o); b.append(delim).append(o); } } private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out); private static void print(Object o, Object ... A) { pw.println(build(o, A)); } private static void err(Object o, Object ... A) { System.err.println(build(o, A)); } private static void exit() { IOUtils.pw.close(); System.out.flush(); err("------------------"); err(IOUtils.time()); System.exit(0); } private static long t; private static long millis() { return System.currentTimeMillis(); } private static String time() { return "Time: " + (millis() - t) / 1000.0; } } public static void main (String[] args) { new E(); IOUtils.exit(); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
2faf313a695f493db65db6b119dcfbe8
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.BufferedReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Arrays; import java.io.InputStreamReader; import java.io.IOException; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Alejandro Lopez */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { byte[] best = new byte[(int) 2e8 + 1]; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; ++i) { a[i] = in.nextInt(); } Arrays.fill(best, (byte) 100); for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) { if ((long) a[i] * j < best.length && j < best[a[i] * j]) { best[a[i] * j] = (byte) j; } } } for (int q = in.nextInt(); q > 0; --q) { int x = in.nextInt(); int ans = best[x]; for (int i = 0; i < n; ++i) { for (int j = 1; j <= k && a[i] * j <= x; ++j) { if (x - a[i] * j == 0 || best[x - a[i] * j] + j <= k) { ans = Math.min(ans, j + best[x - a[i] * j]); } } } out.println(ans > k ? -1 : ans); } } } static class InputReader { BufferedReader in; StringTokenizer tokenizer = null; public InputReader(InputStream inputStream) { in = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (IOException e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
c8bc631ac18c4833741258ab48e4060e
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class ProblemE { private static final long INF = 123456789123456L; BufferedReader rd; private ProblemE() throws IOException { rd = new BufferedReader(new InputStreamReader(System.in)); compute(); } private void compute() throws IOException { long k = longarr()[1]; long[] a = longarr(); int q = pint(); for(int i=0;i<q;i++) { long x = plong(); long min = INF; for(long b: a) { for(int j=1;j<=k;j++) { long rest = x-b*j; if(rest == 0) { min = Math.min(min, j); } else if(rest > 0 && j<k) { for(int m=1;m<=k-j;m++) { if(rest % m == 0) { long y = rest/m; if(Arrays.binarySearch(a, y) >= 0) { min = Math.min(min, j + m); } } } } } } if(min == INF) { out(-1); } else { out(min); } } } private int pint() throws IOException { return pint(rd.readLine()); } private int pint(String s) { return Integer.parseInt(s); } private long plong() throws IOException { return plong(rd.readLine()); } private long plong(String s) { return Long.parseLong(s); } private long[] longarr() throws IOException { return longarr(rd.readLine()); } private long[] longarr(String s) { String[] q = split(s); int n = q.length; long[] a = new long[n]; for(int i=0;i<n;i++) { a[i] = Long.parseLong(q[i]); } return a; } private String[] split(String s) { int n = s.length(); int sp = 0; for(int i=0;i<n;i++) { if(s.charAt(i)==' ') { sp++; } } String[] res = new String[sp+1]; int last = 0; int x = 0; for(int i=0;i<n;i++) { char c = s.charAt(i); if(c == ' ') { res[x++] = s.substring(last,i); last = i+1; } } res[x] = s.substring(last,n); return res; } private static void out(Object x) { System.out.println(x); } public static void main(String[] args) throws IOException { new ProblemE(); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
c2fc21af9f1e384e628d3bb48f3dfaa5
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Map.Entry; import java.util.StringTokenizer; import java.util.TreeMap; public class ATM { public static void main(String[] args) { InputReader r = new InputReader(System.in); int n = r.nextInt(); int k = r.nextInt(); int[] arr = new int[n]; TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(); for (int i = 0; i < arr.length; i++) { arr[i] = r.nextInt(); for (int j = 0; j <= k; j++) { int x = arr[i] * j; if (map.containsKey(x)) { int v = map.get(x); if (v > j) { map.put(x, j); } } else { map.put(x, j); } } } int q = r.nextInt(); while (q-- > 0) { int x = r.nextInt(); int curr = 1 << 28; for (Entry<Integer, Integer> e : map.entrySet()) { int v = e.getKey(); int need = x - v; if (map.containsKey(need)) { int candidate = e.getValue() + map.get(need); if (candidate < curr) { curr = candidate; } } } if (curr == 1 << 28 || curr > k) System.out.println(-1); else System.out.println(curr); } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public InputReader(FileReader stream) { reader = new BufferedReader(stream); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
e44c139873bab1465a62b05d7f302325
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class E { BufferedReader reader; StringTokenizer tokenizer; PrintWriter out; public void solve() throws IOException { int N = nextInt(); int K = nextInt(); long[] A = new long[N]; for (int i = 0; i < N; i++) { A[i] = nextLong(); } HashSet<Long>[] set = new HashSet[K+1]; for (int k = 0; k <= K; k++) { set[k] = new HashSet<Long>(); } for (int i = 0; i < N; i++) { long sum = 0; for (int k = 1; k <= K; k++) { sum += A[i]; set[k].add(sum); } } int Q = nextInt(); int[] best = new int[Q]; Arrays.fill(best, 100); for (int q = 0; q < Q; q++) { long X = nextLong(); for (int k1 = 0; k1 <= K; k1++) { if (set[k1].contains(X)) { best[q] = Math.min(best[q], k1); } } for (int i = 0; i < N; i++) { for (int k = 1; k <= K; k++) { long sum = k * A[i]; for (int k1 = 0; k1 <= K-k; k1++) { if (set[k1].contains(X - sum)) { best[q] = Math.min(best[q], k + k1); } } } } } for (int q = 0; q < Q; q++) { out.println(best[q] == 100? -1: best[q]); } } /** * @param args */ public static void main(String[] args) { new E().run(); } public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; out = new PrintWriter(System.out); solve(); reader.close(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
1151b48d19cae2f31d456ba1128abeb0
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class E { void solve() throws IOException { int n=nextInt(); int k=nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++){ a[i]=nextInt(); } int q=nextInt(); for(int Q=0;Q<q;Q++){ int x=nextInt(); int min=-1; for(int i=0;i<n;i++){ for(int j=1;j<=k;j++){ long sum=a[i]*1l*j; if(sum>x)break; int y=x-(int)sum; if(y==0){ if(min==-1||min>j){ min=j; } } for(int h=1;h+j<=k;h++){ if(y%h!=0)continue; int z=y/h; int l=i+1; int r=n; while(l<r){ int m=(l+r)/2; if(z<=a[m]) r=m; else l=m+1; } if(r!=n&&a[r]==z){ if(min==-1||min>j+h){ min=j+h; } } } } } out.println(min); } } public static void main(String[] args) throws IOException { new E().run(); } void run() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); // reader = new BufferedReader(new FileReader("input.txt")); tokenizer = null; out = new PrintWriter(new OutputStreamWriter(System.out)); // out = new PrintWriter(new FileWriter("output.txt")); solve(); reader.close(); out.flush(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter out; int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
bde27e1c1039e2003c936f1017a7b91f
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class TaskA { public static void main(String args[] ) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter w = new PrintWriter(System.out); StringTokenizer st1 = new StringTokenizer(br.readLine()); int n = ip(st1.nextToken()); int k = ip(st1.nextToken()); StringTokenizer st2 = new StringTokenizer(br.readLine()); int a[][] = new int[k+1][n]; for(int i=0;i<n;i++){ a[1][i] = ip(st2.nextToken()); for(int j=2;j<=k;j++) a[j][i] = j * a[1][i]; } int Q = ip(br.readLine()); for(int q = 0;q<Q;q++){ int x = ip(br.readLine()); int ans = Integer.MAX_VALUE; for(int i=0;i<n;i++) if(x % a[1][i]== 0) ans = Math.min(x/a[1][i],ans); for(int i=0;i<n;i++){ for(int dis=1;dis<=k;dis++){ for(int other=1;other<=k-dis;other++){ int here = dis*a[1][i]; int needed = x - here; int search = Arrays.binarySearch(a[other], needed); if( search >=0 && search < n) ans = Math.min(ans, dis+other); } } } w.println(ans > k ? -1 : ans); } w.close(); } public static int ip(String s){ return Integer.parseInt(s); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
77b2c5783483783a99f8359e8334444e
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.io.*; import java.util.*; public class vkE { static InputReader in = new InputReader(); static String str; static String[] arr; public static void main(String[] args) throws Exception { str = in.readLine(); int n = Integer.parseInt(str.substring(0, str.indexOf(" "))); int k = Integer.parseInt(str.substring(str.indexOf(" ") + 1)); arr = in.readLine().split(" "); int[] a = new int[arr.length]; for(int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(arr[i]); } HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); map.put(0, 0); for(int i = 0; i < n; i++) { for(int j = 1; j <= 20; j++) { if(map.containsKey(a[i] * j)) { map.put(a[i] * j, Math.min(j, map.get(a[i] * j))); } else { map.put(a[i] * j, j); } } } int q = Integer.parseInt(in.readLine()); for(int i = 0; i < q; i++) { int ans = Integer.parseInt(in.readLine()); int min = Integer.MAX_VALUE >> 1; for(int j : map.keySet()) { if(map.containsKey(ans - j)) { min = Math.min(map.get(j) + map.get(ans - j), min); } } System.out.println(min > k ? -1 : min); } } static class Pair { int a, b; public Pair(int tempa, int tempb) { a = tempa; b = tempb; } } static class InputReader { BufferedReader br; public InputReader() { try { br = new BufferedReader(new FileReader("vkE.in")); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String readLine() throws Exception { return br.readLine(); } } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
bbf60af5c36e0224fe63244e14f8399e
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.util.*; public class e { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(), k = input.nextInt(); boolean[] have = new boolean[10000001]; int[] vals = new int[n]; for(int i = 0; i<n; i++) have[vals[i] = input.nextInt()] = true; int Q = input.nextInt(); for(int q = 0; q<Q; q++) { int x = input.nextInt(); int min = k+1; for(int i = 0; i<n; i++) for(int j = 0; j<k; j++) { int need = x - j * vals[i]; if(need < 0) continue; for(int l = 1; l<=k - j; l++) { if(need%l == 0 && need/l < have.length && have[need/l]) min = Math.min(min, j+l); } } System.out.println(min > k ? -1 : min); } } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
89cb1150373f6b1646b4e816b5d74e42
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class E { static void solve() throws IOException { int n = nextInt(); int k = nextInt(); int[] a = new int[n]; for ( int i = 0; i < n; i ++ ) { a[i] = nextInt(); } Arrays.sort( a ); final int[][] s = new int[k + 1][n]; for ( int t = 0; t <= k; t ++ ) { for ( int i = 0; i < n; i ++ ) { s[t][i] = a[i] * t; } } int q = nextInt(); LOOP: for ( int qq = 0; qq < q; qq ++ ) { int x = nextInt(); for ( int ij = 0; ij <= k; ij ++ ) { for ( int j = 0; j <= ij; j ++ ) { int i = ij - j; if ( find( s[i], s[j], x ) ) { out.println( ij ); continue LOOP; } } } out.println( -1 ); } } private static boolean find( int[] a, int[] b, int x ) { for ( int i = 0, j = b.length - 1; i < a.length && j >= 0; ) { if ( a[i] + b[j] == x ) { return true; } else if ( a[i] + b[j] < x ) { i ++; } else { j --; } } return false; } static StreamTokenizer in; static PrintWriter out; static int nextInt() throws IOException { in.nextToken(); return ( int ) in.nval; } public static void main( String[] args ) throws IOException { in = new StreamTokenizer( new InputStreamReader( System.in ) ); out = new PrintWriter( System.out ); solve(); out.close(); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
4316d8e03d8055725ef350ae7b52928a
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.util.*; import java.io.*; public class E { public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); new E(new FastScanner(), out); out.close(); } public E(FastScanner in, PrintWriter out) { int N = in.nextInt(); int K = in.nextInt(); int[] bills = new int[N]; for (int i=0; i<N; i++) bills[i] = in.nextInt(); int Q = in.nextInt(); int[] queries = new int[Q]; for (int i=0; i<Q; i++) queries[i] = in.nextInt(); int[] res = new int[Q]; Arrays.fill(res, K+1); HashMap<Integer, Integer> mmp = new HashMap<Integer, Integer>(); for (int i=0; i<N; i++) { int a = bills[i]; int sum = a; for (int k=1; k<=K; k++) { for (int q=0; q<Q; q++) { int target = queries[q] - sum; if (target < 0) continue; Integer cc = mmp.get(target); if (cc != null) res[q] = Math.min(res[q], cc+k); } Integer cc = mmp.get(sum); if (cc == null || cc.intValue() > k) mmp.put(sum, k); sum += a; } for (int q=0; q<Q; q++) { int target = queries[q]; if (target % a == 0) res[q] = Math.min(res[q], target / a); } } for (int q=0; q<Q; q++) out.println(res[q] > K ? -1 : res[q]); } } class FastScanner{ private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner() { stream = System.in; } 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++]; } boolean isSpaceChar(int c) { return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } boolean isEndline(int c) { return c=='\n'||c=='\r'||c==-1; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } 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(); } String nextLine(){ int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do{ res.appendCodePoint(c); c = read(); }while(!isEndline(c)); return res.toString(); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
9a69c688aaf4ba0dd697ce356697d530
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import static java.lang.Math.*; import java.util.*; import java.io.*; public class E { public void solve() throws Exception { int n = nextInt(), k = nextInt(); int[] types = nextArr(n); TreeMap<Long, Integer> m = new TreeMap<Long, Integer>(); for (int i=0; i<n; ++i) for (int j=0; j<=k; ++j) { long s = (long)types[i] * j; if (!m.containsKey(s) || m.get(s) > j) { m.put(s, j); } } int q = nextInt(); while (q-->0) { int c = nextInt(); int best = k+1; for (int i=0; i<n; ++i) if (types[i] <= c) { long x = 0; for (int j=1; j<=k; ++j) { x += types[i]; if (x == c && j < best) { best = j; } if (x < c) { long need = c - x; if (m.containsKey(need)) { int j2 = m.get(need); if (j+j2 < best) best = j+j2; } } } } println(best > k ? -1 : best); } } static class PairII implements Comparable<PairII> { public final int x,y; public PairII(int x, int y) { this.x = x; this.y = y; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + y; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; PairII other = (PairII) obj; return x == other.x && y == other.y; } @Override public int compareTo(PairII o) { if (x == o.x) { if (y == o.y) return 0; return y > o.y ? 1 : -1; } return x > o.x ? 1 : -1; } @Override public String toString() { return "(" + Integer.toString(x) + "," + Integer.toString(y) + ")"; } } //////////////////////////////////////////////////////////////////////////// boolean showDebug = true; static boolean useFiles = false; static String inFile = "input.txt"; static String outFile = "output.txt"; double EPS = 1e-7; int INF = Integer.MAX_VALUE; long INFL = Long.MAX_VALUE; double INFD = Double.MAX_VALUE; int absPos(int num) { return num<0 ? 0:num; } long absPos(long num) { return num<0 ? 0:num; } double absPos(double num) { return num<0 ? 0:num; } int min(int... nums) { int r = nums[0]; for (int i=1; i<nums.length; ++i) if (nums[i]<r) r=nums[i]; return r; } int max(int... nums) { int r = nums[0]; for (int i=1; i<nums.length; ++i) if (nums[i]>r) r=nums[i]; return r; } long minL(long... nums) { long r = nums[0]; for (int i=1; i<nums.length; ++i) if (nums[i]<r) r=nums[i]; return r; } long maxL(long... nums) { long r = nums[0]; for (int i=1; i<nums.length; ++i) if (nums[i]>r) r=nums[i]; return r; } double minD(double... nums) { double r = nums[0]; for (int i=1; i<nums.length; ++i) if (nums[i]<r) r=nums[i]; return r; } double maxD(double... nums) { double r = nums[0]; for (int i=1; i<nums.length; ++i) if (nums[i]>r) r=nums[i]; return r; } long sumArr(int[] arr) { long res = 0; for (int i=0; i<arr.length; ++i) res+=arr[i]; return res; } long sumArr(long[] arr) { long res = 0; for (int i=0; i<arr.length; ++i) res+=arr[i]; return res; } double sumArr(double[] arr) { double res = 0; for (int i=0; i<arr.length; ++i) res+=arr[i]; return res; } long partsFitCnt(long partSize, long wholeSize) { return (partSize+wholeSize-1)/partSize; } boolean odd(long num) { return (num&1)==1; } boolean hasBit(int num, int pos) { return (num&(1<<pos))>0; } boolean isLetter(char c) { return (c>='a' && c<='z') || (c>='A' && c<='Z'); } boolean isLowercase(char c) { return (c>='a' && c<='z'); } boolean isUppercase(char c) { return (c>='A' && c<='Z'); } boolean isDigit(char c) { return (c>='0' && c<='9'); } boolean charIn(String chars, String s) { if (s==null) return false; if (chars==null || chars.equals("")) return true; for (int i=0; i<s.length(); ++i) for (int j=0; j<chars.length(); ++j) if (chars.charAt(j)==s.charAt(i)) return true; return false; } String stringn(String s, int n) { if (n<1 || s==null) return ""; StringBuilder sb = new StringBuilder(s.length()*n); for (int i=0; i<n; ++i) sb.append(s); return sb.toString(); } String str(Object o) { if (o==null) return ""; return o.toString(); } long timer = System.currentTimeMillis(); void startTimer() { timer = System.currentTimeMillis(); } void stopTimer() { System.err.println("time: "+(System.currentTimeMillis()-timer)/1000.0); } static class InputReader { private byte[] buf; private int bufPos = 0, bufLim = -1; private InputStream stream; public InputReader(InputStream stream, int size) { buf = new byte[size]; this.stream = stream; } private void fillBuf() throws IOException { bufLim = stream.read(buf); bufPos = 0; } char read() throws IOException { if (bufPos>=bufLim) fillBuf(); return (char)buf[bufPos++]; } boolean hasInput() throws IOException { if (bufPos>=bufLim) fillBuf(); return bufPos<bufLim; } } static InputReader inputReader; static BufferedWriter outputWriter; char nextChar() throws IOException { return inputReader.read(); } char nextNonWhitespaceChar() throws IOException { char c = inputReader.read(); while (c<=' ') c=inputReader.read(); return c; } String nextWord() throws IOException { StringBuilder sb = new StringBuilder(); char c = inputReader.read(); while (c<=' ') c=inputReader.read(); while (c>' ') { sb.append(c); c = inputReader.read(); } return new String(sb); } String nextLine() throws IOException { StringBuilder sb = new StringBuilder(); char c = inputReader.read(); while (c<=' ') c=inputReader.read(); while (c!='\n' && c!='\r') { sb.append(c); c = inputReader.read(); } return new String(sb); } int nextInt() throws IOException { int r = 0; char c = nextNonWhitespaceChar(); boolean neg = false; if (c=='-') neg=true; else r=c-48; c = nextChar(); while (c>='0' && c<='9') { r*=10; r+=c-48; c=nextChar(); } return neg ? -r:r; } long nextLong() throws IOException { long r = 0; char c = nextNonWhitespaceChar(); boolean neg = false; if (c=='-') neg=true; else r = c-48; c = nextChar(); while (c>='0' && c<='9') { r*=10L; r+=c-48L; c=nextChar(); } return neg ? -r:r; } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextWord()); } int[] nextArr(int size) throws NumberFormatException, IOException { int[] arr = new int[size]; for (int i=0; i<size; ++i) arr[i] = nextInt(); return arr; } long[] nextArrL(int size) throws NumberFormatException, IOException { long[] arr = new long[size]; for (int i=0; i<size; ++i) arr[i] = nextLong(); return arr; } double[] nextArrD(int size) throws NumberFormatException, IOException { double[] arr = new double[size]; for (int i=0; i<size; ++i) arr[i] = nextDouble(); return arr; } String[] nextArrS(int size) throws NumberFormatException, IOException { String[] arr = new String[size]; for (int i=0; i<size; ++i) arr[i] = nextWord(); return arr; } char[] nextArrCh(int size) throws IOException { char[] arr = new char[size]; for (int i=0; i<size; ++i) arr[i] = nextNonWhitespaceChar(); return arr; } char[][] nextArrCh(int rows, int columns) throws IOException { char[][] arr = new char[rows][columns]; for (int i=0; i<rows; ++i) for (int j=0; j<columns; ++j) arr[i][j] = nextNonWhitespaceChar(); return arr; } char[][] nextArrChBorders(int rows, int columns, char border) throws IOException { char[][] arr = new char[rows+2][columns+2]; for (int i=1; i<=rows; ++i) for (int j=1; j<=columns; ++j) arr[i][j] = nextNonWhitespaceChar(); for (int i=0; i<columns+2; ++i) { arr[0][i] = border; arr[rows+1][i] = border; } for (int i=0; i<rows+2; ++i) { arr[i][0] = border; arr[i][columns+1] = border; } return arr; } void printf(String format, Object... args) throws IOException { outputWriter.write(String.format(format, args)); } void print(Object o) throws IOException { outputWriter.write(o.toString()); } void println(Object o) throws IOException { outputWriter.write(o.toString()); outputWriter.newLine(); } void print(Object... o) throws IOException { for (int i=0; i<o.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(o[i].toString()); } } void println(Object... o) throws IOException { print(o); outputWriter.newLine(); } void printn(Object o, int n) throws IOException { String s = o.toString(); for (int i=0; i<n; ++i) { outputWriter.write(s); if (i!=n-1) outputWriter.write(' '); } } void printnln(Object o, int n) throws IOException { printn(o, n); outputWriter.newLine(); } void printArr(int[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(Integer.toString(arr[i])); } } void printArr(long[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(Long.toString(arr[i])); } } void printArr(double[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(Double.toString(arr[i])); } } void printArr(String[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(arr[i]); } } void printlnArr(int[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(long[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(double[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(String[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void halt(Object... o) throws IOException { if (o.length!=0) println(o); outputWriter.flush(); outputWriter.close(); System.exit(0); } void debug(Object... o) { if (showDebug) System.err.println(Arrays.deepToString(o)); } public static void main(String[] args) throws Exception { Locale.setDefault(Locale.US); if (!useFiles) { inputReader = new InputReader(System.in, 1<<16); outputWriter = new BufferedWriter(new OutputStreamWriter(System.out), 1<<16); } else { inputReader = new InputReader(new FileInputStream(new File(inFile)), 1<<16); outputWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outFile))), 1<<16); } new E().solve(); outputWriter.flush(); outputWriter.close(); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
cdbbe07277f9c3cc19c046539a781274
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.PrintStream; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.StringTokenizer; import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction; /* public class _529E { } */ public class _529E { public void solve() throws FileNotFoundException { InputStream inputStream = System.in; InputHelper inputHelper = new InputHelper(inputStream); PrintStream out = System.out; //actual solution int n = inputHelper.readInteger(); int k = inputHelper.readInteger(); HashSet<Integer> s = new HashSet<Integer>(); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = inputHelper.readInteger(); s.add(a[i]); } int q = inputHelper.readInteger(); for(int i = 0; i < q; i++) { int x = inputHelper.readInteger(); if(n == 1) { if(x % a[0] == 0 && x / a[0] <= k) { System.out.println(x / a[0]); } else { System.out.println(-1); } continue; } int ans = Integer.MAX_VALUE; for(int j = n - 1; j >= 0; j--) { for(int k1 = 0; k1 <= k; k1++) { for(int k2 = 0; k2 <= k; k2++) { if(k1 + k2 <= k) { long fp = (long)k1 * a[j]; if(fp > x) continue; if(fp == x) { ans = Math.min(ans, k1); continue; } long rp = x - fp; if(k2 != 0 && rp % k2 == 0 && s.contains((int)rp / k2)) { ans = Math.min(ans, k1 + k2); } } } } } if(ans == Integer.MAX_VALUE) ans = -1; System.out.println(ans); } //end here } private int solve(int db, int ds, int x, int k) { int ans = Integer.MAX_VALUE; for(int i = Math.min(k, x / db); i >= 0; i--) { int dbp = i * db; int dsp = (x - dbp) / ds; if(i * db + dsp * ds == x) { if(i + dsp <= k) { ans = i + dsp; return ans; } } } return ans; /*int mod = x % db; if(mod % ds == 0) return x / db + mod / ds; int mod2 = mod % ds; int diff = ds - mod2; int mod3 = db % ds; if(mod3 == 0 || diff % mod3 != 0) return Integer.MAX_VALUE; if(diff / mod3 > maxdbp) return Integer.MAX_VALUE; int dbp = maxdbp - diff / mod3; int dsp = (x - dbp * db) / ds; return dsp + dbp;*/ } public static void main(String[] args) throws FileNotFoundException { (new _529E()).solve(); } class InputHelper { StringTokenizer tokenizer = null; private BufferedReader bufferedReader; public InputHelper(InputStream inputStream) { InputStreamReader inputStreamReader = new InputStreamReader( inputStream); bufferedReader = new BufferedReader(inputStreamReader, 16384); } public String read() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { String line = bufferedReader.readLine(); if (line == null) { return null; } tokenizer = new StringTokenizer(line); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } public Integer readInteger() { return Integer.parseInt(read()); } public Long readLong() { return Long.parseLong(read()); } } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
51830e307deb715c91d7e2a8b1a841c7
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.util.HashSet; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top * @author Artem Gilmudinov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Reader in = new Reader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } } class TaskE { public void solve(int testNumber, Reader in, PrintWriter out) { int n, k; n = in.ni(); k = in.ni(); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = in.ni(); } HashSet<Integer> set = new HashSet<Integer>(); for(int i = 0; i < n; i++) { set.add(a[i]); } int q = in.ni(); for(int i = 0; i < q; i++) { int s = in.ni(); boolean found = false; A: for(int j = 1; j <= k; j++) { for(int z = 1; z <= j; z++) { int left = j - z; for(int p = 0; p < n; p++) { if(left == 0) { if(j * a[p] == s) { out.println(j); found = true; break A; } } else { int diff = s - z * a[p]; if(diff >= 0) { if(diff % left == 0 && set.contains(diff / left)) { out.println(j); found = true; break A; } } } } } } if(!found) { out.println(-1); } } } } class Reader { private BufferedReader in; private StringTokenizer st = new StringTokenizer(""); private String delim = " "; public Reader(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); } public String next() { if (!st.hasMoreTokens()) { st = new StringTokenizer(rl()); } return st.nextToken(delim); } public String rl() { try { return in.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } public int ni() { return Integer.parseInt(next()); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
ce59192bf9600bfa0fa62516c4380e34
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
//package vk2015.r1; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; public class E2 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), K = ni(); int[] a = na(n); int[][] as = new int[n*K][]; int p = 0; for(int i = 0;i < n;i++){ for(int k = 1;k <= K;k++){ as[p++] = new int[]{a[i]*k, k}; } } Arrays.sort(as, new Comparator<int[]>() { public int compare(int[] a, int[] b) { if(a[0] != b[0])return a[0] - b[0]; return a[1] - b[1]; } }); p = 0; for(int i = 0;i < n*K;i++){ if(p == 0 || as[i][0] != as[i-1][0]){ as[p++] = as[i]; } } int[] vals = new int[p]; for(int i = 0;i < p;i++)vals[i] = as[i][0]; for(int Q = ni();Q >= 1;Q--){ int num = 999999999; int x = ni(); for(int i = n-1;i >= 0;i--){ if(a[i] * K < x)continue; if(x % a[i] == 0){ num = Math.min(num, x/a[i]); }else{ for(int k = 1;k <= K;k++){ int y = x - k*a[i]; int ind = Arrays.binarySearch(vals, y); if(ind >= 0 && as[ind][1] + k <= K){ num = Math.min(num, as[ind][1] + k); } } } } out.println(num == 999999999 ? -1 : num); } } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E2().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
1299c4a410cfe2acc78308854c3054f5
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.util.HashSet; import java.util.Scanner; import java.util.Vector; public class E { public static final boolean DEBUG = false; Scanner sc; public void debug(Object o) { if (DEBUG) { int ln = Thread.currentThread().getStackTrace()[2].getLineNumber(); String fn = Thread.currentThread().getStackTrace()[2].getFileName(); System.out.println("(" + fn + ":" + ln + "): " + o); } } public void pln(Object o) { System.out.println(o); } public void run() { sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); HashSet<Integer> h = new HashSet<Integer>(); Vector<Integer> v = new Vector<Integer>(); for (int i=0; i<n; i++) { int val = sc.nextInt(); h.add(val); v.add(val); } int q = sc.nextInt(); for (int idx=0; idx<q; idx++) { int t = sc.nextInt(); int best = 100; for (int i=0; i<n; i++) { for (int nr1=1; nr1<=k; nr1++) { int crt = nr1 * v.get(i); if (crt > t) { continue; } else if (crt == t) { if (nr1 < best) best = nr1; } else for (int nr2=1; nr2<=k-nr1; nr2++) if ((t-crt) % nr2 == 0) if (h.contains((t-crt) / nr2)) if (nr1 + nr2 < best) best = nr1 + nr2; } } if (best == 100) pln(-1); else pln(best); } } public static void main(String[] args) { E t = new E(); t.run(); } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
fa9bacda84a1bfd0fcb7f70cf52777ce
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.util.InputMismatchException; import java.io.InputStream; import java.util.HashMap; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; /** * Built using CHelper plug-in * Actual solution is at the top * @author George Marcus */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } } class TaskE { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int K = in.nextInt(); int[] A = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } HashMap<Integer, Integer> numBills = new HashMap<Integer, Integer>(); for (int i = 0; i < N; i++) { int a = 0; for (int j = 1; j <= K; j++) { a += A[i]; if (numBills.containsKey(a)) { numBills.put(a, Math.min(numBills.get(a), j)); } else { numBills.put(a, j); } } } int Q = in.nextInt(); while (Q-- > 0) { int s = in.nextInt(); int ans = Integer.MAX_VALUE; for (int i = 0; i < N; i++) { int t = s; for (int j = 0; j <= K; j++) { if (numBills.containsKey(t)) { int rem = numBills.get(t); if (j + rem <= K) { ans = Math.min(ans, j + rem); } } t -= A[i]; } } if (ans == Integer.MAX_VALUE) { ans = -1; } out.println(ans); } } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(nextString()); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
409293b1888e4fe85610633b16b21f8c
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.util.HashSet; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.math.BigInteger; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.Set; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top * @author shu_mj @ http://shu-mj.com */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } } class TaskE { Scanner in; PrintWriter out; public void solve(int testNumber, Scanner in, PrintWriter out) { this.in = in; this.out = out; run(); } void run() { int n = in.nextInt(); int k = in.nextInt(); int[] is = in.nextIntArray(n); Set<Integer> set = new HashSet<Integer>(); for (int i : is) set.add(i); int q = in.nextInt(); while (q-- != 0) { int x = in.nextInt(); int res = k + 1; for (int i = 0; i < n; i++) { if (x % is[i] == 0) res = Math.min(res, x / is[i]); for (int a = 0; a <= k; a++) { for (int b = 1; b <= k; b++) { if (a + b > k) break; int z = x - a * is[i]; if (z >= 0 && z % b == 0 && set.contains(z / b)) { res = Math.min(res, a + b); } } } } out.println(res > k ? -1 : res); } } } class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } private void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextIntArray(int n) { int[] is = new int[n]; for (int i = 0; i < n; i++) { is[i] = nextInt(); } return is; } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
aff29be5a5086c22387c403fee18d229
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.util.*; public class E { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); Map<Long, Integer> M = new HashMap<Long, Integer>(); long[] A = new long[n]; for(int i=0; i<n; i++) { A[i] = in.nextLong(); } for(int i=0; i<n; i++) { for(int j=0; j<=k; j++) { long val = A[i]*j; if(M.containsKey(val)) { M.put(val, Math.min(M.get(val), j)); } else { M.put(val, j); } } } int q = in.nextInt(); for(int i=0; i<q; i++) { long want = in.nextLong(); long ans = 1000; for(Long key : M.keySet()) { if(M.containsKey(want-key)) { ans = Math.min(ans, M.get(key) + M.get(want-key)); } } System.out.println(ans <= k ? ans : -1); } } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
788ef96bd71644273dd7d9d09925ff80
train_003.jsonl
1426946400
ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; public class E { static InputStreamReader isr = new InputStreamReader(System.in); static BufferedReader br = new BufferedReader(isr); static int[] readIntArray() throws IOException { String[] v = br.readLine().split(" "); int[] ans = new int[v.length]; for (int i = 0; i < ans.length; i++) { ans[i] = Integer.valueOf(v[i]); } return ans; } static int n,k,q; static int[] bills; static HashMap<Long, Integer> poss = new HashMap<Long, Integer>(); static List<Long> posslist = new ArrayList<Long>(); public static void main(String[] args) throws IOException { int[] l = readIntArray(); n = l[0]; k = l[1]; bills = readIntArray(); q = readIntArray()[0]; for (int i = 0; i <= 20; i++) { for (int j = 0; j < bills.length; j++) { long t = ((long)i) * bills[j]; if (!poss.containsKey(t)) { poss.put(t, i); } else { poss.put(t, Math.min(i, poss.get(t))); } } } for (int i = 0; i < q; i++) { int ans = solve(readIntArray()[0]); System.out.println(ans); } } private static int solve(int q2) { int ans = Integer.MAX_VALUE; for(Long t : poss.keySet()) { if (q2 < t) { continue; } long rem = q2 - t; if (poss.containsKey(rem)) { int totalk = poss.get(t) + poss.get(rem); if (totalk <= k) { ans = Math.min(ans, totalk); } } } return ans == Integer.MAX_VALUE ? -1 : ans; } static long lcm(int a, int b) { return (((long)a)*b) / gcd(a,b); } static long gcd(int a, int b) { if (a < b) { return gcd(b, a); } while(a % b != 0) { int t = a % b; a = b; b = t; } return b; } }
Java
["6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950", "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15"]
2 seconds
["6\n20\n19\n20\n-1\n3\n-1\n-1", "1\n1\n1\n2\n2\n2\n2\n-1"]
null
Java 7
standard input
[ "brute force" ]
5d8521e467cad53cf9403200e4c99b89
The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20). The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order. The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make. The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.
1,900
For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.
standard output
PASSED
129a7c2520870928ecc34fffb565cebb
train_003.jsonl
1603548300
Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. A square of size $$$n \times n$$$ is called prime if the following three conditions are held simultaneously: all numbers on the square are non-negative integers not exceeding $$$10^5$$$; there are no prime numbers in the square; sums of integers in each row and each column are prime numbers. Sasha has an integer $$$n$$$. He asks you to find any prime square of size $$$n \times n$$$. Sasha is absolutely sure such squares exist, so just help him!
256 megabytes
import java.util.*; import java.io.*; public class CodeForcesB { public static void main(String[] args)throws IOException { Scanner sc=new Scanner(System.in); // Scanner sc=new Scanner(new File("../../ip.txt")); int tc,n,i,j,a[][]; tc=sc.nextInt(); while(tc-->0) { n=sc.nextInt(); a=new int[n][n]; for(i=0;i<n-1;i+=2) a[i][i/2]=a[i+1][i/2]=a[i][n-1-i/2]=a[i+1][n-1-i/2]=1; if(n%2==1) { a[n-1][n/2]=a[n-1][n/2-1]=a[n-1][n/2+1]=a[n-2][n/2]=1; } prarray2(a); } } public static void prarray2(int ar[][]) { for(int i=0;i<ar.length;i++) { for(int j=0;j<ar.length;j++) System.out.print(ar[i][j]+" "); System.out.println(); } } }
Java
["2\n4\n2"]
1.5 seconds
["4 6 8 1\n4 9 9 9\n4 10 10 65\n1 4 4 4\n1 1\n1 1"]
null
Java 8
standard input
[ "constructive algorithms", "math" ]
df6ee0d8bb25dc2040adf1f115f4a83b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required size of a square.
900
For each test case print $$$n$$$ lines, each containing $$$n$$$ integers — the prime square you built. If there are multiple answers, print any.
standard output
PASSED
0d728c6a704fc768e107d4fd1b652153
train_003.jsonl
1603548300
Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. A square of size $$$n \times n$$$ is called prime if the following three conditions are held simultaneously: all numbers on the square are non-negative integers not exceeding $$$10^5$$$; there are no prime numbers in the square; sums of integers in each row and each column are prime numbers. Sasha has an integer $$$n$$$. He asks you to find any prime square of size $$$n \times n$$$. Sasha is absolutely sure such squares exist, so just help him!
256 megabytes
import java.util.*; import java.util.*; import java.io.*; import java.lang.*; import java.math.BigInteger; public class A { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); sieve(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int a[][] = new int[n][n]; int x = n - 1; HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < 1000; i++) { if (prime[i] == 0) { set.add(i); } } set.add(1); int req = 1; while (true) { if (!set.contains(req) && set.contains(x + req)) { break; } req++; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { a[i][j] = req; } else { a[i][j] = 1; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } } static boolean check(long n) { int sum = 0; while (n > 0) { sum += n % 10; n /= 10; } if (sum % 3 == 0) return true; else { return false; } } static void sort(long[] a) { ArrayList<Long> aa = new ArrayList<>(); for (long i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } static long modInverse(long fac, int p) { return pow(fac, p - 2); } static long nCr(int n, int r, int p) { if (r == 0) return 1; fact[0] = 1; return (fact[n] * modInverse(fact[r], p) % p * modInverse(fact[n - r], p) % p) % p; } public static long[] fact; public static long[] invfact; public static long ncr(int n, int r) { if (r > n) return 0; return ((((fact[n]) * (invfact[r])) % mod) * invfact[n - r]) % mod; } static int mod = (int) 1e9 + 7; static long pow(long x, long y) { long res = 1l; while (y != 0) { if (y % 2 == 1) { res = x * res; } y /= 2; x = x * x; } return res; } static int prime[] = new int[1000]; static void sieve() { Arrays.fill(prime, 0); prime[0] = 1; prime[1] = 1; for (int i = 2; i * i < 1000; i++) { if (prime[i] == 0) { for (int j = i * i; j < 1000; j += i) { prime[j] = 1; } } } } }
Java
["2\n4\n2"]
1.5 seconds
["4 6 8 1\n4 9 9 9\n4 10 10 65\n1 4 4 4\n1 1\n1 1"]
null
Java 8
standard input
[ "constructive algorithms", "math" ]
df6ee0d8bb25dc2040adf1f115f4a83b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required size of a square.
900
For each test case print $$$n$$$ lines, each containing $$$n$$$ integers — the prime square you built. If there are multiple answers, print any.
standard output
PASSED
2ccb3552746830fd8aaab371935e6b83
train_003.jsonl
1603548300
Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. A square of size $$$n \times n$$$ is called prime if the following three conditions are held simultaneously: all numbers on the square are non-negative integers not exceeding $$$10^5$$$; there are no prime numbers in the square; sums of integers in each row and each column are prime numbers. Sasha has an integer $$$n$$$. He asks you to find any prime square of size $$$n \times n$$$. Sasha is absolutely sure such squares exist, so just help him!
256 megabytes
import java.util.*; import java.util.*; import java.io.*; import java.lang.*; import java.math.BigInteger; public class A { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int a[][] = new int[n][n]; a[0][0] = 1; a[n - 1][n - 1] = 1; a[n - 1][0] = 1; a[0][n - 1] = 1; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { if (i == j) { a[i][j] = 1; a[n - 1 - i][j] = 1; } } } if (n % 2 != 0) { a[n / 2][0] += 1; a[n / 2][n - 1] += 1; a[0][n / 2] += 1; a[n - 1][n / 2] += 1; } if (n == 3) { a[n - 1][1] = 1; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } } static boolean check(long n) { int sum = 0; while (n > 0) { sum += n % 10; n /= 10; } if (sum % 3 == 0) return true; else { return false; } } static void sort(long[] a) { ArrayList<Long> aa = new ArrayList<>(); for (long i : a) { aa.add(i); } Collections.sort(aa); for (int i = 0; i < a.length; i++) a[i] = aa.get(i); } static long modInverse(long fac, int p) { return pow(fac, p - 2); } static long nCr(int n, int r, int p) { if (r == 0) return 1; fact[0] = 1; return (fact[n] * modInverse(fact[r], p) % p * modInverse(fact[n - r], p) % p) % p; } public static long[] fact; public static long[] invfact; public static long ncr(int n, int r) { if (r > n) return 0; return ((((fact[n]) * (invfact[r])) % mod) * invfact[n - r]) % mod; } static int mod = (int) 1e9 + 7; static long pow(long x, long y) { long res = 1l; while (y != 0) { if (y % 2 == 1) { res = x * res; } y /= 2; x = x * x; } return res; } }
Java
["2\n4\n2"]
1.5 seconds
["4 6 8 1\n4 9 9 9\n4 10 10 65\n1 4 4 4\n1 1\n1 1"]
null
Java 8
standard input
[ "constructive algorithms", "math" ]
df6ee0d8bb25dc2040adf1f115f4a83b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required size of a square.
900
For each test case print $$$n$$$ lines, each containing $$$n$$$ integers — the prime square you built. If there are multiple answers, print any.
standard output
PASSED
391ad2aaa4338e6ffc3d3649f14327e3
train_003.jsonl
1603548300
Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. A square of size $$$n \times n$$$ is called prime if the following three conditions are held simultaneously: all numbers on the square are non-negative integers not exceeding $$$10^5$$$; there are no prime numbers in the square; sums of integers in each row and each column are prime numbers. Sasha has an integer $$$n$$$. He asks you to find any prime square of size $$$n \times n$$$. Sasha is absolutely sure such squares exist, so just help him!
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public final class prime_square{ public static void main(String[] args) throws java.lang.Exception{ Reader r = new Reader(); r.init(System.in); int t = r.nextInt(); while(t-->0){ int n = r.nextInt(); int num =1; while(true){ if(isPrime(num+(n-1)) && !isPrime(num)) break; num++; } int[][] ans = new int[n][n]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(i==j) ans[i][j] = num; else ans[i][j] =1; System.out.print(ans[i][j]+" "); } System.out.println(); } } } static boolean isPrime(int n){ if(n==1) return false; for(int i=2;i<n;i++){ if(n%i==0){ return false; } } return true; } static int gcd(int a, int b){ if(a==0) return b; return gcd(b%a, a); } /** Class for buffered reading int and double values */ static class Reader { 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() ); } } }
Java
["2\n4\n2"]
1.5 seconds
["4 6 8 1\n4 9 9 9\n4 10 10 65\n1 4 4 4\n1 1\n1 1"]
null
Java 8
standard input
[ "constructive algorithms", "math" ]
df6ee0d8bb25dc2040adf1f115f4a83b
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required size of a square.
900
For each test case print $$$n$$$ lines, each containing $$$n$$$ integers — the prime square you built. If there are multiple answers, print any.
standard output