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 | f41fd365cac1aa9ae0681f51743b5467 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.util.Scanner;
public class A {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
int n = in.nextInt();
char[] c = new char[n];
String line = in.next();
for(int i = 0; i < n; i++) {
c[i] = line.charAt(i);
}
int ind1 = -1, ind2 = -1;
for(int i = 0; i < n; i++) {
if(c[i] != '.' && ind1 == -1) {
ind1 = i;
}
if(c[n-1-i] != '.' && ind2 == -1)
ind2 = n-i-1;
}
if(c[ind1] == 'R') {
int s = ind1 + 1;
int r = s + 1;
for(int i = ind1 + 1; i <= ind2; i++) {
if(c[i] == 'L') {
r = i;
break;
}
}
if(c[ind2] == 'R')
r = ind2 + 2;
System.out.println(s + " " + r);
}
else {
System.out.println((ind2 + 1) + " " + ind1);
}
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 27f84ee5812a3f0eaf93141d76dffb73 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class A {
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
int N = Integer.parseInt(br.readLine().trim());
char[] input = br.readLine().trim().toCharArray();
int count = 0;
for (int i = 0; i < input.length; i++) {
if (input[i] == 'R' || input[i] == 'L')
count++;
}
int rStart = -1;
int lStart = -1;
int rCount = 0;
int lCount = 0;
for (int i = 0; i < input.length; i++) {
if (input[i] == 'R') {
rCount++;
if (rStart == -1)
rStart = i + 1;
} else if (input[i] == 'L') {
lCount++;
if (lStart == -1)
lStart = i + 1;
}
}
if (lCount == 0) {
out.println((rStart) + " " + (rStart + count));
} else if (rCount == 0) {
out.println((lStart + count - 1) + " " + (lStart - 1));
} else {
out.println((rStart) + " " + (lStart - 1));
}
br.close();
out.close();
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | e7d330eed09f9c63525f114230d43dfa | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | //package n180;
import java.util.Scanner;
public class Snow {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt();
String str = s.next();
// System.out.println(str);
s.close();
int start = 0, end = 0;
for (int i = 0; i < n; i++) {
char c = str.charAt(i);
// if (c == '.' && i>0 && str.charAt(i-1) == 'R'){
// start = i;
// end = i+1;
// break;
// }else if (c == '.')
// continue;
//
// if (c == 'R' && start == 0){
// start = i+1;
// }else if (c == 'L' && str.charAt(i-1) == 'R'){
//// if (str.charAt(i+1) == '.'){
//// System.out.println("111");
// end = i;
// break;
//// }
// }else if (c == 'L' && start == 0){
// end = i+1;
// }
if (c == 'R'){
// System.out.println("right");
start = i+1;
int ind = i+1;
char g = str.charAt(ind);
while (g == 'R'){
ind++;
g = str.charAt(ind);
}
if (g == 'L'){
end = ind;
}else{
end = ind+1;
}
break;
}else if (c == 'L'){
end = i;
int ind = i+1;
char g = str.charAt(ind);
while (g == 'L'){
ind++;
g = str.charAt(ind);
}
if (g == '.'){
start = ind;
}
break;
}
}
System.out.println(start+" "+end);
}
}
/* .......
* .R.....
* .RL....
*
*
*/
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 65cbac3b64c9ef9b7507fd72fda1979f | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.util.Scanner;
public class AA {
public static void main(String[] args) {
Scanner in =new Scanner (System.in);
String a="";
int e=0;
int num=in.nextInt();
int s=0,t=0;
a=in.next();
for(int i=0;i<num;i++){
if(a.charAt(i)=='R'){
e=1;
break;
}
}
if(e==1){
for(int i=0;i<num;i++ ){
if(a.charAt(i)=='R'){
s=i+1;
break;
}
}
for(int i=num-1;i>=0;i-- ){
if(a.charAt(i)=='R'){
t=i+1;
break;
}
}
for(int i=0;i<num;i++){
if(a.charAt(i)=='L'){
e=0;
}
}
if(e==1)
t++;
}
else{
for(int i=0;i<num;i++ ){
if(a.charAt(i)=='L'){
t=i;
break;
}
}
for(int i=num-1;i>=0;i-- ){
if(a.charAt(i)=='L'){
s=i+1;
break;
}
}
}
System.out.print(s+" "+t);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | b6b8786884c1327e46231438e81bfcf2 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes |
import java.io.*;
import java.util.*;
/**
*
* @coder Altynbek Nurgaziyev
*/
public class C298A {
void solution() throws Exception {
int n = in.nextInt();
String s = "." + in.next() + ".";
ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i - 1) != s.charAt(i)) {
list.add(i);
}
}
if (s.contains("R")) {
out.println(list.get(0) + " " + list.get(1));
} else {
out.println((list.get(1) - 1) + " " + (list.get(0) - 1));
}
}
public static void main(String[] args) throws Exception {
new C298A().run();
}
Scanner in;
PrintWriter out;
void run() throws Exception {
in = new Scanner(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
solution();
out.flush();
}
class Scanner {
final BufferedReader br;
StringTokenizer st;
Scanner(InputStreamReader stream) {
br = new BufferedReader(stream);
}
String next() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
Integer nextInt() throws Exception {
return Integer.parseInt(next());
}
Long nextLong() throws Exception {
return Long.parseLong(next());
}
Double nextDouble() throws Exception {
return Double.parseDouble(next());
}
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 10c9d3db790d6c86f443cb1c7470ec98 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
public class A {
public static BufferedReader in;
public static PrintWriter out;
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
boolean showLineError = true;
if (showLineError) {
solve();
out.close();
} else {
try {
solve();
} catch (Exception e) {
} finally {
out.close();
}
}
}
static void debug(Object... os) {
out.println(Arrays.deepToString(os));
}
private static void solve() throws IOException {
in.readLine();
String path = in.readLine();
boolean hasR = path.indexOf('R')!=-1;
boolean hasL = path.indexOf('L')!=-1;
int s =-1;
int t =-1;
if(!hasL){
int index =0;
while(path.charAt(index)=='.')
index++;
s=index;
while(path.charAt(index)=='R')
index++;
t=index;
out.println((s+1)+" "+(t+1));
return;
}
if(!hasR){
int index =0;
while(path.charAt(index)=='.')
index++;
t=index-1;
while(path.charAt(index)=='L')
index++;
s=index-1;
out.println((s+1)+" "+(t+1));
return;
}
int index =0;
while(path.charAt(index)!='L')
index++;
t =index;
while(path.charAt(index)=='L')
index++;
s=index-1;
out.println((s+1)+" "+(t+1));
}
} | Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 9f525d76b43edeb785632adb7c74fcad | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class SnowFootprints {
public static void main(String [] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
String road = br.readLine();
int s = 0, t = 0;
while(road.charAt(s) == '.') s++;
if( road.charAt(s) == 'R' )
{
t = road.indexOf( 'L' );
if( t >= 0 )
s++;
else
{
s++;
t = road.indexOf('.', s)+1;
}
}
else {
int tmp = road.indexOf('.', s);
t = s;
s = tmp;
}
bw.write(s+" "+t);
bw.flush();
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 968c4b18567e9f9c086e3d58773cd6dd | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
myScanner.nextInt();
String s = myScanner.next();
if (s.matches(".+R+L+.+")) {
System.out.println((s.indexOf('R') + 1) + " "
+ (s.lastIndexOf('R') + 1));
} else if (s.matches(".+R+.+")) {
System.out.println((s.indexOf('R') + 1) + " "
+ (s.lastIndexOf('R') + 2));
} else if (s.matches(".+L+.+")) {
System.out.println((s.lastIndexOf('L') + 1) + " "
+ (s.indexOf('L')));
}
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | d689458f967dd6250274f1048c32fafa | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.StreamTokenizer;
//You can include standard Java language libraries that you want use.//
import java.util.*;
public class Task{
/* public static StreamTokenizer in;
public static PrintStream out;
public static String readString() throws IOException {
in.nextToken();
return in.sval;
}
public static double readDouble() throws IOException {
in.nextToken();
return in.nval;
}
public static int readInt() throws IOException {
in.nextToken();
return (int) in.nval;
}*/
int nod(int a, int b){
int ada=Math.max(a,b);
int adb=Math.min(a,b);
while (ada%adb!=0){
int curada=ada;
ada=adb;
adb=curada%adb;
}
return adb;
}
public void solution(String inputFilePath, String usrOutputFilePath ) throws IOException{
// in = new StreamTokenizer(new FileReader(inputFilePath) );
Scanner in = new Scanner(System.in);
// PrintStream out = new PrintStream(usrOutputFilePath);
int n=in.nextInt();
String s=in.nextLine();
s=in.nextLine();
int ss=-1;
int tt=-1;
for (int i = 0; i < n-1; i++) {
if (s.charAt(i)=='R')
{
ss=i;
break;
}
}
for (int i = 0; i < n-1; i++) {
if (s.charAt(i)=='L')
{
tt=i;
break;
}
}
if (ss==-1){ss=tt;}
if (tt==-1){
for (int i = n-1; i >=0; i--) {
if (s.charAt(i)=='R')
{
tt=i;
break;
}
}
tt+=2;}
System.out.print((ss+1)+" "+tt+"\n");
in.close();
return;
}
public static void main(String[] args) throws IOException {
Task test = new Task();
test.solution("in.txt", "out.txt");
}
} | Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 8153986a01234638644d90832f6a0af4 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
bf.readLine();
String road = bf.readLine();
int lastR=0,lastL=0,begin=0,end=0;
for(int i=0;i<road.length();i++){
if(road.charAt(i)=='R'){
if(begin==0){
begin = i + 1;
lastR = i + 1;
}else
lastR = i + 1;
}
if(road.charAt(i)=='L'){
if(end==0){
end = i;
lastL = i + 1;
}else
lastL = i + 1;
}
}
if(end==0)
end = lastR + 1;
if(begin==0)
begin = lastL;
System.out.println(begin + " " + end);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | a95dd60ac7ec0e9bcf5afbbe42ad9808 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
bf.readLine();
String road = bf.readLine();
int lastR=0,lastL=0,begin=0,end=0;
for(int i=0;i<road.length();i++){
if(road.charAt(i)=='R'){
if(begin==0){
begin = i + 1;
lastR = i + 1;
}else
lastR = i + 1;
}
if(road.charAt(i)=='L'){
if(end==0){
end = i;
lastL = i + 1;
}else
lastL = i + 1;
}
}
if(end==0)
end = lastR + 1;
if(begin==0)
begin = lastL;
System.out.println(begin + " " + end);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | e339923b69b3fa6a688218007a92337b | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
bf.readLine();
String road = bf.readLine();
int lastR=0,lastL=0,begin=0,end=0;
for(int i=0;i<road.length();i++){
if(road.charAt(i)=='R'){
if(begin==0){
begin = i + 1;
lastR = i + 1;
}else
lastR = i + 1;
}
if(road.charAt(i)=='L'){
if(end==0){
end = i;
lastL = i + 1;
}else
lastL = i + 1;
}
}
if(end==0)
end = lastR + 1;
if(begin==0)
begin = lastL;
System.out.println(begin + " " + end);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 7f92caacd2e1707d302e214cd65aa0bf | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
bf.readLine();
String road = bf.readLine();
int lastR=0,lastL=0,begin=0,end=0;
for(int i=0;i<road.length();i++){
if(road.charAt(i)=='R'){
if(begin==0){
begin = i + 1;
lastR = i + 1;
}else
lastR = i + 1;
}
if(road.charAt(i)=='L'){
if(end==0){
end = i;
lastL = i + 1;
}else
lastL = i + 1;
}
}
if(end==0)
end = lastR + 1;
if(begin==0)
begin = lastL;
System.out.println(begin + " " + end);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | ecaf8319ef0c371855ede7a3cd884f8c | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
bf.readLine();
String road = bf.readLine();
int lastR=0,lastL=0,begin=0,end=0;
for(int i=0;i<road.length();i++){
if(road.charAt(i)=='R'){
if(begin==0){
begin = i + 1;
lastR = i + 1;
}else
lastR = i + 1;
}
if(road.charAt(i)=='L'){
if(end==0){
end = i;
lastL = i + 1;
}else
lastL = i + 1;
}
}
if(end==0)
end = lastR + 1;
if(begin==0)
begin = lastL;
System.out.println(begin + " " + end);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 1256e6e6a92a31d372167ebf8a2b59d3 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
bf.readLine();
String road = bf.readLine();
int lastR=0,lastL=0,begin=0,end=0;
for(int i=0;i<road.length();i++){
if(road.charAt(i)=='R'){
if(begin==0){
begin = i + 1;
lastR = i + 1;
}else
lastR = i + 1;
}
if(road.charAt(i)=='L'){
if(end==0){
end = i;
lastL = i + 1;
}else
lastL = i + 1;
}
}
if(end==0)
end = lastR + 1;
if(begin==0)
begin = lastL;
System.out.println(begin + " " + end);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 597f20b40f0ea0990f8222f9762c0483 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanIn = new Scanner(System.in);
int s,t;
int n = Integer.valueOf(scanIn.nextLine());
char[] prints = scanIn.nextLine().toCharArray();
// System.out.println(n);
// for (char c:prints) {
// System.out.print(c);
// }
// System.out.println();
for (int i = 0; i < n; i++) {
if (prints[i] == '.') continue;
if (prints[i] == 'R') {
s = i+1;
for (int j = i+1; j < prints.length; j++) {
if (prints[j] == 'L') {
System.out.println(s+" "+(j));
return;
}
if (prints[j] == '.') {
System.out.println(s+" "+(j+1));
return;
}
}
}
if (prints[i] == 'L') {
t = i;
for (int j = i; j < prints.length; j++) {
if (prints[j] == '.') { System.out.println((j)+" "+t); return; }
}
}
}
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 9606af093fbf31cd8dfc2ecfa398c7ef | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes |
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static String helper(String s){
String res = s.indexOf("L")!=-1 ? (s.lastIndexOf("L")+1)+" "+(s.indexOf("L")):(s.indexOf("R")+1)+" "+(s.lastIndexOf("R")+2);
return res;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner s = new Scanner(System.in);
while(s.hasNext()){
s.nextInt();
String str = s.next();
if(str.contains("LR") || str.contains("RL"))
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='R' && str.charAt(i+1)=='L' || str.charAt(i)=='L' && str.charAt(i+1)=='R' ){
System.out.println((i+1)+" "+(i+2));
break;
}
}
else System.out.println(Main.helper(str));
}
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | f18992c857879b78713ad5430727a1de | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
public static void main(String[] argv) {
MainRun mainSolve = new MainRun();
mainSolve.run(0, 0);
}
}
class Task {
public void solve(InputReader in, PrintWriter out) {
int n = in.nextInt();
String path = in.next();
path = " " + path;
String myPath = path;
for (int s = 1; s <= n; s++) {
if (path.charAt(s) == '.')
continue;
int ind = s;
while (path.charAt(ind) == 'R')
ind++;
if (path.charAt(ind) == 'L')
ind--;
out.println(s + " " + ind);
return;
}
}
}
class MainRun {
void run(int inF, int outF) {
// inF = outF = 0;
String input = "input.txt";
String output = "output.txt";
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in;
PrintWriter out;
if (inF == 1)
in = new InputReader(input);
else
in = new InputReader(inputStream);
if (outF == 1)
out = getPrintWriter(output);
else
out = getPrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
public static PrintWriter getPrintWriter(OutputStream stream) {
return new PrintWriter(stream);
}
public static PrintWriter getPrintWriter(String f) {
try {
return new PrintWriter(new File(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
InputReader(String f) {
try {
reader = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
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 String getLine() {
try {
return (String)reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int a[] = new int[n + 2];
for (int i = 1; i <= n; i++)
a[i] = nextInt();
return a;
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 337e0f3cf5a1b9b0a01bc389e5814f0c | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | //package round180_div2;
import java.util.Scanner;
public class ProblemA {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int s = 0;
int t = 0;
sc.nextLine();
char[] road = sc.nextLine().toCharArray();
int i = 0;
int countR = 0;
int countL = 0;
boolean init = true;
char cur = road[i];
while((cur = road[++i]) == '.') {}
do {
if (init) {
s = i+1;
init = false;
}
if (cur == 'R') {
countR++;
} else {
countL++;
}
} while ((cur = road[++i]) != '.');
if (countR == 0) {
t = s - 1;
s = i;
} else if (countL == 0) {
t = i + 1;
} else {
t = s + countR - 1;
}
sc.close();
System.out.println(String.format("%d %d", s, t));
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 571fb1f8f8e0734a5264559f6a0cf13a | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader Bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(Bf.readLine());
String s = Bf.readLine();
int start = 0;
int flag = 0;
int x = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'R') {
start = i;
flag = 1;
break;
}
}
if (flag == 0) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'L') {
start = i; break;
}}
x = start;
while (s.charAt(x) == 'L') {
x++;
}
System.out.println(x + " " + (start));
} else {
x = start;
while (s.charAt(x) == 'R') {
x++;
}
if (s.charAt(x) == '.') {
x++;
}
System.out.println(start + 1 + " " + x);
}
}
} | Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 9b7fd640f6cf57e3ffeb4a116683fcfc | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Vaibhav Mittal
*/
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.readInt();
String s = in.readString();
int firstR = s.indexOf('R'), lastR = s.lastIndexOf('R');
int firstL = s.indexOf('L'), lastL = s.lastIndexOf('L');
if (firstL == -1) {
out.printf("%d %d\n", firstR + 1, lastR + 1 + 1);
}
else if (firstR == -1) {
out.printf("%d %d\n", lastL + 1, firstL + 1 - 1);
}
else {
out.printf("%d %d\n", firstR + 1, lastR + 1);
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 46a6cb47206ca917cab114324c0ce27b | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes |
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
///Dude
public class Main {
public void snowprints(int size,String path)
{
int s=0;
int t=0;
int r=path.indexOf("R");
int l=path.indexOf("L");
int lr=path.lastIndexOf("R");
int ll=path.lastIndexOf("L");
if(r>=0&&l>=0)
{
s=r+1;
t=l+1;
}else if(r>=0 && l==-1 && lr==r)
{
s=r+1;
t=s+1;
}else if(r>=0 && l==-1 && lr!=r)
{
s=r+1;
t=lr+2;
}else if(r==-1 && l>=0 && ll==l)
{
s=l+1;
t=l;
}else if(r==-1 && l>=0 && ll!=l)
{
s=ll+1;
t=l;
}
System.out.println(s+" "+t);
}
public static void main(String[] args) throws FileNotFoundException{
Main obj=new Main();
Scanner Scan=new Scanner(System.in);
int n=Scan.nextInt();
Scan.nextLine();
String d=Scan.nextLine();
obj.snowprints(n, d);
}
} | Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 7a3636893a10c8099338b02e88e520ac | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.*;
public class snow
{
public static void main(String args[])throws Exception
{
BufferedReader e=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(e.readLine());
String a=e.readLine();
int d=0,r=0,l=0,i;
for(i=0;i<n;i++)
{
if(a.charAt(i)=='R')
r++;
if(a.charAt(i)=='L')
l++;
}
for(i=0;i<n;i++)
{
if(a.charAt(i)=='.')
d++;
else
break;
}
if((r!=0)&&(l!=0))
{
System.out.print(+(d+1));
System.out.print(" ");
System.out.print(+(d+r));
}
if(r==0)
{
System.out.print(+(d+1));
System.out.print(" ");
System.out.print(+(d));
}
if(l==0)
{
System.out.print(+(d+1));
System.out.print(" ");
System.out.print(+(d+r+1));
}
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 1d4ee6db0e820416e03ece5ad9187b95 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String args[]) throws IOException {
Reader.init(System.in);
int l=0, r=0;
int n=Reader.nextInt();
String s=Reader.next();
System.out.println((!s.contains("L"))?(s.lastIndexOf("R")+1)+" "+(s.lastIndexOf("R")+2):(s.indexOf("L")+1)+" "+s.indexOf("L"));
}
}
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 long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static public void nextIntArrays(int[]... arrays) throws IOException {
for (int i = 1; i < arrays.length; ++i) {
if (arrays[i].length != arrays[0].length) {
throw new InputMismatchException("Lengths are different");
}
}
for (int i = 0; i < arrays[0].length; ++i) {
for (int[] array : arrays) {
array[i] = nextInt();
}
}
}
static public void nextLineArrays(String[]... arrays) throws IOException {
for (int i = 1; i < arrays.length; ++i) {
if (arrays[i].length != arrays[0].length) {
throw new InputMismatchException("Lengths are different");
}
}
for (int i = 0; i < arrays[0].length; ++i) {
for (String[] array : arrays) {
array[i] = reader.readLine();
}
}
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 0cc0e6bc8d81d74f46f1d63b6b865c84 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.util.*;
public class SnowFootprints{
public static void main(String[] Args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
boolean hasR = false;
boolean hasL = false;
int lastR = -1;
int firstL = 1001;
for(int k = 0; k < s.length(); k++){
if(s.charAt(k)=='R'){
hasR = true;
lastR = k;
}
if(s.charAt(k)=='L'){
hasL = true;
firstL = (firstL > k)?k:firstL;
}
}
if(!hasL){
System.out.println((lastR+1) + " " + (lastR+2));
}
else
System.out.println((firstL + 1) + " " + firstL);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | c4012b8f00193610d15bafd6f041de5e | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
*
..RRLL...
123456789
......>>o
..RRLL...
......o<<
---------------------
11
.RRRLLLLL..
12345678901
....>>>o...
..o<<<<<...
---------------------
*
*
----------------
LLLLRRRRRRR
.RRRLLLLL..
12345678901
......o....
12345678901
......>o..
12345678901
......>>>o.RRR
12345678901
....o<<<<<.LLLLL
17
.......RRRRR.....
12345678901234567
.......>>>>>o....
*
*
*/
public class A_div2_180 {
public static void main(String[]arg) throws IOException
{
new A_div2_180().solve();
}
public void solve()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int size,i,j,R = 0,L = 0,s = 10001,t = 0;
char r;
size = Integer.parseInt(in.readLine());
String road = in.readLine();
for(i = 0; i < size; i++)
{
r = road.charAt(i);
if(r == 'R')
{
if(i > t)
t = i;
if(i < s)
s = i;
R++;
}
else if(r == 'L')
{
if(i > t)
t = i;
if(i < s)
s = i;
L++;
}
}
s++;t++;
System.out.println();
if(L == 0)
{
t++;
}
else if(R == 0)
{
i = s;
s = t;
t = i - 1;
}
else
{
if(R > L)
{
i = s; j = t;
t = j - L + 1;
}
else
{
i = s; j = t;
s = j + 1 - R;
t = j - L + 1;
}
}
System.out.println(s + " " + t);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 221766ae60eca946b167f7bb5ffcb62d | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br1 = new BufferedReader(
new InputStreamReader(System.in));
int n = Integer.parseInt(br1.readLine());
String data = br1.readLine();
int Rstart = -1;
int Lstart = -1;
int Rend = -1;
int Lend = -1;
boolean Rocc = false;
boolean Locc = false;
boolean RLocc = false;
boolean LLocc = false;
int j = 0;
for (int i = 0; i < n; i++) {
j = n - 1 - i;
if (data.charAt(i) == 'R' && !Rocc) {
Rstart = i;
Rocc = true;
}
if (data.charAt(i) == 'L' && !Locc) {
Lstart = i;
Locc = true;
}
if (data.charAt(j) == 'R' && !RLocc) {
Rend = j;
RLocc = true;
}
if (data.charAt(j) == 'L' && !LLocc) {
Lend = j;
LLocc = true;
}
}
if(Lend ==-1 || Lstart ==-1 )
{
System.out.println((Rstart+1)+" "+(Rend+2));
return;
}
if(Rend == -1 || Rstart == -1)
{
System.out.println((Lend+1)+" "+(Lstart));
return;
}
System.out.println((Rstart + 1) + " " + (Lstart));
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | f608c9c06731283b4422df71a78e1304 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.util.Scanner;
public class Snow {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
char[] road = new char[n];
String str = sc.next();
for(int k=0;k<n;k++)
road[k] = str.charAt(k);
int Rleft=1,Rright=n,Lleft=1,Lright=n;
for(int i=0;i<n;i++) {
if(road[i] == 'R') {
Rleft = i+1;
break;
}
}
for(int i=0;i<n;i++) {
if(road[i] == 'L') {
Lleft = i+1;
break;
}
}
for(int i=n-1;i>=0;i--) {
if(road[i] == 'R') {
Rright = i+1;
break;
}
}
for(int i=0;i<n;i++) {
if(road[i] == 'L') {
Lright = i+1;
break;
}
}
int s=0,t=0;
s = (Rleft==1)?Lright:Rleft;
t = (Lright==n)?(Rright+1):(Lleft-1);
System.out.println(s+" "+t);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 6f0217ae64a2d90891b208f62689ee22 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Diego Huerfano ( diego.link )
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
final int n=in.readInt();
String s=in.readString();
int ini=0, end;
while( s.charAt(ini)=='.' ) ini++;
if( s.charAt(ini)=='R' ) {
end=s.indexOf( 'L' );
if( end>=0 ) {
out.printLine( (ini+1) + " " + (end) );
} else {
out.printLine( (ini+1) + " " + (s.indexOf('.', ini)+1) );
}
} else {
end=s.indexOf( '.', ini );
out.printLine( (end) + " " + (ini) );
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print( objects );
writer.println();
}
public void close() {
writer.close();
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | d265ea4b59b9fdc108e7bdeb85da694a | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.IOException;
import java.util.InputMismatchException;
public class SnowFootprints {
public static void main(String[] args) {
FasterScanner sc = new FasterScanner();
sc.nextInt();
String S = sc.nextString();
int firstR = S.indexOf('R');
if (firstR >= 0) {
int lastR = S.lastIndexOf('R');
System.out.println((firstR + 1) + " " + (lastR + 2));
} else {
int firstL = S.indexOf('L');
int lastL = S.lastIndexOf('L');
System.out.println((lastL + 1) + " " + firstL);
}
}
public static class FasterScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
} | Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | b47331465a66941e50cabc61a0f9bc34 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.util.*;
public class Main{
static String test1 = "9\n..RRLL...";
static String test2 = "11\n.RRRLLLLL..";
static String test3 = "3\n.R.";
static String test4 = "3\n.L.";
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String prints = in.nextLine();//numbereater
prints = in.nextLine();
int pos = prints.length()-1;
int leftL = -1;
while(pos>=0){
if(prints.charAt(pos)=='L')leftL = pos;
pos--;
}
pos=0;
int rightR =-1;
while(pos<prints.length()){
if(prints.charAt(pos)=='R')rightR=pos;
pos++;
}
int start=0;
int end = 0;
if(leftL>=0) start = leftL;
if(rightR>=0)start = rightR;
if(rightR==-1)end = leftL-1;
else if(leftL==-1)end = rightR+1;
else end = rightR;
System.out.println((start+1)+" "+(end+1));
}//main
}//class | Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 168aac9a8bbef11e4356ca96342e082e | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class SnowFootprints {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String steps = br.readLine();
int i;
int j;
int start=0;
int end=0;
for (i = 0; i<n; i++){
if (steps.charAt(i)=='R'){
start = i+1;
for (j =i; steps.charAt(j)=='R'; j++){
}
if (steps.charAt(j)=='L')
end = j;
else
end = j+1;
break;
}
}
if (start ==0){
for (i = 0; i<n; i++){
if (steps.charAt(i)=='L'){
end = i;
for (j =i; steps.charAt(j)=='L'; j++){
}
start = j;
break;
}
}
}
System.out.print(start +" "+ end);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 16410c15aeebb2090d8d220429b76f31 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class SnoFootprints {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String steps = br.readLine();
int i;
int j;
int start=0;
int end=0;
for (i = 0; i<n; i++){
if (steps.charAt(i)=='R'){
start = i+1;
for (j =i; steps.charAt(j)=='R'; j++){
}
if (steps.charAt(j)=='L')
end = j;
else
end = j+1;
break;
}
}
if (start ==0){
for (i = 0; i<n; i++){
if (steps.charAt(i)=='L'){
end = i;
for (j =i; steps.charAt(j)=='L'; j++){
}
start = j;
break;
}
}
}
System.out.print(start +" "+ end);
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 0718ef38e96e4a3d85d4a0aad0e03012 | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class my_class {
public static void main(String[] args) {
Scanner ololo = new Scanner(System.in);
int n = ololo.nextInt(), s, t;
ololo.nextLine();
String str = ololo.nextLine();
if(!str.contains("R")){
System.out.println((str.lastIndexOf("L") + 1) + " " + str.indexOf("L"));
}
else if(!str.contains("L")){
System.out.println((str.indexOf("R") + 1) + " " + (str.lastIndexOf("R") + 2));
} else{
System.out.println((str.indexOf("R") + 1) + " " + str.indexOf("L"));
}
}
}
| Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 0a3484fefc10b98177f9c2e6c73bc09d | train_002.jsonl | 1366385400 | There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (iβ+β1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (iβ-β1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of s,βt by looking at the footprints. | 256 megabytes | import java.util.*;
import java.io.*;
public class A180 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
sc.nextLine();
String foot = sc.nextLine();
sc.close();
int start = -1;
int end = -1;
if(!foot.contains("R")){
end = foot.indexOf("L");
for(int i=0;i<n-1;i++){
if(foot.charAt(i)=='L' && foot.charAt(i+1)=='.')
start = i+1;
}
}
else {
start = foot.indexOf("R")+1;
for(int i=0;i<n-1;i++){
if(foot.charAt(i)=='R' && (foot.charAt(i+1)=='.'||foot.charAt(i+1)=='L'))
end = i+1+1;
}
}
out.println(start+" "+end);
out.flush();
}
} | Java | ["9\n..RRLL...", "11\n.RRRLLLLL.."] | 1 second | ["3 4", "7 5"] | NoteThe first test sample is the one in the picture. | Java 7 | standard input | [
"implementation",
"greedy"
] | 3053cba2426ebd113fcd70a9b026dad0 | The first line of the input contains integer n (3ββ€βnββ€β1000). The second line contains the description of the road β the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. | 1,300 | Print two space-separated integers β the values of s and t. If there are several possible solutions you can print any of them. | standard output | |
PASSED | 32f8be140b3e609d8f2f8eea8570d0c6 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.*;
import java.util.ArrayDeque;
import java.util.StringTokenizer;
public class Main {
public static Reader in = new Reader();
public static Writer out = new Writer();
public static void main(String[] args) {
char[] c = in.readLine().toCharArray();
final int N = c.length;
int[] len = new int[N];
int longest = 0;
int count = 0;
ArrayDeque<Integer> match = new ArrayDeque<Integer>();
for(int i=0; i<N; i++) {
if(c[i] == '(') {
match.push(i);
} else if(!match.isEmpty()) {
int lm = match.pop();
len[i] = i - lm + 1;
if(len[i] < i+1) {
len[i] += len[i - len[i]];
}
if(len[i] > longest) {
count = 0;
longest = len[i];
}
if(len[i] == longest) count++;
}
}
if(longest==0) out.println("0 1");
else out.printfln("%d %d", longest, count);
out.close();
}
}
class Reader {
private BufferedReader input;
private StringTokenizer line = new StringTokenizer("");
public Reader() {
input = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(String s) {
try {
input = new BufferedReader(new FileReader(s));
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void fill() {
try {
if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public double nextDouble() {
fill();
return Double.parseDouble(line.nextToken());
}
public String nextWord() {
fill();
return line.nextToken();
}
public int nextInt() {
fill();
return Integer.parseInt(line.nextToken());
}
public long nextLong() {
fill();
return Long.parseLong(line.nextToken());
}
public double readDouble() {
double d = 0;
try {
d = Double.parseDouble(input.readLine());
} catch(IOException io) {io.printStackTrace(); System.exit(0);}
return d;
}
public int readInt() {
int i = 0;
try {
i = Integer.parseInt(input.readLine());
} catch(IOException io) {io.printStackTrace(); System.exit(0);}
return i;
}
public int[] readInts(int n) {
int[] a = new int[n];
for(int i=0; i<n; i++)
a[i] = nextInt();
return a;
}
public void fillInts(int[] a) {
for(int i=0; i<a.length; i++)
a[i] = nextInt();
}
public long readLong() {
long l = 0;
try {
l = Long.parseLong(input.readLine());
} catch(IOException io) {io.printStackTrace(); System.exit(0);}
return l;
}
public String readLine() {
String s = "";
try {
s = input.readLine();
} catch(IOException io) {io.printStackTrace(); System.exit(0);}
return s;
}
}
class Writer {
private BufferedWriter output;
public Writer() {
output = new BufferedWriter(new OutputStreamWriter(System.out));
}
public Writer(String s) {
try {
output = new BufferedWriter(new FileWriter(s));
} catch(Exception ex) { ex.printStackTrace(); System.exit(0);}
}
public void println() {
try {
output.append("\n");
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void print(Object o) {
try {
output.append(o.toString());
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void println(Object o) {
try {
output.append(o.toString()+"\n");
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void printf(String format, Object... args) {
try {
output.append(String.format(format, args));
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void printfln(String format, Object... args) {
try {
output.append(String.format(format, args)+"\n");
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void flush() {
try {
output.flush();
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
public void close() {
try {
output.close();
} catch(IOException io) { io.printStackTrace(); System.exit(0);}
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | a207867c78af50bce95fbbc43871590c | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.IOException;
import java.io.*;
import java.util.Stack;
import java.util.StringTokenizer;
/**
*
* @author shnovruzov
*/
public class Main{
public void solve() throws IOException {
String s;
Stack<Integer> stack = new Stack();
s = in.readLine();
int dp[] = new int[s.length()];
char c[] = s.toCharArray();
int maxi = 0, count = 0;
int length = s.length();
for (int i = 0; i < length; i++) {
if (c[i] == ')') {
if (!stack.isEmpty()) {
int idx = stack.pop();
if (idx - 1 > 0 && c[idx - 1] == ')') {
dp[i] = dp[idx - 1] + i - idx + 1;
} else {
dp[i] = i - idx + 1;
}
if (dp[i] > maxi) {
maxi = dp[i];
count = 1;
} else if (dp[i] == maxi) {
count++;
}
}
} else {
stack.add(i);
}
}
if (maxi == 0) {
out.println("0 1");
} else {
out.println(maxi + " " + count);
}
}
public static void main(String[] args) throws IOException {
new Main().run();
}
BufferedReader in;
StringTokenizer st;
PrintWriter out;
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
st = null;
out = new PrintWriter(System.out);
solve();
in.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 (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 233faf3b8b73a16687ad9a17405a2310 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.BufferedInputStream;
import java.util.Scanner;
import java.util.Stack;
/**
* User: SDU_Phonism
* Flag: String problem
* Date: 12-12-4
*/
public class cf5C {
public static void main(String ags[]) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
char[] v= cin.next().trim().toCharArray();
int best = 0, base = 1, last = -1;
Stack<Integer> adj = new Stack<Integer>();
for (int i = 0; i < v.length; i++) {
if (v[i] == '(') adj.push(i);
else if (adj.isEmpty()) last = i;
else {
adj.pop();
int cnt = i - (adj.isEmpty() ? last : adj.peek());
if (cnt == best) base++;
if (cnt > best) {
best = cnt; base = 1;
}
}
}
System.out.println(best + " " + base);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 1379ea0b1a9f07255c0b452497586803 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes |
import java.io.*;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.StringTokenizer;
/**
* CodeForces Round 5C. Longest Regular Bracket Sequence
* Created by Darren on 14-9-14.
*/
public class Main {
Reader in = new Reader(System.in);
PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
new Main().run();
}
void run() throws IOException {
String seq = in.readLine();
int n = seq.length();
int longest = 0, count = 1, start = 0;
Deque<Integer> stack = new ArrayDeque<Integer>();
for (int i = 0; i < n; i++) {
if (seq.charAt(i) == '(') {
stack.push(i);
} else {
if (stack.isEmpty()) {
start = i+1;
continue;
}
stack.pop();
if (stack.isEmpty()) {
if (i-start+1 > longest) {
longest = i - start + 1;
count = 1;
} else if (i-start+1 == longest) {
count++;
}
} else {
if (i-stack.peek() > longest) {
longest = i - stack.peek();
count = 1;
} else if (i-stack.peek() == longest) {
count++;
}
}
}
}
out.print(longest);
out.print(' ');
out.println(count);
out.flush();
}
void solve() throws IOException {
}
static class Reader {
BufferedReader reader;
StringTokenizer tokenizer;
public Reader(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/** get next word */
String nextToken() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer( reader.readLine() );
}
return tokenizer.nextToken();
}
String readLine() throws IOException {
return reader.readLine();
}
char nextChar() throws IOException {
return (char)reader.read();
}
int nextInt() throws IOException {
return Integer.parseInt( nextToken() );
}
long nextLong() throws IOException {
return Long.parseLong( nextToken() );
}
double nextDouble() throws IOException {
return Double.parseDouble( nextToken() );
}
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | fde01caf19f816c3eeb12ff7048ce533 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class LongBracket {
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
char[]brackets = in.readLine().trim().toCharArray();
int size= 0;
int num =1;
int length=brackets.length;
int hight=0;
HashMap<Integer,Integer> Last= new HashMap<Integer,Integer>();
Last.put(0, -1);
for (int i=0; i<brackets.length;++i){
if(brackets[i]=='('){
Integer l=Last.get(hight);
++hight;
Integer h=Last.get(hight);
if(h==null||(l!=null&&l>h))
Last.put(hight, i);
}else{
Last.remove(hight);
--hight;
Integer h=Last.get(hight);
Integer l=Last.get(hight-1);
if(h!=null&&(l==null||l<h)){
int s=i-h;
if(s==size)
++num;
else if(s>size){
num=1;
size=s;
}
}else{
Last.put(hight, i);
}
}
}
System.out.println(size+ " "+ num);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 057ddc30e16c4db7e7d0eb51a9dc5167 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class C {
static BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
public static void main(String[] args) throws IOException {
String s = in.readLine();
int index = -1;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int[] stack = new int[s.length()];
int max = -1;
int last = -1;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(')
stack[++index] = i;
else {
if(index == -1)
last = i;
else {
index--;
int len ;
if(index == -1)
len = i - last;
else
len = i - stack[index];
int v = 0;
if (map.containsKey(len))
v = map.get(len);
map.put(len, v + 1);
if (len > max)
max = len;
}
}
}
if (max == -1)
System.out.println("0 1");
else
System.out.println(max + " " + map.get(max));
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | f0c828c0a4cbb9354454895af1479a21 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = " " + reader.readLine();
int len = line.length() - 1, longest = 0, count = 0;
Stack<Integer> stack = new Stack<>();
int[] seqlens = new int[len + 1];
for (int i = 1; i <= len; ++i) {
if (line.charAt(i) == '(') {
stack.push(i);
} else if (stack.isEmpty()) {
seqlens[i] = 0;
} else {
int k = stack.pop();
seqlens[i] = seqlens[k - 1] + (i - k + 1);
longest = Math.max(longest, seqlens[i]);
}
}
if (longest == 0) {
System.out.println("0 1");
} else {
for (int i = 1; i <= len; ++i) {
if (seqlens[i] == longest) {
++count;
}
}
System.out.println(longest + " " + count);
}
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | e0891d9e9bbcac09351467de2fca60cc | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.util.TreeMap;
public class Solution{
///////////////////////////////////////////////////////////////////////////
static class FastScanner{
BufferedReader s;
StringTokenizer st;
public FastScanner(){
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(System.in));
}
public FastScanner(File f) throws FileNotFoundException{
st = new StringTokenizer("");
s = new BufferedReader (new FileReader(f));
}
public int nextInt() throws IOException{
if(st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
public double nextDouble() throws IOException{
if(st.hasMoreTokens())
return Double.parseDouble(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextDouble();
}
}
public long nextLong() throws IOException{
if(st.hasMoreTokens())
return Long.parseLong(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextLong();
}
}
public String nextString() throws IOException{
if(st.hasMoreTokens())
return st.nextToken();
else{
st = new StringTokenizer(s.readLine());
return nextString();
}
}
public String readLine() throws IOException{
return s.readLine();
}
public void close() throws IOException{
s.close();
}
}
////////////////////////////////////////////////////////////////////
// Number Theory
public static int divisor(int x){
int limit = x;
int numberOfDivisors = 0;
for (int i=1; i < limit; ++i) {
if (x % i == 0) {
limit = x / i;
if (limit != i) {
numberOfDivisors++;
}
numberOfDivisors++;
}
}
return numberOfDivisors;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b,a%b);
}
public static long lcm(long a,long b, long c){
return lcm(lcm(a,b),c);
}
public static long lcm(long a, long b){
return a*b/gcd(a,b);
}
////////////////////////////////////////////////////////////////////
// private static FastScanner s = new FastScanner(new File("input.txt"));
// private static PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
private static FastScanner s = new FastScanner();
private static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));
// private static Scanner s = new Scanner(System.in);
@SuppressWarnings("unused")
private static Map<Integer,Integer> mm = new HashMap<Integer,Integer>();
@SuppressWarnings("unused")
private static Map<Integer,node> mn = new HashMap<Integer,node>();
@SuppressWarnings("unused")
private static int[][] states = { {-1,0} , {1,0} , {0,-1} , {0,1} };
////////////////////////////////////////////////////////////////////
static class node implements Comparable<node>{
int a;
int b;
public node(int x, int y){
this.a = x;
this.b = y;
}
@Override
public int compareTo(node arg0) {
// TODO Auto-generated method stub
return Integer.compare(arg0.b, b);
}
}
public static void main(String[] args) throws IOException{
Solution so = new Solution();
so.solve();
}
////////////////////////////////////////////////////////////////////
void solve() throws IOException{
char[] arr = s.nextString().toCharArray();
int len = arr.length;
int[] dp = new int[len];
Stack<Integer> st = new Stack<Integer>();
int ans = Integer.MIN_VALUE;
int count = 0;
for(int i=0;i<len;i++){
if(arr[i] == '(')
st.push(i);
else if(!st.isEmpty()){
int pre_index = st.pop();
dp[i] = i - pre_index + 1;
if(dp[i] < i+1)
dp[i] += dp[i-dp[i]];
if(dp[i] > ans){
ans = dp[i];
count = 0;
}
if(ans == dp[i]) count++;
}
}
if(ans == Integer.MIN_VALUE) ww.print(0+" "+1);
else ww.print(ans+" "+count);
s.close();
ww.close();
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 952b585ae22bbfd108ebb8285668e0bb | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF5C {
public static void main(String[] args){
Scanner br = new Scanner(System.in);
String line = br.next();
Stack<Integer> st = new Stack<Integer>();
Stack<Integer> ends = new Stack<Integer>();
int max = 0;
int count = 1;
for(int i = 0;i<line.length();i++){
if(line.charAt(i) == ')'){
if(st.size() > 0){
int cur = st.pop();
while(ends.size() > 0 && ends.peek() > cur){
ends.pop();
ends.pop();
}
int len = (i-cur+1);
if(ends.size() > 0 && ends.peek() == cur-1){
int temp = ends.pop();
len+=ends.peek();
ends.push(temp);
}
if(len > max){
max = len;
count = 1;
}
else if(len == max){
count++;
}
ends.push(len);
ends.push(i);
}
else{
ends.clear();
}
}
else{
st.push(i);
}
}
System.out.println(max+" "+count);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 7cc1629cc4e7d265d09cad4a7328be74 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.Scanner;
import java.util.Stack;
public class BracketSeq {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int[] maxStrLength = new int[1000005];
String string = s.nextLine();
int longestSub = 0;
int subCount = 1;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0 ; i<string.length() ; i++){
if(string.charAt(i) == '(' ) {
stack.push(i);
maxStrLength[i] = 0;
}
else{
if(stack.empty())
maxStrLength[i] = 0;
else{
int position = stack.peek();
stack.pop();
int length = i - position + 1;
maxStrLength[i] = length;
if(position > 0)
maxStrLength[i] += maxStrLength[position - 1];
}
}
}
for(int i = 0 ; i<string.length() ;i++){
if(maxStrLength[i] == longestSub && longestSub != 0)
subCount++;
else if(maxStrLength[i] > longestSub){
longestSub = maxStrLength[i];
subCount = 1;
}
}
System.out.println(longestSub + " " + subCount);
s.close();
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 735d0d5a89be0bf477eb4b4c0f5ea8fe | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.*;
public class LongestBracketSequence
{
public static void main(String[] args)
{
String input = "";
int temp = 0;
int ans = 0;
int count = 1;
int max = 0;
int x = 0;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input = br.readLine();
}
catch(IOException e)
{
System.out.println("IOException");
}
char[] ch = input.toCharArray();
int[] stack = new int[ch.length + 1];
for (int i = 0; i < ch.length; i++)
{
if (ch[i] == '(')
{
stack[temp++] = x;
x = i + 1;
}
else
{
if (temp != 0)
{
ans = i + 1 - stack[temp - 1];
if (ans == max) count++;
if (ans > max) { max = ans; count = 1; }
x = stack[temp - 1];
temp--;
}
else x = i + 1;
}
}
System.out.println(max + " " + count);
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | c9cb0f9957c05fa618723fc8b522185f | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main
{
static StringTokenizer st=new StringTokenizer("");
static BufferedReader br;
///////////////////////////////////////////////////////////////////////////////////
public static void main(String args[]) throws FileNotFoundException, IOException, Exception
{
//Scanner in =new Scanner(new FileReader("input.txt"));
//PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
//Scanner in=new Scanner(System.in);
br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
//StringTokenizer st;//=new StringTokenizer(br.readLine());
//////////////////////////////////////////////////////////////////////////////////////
String s=ns();
Stack<Integer> st=new Stack<Integer>();
int arr[]=new int[s.length()];
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='(') st.add(i);
else
{
if(st.isEmpty()){arr[i]=0;}
else
{
int k=st.pop();
arr[i]=(i-k+1);
if(k-1>=0)
arr[i]+=arr[k-1];
}
}
}
//for(int i=0;i<arr.length;i++)out.print(arr[i]+ " ");
//out.println();
int max=0;
for(int i=0;i<s.length();i++) max=Math.max(arr[i],max);
int no=0;
for(int i=0;i<s.length();i++) if(arr[i]==max)no++;
if(max!=0)
out.print(max+" "+no);
else out.print("0 1");
//////////////////////////////////////////////////////////////////////////////////////
out.close();
}
///////////////////////////////////////////////////////////////////////////////
static public class pair implements Comparable<pair>
{
int ind,cap;
pair(){ind=0;cap=0;}
pair(int first,int second){ind=first;cap=second;}
public int compareTo(pair o) {
return cap-o.cap;
}
}
static public class triple implements Comparable<triple>
{
int ind,price,cap;
triple(){ind=0;cap=0;price=0;}
triple(int f,int s,int t){ind=f;cap=s;price=t;}
public int compareTo(triple o) {
return o.price-price;
}
}
public static Integer ni() throws Exception
{
if(!st.hasMoreTokens())
st=new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
public static BigInteger nb()throws Exception
{
if(!st.hasMoreElements())
st=new StringTokenizer(br.readLine());
return BigInteger.valueOf(Long.parseLong(st.nextToken()));
}
public static Long nl() throws Exception
{
if(!st.hasMoreTokens())
st=new StringTokenizer(br.readLine());
return Long.parseLong(st.nextToken());
}
public static Double nd()throws Exception
{
if(!st.hasMoreElements())
st=new StringTokenizer(br.readLine());
return Double.parseDouble(st.nextToken());
}
public static String ns()throws Exception
{
if(!st.hasMoreElements())
st=new StringTokenizer(br.readLine());
return st.nextToken();
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | bbdb7581ddd7b9df17fc3f79c2b3b7f1 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.*;
public class Bracket {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
int[] counts = new int[s.length()+1];
counts[0] = 0;
int startingIdx = 0;
int bestLen = 0;
int bestCount = 1;
ArrayDeque<Integer> stack = new ArrayDeque<Integer>();
for (int i = 1; i < counts.length; ++i) {
char c = s.charAt(i-1);
counts[i] = counts[i-1] + (c == '(' ? 1 : -1);
if (c == '(') {
stack.push(1);
} else {
if (stack.size() > 1) {
int prev = stack.pop();
int prev2 = stack.pop();
int extra = (prev2 % 2 == 0 ? 1 : 0);
prev2 += prev + extra;
if (bestLen < prev2) {
bestLen = prev2;
bestCount = 1;
} else if (bestLen == prev2) {
++bestCount;
}
prev2 += (1 - extra);
stack.push(prev2);
} else if (stack.size() == 1) {
int prev = stack.pop();
if (prev % 2 == 1) {
prev += 1;
if (bestLen < prev) {
bestLen = prev;
bestCount = 1;
} else if (bestLen == prev) {
++bestCount;
}
stack.push(prev);
}
}
}
//System.out.printf("%c : %s\n", c, stack.toString());
}
System.out.println(String.format("%d %d", bestLen, bestCount));
//System.out.println(Arrays.toString(counts));
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 002077e80b7a041250063c83cec3a192 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
char[] l1 = scan.next().toCharArray();
ArrayList<Integer> last = new ArrayList<>();
char[] ok = new char[l1.length];
for (int i = 0 ; i < l1.length; i++){
if (l1[i] == '('){
last.add(i);
}
else {
if (last.size()>0){
int match = last.get(last.size() - 1);
ok[match] = 'a';
ok[i] = 'a';
last.remove(last.size()-1);
}
}
}
String[] processed = String.valueOf(ok).split("\0");
int max = 0;
int count = 1;
for (String e: processed){
if (e.length() > 0){
if (e.length()> max){
max = e.length();
count = 1;
}
else if (e.length() == max){
count++;
}
}
}
System.out.println(max +" "+count);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | a55de81ed675195f56d86791bdaa8ba7 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main implements Runnable {
public void solve() throws IOException {
char[] c = nextToken().toCharArray();
ArrayDeque<Integer> stack = new ArrayDeque<>();
stack.add(0); //the first illegal position
int ansLen = 0;
int cnt = 1;
for(int i = 0 ; i < c.length; i++){
if(c[i] == '(') stack.addFirst(i + 1);
else{
stack.pollFirst();
if(stack.isEmpty()){ //it means we removed the illegel one, thus the current one is also illegal
stack.addFirst(i + 1); //now this is the very first illegeal place
continue;
}
//System.out.println(stack.peekFirst());
int curlen = i + 1 - stack.peekFirst() ; //inclusive length
if(curlen == ansLen){
cnt++;
}
if(curlen > ansLen){
ansLen = curlen;
cnt = 1;
}
}
}
if(ansLen == 0) System.out.println(0 + " " + 1);
else System.out.println(ansLen + " " + cnt);
}
//-----------------------------------------------------------
public static void main(String[] args) {
new Main().run();
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
tok = null;
solve();
in.close();
} catch (IOException e) {
System.exit(0);
}
}
public String nextToken() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.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());
}
BufferedReader in;
StringTokenizer tok;
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 7598b7b3f6be22b9d585ba36fddba88d | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class LongBracketSequence
{
public static void main(String args[])throws IOException
{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
//BufferedReader reader=new BufferedReader(new FileReader("bracket"));
String[] sequence=reader.readLine().split("");
int max=0;
int count=0;
int endInd=0;
int val=0;
Stack <Integer> seq=new Stack<>();
for(int i=0; i<sequence.length; i++)
{
if(sequence[i].equals("("))
{
seq.push(i);
}
else if(seq.isEmpty())
{
endInd=i;
}
else if(sequence[i].equals(")"))
{
seq.pop();
if(seq.isEmpty())
{
val=i-endInd;
}
else
{
val=i-seq.peek();
}
if(val>max)
{
max=val;
count=1;
}
else if(val==max)
{
count++;
}
else
{
val=0;
}
}
}
if(max==0)
{
System.out.println("0 1");
}
else
{
System.out.println(max+" "+count);
}
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 1bb161924cd6d5350e82597a732a302b | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class LongBracketSequence
{
public static void main(String args[])throws IOException
{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
//BufferedReader reader=new BufferedReader(new FileReader("bracket"));
String[] sequence=reader.readLine().split("");
int max=0;
int count=0;
int endInd=0;
Stack <Integer> seq=new Stack<>();
for(int i=0; i<sequence.length; i++)
{
if(sequence[i].equals("("))
{
seq.push(i);
}
else if(seq.isEmpty())
{
endInd=i;
}
else if(sequence[i].equals(")"))
{
seq.pop();
int val = 0;
if(seq.isEmpty())
{
val=i-endInd;
}
else
{
val=i-seq.peek();
}
if(val>max)
{
max=val;
count=1;
}
else if(val==max)
{
count++;
}
}
}
if(max>0)
{
System.out.println(max+" "+count);
}
else
{
System.out.println("0 1");
}
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 3da13ee3e97395e23acc5d89fa4af1a9 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
public class R5qCLongestRegularBracketSequence {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter w = new PrintWriter(System.out);
Stack<Integer> stack = new Stack<Integer>();
int lastun = -1;
int max = 0,count = 1;
char s[] = br.readLine().toCharArray();
int n = s.length;
for(int i=0;i<n;i++){
if(s[i] == '(')
stack.push(i);
else{
if(stack.isEmpty())
lastun = i;
else
stack.pop();
}
int dis = stack.isEmpty() ? i - lastun : i - Math.max(lastun, stack.peek());
if(dis > max){
max = dis;
count = 1;
}
else if(dis == max)
count++;
}
if(max == 0) count = 1;
w.println(max + " " + count);
w.close();
}
public static int ip(String s){
return Integer.parseInt(s);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 82181d131ebabdf71733763570d1493b | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
/**
*
* @author 753617
*/
public class Bseq {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String i=" "+in.nextLine();
boolean b[] = new boolean[i.length()];
Stack<Integer> s= new Stack<Integer>();
//(())
int F[]= new int[i.length()];
Arrays.fill(F, 0);
int size=0;
int max=0;
for(int j=1; j<=i.length()-1; j++)
{
if((i.charAt(j)=='('))
{
s.push(j);
}
else if(!s.isEmpty()){
F[0]=0;
int k= s.pop();
F[j]=(j-k)+1+F[k-1];
}
}
// System.out.println(Arrays.toString(F));
int count=0;
max=0;
int c=0;
for(int l =0; l<F.length; l++)
{
if(F[l]>max)
{
max=F[l];
}
}
for(int l=0; l<F.length; l++){
if(max!=0){
count=(F[l]==max)?++count:count;
}
else{
break;
}}
count=count>0?count:1;
System.out.println(max+" "+count);
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 674a8de0d0532d3995481bd219174563 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Stack<Integer> stack = new Stack<Integer>();
Set<Integer> set= new HashSet<Integer>();
String s=in.next();
int len = s.length(),i;
int length=0,max=0,num=0;
boolean Flag=false;
for (i = 0; i < len; ++i) {
if (s.charAt(i) == '(')
stack.push(i);
else
if (!stack.isEmpty())
stack.pop();
}
while(!stack.isEmpty())
set.add(stack.pop());
for (i = 0; i < len; ++i) {
if (s.charAt(i) == '(')
{
Flag=true;
if(!set.contains(i))
stack.push(i);
else
{
length=0;
Flag=false;
}
}
else {
if (!stack.isEmpty())
{
length+=2;
if(length>max)
max=length;
Flag=true;
stack.pop();
}
else
{
Flag=false;
length=0;
}
}
}
for (i = 0; i < len; ++i) {
if (s.charAt(i) == '(')
{
Flag=true;
if(!set.contains(i))
stack.push(i);
else
{ if(length==max&&max!=0)
num++;
length=0;
Flag=false;
}
}
else {
if (!stack.isEmpty())
{
length+=2;
Flag=true;
stack.pop();
}
else
{ if(length==max&&max!=0)
num++;
Flag=false;
length=0;
}
}
}
if(num==0)num++;
System.out.print(max + " " + num);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 5641b167df42ef7bf9ab6ec1dbd3c784 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.*;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int cishu = 1;
int length = 0;
int temp = 0;
int cunzai = 0;
int flag[] = new int[10000];
int cnt = 0;
int stack[] = new int[1000005];
int index[] = new int[1000005];
int pre[] = new int[1000005];
int top = 0;
stack[top] = -1;
index[top] = -1;
top++;
for( int i = 0 ; i < str.length() ; i++ )
{
pre[i] = -1;
if( str.charAt(i) == '(' )
{
stack[top] = 1;
index[top] = i;
top++;
}
if( str.charAt(i) == ')' )
{
if(stack[top - 1] != 1) {
stack[top] = 2;
index[top] = i;
top++;
}
else {
pre[i] = index[top - 1];
if(pre[i] - 1 >= 0 && pre[pre[i] - 1] != -1) {
pre[i] = pre[pre[i] - 1];
}
top--;
}
}
}
for(int i = str.length() - 1; i >= 0; i--) {
if(pre[i] == -1) continue;
int l = i - pre[i] + 1;
if(l > length) {
length = l;
cishu = 1;
}
else if(l == length) {
cishu++;
}
}
System.out.println(length + " " + cishu);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | b7d357301d30ce46352d133bfdab645b | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class C0005 {
public static void main(String args[]) throws Exception {
new C0005();
}
C0005() throws Exception {
PandaScanner sc = null;
PrintWriter out = null;
try {
sc = new PandaScanner(System.in);
out = new PrintWriter(System.out);
} catch (Exception ignored) {
}
char[] c = sc.br.readLine().toCharArray();
ArrayDeque<Integer> stk = new ArrayDeque<Integer>();
int[] left = new int[c.length];
int max = 0;
int freq = 1;
for (int i = 0; i < c.length; i++) {
if (c[i] == '(') {
stk.push(i);
left[i] = -1;
}
else {
if (stk.isEmpty()) {
left[i] = -1;
}
else {
left[i] = stk.pop();
if (left[i] != 0) {
if (left[left[i] - 1] != -1) {
left[i] = left[left[i] - 1];
}
}
int w = i - left[i] + 1;
if (w > max) {
freq = 1;
max = w;
}
else if (w == max) {
freq++;
}
}
}
}
out.println(max + " " + freq);
out.close();
System.exit(0);
}
//The PandaScanner class, for Panda fast scanning!
public class PandaScanner {
BufferedReader br;
StringTokenizer st;
InputStream in;
PandaScanner(InputStream in) throws Exception {
br = new BufferedReader(new InputStreamReader(this.in = in));
}
public String next() throws Exception {
if (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine().trim());
return next();
}
return st.nextToken();
}
public boolean hasNext() throws Exception {
return (st != null && st.hasMoreTokens()) || in.available() > 0;
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | c5732b266ea031a2d2bcf292bfdcee76 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.Scanner;
import java.util.Stack;
public class CF5C {
private static int[] solve(String sequence) {
int maxLen = 0, sum = 1;
int[] len = new int[sequence.length()];
Stack<Integer> bracket = new Stack<Integer>();
for(int i = 0; i < sequence.length(); i++) {
if(sequence.charAt(i) == '(') {
bracket.push(i);
} else {
if(bracket.empty())
continue;
len[i] = i - bracket.pop() + 1;
}
}
int nowLen;
for(int i = sequence.length() - 1; i >= 0; i--) {
nowLen = 0;
while(i >= 0 && len[i] > 0) {
nowLen += len[i];
i -= len[i];
}
if(maxLen < nowLen) {
maxLen = nowLen;
sum = 1;
} else if(maxLen == nowLen)
sum ++;
}
return new int[]{maxLen, maxLen == 0 ? 1 :sum};
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] ans = solve(sc.nextLine());
sc.close();
System.out.println(ans[0] + " " + ans[1]);
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | d33a5bc3892ebd4849dcb681dcffa252 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.Scanner;
import java.util.TreeMap;
public class CF5C {
private static int[] solve(String sequence) {
int maxLen = 0, sum = 1, balance = 0;
TreeMap<Integer, Integer> loc = new TreeMap<Integer, Integer>();
for(int i = 0; i < sequence.length(); i++) {
if(sequence.charAt(i) == '(') {
balance ++;
if(!loc.containsKey(balance))
loc.put(balance, i);
} else {
while(loc.higherKey(balance) != null)
loc.remove(loc.higherKey(balance));
if(!loc.containsKey(balance))
continue;
if(maxLen < i - loc.get(balance) + 1) {
maxLen = i - loc.get(balance) + 1;
sum = 1;
}
else if(maxLen == i - loc.get(balance) + 1)
sum ++;
balance --;
}
}
return new int[]{maxLen, sum};
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] ans = solve(sc.nextLine());
sc.close();
System.out.println(ans[0] + " " + ans[1]);
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | a490d14c74247057b7daac710037da42 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.Scanner;
import java.util.Stack;
public class CF5C {
static class Bracket {
int level, loc;
Bracket(int lvl, int l) {
level = lvl;
loc = l;
}
}
private static int[] solve(String sequence) {
int maxLen = 0, sum = 1, balance = 0;
Stack<Bracket> bracket = new Stack<Bracket>();
for(int i = 0; i < sequence.length(); i++) {
if(sequence.charAt(i) == '(') {
balance ++;
if(bracket.empty() || bracket.peek().level < balance)
bracket.push(new Bracket(balance, i));
} else {
while(!bracket.empty() && bracket.peek().level > balance)
bracket.pop();
if(bracket.empty() || bracket.peek().level < balance)
continue;
if(maxLen < i - bracket.peek().loc + 1) {
maxLen = i - bracket.peek().loc + 1;
sum = 1;
}
else if(maxLen == i - bracket.peek().loc + 1)
sum ++;
balance --;
}
}
return new int[]{maxLen, sum};
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] ans = solve(sc.nextLine());
sc.close();
System.out.println(ans[0] + " " + ans[1]);
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 7b45def4c33cf03ffa04a555399d5a98 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.*;
public class Problem5C
{
public static int[] dp1 = new int[1000001];
public static int[] dp2 = new int[1000001];
public static boolean[] done = new boolean[1000001];
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
String seq = in.nextLine();
int n = seq.length();
Stack<Integer> stack = new Stack<Integer>();
for(int i=0; i<n; i++)
{
if(seq.substring(i, i+1).equals("(")) stack.push(i+1);
else if(stack.isEmpty()) dp1[i+1] = -1;
else dp1[i+1] = stack.pop();
}
int max = 0;
int count = 0;
for(int i=1; i<=n; i++)
{
int curr = recursion(i);
if(curr==max) count++;
else if(curr>max)
{
max=curr;
count=1;
}
}
if(max==0) System.out.println(0+" "+1);
else System.out.println(max+" "+count);
}
public static int recursion(int i)
{
if(done[i]) return dp2[i];
else
{
if(dp1[i]<=0) return dp2[i]=0;
else
{
dp2[i] = i-dp1[i]+1;
if(dp1[i]-1>0) dp2[i]+=recursion(dp1[i]-1);
done[i]=true;
return dp2[i];
}
}
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 3c02bb233b0295ce36068b804315bc9b | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
public class LongestBrackets {
static class Pair{
int index;
char type;
public Pair(int index,char type)
{
this.index = index;
this.type = type;
}
}
public static void main(String[]args)throws Throwable
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char c[] = br.readLine().toCharArray();
int assign[] = new int[c.length];
int prefix[] = new int[c.length];
Stack<Pair> s = new Stack();
Pair end = new Pair(-1,'b');
s.push(end);
for(int i = 0 ; i < c.length ; i++)
{
Pair p = new Pair(i,c[i]);
if(c[i] == '(') s.push(p);
else
{
if(s.peek() == end) continue;
if(s.peek().type == ')')
{
s.push(end);
continue;
}
Pair out = s.pop();
assign[out.index] = 1;
assign[i] = 1;
}
}
// System.out.println("");
// for(int i = 0 ; i < c.length ; i++)
// {
// System.out.print(assign[i] +" ");
// }
// System.out.println("");
int i = 0;
int N = c.length;
int best = 0;
int cnt = 0;
while(i < N)
{
int j = i;
while(j < N && assign[j] == 1)
{
++j;
}
int currLen = j - i;
if(best < currLen)
{
best = currLen;
cnt = 0;
}
if(best == currLen) ++cnt;
i = j + 1;
}
if(best == 0) System.out.println(0 +" "+ 1);
else System.out.println(best + " " + cnt);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 86cef246656ec2bde283de6fa1851f69 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.*;
public class TaskC {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String st;
Stack<Integer> S = new Stack<Integer>();
int N, i;
st = in.nextLine();
N = st.length();
st = " " + st;
int[] F = new int[N + 1];
for (i = 1; i <= N; ++i) {
if (st.charAt(i) == '(') S.push(i); else {
if (S.isEmpty()) F[i] = 0; else {
int k = S.pop();
F[i] = F[k - 1] + (i - k + 1);
}
}
}
int max = 0, res = 0;
for (i = 1; i <= N; ++i) max = Math.max(max, F[i]);
if (max == 0) res = 1;
else for (i = 1; i <= N; ++i) res = res + ((F[i] == max) ? 1 : 0);
System.out.print(max + " " + res);
in.close();
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | da18b6125f7a8a14cdaf8e53bf33fe0a | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static BufferedReader reader
= new BufferedReader(new InputStreamReader(System.in));
static StringBuilder out = new StringBuilder();
public static void main(String[] args){
String str = nextLine();
boolean[] brack = new boolean[str.length()];
//initialize
for(int i=0;i<str.length();i++){
brack[i]=false;
}
ArrayList<Integer> stack = new ArrayList<Integer>();
for(int i=0;i<str.length();i++){
if(str.charAt(i)==')'){
if(stack.size()!=0){
int last=stack.size()-1;
int index = stack.get(last);
stack.remove(last);
brack[index]=true;
brack[i]=true;
}
}
else if(str.charAt(i)=='('){
stack.add(i);
}
}
/*
System.out.println();
for(int i=0;i<brack.length;i++){
if(brack[i]==true){
System.out.print("T");
}
else{
System.out.print("F");
}
}
System.out.println();
*/
//search max length
int max=0;
int count=0;
for(int i=0;i<brack.length;i++){
if(brack[i]==true){
count++;
if(max<count){
max=count;
}
}
else{
count=0;
}
}
//System.out.println(max);
//search max substrings
count=0;
int subs=0;
for(int i=0;i<brack.length;i++){
if(brack[i]==true){
count++;
if(count==max){
subs++;
}
}
else{
count=0;
}
}
if(max==0){
System.out.println("0 1");
}
else{
System.out.println(max+" "+subs);
}
return;
}
// the followings are methods to take care of inputs.
static int nextInt(){
return Integer.parseInt(nextLine());
}
static long nextLong(){
return Long.parseLong(nextLine());
}
static int[] nextIntArray(){
String[] inp = nextLine().split("\\s+");
int[] ary = new int[inp.length]; for (int i = 0; i < ary.length; i++){
ary[i] = Integer.parseInt(inp[i]);
}
return ary;
}
static int[] nextIntArrayFrom1(){
String[] inp = nextLine().split("\\s+");
int[] ary = new int[inp.length + 1];
for (int i = 0; i < inp.length; i++){
ary[i+1] = Integer.parseInt(inp[i]);
}
return ary;
}
static long[] nextLongArray(){
String[] inp = nextLine().split("\\s+");
long[] ary = new long[inp.length];
for (int i = 0; i < inp.length; i++){
ary[i] = Long.parseLong(inp[i]);
}
return ary;
}
static long[] nextLongArrayFrom1(){
String[] inp = nextLine().split("\\s+");
long[] ary = new long[inp.length + 1];
for (int i = 0; i < inp.length; i++){
ary[i+1] = Long.parseLong(inp[i]);
}
return ary;
}
static String nextLine(){
try {
return reader.readLine().trim();
} catch (Exception e){}
return null;
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 331fe603878e0f1782b8c4b28e7bbc37 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.*;
import java.io.*;
public class LongestRegularBracketSequence
{
public static void main(String[] args) throws IOException
{
FastScanner in = new FastScanner();
String sequence = in.next();
boolean[] invalid = new boolean[sequence.length()];
int depth = 0;
for(int x = 0; x < sequence.length(); x++)
{
if(sequence.charAt(x) == '(')
{
depth++;
}
else
{
if(depth == 0)
{
invalid[x] = true;
}
else
{
depth--;
}
}
}
depth = 0;
for(int y = sequence.length() - 1; y >= 0; y--)
{
if(sequence.charAt(y) == ')')
{
depth++;
}
else
{
if(depth == 0)
{
invalid[y] = true;
}
else
{
depth--;
}
}
}
int prev = -1;
int max = 0;
int num = 1;
for(int z = 0; z < invalid.length; z++)
{
if(invalid[z])
{
int currentSize = z - prev - 1;
if(currentSize > max)
{
max = currentSize;
num = 1;
}
else if(currentSize == max)
{
num++;
}
prev = z;
}
}
int currentSize = invalid.length - prev - 1;
if(currentSize > max)
{
max = currentSize;
num = 1;
}
else if(currentSize == max)
{
num++;
}
if(max == 0)
{
num = 1;
}
System.out.println(max + " " + num);
}
static class FastScanner
{
BufferedReader br;
StringTokenizer st;
public FastScanner()
{
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
public FastScanner(File f) throws IOException
{
br = new BufferedReader(new FileReader(f));
st = new StringTokenizer("");
}
public int nextInt() throws IOException
{
if(st.hasMoreTokens())
{
return Integer.parseInt(st.nextToken());
}
else
{
st = new StringTokenizer(br.readLine());
return nextInt();
}
}
public String next() throws IOException
{
if(st.hasMoreTokens())
{
return st.nextToken();
}
else
{
st = new StringTokenizer(br.readLine());
return next();
}
}
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 7a3ac2daf9e46d92fda5ef7203d0b2c7 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.util.Scanner;
import java.util.Stack;
public class C5 {
private String ip;
private int[] ipArr;
private int longest;
private int occurences = 1;
public static void main(String[] args) {
C5 obj = new C5();
obj.init();
obj.compute();
}
private void init() {
Scanner scn = new Scanner(System.in);
ip = scn.nextLine();
ipArr = new int[ip.length()];
for(int i=0;i<ip.length();i++)
{
char temp = ip.charAt(i);
if(temp == '(')
{
ipArr[i] = 1;
}
else {
ipArr[i] = -1;
}
}
}
private void compute() {
int startIndex = 0;
int[] ansArr = new int[ipArr.length];
int maxDiff = 0;
int counter = 0;
int sum = 0;
int prev = -1;
int prevstart = -1;
Stack<Integer> stack = new Stack<>();
for(int i=0;i<ipArr.length;i++)
{
if(ipArr[i] > 0)
stack.push(i);
sum += ipArr[i];
if(sum < 0)//negative
{
startIndex = i+1;
sum = 0;
}
else if(sum == 0)
{
int diff = i - startIndex + 1;
for(int j=startIndex;j<=i;j++)
{
ansArr[j] = 1;
}
/*if(diff > maxDiff)
{
maxDiff = diff;
counter = 1;
}
else if(diff == maxDiff)
{
counter++;
}*/
startIndex = i+1;
}
else if(ipArr[i] < 0){
//int sumA = ipArr[i];
ansArr[stack.pop()] = 1;
ansArr[i] = 1;
/*for(int k = i;sumA!=0;k--)
{
ansArr[k] = 1;
if(k!=i)
sumA += ipArr[k];
}*/
//prev = i;
}//closing bracket.
}
int ansSum=0;
for(int i=0;i<ansArr.length;i++)
{
if(ansArr[i] == 0)
{
ansSum = 0;
}
else
{
ansSum+=ansArr[i];
if(maxDiff < ansSum)
{
maxDiff = ansSum;
counter = 1;
}
else if(maxDiff == ansSum)
{
counter++;
}
}
}
if(maxDiff > 0)
{
System.out.println(maxDiff + " " + counter);
}
else
{
System.out.println("0 1");
}
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 2c77b2998d5b45ba2b7d11bc875d3cb3 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.Closeable;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Collection;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Jacob Jiang
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
QuickScanner in = new QuickScanner(inputStream);
ExtendedPrintWriter out = new ExtendedPrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, QuickScanner in, ExtendedPrintWriter out) {
char[] sequence = in.next().toCharArray();
int n = sequence.length;
Deque<Integer> stack = new ArrayDeque<Integer>();
int[] correct = new int[n];
Arrays.fill(correct, -1);
for (int i = 0; i < n; i++) {
if (sequence[i] == '(') {
stack.push(i);
} else {
if (stack.isEmpty()) {
correct[i] = -1;
} else {
int pre = stack.poll();
correct[i] = pre;
if (pre > 0 && correct[pre - 1] != -1) {
correct[i] = correct[pre - 1];
}
}
}
}
int answer = 0;
for (int i = 0; i < n; i++) {
if (correct[i] != -1) {
answer = Math.max(answer, i - correct[i] + 1);
}
}
if (answer == 0) {
out.println("0 1");
} else {
int count = 0;
for (int i = 0; i < n; i++) {
if (correct[i] != -1 && i - correct[i] + 1 == answer) {
count++;
}
}
out.printLine(answer, count);
}
}
}
class QuickScanner implements Iterator<String>, Closeable {
BufferedReader reader;
StringTokenizer tokenizer;
boolean endOfFile = false;
public QuickScanner(InputStream inputStream){
reader = new BufferedReader(new InputStreamReader(inputStream));
tokenizer = null;
}
public boolean hasNext() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
checkNext();
} catch (NoSuchElementException ignored) {
}
}
return !endOfFile;
}
private void checkNext() {
if (endOfFile) {
throw new NoSuchElementException();
}
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
endOfFile = true;
throw new NoSuchElementException();
}
}
public String next() {
checkNext();
return tokenizer.nextToken();
}
public void remove() {
throw new UnsupportedOperationException();
}
public void close() {
try {
reader.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
class ExtendedPrintWriter extends PrintWriter {
public ExtendedPrintWriter(Writer out) {
super(out);
}
public ExtendedPrintWriter(OutputStream out) {
super(out);
}
public void printItems(Object... items) {
for (int i = 0; i < items.length; i++) {
if (i != 0) {
print(' ');
}
print(items[i]);
}
}
public void printLine(Object... items) {
printItems(items);
println();
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 918e73799c791ba8a97bc688c2cf66db | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | import java.io.ObjectInputStream;
import java.io.Serializable;
import java.io.Closeable;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.util.ConcurrentModificationException;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Jacob Jiang
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
QuickScanner in = new QuickScanner(inputStream);
ExtendedPrintWriter out = new ExtendedPrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, QuickScanner in, ExtendedPrintWriter out) {
char[] sequence = in.next().toCharArray();
int n = sequence.length;
IntList stack = new ArrayIntList();
int[] correct = new int[n];
Arrays.fill(correct, -1);
for (int i = 0; i < n; i++) {
if (sequence[i] == '(') {
stack.add(i);
} else {
if (stack.isEmpty()) {
correct[i] = -1;
} else {
int pre = stack.get(stack.size() - 1);
stack.removeElementAt(stack.size() - 1);
correct[i] = pre;
if (pre > 0 && correct[pre - 1] != -1) {
correct[i] = correct[pre - 1];
}
}
}
}
int answer = 0;
for (int i = 0; i < n; i++) {
if (correct[i] != -1) {
answer = Math.max(answer, i - correct[i] + 1);
}
}
if (answer == 0) {
out.println("0 1");
} else {
int count = 0;
for (int i = 0; i < n; i++) {
if (correct[i] != -1 && i - correct[i] + 1 == answer) {
count++;
}
}
out.printLine(answer, count);
}
}
}
class QuickScanner implements Iterator<String>, Closeable {
BufferedReader reader;
StringTokenizer tokenizer;
boolean endOfFile = false;
public QuickScanner(InputStream inputStream){
reader = new BufferedReader(new InputStreamReader(inputStream));
tokenizer = null;
}
public boolean hasNext() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
checkNext();
} catch (NoSuchElementException ignored) {
}
}
return !endOfFile;
}
private void checkNext() {
if (endOfFile) {
throw new NoSuchElementException();
}
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
endOfFile = true;
throw new NoSuchElementException();
}
}
public String next() {
checkNext();
return tokenizer.nextToken();
}
public void remove() {
throw new UnsupportedOperationException();
}
public void close() {
try {
reader.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
class ExtendedPrintWriter extends PrintWriter {
public ExtendedPrintWriter(Writer out) {
super(out);
}
public ExtendedPrintWriter(OutputStream out) {
super(out);
}
public void printItems(Object... items) {
for (int i = 0; i < items.length; i++) {
if (i != 0) {
print(' ');
}
print(items[i]);
}
}
public void printLine(Object... items) {
printItems(items);
println();
}
}
interface IntList extends IntCollection {
boolean add(int element);
boolean equals(Object that);
int get(int index);
int hashCode();
IntIterator iterator();
int removeElementAt(int index);
}
class ArrayIntList extends RandomAccessIntList implements IntList, Serializable {
public ArrayIntList() {
this(8);
}
public ArrayIntList(int initialCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("capacity " + initialCapacity);
}
_data = new int[initialCapacity];
_size = 0;
}
public ArrayIntList(IntCollection that) {
this(that.size());
addAll(that);
}
public int get(int index) {
checkRange(index);
return _data[index];
}
public int size() {
return _size;
}
public int removeElementAt(int index) {
checkRange(index);
incrModCount();
int oldval = _data[index];
int numtomove = _size - index - 1;
if (numtomove > 0) {
System.arraycopy(_data, index + 1, _data, index, numtomove);
}
_size--;
return oldval;
}
public void add(int index, int element) {
checkRangeIncludingEndpoint(index);
incrModCount();
ensureCapacity(_size + 1);
int numtomove = _size - index;
System.arraycopy(_data, index, _data, index + 1, numtomove);
_data[index] = element;
_size++;
}
public void ensureCapacity(int mincap) {
incrModCount();
if (mincap > _data.length) {
int newcap = (_data.length * 3) / 2 + 1;
int[] olddata = _data;
_data = new int[newcap < mincap ? mincap : newcap];
System.arraycopy(olddata, 0, _data, 0, _size);
}
}
private final void checkRange(int index) {
if (index < 0 || index >= _size) {
throw new IndexOutOfBoundsException("Should be at least 0 and less than " + _size + ", found " + index);
}
}
private final void checkRangeIncludingEndpoint(int index) {
if (index < 0 || index > _size) {
throw new IndexOutOfBoundsException("Should be at least 0 and at most " + _size + ", found " + index);
}
}
private transient int[] _data = null;
private int _size = 0;
}
interface IntCollection {
boolean isEmpty();
IntIterator iterator();
int size();
}
interface IntIterator {
boolean hasNext();
int next();
}
interface IntListIterator extends IntIterator {
boolean hasNext();
int next();
}
abstract class RandomAccessIntList extends AbstractIntCollection implements IntList {
protected RandomAccessIntList() {
}
public abstract int get(int index);
public abstract int size();
public int removeElementAt(int index) {
throw new UnsupportedOperationException();
}
public void add(int index, int element) {
throw new UnsupportedOperationException();
}
public boolean add(int element) {
add(size(), element);
return true;
}
public IntIterator iterator() {
return listIterator();
}
public IntListIterator listIterator() {
return listIterator(0);
}
public IntListIterator listIterator(int index) {
return new RandomAccessIntListIterator(this, index);
}
public boolean equals(Object that) {
if (this == that) {
return true;
} else if (that instanceof IntList) {
IntList thatList = (IntList) that;
if (size() != thatList.size()) {
return false;
}
for (IntIterator thatIter = thatList.iterator(), thisIter = iterator(); thisIter.hasNext(); ) {
if (thisIter.next() != thatIter.next()) {
return false;
}
}
return true;
} else {
return false;
}
}
public int hashCode() {
int hash = 1;
for (IntIterator iter = iterator(); iter.hasNext(); ) {
hash = 31 * hash + iter.next();
}
return hash;
}
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("[");
for (IntIterator iter = iterator(); iter.hasNext(); ) {
buf.append(iter.next());
if (iter.hasNext()) {
buf.append(", ");
}
}
buf.append("]");
return buf.toString();
}
protected int getModCount() {
return _modCount;
}
protected void incrModCount() {
_modCount++;
}
private int _modCount = 0;
private static class ComodChecker {
ComodChecker(RandomAccessIntList source) {
_source = source;
resyncModCount();
}
protected RandomAccessIntList getList() {
return _source;
}
protected void assertNotComodified() throws ConcurrentModificationException {
if (_expectedModCount != getList().getModCount()) {
throw new ConcurrentModificationException();
}
}
protected void resyncModCount() {
_expectedModCount = getList().getModCount();
}
private RandomAccessIntList _source = null;
private int _expectedModCount = -1;
}
protected static class RandomAccessIntListIterator extends ComodChecker implements IntListIterator {
RandomAccessIntListIterator(RandomAccessIntList list, int index) {
super(list);
if (index < 0 || index > getList().size()) {
throw new IndexOutOfBoundsException("Index " + index + " not in [0," + getList().size() + ")");
} else {
_nextIndex = index;
resyncModCount();
}
}
public boolean hasNext() {
assertNotComodified();
return _nextIndex < getList().size();
}
public int next() {
assertNotComodified();
if (!hasNext()) {
throw new NoSuchElementException();
} else {
int val = getList().get(_nextIndex);
_lastReturnedIndex = _nextIndex;
_nextIndex++;
return val;
}
}
private int _nextIndex = 0;
private int _lastReturnedIndex = -1;
}
}
abstract class AbstractIntCollection implements IntCollection {
public abstract IntIterator iterator();
public abstract int size();
protected AbstractIntCollection() {
}
public boolean add(int element) {
throw new UnsupportedOperationException("add(int) is not supported.");
}
public boolean addAll(IntCollection c) {
boolean modified = false;
for (IntIterator iter = c.iterator(); iter.hasNext(); ) {
modified |= add(iter.next());
}
return modified;
}
public boolean isEmpty() {
return (0 == size());
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | af568bd367a35231a3f8be958277bf59 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | //<editor-fold defaultstate="collapsed" desc="comment">
import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;
//</editor-fold>
public class Main {
public static void main(String[] args) throws Exception {
StringBuilder resBuilder = new StringBuilder();
new Program(IOGenerator.getScanner(IOGenerator.CONSOLE),
IOGenerator.getPrintStream(IOGenerator.CONSOLE), resBuilder)
.run();
System.out.print(resBuilder);
}
}
// <editor-fold defaultstate="collapsed" desc="comment">
class IOGenerator {
public static final int CONSOLE = 0;
public static final int FILE = 1;
public static Scanner getScanner(int type) throws Exception {
if (type == CONSOLE)
return new Scanner(System.in);
else if (type == FILE)
return new Scanner(new File("c:\\a.txt"));
throw new Exception();
}
public static PrintStream getPrintStream(int type) throws Exception {
if (type == CONSOLE)
return System.out;
else if (type == FILE)
return new PrintStream(new File("c:\\a2.txt"));
throw new Exception();
}
}
// </editor-fold>
class Program {
@SuppressWarnings("unused")
private final Scanner in;
@SuppressWarnings("unused")
private final PrintStream out;
@SuppressWarnings("unused")
private final StringBuilder resBuilder;
Program(Scanner scanner, PrintStream out, StringBuilder resBuilder) {
this.in = scanner;
this.out = out;
this.resBuilder = resBuilder;
}
void run() {
char[] expression = in.nextLine().toCharArray();
Stack<Integer> openingBracePosition = new Stack<>();
int[] closingBracePosition = new int[expression.length];
Arrays.fill(closingBracePosition, -1);
for (int i = 0; i < expression.length; i++) {
if (expression[i] == '(')
openingBracePosition.push(i);
else if (!openingBracePosition.isEmpty())
closingBracePosition[openingBracePosition.pop()] = i;
}
openingBracePosition = null;
int i = 0;
int bestLen = 0;
int bestLenCount = 1;
int validBraceStart = 0;
int newLen;
while (true) {
while (i < expression.length
&& (expression[i] == ')' || closingBracePosition[i] == -1))
i++;
if (i == expression.length)
break;
validBraceStart = i;
while (i < expression.length && closingBracePosition[i] != -1) {
i = closingBracePosition[i];
newLen = i - validBraceStart + 1;
if (newLen > bestLen) {
bestLen = newLen;
bestLenCount = 1;
} else if (newLen == bestLen)
bestLenCount++;
i++;
}
}
resBuilder.append(bestLen).append(" ").append(bestLenCount)
.append("\n");
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 5fcbb78d46be495a6cf257c91a157cd7 | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | //<editor-fold defaultstate="collapsed" desc="comment">
import java.io.File;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
//</editor-fold>
public class Main
{
public static void main(String[] args) throws Exception
{
StringBuilder resBuilder = new StringBuilder();
new Program(IOGenerator.getScanner(IOGenerator.CONSOLE),
IOGenerator.getPrintStream(IOGenerator.CONSOLE), resBuilder).run();
System.out.print(resBuilder);
}
}
// <editor-fold defaultstate="collapsed" desc="comment">
class IOGenerator
{
public static final int CONSOLE = 0;
public static final int FILE = 1;
public static Scanner getScanner(int type) throws Exception
{
if (type == CONSOLE)
return new Scanner(System.in);
else if (type == FILE)
return new Scanner(new File("c:\\a.txt"));
throw new Exception();
}
public static PrintStream getPrintStream(int type) throws Exception
{
if (type == CONSOLE)
return System.out;
else if (type == FILE)
return new PrintStream(new File("c:\\a2.txt"));
throw new Exception();
}
}
// </editor-fold>
class Program
{
@ SuppressWarnings("unused")
private final Scanner in;
@ SuppressWarnings("unused")
private final PrintStream out;
@ SuppressWarnings("unused")
private final StringBuilder resBuilder;
Program(Scanner scanner, PrintStream out, StringBuilder resBuilder)
{
this.in = scanner;
this.out = out;
this.resBuilder = resBuilder;
}
void run()
{
char[] expression = in.nextLine().toCharArray();
Stack<Integer> openingBracePosition = new Stack<>();
int[] closingBracePosition = new int[expression.length];
Arrays.fill(closingBracePosition, -1);
for (int i = 0; i < expression.length; i++)
{
if (expression[i] == '(')
openingBracePosition.push(i);
else if (!openingBracePosition.isEmpty())
closingBracePosition[openingBracePosition.pop()] = i;
}
openingBracePosition = null;
int i = 0;
int bestLen = 0;
int bestLenCount = 1;
int validBraceStart = 0;
int newLen;
while (true)
{
while (i < expression.length && (expression[i] == ')' || closingBracePosition[i] == -1))
i++;
if (i == expression.length)
break;
validBraceStart = i;
while (i < expression.length && closingBracePosition[i] != -1)
{
i = closingBracePosition[i];
newLen = i - validBraceStart + 1;
if (newLen > bestLen)
{
bestLen = newLen;
bestLenCount = 1;
}
else if (newLen == bestLen)
bestLenCount++;
i++;
}
}
resBuilder.append(bestLen).append(" ").append(bestLenCount).append("\n");
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | 7244ff9544ea3d0570e0db178337ae0f | train_002.jsonl | 1269100800 | This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. | 256 megabytes | //<editor-fold defaultstate="collapsed" desc="comment">
import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;
//</editor-fold>
public class Main
{
public static void main(String[] args) throws Exception
{
StringBuilder resBuilder = new StringBuilder();
new Program(IOGenerator.getScanner(IOGenerator.CONSOLE), IOGenerator.getPrintStream(IOGenerator.CONSOLE), resBuilder).run();
System.out.print(resBuilder);
}
}
//<editor-fold defaultstate="collapsed" desc="comment">
class IOGenerator
{
public static final int CONSOLE = 0;
public static final int FILE = 1;
public static Scanner getScanner(int type) throws Exception
{
if (type == CONSOLE)
return new Scanner(System.in);
else if (type == FILE)
return new Scanner(new File("c:\\a.txt"));
throw new Exception();
}
public static PrintStream getPrintStream(int type) throws Exception
{
if (type == CONSOLE)
return System.out;
else if (type == FILE)
return new PrintStream(new File("c:\\a2.txt"));
throw new Exception();
}
}
//</editor-fold>
class Program
{
@SuppressWarnings("unused")
private final Scanner in;
@SuppressWarnings("unused")
private final PrintStream out;
@SuppressWarnings("unused")
private final StringBuilder resBuilder;
Program(Scanner scanner, PrintStream out, StringBuilder resBuilder)
{
this.in = scanner;
this.out = out;
this.resBuilder = resBuilder;
}
void run()
{
char[] expression = in.nextLine().toCharArray();
Stack<Integer> openingBracePosition = new Stack<>();
int[] closingBracePosition = new int[expression.length];
Arrays.fill(closingBracePosition, -1);
for (int i = 0; i < expression.length; i++)
{
if (expression[i] == '(')
openingBracePosition.push(i);
else if (!openingBracePosition.isEmpty())
closingBracePosition[openingBracePosition.pop()] = i;
}
openingBracePosition = null;
int i = 0;
int bestLen = 0;
int bestLenCount = 1;
int validBraceStart = 0;
int newLen;
while (true)
{
while (i < expression.length && (expression[i] == ')' || closingBracePosition[i] == -1))
i++;
if (i == expression.length)
break;
validBraceStart = i;
while (i < expression.length && closingBracePosition[i] != -1)
{
i = closingBracePosition[i];
newLen = i - validBraceStart + 1;
if (newLen > bestLen)
{
bestLen = newLen;
bestLenCount = 1;
}
else if (newLen == bestLen)
bestLenCount++;
i++;
}
}
resBuilder.append(bestLen).append(" ").append(bestLenCount).append("\n");
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 7 | standard input | [
"dp",
"greedy",
"constructive algorithms",
"sortings",
"data structures",
"strings"
] | 91c3af92731cd7d406674598c0dcbbbc | The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106. | 1,900 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". | standard output | |
PASSED | a810041901c8bf09775ec0074da7714c | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class geek1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
long n=s.nextLong();
long m=s.nextLong();
ArrayList<Long> arr= new ArrayList<Long>();
long sum1=0;
long sum2=0;
for(int i=0;i<n;i++)
{
long a = s.nextLong();
long b=s.nextLong();
arr.add(a-b);
sum1+=a;
sum2+=b;
}
Collections.sort(arr);
if(sum1<=m)
{
System.out.println("0");
return;
}
if(sum2>m)
{
System.out.println("-1");
return;
}
long c=0;
long i=0;
while(sum1>m && i>=0)
{
// System.out.println(sum1);
sum1=sum1-arr.get((int) (arr.size()-1-i));
i++;
c++;
}
System.out.println(c);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 54d43a74d055d4c08af025c0cca401de | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | import java.util.*;
public class gfdgf {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
ArrayList<Integer> arr=new ArrayList<Integer>();
long count=0;
int sum=0;
int add=0;
for(int i=0;i<n;i++) {
int a=sc.nextInt();
int b=sc.nextInt();
arr.add(a-b);
count+=a;
sum+=b;
}
if(count<=m) {
System.out.println(0);
return;
}else {
Collections.sort(arr);
for(int i=n-1;i>=0;i--) {
count-=arr.get(i);
add++;
if(count<=m) {
System.out.println(add);
return;
}
}
}
System.out.println(-1);
}
} | Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | d0b1a994e0484352ef41007241c3de33 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | //package contest_488;
//import javafx.util.Pair;
import java.io.*;
import java.util.*;
//import contest_488.InputReader.SpaceCharFilter;
public class q2
{
class Node implements Comparable<Node>
{
long a;
long b;
public Node(long a, long b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Node o) {
return Long.compare(o.a - o.b, this.a - this.b);
}
//@Override
//public String toString() {
// return a + " " + b;
// }
}
public static void main(String[] args)
{
q2 obj=new q2();
InputReader in =new InputReader(System.in);
OutputWriter out=new OutputWriter(System.out);
int n=in.readInt();
int m=in.readInt();
Node[] p=new Node[n];
//Vector<Integer> s=new Vector();
long sum=0;
long sum1=0;
for(int i=0;i<n;i++)
{
int a=in.readInt();
int b=in.readInt();
p[i]=obj.new Node(a,b);
sum+=a;
sum1+=b;
}
//Collections.sort(s,Collections.reverseOrder());
Arrays.sort(p);
int k=0;
int count=0;
int i=0;
if(sum1>m)
{
System.out.print(-1);
System.exit(0);
}
while (i < n) {
if (sum <= m) {
break;
}
sum -= (p[i].a - p[i].b);
i++;
}
out.print(i);
out.flush();
out.close();
}
}
// Driver code
//This code is contributed by Mihir Joshi
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
}
| Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 8ab55d35c8706f1cabaa56deaa01b70d | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | //package contest_488;
//import javafx.util.Pair;
import java.io.*;
import java.util.*;
//import contest_488.InputReader.SpaceCharFilter;
public class q2
{
public static void main(String[] args)
{
InputReader in =new InputReader(System.in);
OutputWriter out=new OutputWriter(System.out);
int n=in.readInt();
int m=in.readInt();
PriorityQueue<Integer> s=new PriorityQueue(Collections.reverseOrder());
long sum=0;
for(int i=0;i<n;i++)
{
int a=in.readInt();
int b=in.readInt();
s.add(a-b);
sum+=a;
}
//Arrays.sort(s);
int k=0;
int count=0;
while(sum>m)
{
if(s.isEmpty())
k=1;
if(s.isEmpty())
break;
int t=s.poll();
//out.print(t+" ");
sum-=t;
count++;
}
if(k==1)
out.print(-1);
else
out.print(count);
out.flush();
out.close();
}
}
// Driver code
//This code is contributed by Mihir Joshi
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
}
| Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | dd2b2643e9ea3c9aeb940a0fb2f5e9f3 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes |
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class vew {
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
long n=scan.nextInt();
long space=scan.nextInt();
pair songs[]=new pair[(int)n];
long sum=0;
long com=0;
for(int i=0;i<n;i++)
{
songs[i]=new pair(scan.nextLong(),scan.nextLong());
sum+=songs[i].a;
com+=songs[i].b;
}
if(sum<=space)
{
System.out.println(0);
return;
}
if(com>space)
{
System.out.println(-1);
return;
}
Arrays.sort(songs,new sorter());
for(int i=(int)n-1;i>=0;i--)
{
sum-=songs[i].a;
sum+=songs[i].b;
// System.out.println("removed "+songs[i].a+" added"+songs[i].b);
if(sum<=space)
{
System.out.println(n-i);
return;
}
}
}
}
class pair{
long a;
long b;
pair(long x,long y)
{
a=x;
b=y;
}
}
class sorter implements Comparator<pair>
{
@Override
public int compare(pair a, pair b) {
if((a.a-a.b)>(b.a-b.b))
{
return 1;
}else if((b.a-b.b)>(a.a-a.b))
{
return -1;
}else{
return 0;
}
}
}
| Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | a5e865fd4259ed5927fc5473dcf41bd2 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | //package math_codet;
import java.io.*;
import java.util.*;
/******************************************
* AUTHOR: AMAN KUMAR SINGH *
* INSTITUITION: KALYANI GOVERNMENT ENGINEERING COLLEGE *
******************************************/
public class lets_do {
InputReader in;
PrintWriter out;
Helper_class h;
final long mod=1000000007;
final int N=200005;
public static void main(String[] args) throws java.lang.Exception{
new lets_do().run();
}
void run() throws Exception{
in=new InputReader(System.in);
out = new PrintWriter(System.out);
h = new Helper_class();
int t=1;
while(t-->0){
solve();
}
out.flush();
out.close();
}
TreeMap<Long,Integer> tmap=new TreeMap<Long,Integer>();
HashMap<Long,Integer> hmap=new HashMap<Long,Integer>();
void solve(){
int n=(int)h.nl();
long m=h.nl();
int i=0;
long[] a=new long[n];
long[] b=new long[n];
long sum1=0,sum2=0,sum3=0;
long[] diff=new long[n];
for(i=0;i<n;i++){
a[i]=h.nl();
b[i]=h.nl();
sum1+=a[i];
sum2+=b[i];
diff[i]=a[i]-b[i];
sum3+=diff[i];
}
//Collections.shuffle(diff);
shuffleArray(diff);
if(sum2>m)
h.pn(-1);
else{
Arrays.sort(diff);
long sum4=0;
int count=0;
if(sum1>m){
for(i=n-1;i>=0;i--){
sum4+=diff[i];
count++;
if(sum1-sum4<=m)
break;
}
}
h.pn(count);
}
}
void shuffleArray(long[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static final Comparator<Entity> com2=new Comparator<Entity>(){
public int compare(Entity x, Entity y){
int xx=Long.compare(x.a,y.a);
if(xx==0)
return Long.compare(x.b,y.b);
else
return xx;
}
};
class Entity{
long a;
long b;
Entity(long p, long q){
a=p;
b=q;
}
}
class Helper_class{
long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
long gcd1(long a, long b){return (b==0)?a:gcd(b,a%b);}
int bitcount(long n){return (n==0)?0:(1+bitcount(n&(n-1)));}
void p(Object o){out.print(o);}
void pn(Object o){out.println(o);}
void pni(Object o){out.println(o);out.flush();}
long nl(){return in.nextLong();}
long mul(long a,long b){
if(a>=mod)a%=mod;
if(b>=mod)b%=mod;
a*=b;
if(a>=mod)a%=mod;
return a;
}
long modPow(long a, long p){
long o = 1;
while(p>0){
if((p&1)==1)o = mul(o,a);
a = mul(a,a);
p>>=1;
}
return o;
}
long add(long a, long b){
if(a>=mod)a%=mod;
if(b>=mod)b%=mod;
if(b<0)b+=mod;
a+=b;
if(a>=mod)a-=mod;
return a;
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 20ddf066ce248b7491d89f8c98210e18 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | //package math_codet;
import java.io.*;
import java.util.*;
/******************************************
* AUTHOR: AMAN KUMAR SINGH *
* INSTITUITION: KALYANI GOVERNMENT ENGINEERING COLLEGE *
******************************************/
public class lets_do {
InputReader in;
PrintWriter out;
Helper_class h;
final long mod=1000000007;
final int N=200005;
public static void main(String[] args) throws java.lang.Exception{
new lets_do().run();
}
void run() throws Exception{
in=new InputReader(System.in);
out = new PrintWriter(System.out);
h = new Helper_class();
int t=1;
while(t-->0){
solve();
}
out.flush();
out.close();
}
TreeMap<Long,Integer> tmap=new TreeMap<Long,Integer>();
HashMap<Long,Integer> hmap=new HashMap<Long,Integer>();
void solve(){
int n=h.ni();
long m=h.nl();
int i=0;
long[] a=new long[n];
long[] b=new long[n];
long sum1=0,sum2=0,sum3=0;
long[] diff=new long[n];
for(i=0;i<n;i++){
a[i]=h.nl();
b[i]=h.nl();
sum1+=a[i];
sum2+=b[i];
diff[i]=a[i]-b[i];
sum3+=diff[i];
}
if(sum2>m)
h.pn(-1);
else{
shuffleArray(diff);
Arrays.sort(diff);
long sum4=0;
int count=0;
if(sum1>m){
for(i=n-1;i>=0;i--){
sum4+=diff[i];
count++;
if(sum1-sum4<=m)
break;
}
}
h.pn(count);
}
}
void shuffleArray(long[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static final Comparator<Entity> com2=new Comparator<Entity>(){
public int compare(Entity x, Entity y){
int xx=Long.compare(x.a,y.a);
if(xx==0)
return Long.compare(x.b,y.b);
else
return xx;
}
};
class Entity{
long a;
long b;
Entity(long p, long q){
a=p;
b=q;
}
}
class Helper_class{
long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
long gcd1(long a, long b){return (b==0)?a:gcd(b,a%b);}
int bitcount(long n){return (n==0)?0:(1+bitcount(n&(n-1)));}
void p(Object o){out.print(o);}
void pn(Object o){out.println(o);}
void pni(Object o){out.println(o);out.flush();}
String n(){return in.next();}
String nln(){return in.nextLine();}
int ni(){return in.nextInt();}
long nl(){return in.nextLong();}
double nd(){return in.nextDouble();}
long mul(long a,long b){
if(a>=mod)a%=mod;
if(b>=mod)b%=mod;
a*=b;
if(a>=mod)a%=mod;
return a;
}
long modPow(long a, long p){
long o = 1;
while(p>0){
if((p&1)==1)o = mul(o,a);
a = mul(a,a);
p>>=1;
}
return o;
}
long add(long a, long b){
if(a>=mod)a%=mod;
if(b>=mod)b%=mod;
if(b<0)b+=mod;
a+=b;
if(a>=mod)a-=mod;
return a;
}
}
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 UnknownError();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public void skip(int x) {
while (x-- > 0)
read();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String nextString() {
return next();
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean hasNext() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value != -1;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 23bf3e3c1d163a49f3f79da7a6913455 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
*
* @author thachlp
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
FastReader fr = new FastReader();
int n = fr.nextInt();
int m = fr.nextInt();
long s1 = 0;
long s2 = 0;
Long[] nums1 = new Long[n];
Long[] nums2 = new Long[n];
Long[] change = new Long[n];
for (int i = 0; i < n; i++) {
nums1[i] = fr.nextLong();
nums2[i] = fr.nextLong();
change[i] = nums1[i] - nums2[i];
s1 += nums1[i];
s2 += nums2[i];
}
if (s2 > m) {
System.out.println(-1);
return;
}
if (m >= s1) {
System.out.println(0);
return;
}
Arrays.sort(change);
int count = 0;
boolean tmp = false;
for (int i = n - 1; i >= 0; i--) {
s1 -= change[i];
count++;
if (s1 <= m) {
tmp = true;
break;
}
}
if (tmp) {
System.out.println(count);
} else {
System.out.println(-1);
}
}
}
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) {
}
}
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) {
}
return str;
}
}
| Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | f08746d754fbf7a52ef464b54ce795eb | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes |
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.io.*;
import java.math.BigInteger;
public class MainClass {
public static void main(String[] args) {
FastReader in = new FastReader();
int n = in.nextInt(), m = in.nextInt();
pair p[] = new pair[n];
long sum = 0,sum1=0;
for (int i=0; i<n; i++) {
int a = in.nextInt(), b = in.nextInt();
sum += a;
sum1 += b;
p[i] = new pair(a,b);
}
if(sum<=m) {
System.out.println("0");return;
}else if(sum1>m) {
System.out.println("-1");return;
}
Arrays.sort(p,new Comparator<pair>() {
@Override
public int compare(pair arg0, pair arg1) {
return arg1.s-arg0.s;
}
});
int ans = 0;
long var = sum - m;
long temp = 0;
boolean f = false;
for (int i=0; i<n; i++) {
if(var<=0)
break;
ans++;
var -= p[i].s;
}
if(var<=0)
System.out.println(ans);
else
System.out.println("-1");
}
static class pair{
int a,b,s;
public pair(int a,int b) {
this.a = a;
this.b = b;
this.s = a-b;
}
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
| Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 8c7d1b7f58af8d0c562b8994f7a7aab7 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.regex.*;
public class vk18
{
public static void main(String[]stp) throws Exception
{
Scanner scan=new Scanner(System.in);
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//String []s;
int n=scan.nextInt(),i;
long m=scan.nextLong(),sum=0;
ArrayList<Long> al=new ArrayList<>();
while(n!=0)
{
long a=scan.nextLong(),b=scan.nextLong();
al.add(a-b);
sum+=a;
n--;
}
long count=0;
Collections.sort(al,Collections.reverseOrder());
for(i=0;i<al.size();i++)
{
if(sum > m) {count++; sum-=al.get(i); }
else { System.out.println(count); System.exit(0); }
}
if(sum > m) System.out.println("-1");
else System.out.println(count);
}
} | Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | f96e32653f7b1f5ee74a8e50dc97c1e2 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | import java.io.*;
import java.util.*;
public class d {
public static void main(String args[]) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n = sc.nextInt(), m = sc.nextInt();
pair[] a = new pair[n];
long sum = 0;
for (int i = 0; i < n; i++) {
int f = sc.nextInt(), s = sc.nextInt();
a[i] = new pair(f, s);
sum += f;
}
int ans = 0;
if (sum > m) {
PriorityQueue<Integer> pq = new PriorityQueue<>((b, c) -> Integer.compare(c, b));
for (int i = 0; i < n; i++) {
pq.add(a[i].f - a[i].s);
}
while (sum > m && !pq.isEmpty()) {
sum -= pq.poll();
ans++;
}
if (sum > m)
ans = -1;
}
out.print(ans);
out.close();
}
public static void shuffle(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static void shuffle(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.valueOf(next());
}
public double nextDouble() {
return Double.valueOf(next());
}
String nextLine() throws IOException {
return reader.readLine();
}
}
}
class pair{
int f,s;
pair(int f,int s){
this.f=f;
this.s=s;
}
} | Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | c98323431bf8221ee273e11ad1f9f41b | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | import java.io.*;
import java.util.*;
public class d {
public static void main(String args[]) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n = sc.nextInt(), m = sc.nextInt();
int[] a = new int[n];
long sum = 0;
for (int i = 0; i < n; i++) {
int f = sc.nextInt(), s = sc.nextInt();
a[i] = f-s;
sum += f;
}
int ans = 0;
if (sum > m) {
shuffle(a);
Arrays.sort(a);
for (int i = n-1; i >= 0 && sum > m; i--) {
sum -= a[i];
ans++;
}
if (sum > m)
ans = -1;
}
out.print(ans);
out.flush();
out.close();
}
public static void shuffle(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static void shuffle(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.valueOf(next());
}
public double nextDouble() {
return Double.valueOf(next());
}
String nextLine() throws IOException {
return reader.readLine();
}
}
}
class pair{
int f,s;
pair(int f,int s){
this.f=f;
this.s=s;
}
} | Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | a416835a658d3225ff96129fc44bb780 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | import java.io.*;
import java.util.*;
public class d {
public static void main(String args[]) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n = sc.nextInt(), m = sc.nextInt();
int[] a = new int[n];
long sum = 0;
for (int i = 0; i < n; i++) {
int f = sc.nextInt(), s = sc.nextInt();
a[i] = f-s;
sum += f;
}
int ans = 0;
if (sum > m) {
shuffle(a);
Arrays.sort(a);
for (int i = n-1; i >= 0 && sum > m; i--) {
sum -= a[i];
ans++;
}
if (sum > m)
ans = -1;
}
out.print(ans);
out.close();
}
public static void shuffle(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static void shuffle(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.valueOf(next());
}
public double nextDouble() {
return Double.valueOf(next());
}
String nextLine() throws IOException {
return reader.readLine();
}
}
}
| Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 18644ab420155827d22269c2ec83a3b7 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class zzz_1015C {
public static void main(String[] args) {
FastReader s = new FastReader();
int n = s.nextInt();
long m = s.nextInt();
long[] diff = new long[n];
long a, b, sum_a, sum_b;
sum_a = sum_b = 0;
for (int i = 0; i < n; i++) {
a = s.nextInt();
b = s.nextInt();
diff[i] = a - b;
sum_a += a;
sum_b += b;
}
if (sum_b <= m) {
sortAllArray(diff);
int count = 0, i = n - 1;
while (sum_a > m && i >= 0) {
sum_a -= diff[i];
i--;
count++;
}
System.out.println(count);
return;
}
System.out.println(-1);
}
//////////////////////////////////////////////////////////////////////////////
// utils
/////////////////////////////////////////////////////////////////////////////
static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long [n1];
long R[] = new long [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void sortAllArray(long arr[]) {
sort(arr,0,arr.length-1);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | e5d31c8ddf13963cfa2366f4bd73e1fe | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class a_1015C {
public static void main(String[] args) {
FastReader s = new FastReader();
int n = s.nextInt();
long m = s.nextInt();
long[] diff = new long[n];
long a, b, sum_a, sum_b;
sum_a = sum_b = 0;
for (int i = 0; i < n; i++) {
a = s.nextInt();
b = s.nextInt();
diff[i] = a - b;
sum_a += a;
sum_b += b;
}
if (sum_b <= m) {
MergeSort.sortAllArray(diff);
int count = 0, i = n - 1;
while (sum_a > m && i >= 0) {
sum_a -= diff[i];
i--;
count++;
}
System.out.println(count);
return;
}
System.out.println(-1);
}
//////////////////////////////////////////////////////////////////////////////
// utils
/////////////////////////////////////////////////////////////////////////////
static class MergeSort {
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
// Create temp arrays
long L[] = new long [n1];
long R[] = new long [n2];
// Copy data to temp arrays
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
// Merge the temp arrays
// Initial indexes of first and second sub-arrays
int i = 0, j = 0;
// Initial index of merged sub-array array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
// Copy remaining elements of L[] if any
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
// Copy remaining elements of R[] if any
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void sortAllArray(long arr[])
{
sort(arr,0,arr.length-1);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | c4286962825fbce40ab2ba62fee97df6 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | import java.util.Scanner;
public class Main {
static int[] array;
static int[] tempMergArr;
static int length;
static void mySort(int inputArr[]) {
array = inputArr;
length = inputArr.length;
tempMergArr = new int[length];
doMergeSort(0, length - 1);
}
static void doMergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
doMergeSort(lowerIndex, middle);
doMergeSort(middle + 1, higherIndex);
mergeParts(lowerIndex, middle, higherIndex);
}
}
static void mergeParts(int lowerIndex, int middle, int higherIndex) {
for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), m = in.nextInt(), x, y;
//n for test cases , m for capacity of Ivan's flash drive
int adif[] = new int[n];
long sumx = 0, sumy = 0;
for (int i = 0; i < n; i++) {
x = in.nextInt();
y = in.nextInt();
sumx += x;
sumy += y;
adif[i] = x - y;
}
mySort(adif);
long dif = sumx - m;
if (sumy > m) {
System.out.println(-1);
} else /*if (dif>0)*/ {
//code hear
long sumdif = 0;
int i, j = 0;
for (i = adif.length - 1; i >= 0; i--) {
if (sumdif >= dif) {
break;
}
sumdif += adif[i];
j++;
}
System.out.println(j);
}
}
}
| Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 191d2d4a70eab307145a6261fab57019 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
public class Main implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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 String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new Main(),"Main",1<<26).start();
}
static long gcd(long a,long b){ if(b==0)return a;return gcd(b,a%b); }
static long modPow(long a,long p,long m){ if(a==1)return 1;long ans=1;while (p>0){ if(p%2==1)ans=(ans*a)%m;a=(a*a)%m;p>>=1; }return ans; }
static long modInv(long a,long m){return modPow(a,m-2,m);}
public void run() {
InputReader sc = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=sc.nextInt();
int m=sc.nextInt();
Node a[]=new Node[n];
long sum=0;
for (int i = 0; i <n ; i++) {
int x=sc.nextInt();
int y=sc.nextInt();
sum+=x;
a[i]=new Node(x,y,x-y);
}
Arrays.sort(a, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
return Integer.compare(o2.diff,o1.diff);
}
});
for (int i = 0; i <n ; i++) {
if(sum<=m){
out.println(i);
out.close();
return;
}
sum-=a[i].diff;
}
if(sum<=m)out.println(n);
else
out.println(-1);
out.close();
}
class Node{
int ai,bi,diff;
public Node(int ai, int bi,int diff) {
this.ai = ai;
this.bi = bi;
this.diff=diff;
}
}
} | Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 75d0777c9c10b77a96df34258f3dbc15 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes |
public class Main {
private static void solve() {
int n = ni();
long m = ni();
int[] x = new int[n];
for (int i = 0; i < n; i ++) {
int a = ni();
int b = ni();
m -= b;
x[i] = a - b;
}
if (m < 0) {
System.out.println(-1);
return;
}
radixSort(x);
for (int i = 0; i < n; i ++) {
m -= x[i];
if (m < 0) {
System.out.println(n - i);
return;
}
}
System.out.println(0);
}
public static int[] radixSort(int[] f){ return radixSort(f, f.length); }
public static int[] radixSort(int[] f, int n)
{
int[] to = new int[n];
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(f[i]&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[f[i]&0xffff]++] = f[i];
int[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(f[i]>>>16)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[f[i]>>>16]++] = f[i];
int[] d = f; f = to;to = d;
}
return f;
}
public static void main(String[] args) {
new Thread(null, new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
String debug = args.length > 0 ? args[0] : null;
if (debug != null) {
try {
is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
solve();
out.flush();
tr((System.currentTimeMillis() - start) + "ms");
}
}, "", 64000000).start();
}
private static java.io.InputStream is = System.in;
private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
private static java.util.StringTokenizer tokenizer = null;
private static java.io.BufferedReader reader;
public static String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new java.util.StringTokenizer(reader.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
private static double nd() {
return Double.parseDouble(next());
}
private static long nl() {
return Long.parseLong(next());
}
private static int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private static char[] ns() {
return next().toCharArray();
}
private static long[] nal(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private static int[][] ntable(int n, int m) {
int[][] table = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
table[i][j] = ni();
}
}
return table;
}
private static int[][] nlist(int n, int m) {
int[][] table = new int[m][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
table[j][i] = ni();
}
}
return table;
}
private static int ni() {
return Integer.parseInt(next());
}
private static void tr(Object... o) {
if (is != System.in)
System.out.println(java.util.Arrays.deepToString(o));
}
}
| Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 69389330e65171a895faca7a45cb92eb | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes |
public class Main {
private static void solve() {
int n = ni();
long m = ni();
int[] x = new int[n];
for (int i = 0; i < n; i ++) {
int a = ni();
int b = ni();
m -= b;
x[i] = a - b;
}
if (m < 0) {
System.out.println(-1);
return;
}
radixSort(x);
for (int i = 0; i < n; i ++) {
m -= x[i];
if (m < 0) {
out.println(n - i);
return;
}
}
out.println(0);
}
public static int[] radixSort(int[] f){ return radixSort(f, f.length); }
public static int[] radixSort(int[] f, int n)
{
int[] to = new int[n];
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(f[i]&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[f[i]&0xffff]++] = f[i];
int[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(f[i]>>>16)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[f[i]>>>16]++] = f[i];
int[] d = f; f = to;to = d;
}
return f;
}
public static void main(String[] args) {
new Thread(null, new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
String debug = args.length > 0 ? args[0] : null;
if (debug != null) {
try {
is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
solve();
out.flush();
tr((System.currentTimeMillis() - start) + "ms");
}
}, "", 64000000).start();
}
private static java.io.InputStream is = System.in;
private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
private static java.util.StringTokenizer tokenizer = null;
private static java.io.BufferedReader reader;
public static String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new java.util.StringTokenizer(reader.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
private static double nd() {
return Double.parseDouble(next());
}
private static long nl() {
return Long.parseLong(next());
}
private static int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private static char[] ns() {
return next().toCharArray();
}
private static long[] nal(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private static int[][] ntable(int n, int m) {
int[][] table = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
table[i][j] = ni();
}
}
return table;
}
private static int[][] nlist(int n, int m) {
int[][] table = new int[m][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
table[j][i] = ni();
}
}
return table;
}
private static int ni() {
return Integer.parseInt(next());
}
private static void tr(Object... o) {
if (is != System.in)
System.out.println(java.util.Arrays.deepToString(o));
}
}
| Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 2df76fde6e450a5c00f82a9aeeaec800 | train_002.jsonl | 1533047700 | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. | 256 megabytes |
import java.util.*;
public class A_B_and_team_trainin{
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
long n=scn.nextInt();
long m=scn.nextInt();
PriorityQueue<Long> pq=new PriorityQueue<>(Collections.reverseOrder());
long sum=0;
for(int i=0;i<n;i++){
long a=scn.nextInt();
long b=scn.nextInt();
sum+=a;
pq.add(a-b);
}
int cnt=0;
while(pq.size()>0 && m<sum){
long comp=pq.poll();
sum-=comp;
cnt++;
}
if(m>=sum){
System.out.println(cnt);
}else{
System.out.println(-1);
}
}
} | Java | ["4 21\n10 8\n7 4\n3 1\n5 4", "4 16\n10 8\n7 4\n3 1\n5 4"] | 1 second | ["2", "-1"] | NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | Java 8 | standard input | [
"sortings"
] | 91541d10c5ae52be8da33412359bd019 | The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) β the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) β the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. | 1,100 | If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. | standard output | |
PASSED | 66c3f33a3730c254eb3928d218743fc0 | train_002.jsonl | 1476522300 | Several years ago Tolya had n computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD in clockwise order. The names were distinct, the length of each name was equal to k. The names didn't overlap.Thus, there is a cyclic string of length nΒ·k written on the CD.Several years have passed and now Tolya can't remember which games he burned to his CD. He knows that there were g popular games that days. All of the games he burned were among these g games, and no game was burned more than once.You have to restore any valid list of games Tolya could burn to the CD several years ago. | 512 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf727e {
public static void main(String[] args) throws IOException {
int n = rni(), k = ni(), ind[] = new int[2 * n * k];
fill(ind, -1);
char[] s1 = rcha(), s = new char[2 * n * k];
for (int i = 0; i < n * k; ++i) {
s[i] = s[n * k + i] = s1[i];
}
int g = ri();
ACTrie ac = new ACTrie();
for (int i = 0; i < g; ++i) {
ac.insert(rcha());
}
ac.build();
ac.qry(s, ind);
Set<Integer> set = new HashSet<>();
List<Integer> ans = new ArrayList<>();
next: for (int i = 0; i < k; ++i) {
set.clear();
ans.clear();
for (int j = 0; j < n; ++j) {
int end = (j + 1) * k + i - 1;
if (set.contains(ind[end]) || ind[end] == -1) {
continue next;
} else {
set.add(ind[end]);
ans.add(ind[end] + 1);
}
}
prY();
prln(ans);
close();
return;
}
prN();
close();
}
static class ACTrie {
ACTrie chd[] = new ACTrie[26], fail;
int lbl = -1, wrdcntr = 0;
ACTrie() {
fill(chd, this);
fail = this;
}
ACTrie(boolean root) {}
void insert(char[] s) {
ACTrie node = this;
int n = s.length;
for (int i = 0; i < n; ++i) {
int next = s[i] - 'a';
if (node.chd[next] == this) {
node.chd[next] = new ACTrie(false);
fill(node.chd[next].chd, this);
node.chd[next].fail = this;
}
node = node.chd[next];
}
node.lbl = wrdcntr++;
}
void build() {
Queue<ACTrie> bfs = new LinkedList<>();
for (int i = 0; i < 26; ++i) {
if (chd[i] != this) {
bfs.offer(chd[i]);
}
}
while (!bfs.isEmpty()) {
ACTrie node = bfs.poll();
for (int i = 0; i < 26; ++i) {
if (node.chd[i] != this) {
node.chd[i].fail = node.fail.chd[i];
bfs.offer(node.chd[i]);
} else {
node.chd[i] = node.fail.chd[i];
}
}
}
}
void qry(char[] s, int[] ind) {
ACTrie node = this;
int n = s.length;
for (int i = 0; i < n; ++i) {
node = node.chd[s[i] - 'a'];
for (ACTrie j = node; j != this && j.lbl >= 0; j = j.fail) {
ind[i] = j.lbl;
}
}
}
}
/* static class ACTrie {
int max, cntr = 1, wrdcntr = 0, trie[][], fail[], lbl[];
ACTrie() {
this(1000000);
}
ACTrie(int m) {
max = m;
trie = new int[max][26];
fail = new int[max];
// default label: inserted order
lbl = new int[max];
fill(lbl, -1);
}
void insert(char[] s) {
int node = 0, n = s.length;
for (int i = 0; i < n; ++i) {
int chd = s[i] - 'a';
if (trie[node][chd] == 0) {
trie[node][chd] = cntr++;
}
node = trie[node][chd];
}
lbl[node] = wrdcntr++;
}
void build() {
Queue<Integer> bfs = new LinkedList<>();
for (int i = 0; i < 26; ++i) {
if (trie[0][i] > 0) {
bfs.offer(trie[0][i]);
}
}
while (!bfs.isEmpty()) {
int node = bfs.poll();
for (int i = 0; i < 26; ++i) {
if (trie[node][i] > 0) {
fail[trie[node][i]] = trie[fail[node]][i];
bfs.offer(trie[node][i]);
} else {
trie[node][i] = trie[fail[node]][i];
}
}
}
}
// returns in order matches by their label (inserted order by default)
void qry(char[] s, int[] ind) {
int node = 0, n = s.length;
for (int i = 0; i < n; ++i) {
node = trie[node][s[i] - 'a'];
for (int j = node; j > 0 && lbl[j] >= 0; j = fail[j]) {
ind[i] = lbl[j];
}
}
}
} */
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __rand = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int)d;}
static int cei(double d) {return (int)ceil(d);}
static long fll(double d) {return (long)d;}
static long cel(double d) {return (long)ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int lcm(int a, int b) {return a * b / gcf(a, b);}
static long lcm(long a, long b) {return a * b / gcf(a, b);}
static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
static long hash(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// graph util
static List<List<Integer>> g(int n) {List<List<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;}
static List<Set<Integer>> sg(int n) {List<Set<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;}
static void c(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);}
static void cto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);}
static void dc(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);}
static void dcto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);}
// input
static void r() throws IOException {input = new StringTokenizer(__in.readLine());}
static int ri() throws IOException {return Integer.parseInt(__in.readLine());}
static long rl() throws IOException {return Long.parseLong(__in.readLine());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()) - 1; return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;}
static char[] rcha() throws IOException {return __in.readLine().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());}
static int ni() {return Integer.parseInt(input.nextToken());}
static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());}
static long nl() {return Long.parseLong(input.nextToken());}
static List<List<Integer>> rg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
static void rg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
static List<List<Integer>> rdg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
static void rdg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
static List<Set<Integer>> rsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}
static void rsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}
static List<Set<Integer>> rdsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}
static void rdsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {__out.println("yes");}
static void pry() {__out.println("Yes");}
static void prY() {__out.println("YES");}
static void prno() {__out.println("no");}
static void prn() {__out.println("No");}
static void prN() {__out.println("NO");}
static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next()); else __out.println();}
static void h() {__out.println("hlfd");}
static void flush() {__out.flush();}
static void close() {__out.close();}
} | Java | ["3 1\nabc\n4\nb\na\nc\nd", "4 2\naabbccdd\n4\ndd\nab\nbc\ncd"] | 4 seconds | ["YES\n2 1 3", "NO"] | null | Java 11 | standard input | [
"data structures",
"string suffix structures",
"hashing",
"strings"
] | ff5b3d5fa52c2505556454011e8a0f58 | The first line of the input contains two positive integers n and k (1ββ€βnββ€β105, 1ββ€βkββ€β105)Β β the amount of games Tolya burned to the CD, and the length of each of the names. The second line of the input contains one string consisting of lowercase English lettersΒ β the string Tolya wrote on the CD, split in arbitrary place. The length of the string is nΒ·k. It is guaranteed that the length is not greater than 106. The third line of the input contains one positive integer g (nββ€βgββ€β105)Β β the amount of popular games that could be written on the CD. It is guaranteed that the total length of names of all popular games is not greater than 2Β·106. Each of the next g lines contains a single stringΒ β the name of some popular game. Each name consists of lowercase English letters and has length k. It is guaranteed that the names are distinct. | 2,300 | If there is no answer, print "NO" (without quotes). Otherwise, print two lines. In the first line print "YES" (without quotes). In the second line, print n integersΒ β the games which names were written on the CD. You should print games in the order they could have been written on the CD, it means, in clockwise order. You can print games starting from any position. Remember, that no game was burned to the CD more than once. If there are several possible answers, print any of them. | standard output | |
PASSED | 9ce1fa4461b3105d55a1f39a7188f498 | train_002.jsonl | 1374913800 | Gerald plays the following game. He has a checkered field of size nβΓβn cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for nβ-β1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: At least one of the chips at least once fell to the banned cell. At least once two chips were on the same cell. At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. | 256 megabytes | import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
/**
* User: xy6er
* Date: 27.07.13
* Time: 13:58
*/
public class D {
private Scanner sc;
int n, m;
int[] x, y;
int ans;
public static void main(String[] args) throws Exception {
D clazz = new D();
clazz.input();
clazz.solve();
clazz.output();
}
public void input() throws Exception {
sc = new Scanner(System.in);
//sc = new Scanner(new File("input.txt"));
n = sc.nextInt();
m = sc.nextInt();
x = new int[m];
y = new int[m];
for (int i = 0; i < m; i++) {
x[i] = sc.nextInt();
y[i] = sc.nextInt();
}
}
public void solve() {
boolean[] useRow, useCol;
useRow = new boolean[n];
useCol = new boolean[n];
Arrays.fill(useRow, true);
Arrays.fill(useCol, true);
useRow[0] = false; useRow[n - 1] = false;
useCol[0] = false; useCol[n - 1] = false;
for (int i = 0; i < m; i++) {
useRow[x[i] - 1] = false;
useCol[y[i] - 1] = false;
}
ans = 0;
for (int i = 1; i < n; i++) {
if(useRow[i]) {
ans++;
}
if(useCol[i]) {
ans++;
}
}
if(n % 2 == 1 && useRow[n / 2] && useCol[n / 2]) {
ans--;
}
}
public void output() {
System.out.println(ans);
}
}
| Java | ["3 1\n2 2", "3 0", "4 3\n3 1\n3 2\n3 3"] | 1 second | ["0", "1", "1"] | NoteIn the first test the answer equals zero as we can't put chips into the corner cells.In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). | Java 7 | standard input | [
"two pointers",
"implementation",
"greedy"
] | a26a97586d4efb5855aa3b930e9effa7 | The first line contains two space-separated integers n and m (2ββ€βnββ€β1000, 0ββ€βmββ€β105) β the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1ββ€βxi,βyiββ€βn) β the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β from left to right from 1 to n. | 1,800 | Print a single integer β the maximum points Gerald can earn in this game. | standard output | |
PASSED | 7631f35b7ad38e18e82b9794c8fc8ea6 | train_002.jsonl | 1374913800 | Gerald plays the following game. He has a checkered field of size nβΓβn cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for nβ-β1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: At least one of the chips at least once fell to the banned cell. At least once two chips were on the same cell. At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. | 256 megabytes |
import java.io.*;
import java.util.*;
public class D {
FastScanner in;
PrintWriter out;
void solve() {
int n = in.nextInt(), m = in.nextInt();
boolean r[] = new boolean[n + 1], c[] = new boolean[n + 1];
for (int i = 0; i < m; i++) {
int x = in.nextInt(), y = in.nextInt();
r[y] = true;
c[x] = true;
}
int ans = 0, nn = 0;
if ((n & 1) == 1) {
nn = (n + 1) / 2;
ans += (!c[nn] || !r[nn]) ? 1 : 0;
}
for (int i = 2; i <= n - 1; i++) {
if (nn == i) continue;
ans += (!c[i]) ? 1 : 0;
ans += (!r[i]) ? 1 : 0;
}
out.print(ans);
}
public static void main(String args[]) {
new D().runIO();
}
void run() {
try {
in = new FastScanner(new File("Timus.in"));
out = new PrintWriter(new File("Timus.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
FastScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| Java | ["3 1\n2 2", "3 0", "4 3\n3 1\n3 2\n3 3"] | 1 second | ["0", "1", "1"] | NoteIn the first test the answer equals zero as we can't put chips into the corner cells.In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). | Java 7 | standard input | [
"two pointers",
"implementation",
"greedy"
] | a26a97586d4efb5855aa3b930e9effa7 | The first line contains two space-separated integers n and m (2ββ€βnββ€β1000, 0ββ€βmββ€β105) β the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1ββ€βxi,βyiββ€βn) β the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β from left to right from 1 to n. | 1,800 | Print a single integer β the maximum points Gerald can earn in this game. | standard output | |
PASSED | 2df2237171d5d44d86ecf3598f940d04 | train_002.jsonl | 1374913800 | Gerald plays the following game. He has a checkered field of size nβΓβn cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for nβ-β1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: At least one of the chips at least once fell to the banned cell. At least once two chips were on the same cell. At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class D {
public static void main(String[] args) throws Exception{
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int m = in.nextInt();
boolean[] badR = new boolean[n];
boolean[] badC = new boolean[n];
badR[0] = true;
badR[n-1] =true;
badC[0] = true;
badC[n-1] =true;
for(int i = 0; i < m; i++){
int r = in.nextInt()-1;
int c = in.nextInt()-1;
badR[r] = true;
badC[c] = true;
}
Map<Integer, Integer> tally = new HashMap<Integer, Integer>();
int count = 0;
for(int i = 0 ; i < n ;i++){
if(i==n-1-i){
//middle, can't swap
if(!badR[i] || !badC[i])
count++;
continue;
}
int k = Math.min(i, n-1-i);
Integer prev = tally.get(k);
if(prev == null)
prev = 0;
if(!badR[i]){
prev++;
}
if(!badC[i]){
prev++;
}
tally.put(k, prev);
}
for(Entry<Integer, Integer> i : tally.entrySet()){
int v = i.getValue();
if(v==1){
count++;
}else if(v==2){
count+=2;
}else if(v==3){
count+=3;
}else if(v==4){
count+=4;
}
}
out.println(count);
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextToken() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["3 1\n2 2", "3 0", "4 3\n3 1\n3 2\n3 3"] | 1 second | ["0", "1", "1"] | NoteIn the first test the answer equals zero as we can't put chips into the corner cells.In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). | Java 7 | standard input | [
"two pointers",
"implementation",
"greedy"
] | a26a97586d4efb5855aa3b930e9effa7 | The first line contains two space-separated integers n and m (2ββ€βnββ€β1000, 0ββ€βmββ€β105) β the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1ββ€βxi,βyiββ€βn) β the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β from left to right from 1 to n. | 1,800 | Print a single integer β the maximum points Gerald can earn in this game. | standard output | |
PASSED | 31ce1056566cf421afba81a440c13d73 | train_002.jsonl | 1374913800 | Gerald plays the following game. He has a checkered field of size nβΓβn cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for nβ-β1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: At least one of the chips at least once fell to the banned cell. At least once two chips were on the same cell. At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class D {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = bf.readLine();
int n = Integer.parseInt(str.split(" ")[0]);
int m = Integer.parseInt(str.split(" ")[1]);
int[] ver = new int[n];
int[] hor = new int[n];
for(int i = 1 ; i < n -1 ; i++){
ver[i] = 1;
hor[i] = 1;
}
for(int i = 0 ; i < m ; i ++){
str = bf.readLine();
int x = Integer.parseInt(str.split(" ")[0]);
int y = Integer.parseInt(str.split(" ")[1]);
hor[x-1] = 0;
ver[y-1] = 0;
}
int rs = 0;
if(n % 2 == 1 && hor[n/2] == 1 && ver[n/2] == 1){
rs = -1;
}
for(int i = 0 ; i < n ; i ++){
if(ver[i] == 1){
rs ++;
}
if(hor[i] == 1){
rs ++;
}
}
System.out.println(rs);
}
}
| Java | ["3 1\n2 2", "3 0", "4 3\n3 1\n3 2\n3 3"] | 1 second | ["0", "1", "1"] | NoteIn the first test the answer equals zero as we can't put chips into the corner cells.In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). | Java 7 | standard input | [
"two pointers",
"implementation",
"greedy"
] | a26a97586d4efb5855aa3b930e9effa7 | The first line contains two space-separated integers n and m (2ββ€βnββ€β1000, 0ββ€βmββ€β105) β the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1ββ€βxi,βyiββ€βn) β the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β from left to right from 1 to n. | 1,800 | Print a single integer β the maximum points Gerald can earn in this game. | standard output | |
PASSED | 519101c7d9c1937a6ad6c6309964ee3b | train_002.jsonl | 1374913800 | Gerald plays the following game. He has a checkered field of size nβΓβn cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for nβ-β1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: At least one of the chips at least once fell to the banned cell. At least once two chips were on the same cell. At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. | 256 megabytes | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Sabelan
*/
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);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
boolean[] bannedRow = new boolean[n], bannedCol = new boolean[n];
for (int i = 0; i < m; i++) {
int x = in.nextInt() - 1;
int y = in.nextInt() - 1;
bannedRow[y] = true;
bannedCol[x] = true;
}
int ans = 0;
for (int i = 1; i < n - 1; i++) {
if (i * 2 + 1 == n) {
if (!bannedRow[i] || !bannedCol[i])
ans++;
continue;
}
if (!bannedRow[i]) ans++;
if (!bannedCol[i]) ans++;
}
out.println(ans);
}
}
| Java | ["3 1\n2 2", "3 0", "4 3\n3 1\n3 2\n3 3"] | 1 second | ["0", "1", "1"] | NoteIn the first test the answer equals zero as we can't put chips into the corner cells.In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). | Java 7 | standard input | [
"two pointers",
"implementation",
"greedy"
] | a26a97586d4efb5855aa3b930e9effa7 | The first line contains two space-separated integers n and m (2ββ€βnββ€β1000, 0ββ€βmββ€β105) β the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1ββ€βxi,βyiββ€βn) β the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β from left to right from 1 to n. | 1,800 | Print a single integer β the maximum points Gerald can earn in this game. | standard output | |
PASSED | 4703dadfcb581e7b5e4f9a1aed493490 | train_002.jsonl | 1374913800 | Gerald plays the following game. He has a checkered field of size nβΓβn cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for nβ-β1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: At least one of the chips at least once fell to the banned cell. At least once two chips were on the same cell. At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. | 256 megabytes | import java.io.*;
import java.util.*;
public class D {
private static StringTokenizer tokenizer;
private static BufferedReader bf;
private static PrintWriter out;
private static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private static String nextToken() throws IOException {
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(bf.readLine());
}
return tokenizer.nextToken();
}
public static void main(String[] args) throws IOException {
bf = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = nextInt(); int m = nextInt();
boolean[][] e = new boolean[n+1][n+1];
boolean[] r = new boolean[n+1];
boolean[] c = new boolean[n+1];
for(int i = 0; i < m; i++) {
int x = nextInt();
int y = nextInt();
e[x][y] = true; r[x] = true; c[y] = true;
}
int res = 0;
for(int i = 2; i <= n-1; i++) {
if(!r[i]) res++;
if(!c[i]) res++;
}
if(n%2 == 1) {
if(!r[n/2+1] && !c[n/2+1]) res--;
}
out.print(res);
out.close();
}
} | Java | ["3 1\n2 2", "3 0", "4 3\n3 1\n3 2\n3 3"] | 1 second | ["0", "1", "1"] | NoteIn the first test the answer equals zero as we can't put chips into the corner cells.In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). | Java 7 | standard input | [
"two pointers",
"implementation",
"greedy"
] | a26a97586d4efb5855aa3b930e9effa7 | The first line contains two space-separated integers n and m (2ββ€βnββ€β1000, 0ββ€βmββ€β105) β the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1ββ€βxi,βyiββ€βn) β the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β from left to right from 1 to n. | 1,800 | Print a single integer β the maximum points Gerald can earn in this game. | standard output | |
PASSED | 17c2125595075dd7291bdab5fedf588b | train_002.jsonl | 1374913800 | Gerald plays the following game. He has a checkered field of size nβΓβn cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for nβ-β1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: At least one of the chips at least once fell to the banned cell. At least once two chips were on the same cell. At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. | 256 megabytes | import java.io.*;
import java.util.*;
public class D {
private static StringTokenizer tokenizer;
private static BufferedReader bf;
private static PrintWriter out;
private static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private static String nextToken() throws IOException {
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(bf.readLine());
}
return tokenizer.nextToken();
}
public static void main(String[] args) throws IOException {
bf = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = nextInt(); int m = nextInt();
boolean[] r = new boolean[n+1];
boolean[] c = new boolean[n+1];
for(int i = 0; i < m; i++) {
int x = nextInt();
int y = nextInt();
r[x] = true; c[y] = true;
}
int res = 0;
for(int i = 2; i <= n-1; i++) {
if(!r[i]) res++;
if(!c[i]) res++;
}
if(n%2 == 1) {
if(!r[n/2+1] && !c[n/2+1]) res--;
}
out.print(res);
out.close();
}
} | Java | ["3 1\n2 2", "3 0", "4 3\n3 1\n3 2\n3 3"] | 1 second | ["0", "1", "1"] | NoteIn the first test the answer equals zero as we can't put chips into the corner cells.In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). | Java 7 | standard input | [
"two pointers",
"implementation",
"greedy"
] | a26a97586d4efb5855aa3b930e9effa7 | The first line contains two space-separated integers n and m (2ββ€βnββ€β1000, 0ββ€βmββ€β105) β the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1ββ€βxi,βyiββ€βn) β the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β from left to right from 1 to n. | 1,800 | Print a single integer β the maximum points Gerald can earn in this game. | standard output | |
PASSED | 3a782ae6d0d9e443cdc4570edbce6854 | train_002.jsonl | 1374913800 | Gerald plays the following game. He has a checkered field of size nβΓβn cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for nβ-β1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: At least one of the chips at least once fell to the banned cell. At least once two chips were on the same cell. At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. | 256 megabytes | /**
* Created with IntelliJ IDEA.
* User: IAIN
* Date: 9/08/13
* Time: 11:03 AM
* To change this template use File | Settings | File Templates.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Chips {
void solve() throws IOException {
int n = getInt();
int m = getInt();
boolean [] x = new boolean[n];
boolean [] y = new boolean[n];
for(int i = 0; i < m; ++i){
int a = getInt();
int b = getInt();
x[a - 1] = true;
y[b - 1] = true;
}
int cnt = 0;
for(int i = 1; i < n / 2; ++i){
if(!x[i])cnt++;
if(!x[n - i - 1])cnt++;
if(!y[i])cnt++;
if(!y[n - i - 1])cnt++;
}
if(n % 2 != 0){
if(!x[n / 2] || !y[n / 2]){
cnt++;
}
}
System.out.println(cnt);
}
BufferedReader bufferedReader;
PrintWriter output;
StringTokenizer tokenizer;
public static void main(String[] args) throws IOException {
Chips contest = new Chips();
contest.run();
}
public String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(bufferedReader.readLine());
}
return tokenizer.nextToken();
}
public int getInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long getLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
void run() throws IOException {
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
output = new PrintWriter(System.out);
solve();
}
}
| Java | ["3 1\n2 2", "3 0", "4 3\n3 1\n3 2\n3 3"] | 1 second | ["0", "1", "1"] | NoteIn the first test the answer equals zero as we can't put chips into the corner cells.In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). | Java 7 | standard input | [
"two pointers",
"implementation",
"greedy"
] | a26a97586d4efb5855aa3b930e9effa7 | The first line contains two space-separated integers n and m (2ββ€βnββ€β1000, 0ββ€βmββ€β105) β the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1ββ€βxi,βyiββ€βn) β the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns β from left to right from 1 to n. | 1,800 | Print a single integer β the maximum points Gerald can earn in this game. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.