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 | 3ffcf55839379a6e5da2140a161a3cea | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.*;
import java.util.Arrays;
public class solve
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int t=in.nextInt();
int flag;
while(t!=0)
{
t--;
int n=in.nextInt();
int x[]=new int[n];
int y[]=new int[n];
int z[]=new int[n];
int x1,y1;
for(int i=0;i<n;i++)
{
x[i]=in.nextInt();
y[i]=in.nextInt();
z[i]=x[i]^y[i];
}
Arrays.sort(x);
Arrays.sort(y);
Arrays.sort(z);
int z1[]=new int[n];
for(int i=0;i<n;i++)
{
z1[i]=x[i]^y[i];
}
Arrays.sort(z1);
x1=0;y1=0;
if(Arrays.equals(z,z1))
{
System.out.println("YES");
for(int i=0;i<n;i++)
{
for(int j=x1;j<x[i];j++)
{
System.out.print("R");
}
x1=x[i];
for(int j=y1;j<y[i];j++)
{
System.out.print("U");
}
y1=y[i];
}
System.out.println();
}
else
System.out.println("NO");
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 5e9d3336acda47de9614156f9873add4 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.*;
import java.util.Arrays;
public class solve
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int t=in.nextInt();
int flag;
while(t!=0)
{
t--;
int n=in.nextInt();
int x[]=new int[n];
int y[]=new int[n];
int z[]=new int[n];
int x1,y1;
for(int i=0;i<n;i++)
{
x[i]=in.nextInt();
y[i]=in.nextInt();
z[i]=x[i]^y[i];
}
Arrays.sort(x);
Arrays.sort(y);
Arrays.sort(z);
int z1[]=new int[n];
for(int i=0;i<n;i++)
{
z1[i]=x[i]^y[i];
}
Arrays.sort(z1);
x1=0;y1=0;
if(Arrays.equals(z,z1))
{
System.out.println("YES");
for(int i=0;i<n;i++)
{
for(int j=x1;j<x[i];j++)
{
System.out.print("R");
}
x1=x[i];
for(int j=y1;j<y[i];j++)
{
System.out.print("U");
}
y1=y[i];
}
System.out.println();
}
else
System.out.println("NO");
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 8a48b1c59a80d3c2dc02fd6e38a4ea2d | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
public class TEST{
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
while(T-->0) {
int n=sc.nextInt();
int a[][]=new int [n][2];
for(int i=0;i<n;i++) {
a[i][0]=sc.nextInt();
a[i][1]=sc.nextInt();
}
a=f(a,n);
a=c(a,n);
/*for(int i=0;i<n;i++) {
System.out.println(a[i][0]+" "+a[i][1]);
}System.out.println("-----------");*/
String ans="";
int sx=0,sy=0;
boolean t=true;
for(int i=0;i<n;i++) {
if(a[i][0]-sx<0||a[i][1]-sy<0) {
t=false;
break;
}else {
int mx=a[i][0]-sx;
int my=a[i][1]-sy;
for(int j=0;j<mx;j++) {
ans+='R';
}
for(int j=0;j<my;j++) {
ans+='U';
}
sx=a[i][0];
sy=a[i][1];
}
}
if(t) {
System.out.println("YES");
System.out.println(ans);
}else {
System.out.println("NO");
}
}
}
public static int[][] f(int [][] a,int n){
for(int i=0;i<n;i++) {
for(int j=i;j<n;j++) {
if(a[i][0]>a[j][0]) {
int m1=a[i][0];
int m11=a[i][1];
a[i][0]=a[j][0];a[i][1]=a[j][1];
a[j][0]=m1;a[j][1]=m11;
}
}
}
return a;
}
public static int[][] c(int [][] a,int n){
for(int i=0;i<n;i++) {
for(int j=i;j<n;j++) {
if(a[i][1]>a[j][1]) {
int m1=a[i][0];
int m11=a[i][1];
a[i][0]=a[j][0];a[i][1]=a[j][1];
a[j][0]=m1;a[j][1]=m11;
}
}
}
return a;
}
}
| Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | c649f272a0dc32cb8c6bfbeb6bd36b0f | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
int p[][] = new int[n][2];
for(int i=0;i<n;i++) {
p[i][0] = sc.nextInt();
p[i][1] = sc.nextInt();
}
Arrays.sort(p, new Comparator<int[]>() {
@Override
public int compare(int a[],int b[]) {
int x=a[0];
int y=b[0];
if(Integer.compare(x, y)==0) {
int t1 = a[1];
int t2 = b[1];
return Integer.compare(t1, t2);
}else {
return Integer.compare(x, y);
}
}
});
StringBuilder sb = new StringBuilder();
boolean ok = true;
int x = 0, y = 0;
for(int i=0;i<n;i++) {
if(p[i][1] < y) {
ok = false;
break;
}
while(x < p[i][0]) {
sb.append("R");
x++;
}
while(y < p[i][1]) {
sb.append("U");
y++;
}
}
if(ok) {
System.out.println("YES");
System.out.println(sb);
}
else {
System.out.println("NO");
}
}
}
}
| Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 143c955732e4650a176cfd7fe42a1ab8 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.*;
import java.io.*;
public class GFG {
static class Pair implements Comparable<Pair>{
int row;
int col;
public Pair(int r, int c){
row = r;
col = c;
}
public int compareTo(Pair p){
if(this.row != p.row){
return this.row - p.row;
}
return this.col - p.col;
}
}
static Pair[] P = new Pair[1010];
static StringBuilder ans = new StringBuilder();
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-- > 0){
int n = sc.nextInt();
for(int i = 0; i < n; i++){
P[i] = new Pair(sc.nextInt(),sc.nextInt());
}
Arrays.sort(P,0,n);
solve(n);
}
System.out.print(ans);
}
static void solve(int n){
boolean flag = true;
int r = 0, c = 0;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++){
if(r <= P[i].row && c <= P[i].col){
for(int j = r; j < P[i].row; j++){
sb.append("R");
}
r = P[i].row;
for(int j = c; j < P[i].col; j++){
sb.append("U");
}
c = P[i].col;
}else{
flag = false;
break;
}
}
if(flag){
ans.append("YES\n");
ans.append(sb.toString());
ans.append("\n");
}else{
ans.append("NO\n");
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | fdaf0290341c1d73efc32b9cd3ab7466 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes |
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
for(int i = 0; i < num; i++)
{
int numPoint = scanner.nextInt();
int[][] numPointLine = new int[numPoint + 1][2];
for(int j = 1; j <= numPoint; j++)
{
numPointLine[j][0] = scanner.nextInt();
numPointLine[j][1] = scanner.nextInt();
}
int haveNo = 0;
for(int j = numPoint; j > 0; j--)
{
for(int k = 0; k < j; k++)
{
if(numPointLine[k][0] > numPointLine[k + 1][0])
{
int[] temp = numPointLine[k];
numPointLine[k] = numPointLine[k + 1];
numPointLine[k + 1] = temp;
}
else if(numPointLine[k][0] == numPointLine[k + 1][0] && numPointLine[k][1] > numPointLine[k + 1][1])
{
int[] temp = numPointLine[k];
numPointLine[k] = numPointLine[k + 1];
numPointLine[k + 1] = temp;
}
}
if(haveNo == 1)
{
break;
}
}
for(int j = 0; j < numPoint; j++)
{
if(numPointLine[j][0] < numPointLine[j + 1][0] && numPointLine[j][1] > numPointLine[j + 1][1])
{
haveNo = 1;
break;
}
}
if(haveNo == 1)
{
System.out.println("NO");
continue;
}
System.out.println("YES");
for(int j = 0; j < numPoint; j++)
{
int last1 = numPointLine[j + 1][0] - numPointLine[j][0];
for(int k = 0; k < last1; k++)
{
System.out.print("R");
}
int last2 = numPointLine[j + 1][1] - numPointLine[j][1];
for(int k = 0; k < last2; k++)
{
System.out.print("U");
}
}
System.out.println();
}
}
}
| Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 500021c21aebd9830531473e3251114a | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes |
import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args) throws Exception {
// FileInputStream inputStream = new FileInputStream("input.txt");
// FileOutputStream outputStream = new FileOutputStream("output.txt");
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
int t = in.nextInt();
// int t = 1;
for (int i = 0 ; i < t ; i++) {
solver.solve(1, in, out);
}
out.close();
}
static class Solver {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[][] a = new int[1111][1111];
int[] r = new int[1111];
int[] c = new int[1111];
for (int i = 0 ; i < n ; i++) {
int x = in.nextInt();
int y = in.nextInt();
a[x][y] = 1;
r[y] += 1;
c[x] += 1;
}
StringBuilder sb = new StringBuilder("");
int x = 0;
int y = 0;
while (n > 0) {
if (a[x][y] == 1) {
a[x][y] = 0;
r[y] -= 1;
c[x] -= 1;
n -= 1;
}
if (n == 0) break;
if (r[y] > 0 && c[x] > 0) {
out.println("NO");
return;
}
if (r[y] > 0) {
x += 1;
sb.append("R");
} else if (c[x] > 0) {
y += 1;
sb.append("U");
} else {
x += 1;
sb.append("R");
}
}
out.println("YES");
out.println(sb.toString());
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 35f7d5983fef1047ad48b62a4bd2dcb6 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args) throws Exception {
//FileInputStream inputStream = new FileInputStream("input.txt");
//FileOutputStream outputStream = new FileOutputStream("output.txt");
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(1, in, out);
out.close();
}
static class Solver {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int t = in.nextInt();
for (int q = 0; q < t; q++) {
int n = in.nextInt();
Point[] p = new Point[n];
for (int i = 0; i < n; i++) {
p[i] = new Point(in.nextInt(), in.nextInt());
}
Arrays.sort(p);
int c = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
if (p[i].x < p[j].x && p[i].y > p[j].y) {
c = 1;
break;
}
if (c == 1) break;
}
if (c == 1) {
out.println("NO");
continue;
}
StringBuilder s = new StringBuilder();
for (int i = 0; i < p[0].x; i++)
s.append('R');
for (int i = 0; i < p[0].y; i++)
s.append('U');
for (int i = 1; i < n; i++) {
for (int j = 0; j < p[i].x-p[i - 1].x; j++)
s.append('R');
for (int j = 0; j < p[i].y-p[i - 1].y; j++)
s.append('U');
}
out.println("YES");
out.println(s);
}
}
class Point implements Comparable<Point> {
int x;
int y;
Point(int _x, int _y) {
x = _x;
y = _y;
}
public int compareTo(Point other) {
if (x != other.x) return x - other.x;
return y - other.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.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 4d50fd5d61489feca664f2793f702994 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args) throws Exception {
//FileInputStream inputStream = new FileInputStream("input.txt");
//FileOutputStream outputStream = new FileOutputStream("output.txt");
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(1, in, out);
out.close();
}
static class Solver {
public static void qs(int[] a, int[] b, int first, int last) {
int i = first;
int j = last;
int k = a[(i+j)/2];
while (i <= j) {
while (a[i] < k) i++;
while (a[j] > k) j--;
if (i <= j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
temp = b[i];
b[i] = b[j];
b[j] = temp;
i++; j--;
}
}
if (i < last) qs(a, b, i, last);
if (j > first) qs(a, b, first, j);
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
int t = in.nextInt();
for (int q = 0; q < t; q++) {
int n = in.nextInt();
Point[] p = new Point[n];
for (int i = 0; i < n; i++) {
p[i] = new Point(in.nextInt(), in.nextInt());
}
Arrays.sort(p);
int c = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
if (p[i].x < p[j].x && p[i].y > p[j].y) {
c = 1;
break;
}
if (c == 1) break;
}
if (c == 1) {
out.println("NO");
continue;
}
StringBuilder s = new StringBuilder();
for (int i = 0; i < p[0].x; i++)
s.append('R');
for (int i = 0; i < p[0].y; i++)
s.append('U');
for (int i = 1; i < n; i++) {
for (int j = 0; j < p[i].x-p[i - 1].x; j++)
s.append('R');
for (int j = 0; j < p[i].y-p[i - 1].y; j++)
s.append('U');
}
out.println("YES");
out.println(s);
}
}
class Point implements Comparable<Point> {
int x;
int y;
Point(int _x, int _y) {
x = _x;
y = _y;
}
public int compareTo(Point other) {
if (x != other.x) return x - other.x;
return y - other.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.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 2f597676eb22cd8c1351beda03c8b02f | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.*;
public class Mohammad {
public static class xy{
int x;
int y;
xy(int x, int y){ this.x = x; this.y = y;}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
ArrayList<xy> l=new ArrayList();
for (int i = 0; i < n; i++)
l.add(new xy(sc.nextInt(),sc.nextInt()));
Collections.sort(l,new Comparator<xy>(){
public int compare(xy obj, xy obj2) {return obj.x - obj2.x + obj.y - obj2.y;}
});
int x = 0, y = 0;
boolean f = true;
String s = "";
for (int i = 0; i < l.size(); i++) {
if(l.get(i).x < x || l.get(i).y < y){
System.out.println("NO");
f = false;
break;
}
int r = l.get(i).x - x;
while(r-->0){
s += "R";}
int u = l.get(i).y - y;
while(u-->0){
s += "U";}
x = l.get(i).x;
y = l.get(i).y;
}
if(f)
System.out.println("YES\n" + s);
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 18d22e4339212504c6a5ae133f9e558c | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.*;
import javafx.util.*;
public class Mohammad {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
ArrayList<Pair<Integer,Integer>> l=new ArrayList();
for (int i = 0; i < n; i++)
l.add(new Pair(sc.nextInt(),sc.nextInt()));
Collections.sort(l,new Comparator<Pair<Integer, Integer>>(){
@Override
public int compare(Pair<Integer, Integer> obj, Pair<Integer, Integer> obj2) {
return obj.getKey() - obj2.getKey() + obj.getValue() - obj2.getValue();
}
});
int x = 0, y = 0;
boolean f = true;
String s = "";
for (int i = 0; i < l.size(); i++) {
if(l.get(i).getKey() < x || l.get(i).getValue() < y){
System.out.println("NO");
f = false;
break;
}
int r = l.get(i).getKey() - x;
while(r-->0){
s += "R";}
int u = l.get(i).getValue() - y;
while(u-->0){
s += "U";}
x = l.get(i).getKey();
y = l.get(i).getValue();
}
if(f)
System.out.println("YES\n" + s);
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | b8ec7c80982cfcd1652a1e50225503fe | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.*;
public class Wael {
public static class point {
int x;
int y;
public point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for (int i = 0; i < t; i++) {
int n = input.nextInt();
ArrayList<point> a = new ArrayList<>();
for (int j = 0; j < n; j++)
a.add(new point(input.nextInt(), input.nextInt()));
Collections.sort(a, (point obj, point obj2) -> obj.x - obj2.x + obj.y - obj2.y);
String ans = "";
boolean result = true;
boolean ys;
int oldPx = 0, lastPX = 0, lastPY = 0;
for (int j = 0; j < n; j++) {
if (lastPX > a.get(j).x) {
result = false;
break;
} else if (lastPX < a.get(j).x) {
for (int k = 0; k < a.get(j).x - lastPX; k++)
ans += "R";
oldPx = lastPX;
lastPX = a.get(j).x;
}
if (lastPY > a.get(j).y) {
result = false;
break;}
if (lastPY != a.get(j).y)
for (int k = 0; k < a.get(j).y - lastPY; k++)
ans += "U";
lastPY = a.get(j).y;
}
System.out.println(!result ? "NO" : "YES\n" + ans);
}
}
}
/* wael melhem */ | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | d2887275a88fdbad6c0ea6dac34031ef | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class RobotPacket 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);
}
}
// if modulo is required set value accordingly
public static long[][] matrixMultiply2dL(long t[][],long m[][])
{
long res[][]= new long[t.length][m[0].length];
for(int i=0;i<t.length;i++)
{
for(int j=0;j<m[0].length;j++)
{
res[i][j]=0;
for(int k=0;k<t[0].length;k++)
{
res[i][j]+=t[i][k]+m[k][j];
}
}
}
return res;
}
static long combination(long n,long r)
{
long ans=1;
for(long i=0;i<r;i++)
{
ans=(ans*(n-i))/(i+1);
}
return ans;
}
public static void sortbyColumn(int arr[][], int col)
{
Arrays.sort(arr, new Comparator<int[]>()
{
public int compare(int[] o1, int[] o2){
return(Integer.valueOf(o1[col]).compareTo(o2[col]));
}
});
}
public static void main(String args[]) throws Exception
{
new Thread(null, new RobotPacket(),"RobotPacket",1<<27).start();
}
// **just change the name of class from Main to reuquired**
public void run()
{
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int tc=sc.nextInt();
while(tc-->0)
{
int n=sc.nextInt();
Point p[]= new Point[n];
for(int i=0;i<n;++i)
{
p[i]= new Point(sc.nextInt(),sc.nextInt());
}
Arrays.sort(p,new sortingPoint());
/*for(int i=0;i<n;++i)
{
System.out.println(p[i].x+" "+p[i].y);
}
*/
boolean f=true;
StringBuilder ans = new StringBuilder();
int curx=0,cury=0;
for(int i=0;i<n;++i)
{
if(p[i].x!=curx && p[i].y<cury)
{
f=false;
break;
}
while(curx<p[i].x)
{
curx++;
ans.append("R");
}
while(cury<p[i].y)
{
cury++;
ans.append("U");
}
}
if(f)
{
w.println("YES");
w.println(ans);
}
else w.println("NO");
}
System.out.flush();
w.close();
}
}
class Point
{
int x;
int y;
Point(int a, int b)
{
x=a;
y=b;
}
}
class sortingPoint implements Comparator<Point>
{
public int compare(Point p1, Point p2)
{
if(p1.x==p2.x) return p1.y-p2.y;
else return p1.x-p2.x;
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | c01ea0a608680ad478f44c2ac9ced252 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | //author @markysans
import java.util.*;
import java.io.*;
public class b{
static Scanner sc=new Scanner(System.in);
static int cnt;
public static void main(String args[]){
int k=1;
k=sc.nextInt();
while(k--!=0){
solve();
}
}
static void solve(){
int n=sc.nextInt();
int r[][]=new int[n][2];
for(int i=0;i<n;i++){
r[i][0]=sc.nextInt();
r[i][1]=sc.nextInt();
}
Arrays.sort(r, (a, b) -> a[0] - b[0]);
String s="";
int x=0,ymax=0,ymin=0;
for(int i=0;i<n;i++){
int x1=r[i][0];
int y1=r[i][1];
if(x1!=x)
ymin=ymax;
if((x!=x1 && y1<ymax)||(x==x1 && y1<ymin) ){
System.out.println("NO");
return;
}
for(int j=1;j<=(x1-x);j++)
s=s+"R";
for(int j=1;j<=(y1-ymax);j++)
s=s+"U";
x=x1;
ymax=Math.max(ymax,y1);
// System.out.println(x+"*"+y);
}
System.out.println("YES");
System.out.println(s);
// for (int i = 0; i < r.length; i++) {
// for (int j = 0; j < r[i].length; j++)
// System.out.print(r[i][j] + " ");
// System.out.println();
// }
}
static int[] initarray(int n){
int A[]=new int[n];
for(int i=0;i<n;i++)
A[i]=sc.nextInt();
return A;
}
}
// static ArrayList<Integer> initlist(int n){
// ArrayList<Integer> A=new ArrayList<Integer>();
// for(int i=0;i<n;i++)
// A.add(sc.nextInt());
// return A;
// }
// static long[] initarray2(int n){
// long A[]=new long[n];
// for(int i=0;i<n;i++)
// A[i]=sc.nextLong();
// return A;
// }
// static ArrayList<Long> initlist2(long n){
// ArrayList<Long> A=new ArrayList<Long>();
// for(long i=0;i<n;i++)
// A.add(sc.nextLong());
// return A;
// }
//System.out.println();
// A.add(5);
// A.remove(5);
// A.get(5);
// A.set(1,5);
// A.indexOf(5);
// A.lastIndexOf(5);
// A.subList(2,4); | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 50489c4df7d6670de813b5de85317162 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.*;
public class d3prac
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int t,i;
t=sc.nextInt();
while(t>0)
{
t--;
int n=sc.nextInt();
String ans="";
int a[][]=new int[1001][1001];
for(i=0;i<n;i++)
{
a[sc.nextInt()][sc.nextInt()]=1;
}
int cp=0;
int p;
int rem=n;
String pos="YES";
for(i=0;i<=1000;i++)
{
int max=0;
p=-1;
for(int j=0;j<=1000;j++)
{
if(a[i][j]==1)
{
if(j<cp)
{
pos="NO";
break;
}
}
if(a[i][j]==1)
{
max++;
for(int k=cp;k<j;k++)
ans+="U";
cp=j;
}
}
rem-=max;
//for(int j=0;j<p;j++)
//ans+="U";
if(rem==0)
break;
else
ans+="R";
}
if(pos.equals("NO"))
{
System.out.println("NO");
continue;
}
else
{
System.out.println("YES");
System.out.println(ans);
}
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | ebae47a9c761fb7cd4bc6f0f3fa88117 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes |
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectingPackages {
public static PrintWriter out;
public static void main(String[] args) throws IOException {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
// ------------------------------(SOLUTION - STARTS)------------------------------------------
int t = sc.nextInt();
int data[] = new int[3];
String yes = "YES";
String no = "NO";
while (t-- != 0) {
int n = sc.nextInt();
List<Node> list = new ArrayList<>();
list.add(new Node(0, 0));
while (n-- != 0) {
int x = sc.nextInt();
int y = sc.nextInt();
list.add(new Node(x, y));
}
Collections.sort(list, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
return o1.d < o2.d ? -1 : 1;
}
});
StringBuilder ans = new StringBuilder();
for (int i = 0; i < list.size() - 1; i++) {
Node a = list.get(i);
Node b = list.get(i + 1);
if (b.x >= a.x && b.y >= a.y) {
ans.append(getCopy(b.x - a.x, "R")).append(getCopy(b.y - a.y, "U"));
} else {
ans = null;
break;
}
}
if (ans == null) {
out.println("NO");
} else {
out.println("YES");
out.println(ans);
}
}
//--------------------------------(SOLUTION - ENDS)-------------------------------------------
out.close();
}
//======================================================================(UTILITY METHODS)============================================================================
private static String getCopy(int x, String s) {
StringBuilder b = new StringBuilder();
for (int i = 1; i <= x; i++)
b.append(s);
return b.toString();
}
static class Node {
int x, y;
long d;
Node(int x, int y) {
this.x = x;
this.y = y;
d = (x * x) + (y * y);
}
}
//======================================================================(FAST INPUT UTILS)===========================================================================
static class MyScanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public MyScanner() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public MyScanner(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
}
| Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | f34889d5bffcd79dd85009e8ea9b3308 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import javafx.util.Pair;
import java.io.*;
import java.util.*;
public class TASK2 {
FastScanner scanner;
PrintStream writer;
public final String INPUT_FILE_NAME = "input.txt";
public final String OUTPUT_FILE_NAME = "output.txt";
public void run() {
try {
writer = System.out;//new PrintWriter(new File(OUTPUT_FILE_NAME));
scanner = new FastScanner(System.in);//new FastScanner(new File(INPUT_FILE_NAME));
solution();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader reader;
StringTokenizer tokenizer;
FastScanner(File file) throws FileNotFoundException {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
}
FastScanner(InputStream is) {
reader = new BufferedReader(new InputStreamReader(is));
}
boolean hasNext() {
boolean result = false;
if (tokenizer != null && tokenizer.hasMoreTokens()) {
result = true;
} else {
String newLine = null;
try {
newLine = reader.readLine();
while(newLine != null) {
newLine = newLine.trim();
if (!newLine.equals("")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (newLine != null) {
result = true;
tokenizer = new StringTokenizer(newLine);
}
}
return result;
}
String next() {
hasNext();
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
public static void main(String[] args) {
new TASK2().run();
}
public final int MAX_SIZE = 1001;
public boolean point_in_rect(int rect_x, int rect_y, int x, int y) {
boolean result = false;
if (x >= rect_x && y >= rect_y) {
result = true;
}
return result;
}
public boolean all_in_rect(Set<Pair<Integer, Integer>> set, int l, int t) {
boolean result = true;
for(Pair<Integer, Integer> pair : set) {
if (!point_in_rect(l, t, pair.getKey(), pair.getValue())) {
result = false;
break;
}
}
return result;
}
public void solution() throws IOException {
int t = scanner.nextInt();
for(int set_of_data = 0; set_of_data < t; set_of_data++) {
int n = scanner.nextInt();
Set<Pair<Integer,Integer>> set = new HashSet<>();
for(int i = 0; i < n; ++i) {
int x = scanner.nextInt();
int y = scanner.nextInt();
set.add(new Pair<Integer, Integer>(x, y));
}
String path = "";
boolean path_exist = true;
String add_to_path = "";
int x = 0;
int y = 0;
while (true) {
if (set.contains(new Pair<Integer, Integer>(x, y))) {
path += add_to_path;
add_to_path = "";
set.remove(new Pair<Integer, Integer>(x, y));
}
if (set.isEmpty()) {
break;
}
if (x + 1 < MAX_SIZE && all_in_rect(set, x + 1, y)) {
x++;
add_to_path += "R";
} else if (y + 1 < MAX_SIZE && all_in_rect(set, x, y + 1)) {
y++;
add_to_path += "U";
} else {
path_exist = false;
break;
}
}
if (!path_exist) {
writer.println("NO");
} else {
writer.println("YES");
writer.println(path);
}
}
}
}
| Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | c6a63a38b2be8b996e3b2adff26bc6d3 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import javafx.util.Pair;
import java.io.*;
import java.util.*;
public class TASK2 {
FastScanner scanner;
PrintStream writer;
public final String INPUT_FILE_NAME = "input.txt";
public final String OUTPUT_FILE_NAME = "output.txt";
public void run() {
try {
writer = System.out;//new PrintWriter(new File(OUTPUT_FILE_NAME));
scanner = new FastScanner(System.in);//new FastScanner(new File(INPUT_FILE_NAME));
solution();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader reader;
StringTokenizer tokenizer;
FastScanner(File file) throws FileNotFoundException {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
}
FastScanner(InputStream is) {
reader = new BufferedReader(new InputStreamReader(is));
}
boolean hasNext() {
boolean result = false;
if (tokenizer != null && tokenizer.hasMoreTokens()) {
result = true;
} else {
String newLine = null;
try {
newLine = reader.readLine();
while(newLine != null) {
newLine = newLine.trim();
if (!newLine.equals("")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (newLine != null) {
result = true;
tokenizer = new StringTokenizer(newLine);
}
}
return result;
}
String next() {
hasNext();
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
public static void main(String[] args) {
new TASK2().run();
}
public final int MAX_SIZE = 1001;
public boolean point_in_rect(int rect_x, int rect_y, int x, int y) {
boolean result = false;
if (x >= rect_x && y >= rect_y) {
result = true;
}
return result;
}
public boolean all_in_rect(Set<Pair<Integer, Integer>> set, int l, int t) {
boolean result = true;
for(Pair<Integer, Integer> pair : set) {
if (!point_in_rect(l, t, pair.getKey(), pair.getValue())) {
result = false;
break;
}
}
return result;
}
public void solution() throws IOException {
int t = scanner.nextInt();
for(int set_of_data = 0; set_of_data < t; set_of_data++) {
int n = scanner.nextInt();
Set<Pair<Integer,Integer>> set = new HashSet<>();
for(int i = 0; i < n; ++i) {
int x = scanner.nextInt();
int y = scanner.nextInt();
set.add(new Pair<Integer, Integer>(x, y));
}
String path = "";
boolean path_exist = true;
String add_to_path = "";
int x = 0;
int y = 0;
while (true) {
if (set.contains(new Pair<Integer, Integer>(x, y))) {
path += add_to_path;
add_to_path = "";
set.remove(new Pair<Integer, Integer>(x, y));
}
if (x == MAX_SIZE - 1 && y == MAX_SIZE - 1) {
break;
}
if (x + 1 < MAX_SIZE && all_in_rect(set, x + 1, y)) {
x++;
add_to_path += "R";
} else if (y + 1 < MAX_SIZE && all_in_rect(set, x, y + 1)) {
y++;
add_to_path += "U";
} else {
path_exist = false;
break;
}
}
if (!path_exist) {
writer.println("NO");
} else {
writer.println("YES");
writer.println(path);
}
}
}
}
| Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 03b17068621d5c18946e00585c073c0f | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class S{
static class Pair implements Comparable<Pair>{
int a;
int b;
public Pair(int x,int y){a=x;b=y;}
public Pair(){}
public int compareTo(Pair p1){
if(a == p1.a)
return b - p1.b;
return a - p1.a;
}
}
static class TrieNode{
TrieNode[]child;
int w;
boolean term;
TrieNode(){
child = new TrieNode[26];
}
}
public static int gcd(int a,int b)
{
if(a<b)
return gcd(b,a);
if(b==0)
return a;
return gcd(b,a%b);
}
//static long ans = 0;
static long mod =(long)( 1e9 + 7);
public static void main(String[] args) throws Exception {
new Thread(null, null, "Anshum Gupta", 99999999) {
public void run() {
try {
solve();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
static void insert(TrieNode head,String str){
int n = str.length();
TrieNode cur = head;
for(int i=0;i<n;i++){
int ch = str.charAt(i)-'A';
if(cur.child[ch] == null)
cur.child[ch]=new TrieNode();
cur = cur.child[ch];
}
cur.term = true;
}
static boolean isRoot(TrieNode cur){
if(cur==root)return true;
return false;
}
static int f(TrieNode head){
int ans = 0;
for(int i=0;i<26;i++){
if(head.child[i]!=null){
ans += f(head.child[i]);
}
}
if(head.term)ans++;
if(ans >=2 && !isRoot(head))
ans-=2;
return ans;
}
static boolean done(int[]arr){
for(int i=1;i<arr.length;i++){
if(arr[i-1] > arr[i] )
return false;
}
return true;
}
static TrieNode root;
public static void solve() throws Exception {
// solve the problem here
MyScanner s = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out), true);
int t = s.nextInt();
int tc = 0;
while(tc++<t){
int n = s.nextInt();
Pair[]arr=new Pair[n];
for(int i=0;i<n;i++){
arr[i]=new Pair(s.nextInt(),s.nextInt());
}
Arrays.sort(arr);
String ans = "";
int fl = 0;
for(int i=1;i<n;i++){
if(arr[i].b < arr[i-1].b){
fl=1;break;
}
}
if(fl == 1){
out.println("NO");continue;
}
out.println("YES");
int x_dif = arr[0].a;
int y_dif = arr[0].b;
for(int j=0;j<x_dif;j++)ans+='R';
for(int j=0;j<y_dif;j++)ans+='U';
for(int i=1;i<n;i++){
y_dif = arr[i].b-arr[i-1].b;
x_dif = arr[i].a-arr[i-1].a;
for(int j=0;j<x_dif;j++)ans+='R';
for(int j=0;j<y_dif;j++)ans+='U';
}
out.println(ans);
}
out.flush();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 8c5a843beda80b1cfa7964b3c0e71dd7 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class CollectingPackages {
private static class Point {
private int x;
private int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
int getX() { return x; }
int getY() { return y; }
}
private static void appendDir(StringBuilder sb, String dir, int count) {
for(int i=0; i<count; i++)
sb.append(dir);
}
public static void main(String[] args) {
Scanner scanner = new Scanner();
int t = scanner.nextInt();
while(t > 0) {
int n = scanner.nextInt();
Point[] points = new Point[n];
for(int i=0; i<n; i++)
points[i] = new Point(scanner.nextInt(), scanner.nextInt());
Arrays.sort(points, Comparator.comparingInt(Point::getX).thenComparingInt(Point::getY));
boolean yes = true;
int diffX;
int diffY;
StringBuilder sb = new StringBuilder();
appendDir(sb, "R", points[0].getX());
appendDir(sb, "U", points[0].getY());
for(int i=1; i<points.length; i++) {
diffX = points[i].getX() - points[i-1].getX();
diffY = points[i].getY() - points[i-1].getY();
if(diffX < 0 || diffY < 0) {
yes = false;
break;
}
appendDir(sb, "R", diffX);
appendDir(sb, "U", diffY);
}
if(yes) {
System.out.println("YES");
System.out.println(sb.toString());
} else {
System.out.println("NO");
}
t--;
}
}
private static class Scanner {
private BufferedReader br;
private StringTokenizer st;
Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); }
String next() {
if(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 | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 47a241d2916dbfdd56b2c19d8e662024 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class CollectingPackages {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0) {
int packages = scanner.nextInt();
Pair[] arrPack = new Pair[packages + 1];
arrPack[0] = new Pair(0, 0);
for (int i = 1; i <= packages; i++) arrPack[i] = new Pair(scanner.nextInt(), scanner.nextInt());
Arrays.sort(arrPack, (Pair a, Pair b) -> {
if (a.y == b.y) return Integer.compare(a.x, b.x);
else return Integer.compare(a.y, b.y);
});
boolean isPossi = true;
StringBuilder answer = new StringBuilder("");
for (int i = 0; i < packages; i++) {
if (isPossible(arrPack[i], arrPack[i + 1])) {
answer.append(getString(arrPack[i], arrPack[i + 1]));
} else {
System.out.println("NO"); isPossi = false; break;
}
}
if (isPossi) {
System.out.println("YES");
System.out.println(answer);
}
}
}
private static String getString(Pair left, Pair right) {
// return "R".repeat(Math.max(0, right.x - left.x + 1)) +
// "U".repeat(Math.max(0, right.y - left.y + 1));
StringBuilder ans = new StringBuilder();
for (int i = left.x; i < right.x; i++) {
ans.append("R");
}
for (int i = left.y; i < right.y; i++) {
ans.append("U");
}
return ans.toString();
}
private static boolean isPossible(Pair leftPair, Pair rightPair) {
return leftPair.x <= rightPair.x && leftPair.y <= rightPair.y;
}
private static class Pair {
int x, y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
}
| Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 1aa4bb8b0caaac77b994d6cfb0238629 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | //package que_b;
import java.io.*;
import java.util.*;
public class utkarsh {
BufferedReader br;
PrintWriter out;
long mod = (long) (1e9 + 7), inf = (long) (3e18);
class pair {
int X, Y;
pair(int x, int y) {
X = x; Y = y;
}
}
void solve() {
int t = ni();
A: while(t-- > 0) {
int n = ni();
pair a[] = new pair[n];
for(int i = 0; i < n; i++) {
a[i] = new pair(ni(), ni());
}
Arrays.sort(a, (pair u, pair v) -> ((u.X != v.X) ? (u.X - v.X) : (u.Y - v.Y)));
ArrayList<Character> ans = new ArrayList<>();
int x = 0, y = 0, i = 0;
while(i < n) {
if(x < a[i].X) {
ans.add('R');
x++;
} else if(x > a[i].X) {
out.println("NO");
continue A;
} else if(y < a[i].Y) {
ans.add('U');
y++;
} else if(y > a[i].Y) {
out.println("NO");
continue A;
} else {
i++;
}
}
out.println("YES");
for(char c : ans) out.print(c);
out.println();
}
}
long mp(long b, long e) {
long r = 1;
while(e > 0) {
if( (e&1) == 1 ) r = (r * b) % mod;
b = (b * b) % mod;
e >>= 1;
}
return r;
}
// -------- I/O Template -------------
char nc() {
return ns().charAt(0);
}
String nLine() {
try {
return br.readLine();
} catch(IOException e) {
return "-1";
}
}
double nd() {
return Double.parseDouble(ns());
}
long nl() {
return Long.parseLong(ns());
}
int ni() {
return Integer.parseInt(ns());
}
StringTokenizer ip;
String ns() {
if(ip == null || !ip.hasMoreTokens()) {
try {
ip = new StringTokenizer(br.readLine());
} catch(IOException e) {
throw new InputMismatchException();
}
}
return ip.nextToken();
}
void run() {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.flush();
}
public static void main(String[] args) {
new utkarsh().run();
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 4b3ef2d27c25ed8554146a52cc70eb29 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
int p[][] = new int[n][2];
for(int i=0;i<n;i++) {
p[i][0] = sc.nextInt();
p[i][1] = sc.nextInt();
}
Arrays.sort(p, new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
if(a[0] == b[0])
return Integer.compare(a[1], b[1]);
else
return Integer.compare(a[0], b[0]);
}
});
StringBuilder sb = new StringBuilder();
boolean ok = true;
int x = 0, y = 0;
for(int i=0;i<n;i++) {
if(p[i][1] < y) {
ok = false;
break;
}
while(x < p[i][0]) {
sb.append("R");
x++;
}
while(y < p[i][1]) {
sb.append("U");
y++;
}
}
if(ok) {
System.out.println("YES");
System.out.println(sb);
}
else {
System.out.println("NO");
}
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 435110d7b8c881efbd9d06bcc1cb6d2e | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
int p[][] = new int[n][2];
for(int i=0;i<n;i++) {
p[i][0] = sc.nextInt();
p[i][1] = sc.nextInt();
}
Arrays.sort(p, (a, b) -> a[0] == b[0] ? Integer.compare(a[1], b[1]) : Integer.compare(a[0], b[0]));
StringBuilder sb = new StringBuilder();
boolean ok = true;
int x = 0, y = 0;
for(int i=0;i<n;i++) {
if(p[i][1] < y) {
ok = false;
break;
}
while(x < p[i][0]) {
sb.append("R");
x++;
}
while(y < p[i][1]) {
sb.append("U");
y++;
}
}
if(ok) {
System.out.println("YES");
System.out.println(sb);
}
else {
System.out.println("NO");
}
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 3d8b043b8be335837b2e877e58f42dab | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class CollectingPackages {
public static void main(String[] args) {
FastReader input=new FastReader();
int t=input.nextInt();
while(t-->0)
{
int n=input.nextInt();
int x[]=new int[n];
int y[]=new int[n];
for(int i=0;i<n;i++)
{
x[i]=input.nextInt();
y[i]=input.nextInt();
}
selectionSort(x,y);
int flag=0;
for(int i=0;i<n-1;i++)
{
if(x[i]==x[i+1])
{
if(y[i]>y[i+1])
{
int temp=y[i];
y[i]=y[i+1];
y[i+1]=temp;
}
}
if(y[i]>y[i+1])
{
flag=1;
}
}
if(flag==1)
{
System.out.println("NO");
}
else
{
System.out.println("YES");
int inx=0,iny=0;
for(int i=0;i<n;i++)
{
int d1=x[i]-inx;
int d2=y[i]-iny;
for(int j=0;j<d1;j++)
{
System.out.print('R');
}
for(int j=0;j<d2;j++)
{
System.out.print('U');
}
inx=x[i];
iny=y[i];
}
System.out.println();
}
}
}
public static void selectionSort(int a[],int b[])
{
for(int i=0;i<a.length-1;i++)
{
int k=i;
for(int j=i+1;j<a.length;j++)
{
if(a[k]>a[j])
{
int temp=a[j];
a[j]=a[k];
a[k]=temp;
temp=b[j];
b[j]=b[k];
b[k]=temp;
}
else if(a[k]==a[j])
{
int temp=a[j];
a[j]=a[k];
a[k]=temp;
if(b[k]>b[j])
{
temp=b[j];
b[j]=b[k];
b[k]=temp;
}
}
}
}
}
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 | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | a817df88dd32e2b5249b046106594117 | train_003.jsonl | 1579703700 | There is a robot in a warehouse and $$$n$$$ packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point $$$(0, 0)$$$. The $$$i$$$-th package is at the point $$$(x_i, y_i)$$$. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point $$$(x, y)$$$ to the point ($$$x + 1, y$$$) or to the point $$$(x, y + 1)$$$.As we say above, the robot wants to collect all $$$n$$$ packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string $$$s$$$ of length $$$n$$$ is lexicographically less than the string $$$t$$$ of length $$$n$$$ if there is some index $$$1 \le j \le n$$$ that for all $$$i$$$ from $$$1$$$ to $$$j-1$$$ $$$s_i = t_i$$$ and $$$s_j < t_j$$$. It is the standard comparison of string, like in a dictionary. Most programming languages compare strings in this way. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main
{
static class Pair{
int x,y;
Pair(int x, int y){
this.x = x; this.y = y;
}
}
List<Pair> pair = new ArrayList<>();
static class SortByRThenU implements Comparator<Pair>{
public int compare(Pair p1, Pair p2){
if(p1.x==p2.x){
return p1.y - p2.y;
}
return p1.x - p2.x;
}
}
public static void main(String[] args)throws Exception {
Main obj = new Main();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int diff,n,i,TEST = Integer.parseInt(in.readLine());
boolean flag;
String temp[];
while(TEST-->0){
flag = true;
obj.pair.clear();
obj.pair.add(new Pair(0,0));
StringBuffer out = new StringBuffer();
n = Integer.parseInt(in.readLine());
while(n-->0){
temp = in.readLine().split(" ");
obj.pair.add(new Pair(Integer.parseInt(temp[0]),Integer.parseInt(temp[1])));
}
Collections.sort(obj.pair, new SortByRThenU());
//System.out.println(obj.pair);
for(i=1;i<obj.pair.size();i++){
if (obj.pair.get(i).y < obj.pair.get(i-1).y){
flag = false;
System.out.println("NO");
break;
}
diff = obj.pair.get(i).x - obj.pair.get(i-1).x;
while(diff-->0) out.append("R");
diff = obj.pair.get(i).y - obj.pair.get(i-1).y;
while(diff-->0) out.append("U");
}
if(flag){
System.out.println("YES\n"+out.toString());
}
}
}
} | Java | ["3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3"] | 1 second | ["YES\nRUUURRRRUU\nNO\nYES\nRRRRUUU"] | NoteFor the first test case in the example the optimal path RUUURRRRUU is shown below: | Java 8 | standard input | [
"implementation",
"sortings"
] | b01c627abc00f97767c237e304f8c17f | The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then test cases follow. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of packages. The next $$$n$$$ lines contain descriptions of packages. The $$$i$$$-th package is given as two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le 1000$$$) — the $$$x$$$-coordinate of the package and the $$$y$$$-coordinate of the package. It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point $$$(0, 0)$$$ doesn't contain a package. The sum of all values $$$n$$$ over test cases in the test doesn't exceed $$$1000$$$. | 1,200 | Print the answer for each test case. If it is impossible to collect all $$$n$$$ packages in some order starting from ($$$0,0$$$), print "NO" on the first line. Otherwise, print "YES" in the first line. Then print the shortest path — a string consisting of characters 'R' and 'U'. Among all such paths choose the lexicographically smallest path. Note that in this problem "YES" and "NO" can be only uppercase words, i.e. "Yes", "no" and "YeS" are not acceptable. | standard output | |
PASSED | 612006f5349b034bbe1d8f18dc3b172b | train_003.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 in = new Scanner(System.in);
char[] s = in.next().toCharArray();
int[] d = new int[1000020];
int[] q = new int[1000020];
int top = 0;
Arrays.fill(d, -1);
for(int i = 0; i<s.length; i++)
if (s[i] == '('){
q[top] = i;
top++;
}
else if (top > 0){
top--;
if (q[top] == 0 || d[q[top]-1] == -1){
d[i] = q[top];
} else {
d[i] = d[q[top]-1];
}
}
int ans = 0, count = 1;
for(int i = 0; i<s.length; i++)
if (d[i] > -1)
if (i-d[i]+1 > ans){
ans = i-d[i]+1;
count = 1;
}
else if (i-d[i]+1 == ans)
count++;
System.out.println(ans+" "+count);
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 7d04ca14d122c304a184a4243cb26c6c | train_003.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.math.*;
import static java.lang.Math.*;
public class Main implements Runnable {
final int oo = Integer.MAX_VALUE/2;
private void solve() throws Exception {
char [] a = in.readLine().trim().toCharArray();
int n = a.length;
int [][] dp = new int [2][n+1];
dp[0][n-1] = dp[1][n-1] = 0;
for (int i = n - 2; i >= 0; i--)
if (isClose(a[i])) dp[0][i] = dp[1][i] = 0;
for (int i = n - 2; i >= 0; i--){
if(isClose(a[i])) continue;
if (isClose(a[i+1]) && isOpposite(a[i], a[i+1])){
dp[0][i] = 2 + dp[0][i+2];
dp[1][i] = 1 + dp[1][i+2];
}
if (isOpen(a[i+1])){
if (i+1+dp[0][i+1] < n && isOpposite(a[i], a[i+1+dp[0][i+1]] )){
dp[0][i] = 2 + dp[0][i+1] + dp[0][i+1+dp[0][i+1]+1];
dp[1][i] = 1 + dp[1][i+1+dp[1][i+1]+1];
}
}
}
int max = 0;
for (int i = 0; i < n; i++)
if (dp[0][i] > max) max = dp[0][i];
if (max == 0){
out.println("0 1");
return;
}
int count = 0;
for (int i = 0; i < n; i++) if (dp[0][i] == max) count++;
out.println(max + " " + count);
}
public boolean isOpposite(char c1, char c){
if (c1 == '<') return c == '>';
if (c1 == '[') return c == ']';
if (c1 == '{') return c == '}';
if (c1 == '(') return c == ')';
return false;
}
public boolean isClose(char c){
return c == ')' || c == '>' || c == ']' || c == '}';
}
public boolean isOpen(char c){
return c == '(' || c == '<' || c == '[' || c == '{';
}
BufferedReader in;
StringTokenizer st;
PrintWriter out;
StreamTokenizer stn;
private String next() throws Exception {
if (st == null || !st.hasMoreElements())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
private int nextInt() throws Exception {
if (fromStreamTokenizer)
return (int) nextDouble();
else
return Integer.parseInt(next());
}
private long nextLong() throws Exception {
if (fromStreamTokenizer)
return (long) nextDouble();
else
return Long.parseLong(next());
}
private double nextDouble() throws Exception {
if (fromStreamTokenizer) {
stn.nextToken();
return stn.nval;
} else
return Double.parseDouble(next());
}
private final boolean fromStreamTokenizer = false;
public void run() {
try {
if (fromStreamTokenizer)
stn = new StreamTokenizer(new InputStreamReader(System.in));
else
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
if (in != null)
in.close();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String... strings) {
new Thread(new Main()).start();
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | c3237d1a6b4c0a4dba24c0d016b448b1 | train_003.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 static java.lang.Math.*;
import static java.util.Arrays.*;
import java.io.*;
import java.util.*;
public class Main {
static boolean LOCAL = System.getSecurityManager() == null;
Scanner sc = new Scanner(System.in);
void run() {
char[] cs = sc.next().toCharArray();
int n = cs.length;
int[] is = new int[n + 1];
for (int i = 0; i < n; i++) is[i + 1] = (cs[i] == '(') ? 1 : -1;
for (int i = 0; i < n; i++) is[i + 1] += is[i];
int max = 0, num = 1;
int[] stack = new int[n + 2];
int sp = 0;
for (int i = 0; i <= n; i++) {
int j = i;
while (sp > 0 && is[stack[sp - 1]] >= is[i]) {
if (is[stack[sp - 1]] == is[i]) {
if (max < i - stack[sp - 1]) {
max = i - stack[sp - 1];
num = 1;
} else if (max == i - stack[sp - 1]) {
num++;
}
j = stack[sp - 1];
}
sp--;
}
stack[sp++] = j;
}
System.out.printf("%d %d%n", max, num);
}
class Scanner {
BufferedReader br;
StringTokenizer st;
Scanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
eat("");
}
void eat(String s) {
st = new StringTokenizer(s);
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
boolean hasNext() {
while (!st.hasMoreTokens()) {
String s = nextLine();
if (s == null) return false;
eat(s);
}
return true;
}
String next() {
hasNext();
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
void debug(Object...os) {
System.err.println(deepToString(os));
}
public static void main(String[] args) {
if (LOCAL) {
try {
// System.setIn(new FileInputStream("in.txt"));
} catch (Throwable e) {
LOCAL = false;
}
}
if (!LOCAL) {
try {
Locale.setDefault(Locale.US);
System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
} catch (Throwable e) {
}
}
new Main().run();
System.out.flush();
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | a1ab90f09c05422eb2628fd7bd0ed2b4 | train_003.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.IOException;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.ArrayList;
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.math.BigInteger;
import java.io.InputStream;
import java.util.PriorityQueue;
import java.util.HashMap;
import java.util.Stack;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.TreeSet;
public class Main{
public static void main(String []args)throws IOException{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
//BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
OutputWriter outt = new OutputWriter(outputStream);
StringBuilder out=new StringBuilder();
String g=in.readString();
char []s=g.toCharArray();
Stack<Integer> z=new Stack<Integer>();
int l=s.length;
int a[]=new int[l+5];
for(int i=0;i<l+3;i++){a[i]=-1;}
int max=0;String ans="";int count=0;
for(int i=0;i<l;i++){
if(s[i]=='('){
z.push(i);
}
else{
if(z.empty()){a[i]=-1;}
else{
a[i]=z.pop();
if(a[i]-1>=0&&a[a[i]-1]!=-1){a[i]=a[a[i]-1];}
if((i-a[i])>max){
count=0;
max=i-a[i];
ans=g.substring(a[i],i+1);
}
if((i-a[i])==max){count++;}
}
}
}
if(max==0){outt.printLine("0 1");}
else {
outt.printLine(ans.length()+" "+count);
}
outt.printLine();
outt.close();
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | cdd43a0486cba9dea0a67373d0fd2da8 | train_003.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) throws IOException
{
new Main().run();
}
void run() throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader input = new BufferedReader(new FileReader("input.in"));
char[] cs = input.readLine().toCharArray();
int n = cs.length;
ArrayList<Integer>[] list = new ArrayList[2 * n + 1];
for(int i = 0; i <= n << 1; i++) list[i] = new ArrayList<Integer>();
int[] f = new int[n + 1];
int[] a = new int[n + 1];
int[] count = new int[n + 1];
list[n].add(0);
for(int i = 1; i <= cs.length; i++)
{
if(cs[i - 1] == '(') a[i] = a[i - 1] + 1;
else a[i] = a[i - 1] - 1;
int j = i - 1;
while(j > 0 && a[i] <= a[j]) j = f[j];
f[i] = j;
int s = a[i] + n;
int low = 0, high = list[s].size() - 1;
while(low < high)
{
int mid = (low + high) >> 1;
if(list[s].get(mid) < f[i]) low = mid + 1;
else high = mid;
}
if(low < list[s].size() && list[s].get(low) >= f[i]) count[i - list[s].get(low)]++;
list[s].add(i);
}
count[0]++;
for(int i = n; i >= 0; i--)
if(count[i] > 0)
{
System.out.println(i + " " + count[i]);
break;
}
}
void print(Object...o)
{
System.out.println(Arrays.deepToString(o));
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 236a4e0e2e2fdefbf418e4dace6b955b | train_003.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 LongestDS {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Stack<Integer> stack = new Stack<Integer>();
String s = in.nextLine();
stack.push(-1);
int length = 0;
int a = 0;
int b = 0;
for(int i = 0 ;i < s.length();i++){
if(s.charAt(i) == '(')
stack.push(i);
else if(stack.size() > 1){
stack.pop();
length = i - stack.peek();
if(length > a){
a = length;
b = 1;
}
else if (length == a){
b += 1;
}
}
else{
stack.pop();
stack.push(i);
}
}
if(a == 0 && b == 0){
System.out.println(0 + " "+1);
return;
}
System.out.println(a + " "+b);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | ced1c4738e1baf0fd66146f265ef57bc | train_003.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.Stack;
public class C5 {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int k = 0, max = 0, l = -1;
Stack<Integer> st = new Stack<Integer>();
char[] c = bf.readLine().trim().toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == '(')
st.push(i);
else {
if (st.isEmpty())
l = i;
else {
st.pop();
int x = i - (st.isEmpty()? l : st.peek());
if (x == max) {
k++;
}
if (x > max) {
max = x;
k = 1;
}
}
}
}
if (max == 0)
k = 1;
System.out.println(max+" "+k);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | fd876fa5f1b34f78bb466066c086c3f5 | train_003.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.text.*;
import java.math.*;
import java.util.*;
public class Main {
private BufferedReader in;
private BufferedWriter out;
// )(()()))(())))
public void solve() throws Exception {
String s = in.readLine();
int[] x = new int[s.length() + 1];
int num = 0, first = 0;
int ans = 0, cnt = 1;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
x[num++] = first;
first = i + 1;
} else {
if (num == 0) {
first = i + 1;
continue;
}
int x1 = x[num - 1], x2 = i;
if (x2 - x1 + 1 > ans) {
ans = x2 - x1 + 1;
cnt = 1;
} else if (x2 - x1 + 1 == ans) {
cnt++;
}
first = x1;
num--;
}
}
out.write(ans + " " + cnt + "\n");
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new BufferedWriter(new OutputStreamWriter(System.out));
solve();
out.flush();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
new Main().run();
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | ee7d87c2a17a2486d645b8a052e57b5c | train_003.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;
public class Main {
static Scanner in = new Scanner(System.in);
public static void main(String... args) {
char[] s = in.nextLine().toCharArray();
int[] a = new int[1000010];
int ans = 0, ct = 1, x = 0;
for(char c : s) {
if(c == ')') {
if(x > 0) {
a[--x] += a[x + 1] + 2;
if(a[x] > ans) {
ans = a[x];
ct = 1;
} else if(a[x] == ans) {
++ct;
}
} else {
a[0] = 0;
}
} else {
a[++x] = 0;
}
}
System.out.printf("%d %d\n", ans, ct);
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 635b37e4cbf4d42cd6ad6029cdeda417 | train_003.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 a {
static long mod = 1000000007;
public static void main(String[] args) throws IOException
{
input.init(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String s = input.next();
int n = s.length();
Stack<Integer> stk = new Stack<Integer>();
int[] last = new int[n], first = new int[n];
Arrays.fill(first, -1);
for(int i = 0; i<n; i++)
{
char c = s.charAt(i);
if(c=='(') stk.push(i);
else
{
if(stk.isEmpty()) continue;
else
{
int l = stk.pop();
first[i] = last[i] = l;
if(l>0 && s.charAt(l-1) == ')' && first[l-1] != -1) first[i] = first[l-1];
}
}
//out.println(i+" "+first[i]);
}
int max = 0, count = 0;
for(int i = 0; i<n; i++)
{
if(first[i] == -1) continue;
int d = i - first[i] + 1;
if(d>max) {max = d; count=1;}
else if(d==max) count++;
}
if(max == 0) count = 1;
out.println(max+" "+count);
out.close();
}
static class Cup implements Comparable<Cup>
{
int x, i;
public Cup(int xx, int ii)
{
x = xx; i = ii;
}
@Override
public int compareTo(Cup o) {
// TODO(mkirsche): Auto-generated method stub
return this.x - o.x;
}
}
static class input {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
static String nextLine() throws IOException {
return reader.readLine();
}
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 647d448868b1ac7fe91ea1507a696154 | train_003.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;
public class Main {
/**
* @param args
*/
public static String line;
public static int[] x;
public static int n;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String line=sc.next();
x=new int[line.length()];
int v=0;
//System.out.println(Arrays.toString(x));
for (int i = x.length-1; i>=0; i--) {
char c=line.charAt(i);
if(c==')')v++;
else v--;
if(v<0){v++;
x[i]=1;}
}
//System.out.println(Arrays.toString(x));
v=0;
for (int i = 0; i<x.length; i++) {
char c=line.charAt(i);
if(c=='(')v++;
else v--;
if(v<0){v++;
x[i]=1;}
}
int len=0;
int num=1;
int tmp=0;
for (int i = 0; i < x.length; i++) {
if(x[i]==1)tmp=0;
else{
tmp++;
if(tmp==len)num+=1;
if(tmp>len){len=tmp;num=1;}
}
}
//System.out.println(Arrays.toString(x));
printFormat("%d %d", len, num);
// TODO Auto-generated method stub
}
public static void printFormat(String format, Object... args){
System.out.println(String.format(format, args));
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 0efc5f99cd8c6e05d796961c8f6a3b0f | train_003.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.lang.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class _P005C{
Scanner sc=new Scanner(System.in);
int INF=1<<28;
double EPS=1e-9;
String s;
void run(){
s=sc.nextLine();
solve();
}
void solve(){
int n=s.length();
int[] count=new int[n+1];
int len=0;
int stack=0;
for(int i=0; i<n; i++){
if(s.charAt(i)=='('){
len++;
stack++;
}else{
stack--;
if(stack<0){
count[len*2]++;
len=0;
stack=0;
}
}
}
if(stack==0){
count[len*2]++;
}else if(stack>0){
len=0;
stack=0;
for(int i=n-1; i>=0; i--){
if(s.charAt(i)==')'){
len++;
stack++;
}else{
stack--;
if(stack<0){
count[len*2]++;
len=0;
stack=0;
if(len>0){
break;
}
}
}
}
}
count[0]=1;
for(int i=n; i>=0; i--){
if(count[i]>0){
println(i+" "+count[i]);
break;
}
}
}
void println(String s){
System.out.println(s);
}
void print(String s){
System.out.print(s);
}
void debug(Object... os){
System.err.println(Arrays.deepToString(os));
}
public static void main(String[] args){
new _P005C().run();
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | cd586e0e577be15bea0464365760e024 | train_003.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.Vector;
/**
* Date: 19.02.2010
* Time: 14:56:28
*
* @author Sergey Bankevich (Sergey.Bankevich@gmail.com)
*/
public class C5 {
static BufferedReader in;
public static void main( String[] args ) throws IOException {
in = new BufferedReader( new InputStreamReader( System.in ) );
char[] c = in.readLine().toCharArray();
int r = 0;
int cnt = 1;
int[] left = new int[c.length];
boolean[] isEnd = new boolean[c.length];
int top = -1;
for ( int i = 0; i < c.length; i ++ ) {
char d = c[i];
if ( d == '(' ) {
top ++;
if ( i > 0 && ! isEnd[i - 1] ) {
left[top] = i;
}
} else {
if ( top >= 0 ) {
isEnd[i] = true;
if ( i - left[top] + 1 > r ) {
r = i - left[top] + 1;
cnt = 1;
} else if ( i - left[top] + 1 == r ) {
cnt ++;
}
top --;
}
}
/*System.out.println( d + " " + top + " " + r + " " + cnt );
for ( int j = 0; j < 4; j ++ ) {
System.out.print( left[j] + " " );
}
System.out.println();*/
}
System.out.println( r + " " + cnt );
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 16c513bddd55d7910e70b2c897f2f97e | train_003.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 Solution {
private BufferedReader in;
private PrintWriter out;
private StringTokenizer st;
void solve() throws IOException {
String s = next();
int maxCount = 1, max = 0;
ArrayList<Integer> startStack = new ArrayList<Integer>();
startStack.add(-1);
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == '(') {
startStack.add(i);
} else {
if (startStack.size() == 1) {
startStack.set(0, i);
} else {
startStack.remove(startStack.size() - 1);
int len = i - startStack.get(startStack.size() - 1);
if (len > max) {
max = len;
maxCount = 0;
}
if (len == max) {
++maxCount;
}
}
}
}
out.println(max + " " + maxCount);
}
Solution() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
eat("");
solve();
in.close();
out.close();
}
private void eat(String str) {
st = new StringTokenizer(str);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
new Solution();
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 610600db9e5017687f6aeec088130a61 | train_003.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 C implements Runnable {
public static void main(String[] args) {
new Thread(new C()).start();
}
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return "0";
}
}
return st.nextToken();
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.flush();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
final int maxB = 1000000;
final int maxN = (1 << 20);
void update(int x, int y) {
x += maxN;
tr[x] = y;
while (x > 1) {
x /= 2;
tr[x] = Math.min(tr[2 * x], tr[2 * x + 1]);
}
}
int getMin(int l, int r) {
l += maxN;
r += maxN;
if (l > r) {
return Integer.MAX_VALUE;
}
int ret = Integer.MAX_VALUE;
while (l <= r) {
if ((l & 1) == 1) {
ret = Math.min(ret, tr[l]);
l++;
}
if ((r & 1) == 0) {
ret = Math.min(ret, tr[r]);
r--;
}
l /= 2;
r /= 2;
}
return ret;
}
int[] tr;
void solve() {
char[] c = nextToken().toCharArray();
int[] mi = new int[maxB * 2 + 1];
int cur = maxB;
int difAns = 0;
Arrays.fill(mi, 3000000);
tr = new int[2 * maxN + 4];
Arrays.fill(tr, 2000000);
mi[maxB] = -1;
for (int i = 0; i < c.length; i++) {
if (c[i] == '(') {
cur++;
} else {
cur--;
}
if (mi[cur] < i && getMin(mi[cur] + 1, i - 1) >= cur) {
if (i - mi[cur] > difAns) {
difAns = i - mi[cur];
}
} else {
mi[cur] = i;
}
update(i, cur);
}
if (difAns == 0) {
out.println("0 1");
return;
}
Arrays.fill(tr, 2000000);
Arrays.fill(mi, 3000000);
cur = maxB;
int kol = 0;
mi[maxB] = -1;
for (int i = 0; i < c.length; i++) {
if (c[i] == '(') {
cur++;
} else {
cur--;
}
if (mi[cur] < i && getMin(mi[cur] + 1, i - 1) >= cur) {
if (i - mi[cur] == difAns) {
kol++;
}
} else {
mi[cur] = i;
}
update(i, cur);
}
out.println(difAns + " " + kol);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 1ee085a3bf63e71f3557cf677eacef05 | train_003.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.math.*;
public class Main implements Runnable {
StreamTokenizer ST;
PrintWriter out;
BufferedReader br;
Scanner in;
int inf = 1000000000;
double x = 1e-8;
int nextInt() throws IOException{
ST.nextToken();
return (int)ST.nval;
}
long nextLong() throws IOException{
ST.nextToken();
return (long)ST.nval;
}
String next() throws IOException{
ST.nextToken();
return ST.sval;
}
double nextD() throws IOException{
ST.nextToken();
return ST.nval;
}
public static void main(String[] args) throws IOException {
new Thread(new Main()).start();
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//in = new Scanner(br);
ST = new StreamTokenizer(br);
solve();
out.close();
//in.close();
br.close();
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
public void solve() throws IOException {
char[] s = br.readLine().toCharArray();
int n = s.length;
int next[] = new int[n];
Arrays.fill(next, -1);
Stack<Integer> st = new Stack<Integer>();
Vector<Integer> v = new Vector<Integer>();
for (int i=0; i<n; i++) if (s[i]=='(') st.push(i); else {
if (!st.isEmpty()) {
int pr = st.pop();
next[pr] = i;
if (st.isEmpty()) v.add(i-pr+1);
} else {
next[i] = -1;
}
}
int res = 0, cnt = 1;
int p = 0;
while (p<n) {
if (next[p]<0) {
p++; continue;
}
int x = 0;
while (p<n&& next[p]>0) {
x += next[p]-p+1;
p = next[p]+1;
}
if (x>res) {
res = x; cnt = 1;
} else if (x==res){
cnt++;
}
}
if (res==0) cnt = 1;
out.println(res+" "+cnt);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 8d7e7c4e12ba946899253210df253cc3 | train_003.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.*;
import java.lang.*;
import java.math.*;
import java.math.BigInteger;
public class Main {
final int INF = 1000000000;
final int MAXN = 100100;
Scanner input = new Scanner(System.in);
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
new Main().run();
}
void run() throws IOException {
String st = inp.readLine();
Stack<Data> stack = new Stack<Data>();
int res, cnt;
res = 0;
cnt = 1;
for (int i = 0; i < st.length(); ++i) {
if (st.charAt(i) == '(') {
stack.push(new Data(0, 0));
} else {
//out.println(i + " " + stack.size());
if (!stack.empty()) {
Data last = stack.pop();
if (last.len + 2 > res) {
res = last.len + 2;
cnt = 1;
} else {
if (last.len + 2 == res) {
++cnt;
}
}
if (!stack.empty()) {
Data temp = stack.pop();
stack.push(new Data(temp.len + last.len + 2, temp.seg + 1));
} else {
if (i + 1 < st.length() && st.charAt(i + 1) == '(') {
++i;
stack.push(new Data(last.len + 2, 1));
}
}
//out.println(i + " " + res);
}
}
}
while (!stack.empty()) {
Data temp = stack.pop();
if (temp.seg > 1) {
if (temp.len > res) {
res = temp.len;
cnt = 1;
} else {
if (temp.len == res) {
++cnt;
}
}
}
}
out.println(res + " " + cnt);
out.close();
}
class Data {
int len, seg;
Data(int len, int seg) {
this.len = len;
this.seg = seg;
}
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 011e37d212cf82f3c5b25dcd27817904 | train_003.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.*;
import java.lang.*;
import java.math.*;
import java.math.BigInteger;
public class Main {
final int INF = 1000000000;
final int MAXN = 100100;
Scanner input = new Scanner(System.in);
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
new Main().run();
}
void run() throws IOException {
String st = inp.readLine();
Stack<Data> stack = new Stack<Data>();
int res, cnt;
res = 0;
cnt = 1;
for (int i = 0; i < st.length(); ++i) {
if (st.charAt(i) == '(') {
stack.push(new Data(0, 0));
} else {
//out.println(i + " " + stack.size());
if (!stack.empty()) {
Data last = stack.pop();
if (last.len + 2 > res) {
res = last.len + 2;
cnt = 1;
} else {
if (last.len + 2 == res) {
++cnt;
}
}
if (!stack.empty()) {
Data temp = stack.pop();
stack.push(new Data(temp.len + last.len + 2, temp.seg + 1));
} else {
if (i + 1 < st.length() && st.charAt(i + 1) == '(') {
++i;
stack.push(new Data(last.len + 2, 1));
}
}
//out.println(i + " " + res);
}
}
}
while (!stack.empty()) {
Data temp = stack.pop();
if (temp.seg > 1) {
if (temp.len > res) {
res = temp.len;
cnt = 1;
} else {
if (temp.len == res) {
++cnt;
}
}
}
}
out.println(res + " " + cnt);
out.close();
}
class Data {
int len, seg;
Data(int len, int seg) {
this.len = len;
this.seg = seg;
}
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | b9870c8d12619cef5c2682c9e28ec47f | train_003.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;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author lolo
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char []brckt = in.nextLine().toCharArray();
int []avail = new int[brckt.length];
int p = 0, q = 0;
int max = 0;
int num = 1;
for (int i = 0; i < brckt.length; i++){
int len = 0;
if (brckt[i] == ')' && p < q){
if (q > 0 && brckt[i-1] == ')') q--;
if (q > 0)
len = i - avail[q-1] + 1;
} else if (brckt[i] == '(' && (q == 0 || brckt[i-1] == '(')) avail[q++] = i;
if (len > max){
max = len;
num = 1;
}else if (len == max && len != 0) num++;
}
System.out.println(max + " " + num);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 6a1c6e42ad74d1165b5e041966c96a24 | train_003.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;
public class Bracket {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()){
char []brckt = in.nextLine().toCharArray();
int []avail = new int[brckt.length];
int p = 0, q = 0;
int max = 0;
int num = 1;
for (int i = 0; i < brckt.length; i++){
int len = 0;
if (brckt[i] == ')' && p < q){
if (q > 0 && brckt[i-1] == ')') q--;
if (q > 0)
len = i - avail[q-1] + 1;
} else if (brckt[i] == '(' && (q == 0 || brckt[i-1] == '(')) avail[q++] = i;
if (len > max){
max = len;
num = 1;
}else if (len == max && len != 0) num++;
}
System.out.println(max + " " + num);
}
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 9ed97991d23e8187dbe6d0c6413e6832 | train_003.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.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.regex.*;
public class Main {
//StreamTokenizer in;
BufferedReader in;
PrintWriter out;
public static void main(String args[]) throws IOException {
new Main().run();
}
public void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
//in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
//in = new BufferedReader(new FileReader("input.txt"));
//out = new PrintWriter(new FileWriter("output.txt"));
solve();
out.flush();
}
private void solve() throws IOException {
String s = in.readLine();
int n = s.length();
int[] dp = new int[n];
for(int i = 0; i < n;) {
if(s.charAt(i) == ')') {
i++;
continue;
}
Stack<Integer> st = new Stack<Integer>();
st.push(0);
int t = i;
i++;
while(!st.empty() && i < n) {
if(s.charAt(i) == '(') st.push(i);
else {
int start = st.pop();
dp[i] = i - start + 1;
}
i++;
}
if(st.empty()) {
dp[i-1] =((t == 0) ? 0 : dp[t-1]) + i-t;
}
else {
for(int j = t; j < i; j++)
dp[j] += (j-dp[j] >= 0) ? dp[j-dp[j]] : 0;
}
}
int max = 0;
int count = 0;
for(int i = 0; i < n; i++) {
if(dp[i] > max) {
max = dp[i]; count = 1;
}
else if(dp[i] == max) {
count++;
}
}
if(max == 0) {
out.print("0 1");
}
else {
out.print(max); out.print(" "); out.print(count);
}
}
/*
void solve2() throws IOException {
int a = ni(), v = ni();
int l = ni(), d = ni(), w = ni();
double ret = 0;
if(w >= v) {
double h = (v*v*1.0)/(2*a);
if(h <= l) {
ret += (v*1.0)/a;
h = l - h;
ret += h / v;
}
else {
ret = Math.sqrt((2*l*1.0) / a);
}
}
else {
double vi = Math.sqrt(2*a*d);
if(vi <= w) {
double h = (v*v*1.0)/(2*a);
if(h <= l) {
ret += (v*1.0)/a;
h = l - h;
ret += h / v;
}
else {
ret = Math.sqrt((2*l*1.0) / a);
}
}
else {
double di = 2*w*w-4*a*d;
if(di >= 0) {
double t = (2*w + Math.sqrt(2*w*w - 4*a*d))/(2*a);
ret += t + (w - a*t)/a;
double h = l - d;
di = 4*w*w + 8*a*h;
ret += (-2*w + Math.sqrt(di))/(2*a);
}
else {
double h = (w*w*1.0)/(2*a);
ret += (w*1.0)/a;
ret += (-2*w + Math.sqrt(4*w*w + 4*a*d))/a;
h = l - d;
di = 4*w*w + 8*a*h;
ret += (-2*w + Math.sqrt(di))/(2*a);
}
}
}
out.print(new BigDecimal(ret).setScale(8, BigDecimal.ROUND_HALF_UP));
}
int ni() throws IOException {in.nextToken(); return (int)in.nval;}
*/
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 06cc3f886b1cfb9f22c337d7d9e7dd5a | train_003.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.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class C implements Runnable {
BufferedReader in;
BufferedWriter out;
public void solve() throws Exception
{
String s = readword();
int n = s.length();
int stack[] = new int[n];
int top = 0;
int length = 0;
int count = 1;
boolean isBreak = false;
for( int i = 0; i < n; i++ ) {
switch(s.charAt(i)) {
case '(':
if( i == 0 || s.charAt(i-1) == '(' || isBreak) {
stack[top] = i;
}
top++;
break;
case ')':
{
if( top != 0 ) {
top--;
int len = i - stack[top] + 1;
if( len > length ) {
length = len;
count = 1;
} else if( len == length ) {
count++;
}
isBreak = false;
} else isBreak = true;
}
break;
}
}
out.write(length + " " + count + "\n");
}
public String readword() throws IOException {
int c = in.read();
while( c >= 0 && c <= ' ' ) c = in.read();
if( c < 0 ) return "";
StringBuilder bld = new StringBuilder();
while( c > ' ' ) {
bld.append((char)c);
c = in.read();
}
return bld.toString();
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new BufferedWriter(new OutputStreamWriter(System.out));
solve();
out.flush();
} catch( Exception e ) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
new Thread(new C()).start();
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | bea28f577bae17bd9e77be450ab06f83 | train_003.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.math.*;
public class Solution implements Runnable {
private BufferedReader in;
private PrintWriter out;
private StringTokenizer st;
private Random rnd;
int[] max_len = new int[2];
int[] how_much = new int[2];
void updateAns(int len, int id) {
if(len > max_len[id]) {
max_len[id] = len;
how_much[id] = 1;
} else if(len != 0 && len == max_len[id]) {
++how_much[id];
}
}
void check(String line, int id) {
int opened = 0, cur_len = 0;
for(int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if(c == '(') {
++opened;
} else {
if(opened > 0) {
--opened;
cur_len += 2;
} else {
opened = 0;
cur_len = 0;
}
if(opened == 0) {
updateAns(cur_len, id);
}
}
}
}
public void solve() throws IOException {
how_much[0] = how_much[1] = 1;
String line = in.readLine();
StringBuilder rev = new StringBuilder();
for(int i = line.length() - 1; i >= 0; i--) {
if(line.charAt(i) == '(') rev.append(')');
else rev.append('(');
}
check(line, 0);
check(rev.toString(), 1);
if(max_len[0] > max_len[1]) {
out.println(max_len[0] + " " + how_much[0]);
} else if(max_len[1] > max_len[0]) {
out.println(max_len[1] + " " + how_much[1]);
} else {
out.println(max_len[0] + " " + Math.max(how_much[0], how_much[1]));
}
}
public static void main(String[] args) {
new Solution().run();
}
public void run() {
try {
//in = new BufferedReader(new FileReader("input.txt"));
//out = new PrintWriter(new FileWriter("output.txt"));
in = new BufferedReader(new InputStreamReader((System.in)));
out = new PrintWriter(System.out);
st = null;
rnd = new Random();
solve();
out.close();
} catch(IOException e) {
e.printStackTrace();
}
}
private String nextToken() throws IOException, NullPointerException {
while(st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | fb953e4de5f417d5e0a39f480c1f8a8f | train_003.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 C
{
public static void main(String[] args)
{
new C(new Scanner(System.in));
}
public C(Scanner in)
{
String s = in.next();
ArrayList<Node> vs = new ArrayList<Node>();
int i=0;
vs.add(new Node(i, 1));
for (char c : s.toCharArray())
{
i++;
if (c == '(')
vs.add(new Node(i, 0));
else
vs.add(new Node(i, 1));
}
int best = 0;
int cnt = 1;
ArrayDeque<Node> stk = new ArrayDeque<Node>();
for (Node n : vs)
{
if ((n.v == 0)||(stk.size() == 0))
stk.push(n);
else
{
Node tv = stk.pop();
if (tv.v != 0)
{
stk.push(tv);
stk.push(n);
continue;
}
Node pp = stk.pop(); stk.push(pp);
int len = n.i-pp.i;
if (best == len)
cnt++;
else if (len > best)
{
best = len;
cnt = 1;
}
}
}
System.out.printf("%d %d%n", best, cnt);
}
}
class Node
{
int i;
int v;
public Node(int a, int b)
{
i = a;
v = b;
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 175f89fa95eb6ef6437a85539b96e67e | train_003.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.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;
public class Main implements Runnable {
static Throwable sError;
public static void main(String[] args) throws Throwable {
Thread t = new Thread(new Main());
t.start();
t.join();
if (sError != null)
throw sError;
}
PrintWriter pw;
BufferedReader in;
StringTokenizer st;
void initStreams() throws FileNotFoundException,
UnsupportedEncodingException {
pw = new PrintWriter(System.out);
if (System.getProperty("ONLINE_JUDGE") == null) {
System.setIn(new FileInputStream("2"));
}
in = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-9"));
}
String nextString() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextString());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextString());
}
public void run() {
try {
initStreams();
int sp = 0;
char[] line = in.readLine().toCharArray();
boolean[] bad = new boolean[line.length];
int[] st = new int[line.length];
for (int i = 0; i < line.length; i++) {
if (line[i] == '(') {
st[sp++] = i;
} else {
if (sp > 0) {
--sp;
}
}
}
for (int i = 0; i < sp; i++) {
bad[st[i]] = true;
}
sp = 0;
int ans = 0, count = 0;
int current = 0;
for (int i = 0; i < line.length; i++) {
if (line[i] == '(') {
++sp;
if (bad[i])
current = 0;
else
current++;
} else if (line[i] == ')') {
if (sp > 0) {
sp--;
current++;
if (current > ans) {
ans = current;
count = 1;
} else if (current == ans) {
count++;
}
} else {
current = 0;
}
}
}
if (ans == 0) {
pw.println("0 1");
} else {
pw.print(ans);
pw.print(' ');
pw.println(count);
}
} catch (Throwable e) {
sError = e;
} finally {
pw.flush();
pw.close();
}
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | bb8b191d0a70323ce7f1c0a34694885b | train_003.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;
public class C {
public static void main(String[] args) throws IOException {
new C().solve();
}
private void solve() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
br.close();
int n = line.length();
int[] d = new int[n + 1];
Arrays.fill(d, -1);
int level = 0;
d[level] = 0;
int max = 0;
int count = 1;
for (int i = 0; i < line.length(); i++) {
final int delta = line.charAt(i) == '(' ? 1 : -1;
level += delta;
if (delta < 0) {
if (level + 1 >= 0) {
d[level + 1] = -1;
}
if (level >= 0 && d[level] != -1) {
int cur = i - d[level] + 1;
if (cur > max) {
max = cur;
count = 1;
} else if (cur == max) {
count++;
}
}
if (level < 0) {
d[level = 0] = i + 1;
}
} else {
if (d[level] == -1) {
d[level] = i + 1;
}
}
}
System.out.println(max + " " + count);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | bb2fa508e782e3e560b903af64422458 | train_003.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.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
* User: Jamshed
* Date: 4/23/12
* Time: 10:10 AM
*/
public class Brackets implements Runnable {
Scanner scanner;
PrintWriter pw;
public static void main(String arguments[]) {
new Thread(new Brackets()).start();
}
public void solution() {
String s = scanner.next();
int maxCount = 1;
int max = 0;
ArrayList<Integer> startStack = new ArrayList<Integer>();
startStack.add(-1);
for(int i = 0; i < s.length(); ++i) {
if(s.charAt(i) == '(') {
startStack.add(i);
} else {
if(startStack.size() == 1) {
startStack.set(0, i);
} else {
startStack.remove(startStack.size() - 1);
int len = i - startStack.get(startStack.size() - 1);
if (len > max) {
max = len;
maxCount = 0;
}
if (len == max) {
++maxCount;
}
}
}
}
pw.println(max + " " + maxCount);
}
public void printA(int A[]) {
for(int i = 0; i < A.length; i++) {
pw.print(A[i] + " ");
}
}
public void insertionSort(int A[]) {
int key;
int index;
for(int i = 0; i < A.length; i++) {
key = scanner.nextInt();
index = i - 1;
while(index > -1 && A[index] > key) {
A[index + 1] = A[index];
index--;
}
A[++index] = key;
}
}
public void run() {
scanner = new Scanner(System.in);
pw = new PrintWriter(System.out);
solution();
scanner.close();
pw.close();
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 2e9729f227c3b9140c0f80dedc8454a6 | train_003.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 C {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
Stack<Integer> S = new Stack<Integer>();
int[] ans = new int[s.length()];
Arrays.fill(ans, -1);
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == '(')
S.push(i);
else {
if (S.isEmpty())
continue;
int len = i - S.pop() + 1;
ans[i - len + 1] = len;
}
int max = 0;
int count = 1;
int res = 0;
for (int i = 0; i < s.length();) {
if (ans[i] < 0) {
i++;
res = 0;
} else {
res += ans[i];
i += ans[i];
if (res == max)
count++;
if (res > max) {
max = res;
count = 1;
}
}
}
System.out.println(max + " " + count);
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 65654cf580c0f7e5828942e731c1de82 | train_003.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.math.*;
import java.text.*;
public class Main {
String str;
int index[];
void run() throws Exception {
str = cin.next();
int i = 0, ans = 0, count = 1, len = str.length();
while (str.charAt(i) == ')' && i < len) i++;
if (i >= len) {
System.out.println("0 1");
return;
}
str = "(" + str.substring(i);
len = str.length();
index = new int[len];
boolean flag = true;
int temp = 0;
for (i = 1; i < len; i++) {
if (str.charAt(i) == '(') {
if (str.charAt(i - 1) == ')' && flag)
temp++;
else index[temp++] = i;
} else if (temp == 0) {
flag = false;
} else {
flag = true;
temp--;
if (i - index[temp] + 1 == ans)
count++;
else if (i - index[temp] + 1 > ans) {
ans = i - index[temp] + 1;
count = 1;
}
}
}
System.out.println(ans + " " + count);
}
public static void main(String[] args) throws Exception {
// InputStreamReader is = new InputStreamReader(new FileInputStream("E:/input.txt"));
// BufferedReader cin = new BufferedReader(is);
new Main().run();
// out.close();
}
// InputStreamReader is = new InputStreamReader(System.in);
// BufferedReader cin = new BufferedReader(is);
static InputStream inputStream = System.in;
static InputReader cin = new InputReader(inputStream);
// static OutputStream outputStream = System.out;
// static OutputWriter out = new OutputWriter(outputStream);
// Scanner cin = new Scanner(new BufferedInputStream(System.in));
}
class Tree {
int lt, rt;
long sum;
}
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)
return -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();
if (c == -1)
return -1;
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();
if (c == -1)
return -1;
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 next() {
StringBuilder str = new StringBuilder();
int ch;
while (isSpaceChar(ch = read()));
if (ch == -1)
return null;
do {
str.appendCodePoint(ch);
} while (!isSpaceChar(ch = read()));
return str.toString();
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
}
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 | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 8ae886d808f965b5a3e65db2a2676871 | train_003.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.Arrays;
import java.util.Stack;
import java.util.StringTokenizer;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Trung Pham
*/
public class CBeta5_Div2 {
public static void main(String[] args) {
Scanner in = new Scanner();
PrintWriter out = new PrintWriter(System.out);
String val = in.next();
int[] c = new int[val.length()];
int[] d = new int[val.length()];
Arrays.fill(d, -1);
Arrays.fill(c, -1);
Stack<Integer> stack = new Stack();
for (int i = 0; i < val.length(); i++) {
if (val.charAt(i) == '(') {
stack.push(i);
} else {
if (!stack.isEmpty()) {
d[i] = stack.pop();
c[i] = d[i];
if (d[i] - 1 > 0 && val.charAt(d[i] - 1) == ')' && c[d[i] - 1] != -1) {
c[i] = c[d[i] - 1];
}
}
}
}
int result = 1;
int max = 0;
for (int i = 0; i < c.length; i++) {
if (c[i] != -1) {
if (i - c[i] + 1 > max) {
max = i - c[i] + 1;
result = 1;
} else if (i - c[i] + 1 == max) {
result++;
}
}
}
out.println(max + " " + result);
out.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 498e74910a3b6bcb8630706d971d02d5 | train_003.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.math.*;
import java.text.*;
public class Main {
LinkedList<Integer> list = new LinkedList<Integer>();
String str;
int num[],MAX,count;
void run(){
str=cin.next();
num=new int[str.length()];
Arrays.fill(num, 0);
MAX=0;count=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='(')
list.add(i);
else{
if(list.size()!=0){
int t=(Integer)(list.getLast());
//System.out.println(t);
list.removeLast();
num[i]=i-t+1;
if(t!=0)num[i]+=num[t-1];
if(num[i]==MAX) count++;
if(num[i]>MAX){
MAX=num[i];
count=1;
}
}
}
}
if(MAX==0)
System.out.println(0+" "+1);
else
System.out.println(MAX+" "+count);
}
public static void main(String[] args){
new Main().run();
}
Scanner cin = new Scanner(new BufferedInputStream(System.in));
}
/*bpksraptqhjgheneikpnkngseoskiambhclfotqcsrircbbkei*/ | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 261bb48d9bb3f140f9ef67e458322ef4 | train_003.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.io.PrintWriter;
import java.util.Arrays;
import java.util.Stack;
public class ProblemC {
public static void main(String[] args) throws IOException {
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
char[] line = s.readLine().toCharArray();
int len = line.length;
int[] cl = new int[len+10];
Arrays.fill(cl, -1);
Stack<Integer> ops = new Stack<Integer>();
for (int i = 0 ; i < len ; i++) {
if (line[i] == '(') {
ops.add(i);
} else {
if (ops.size() >= 1) {
cl[ops.pop()] = i;
}
}
}
long max = 0;
long cnt = 1;
for (int i = 0 ; i < len ; i++) {
if (line[i] == '(') {
int now = i;
int from = i;
int to = i;
while (true) {
if (now >= len) {
to = now-1;
break;
}
if (line[now] == ')' || cl[now] == -1) {
to = now-1;
break;
}
now = cl[now]+1;
}
long l = to - from + 1;
if (l >= 2) {
if (max < l) {
max = l;
cnt = 1;
} else if (max == l) {
cnt++;
}
i = to;
}
}
}
out.println(max + " " + cnt);
out.flush();
}
static boolean bringLeft = true;
private static String decorate(String in, int maxlen) {
int len = in.length();
int spc = maxlen - len;
int left = spc / 2;
int right = spc / 2;
if (spc % 2 == 1) {
if (bringLeft) {
left = spc/2;
right = spc - left;
} else {
right = spc/2;
left = spc - left;
}
bringLeft = !bringLeft;
}
StringBuffer b = new StringBuffer();
b.append("*").append(duplicate(' ', left)).append(in).append(duplicate(' ', right)).append("*");
return b.toString();
}
private static String duplicate(char c, int a) {
StringBuffer b = new StringBuffer();
for (int i = 0 ; i < a ; i++) {
b.append(c);
}
return b.toString();
}
public static void debug(Object... os){
System.err.println(Arrays.deepToString(os));
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 4e3fff00fa2b9170f569b423aeef05c2 | train_003.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.*;
/*
* Current idea is to use a stack. It's getting stuck in situations like "((()))".
*
* The problem is that I need to update multiple elements in the stack, which is
* difficult because I don't want to pop out the stuff in front of them.
*/
public class LongestRegularBracketSequence {
int[] F, S, P, L;
public LongestRegularBracketSequence() {
Scanner sc = new Scanner(System.in);
char[] parens = sc.next().toCharArray();
int N = parens.length;
if(N == 1) {
System.out.println("0 1");
return;
}
F = new int[N];
int dip = 0;
for(int i = 0; i < N; i++) {
char ch = parens[i];
if(i == 0) {
if(ch == '(')
F[i] = -1;
else
F[i] = 1;
}
else
F[i] = b2i(ch == '(') + F[i-1];
dip = Math.min(dip, F[i]);
}
// Normalize it so our sums go from 0 to at most N.
for(int i = 0; i < N; i++)
F[i] -= dip;
S = new int[N+1];
P = new int[N+1];
Arrays.fill(S, -1);
Arrays.fill(P, -1);
for(int i = 0; i < N; i++) {
char ch = parens[i];
if(ch == ')' && P[F[i]+1] >= 0)
S[P[F[i]+1]] = i;
if(ch == '(')
P[F[i]] = i;
}
int len = 0, cnt = 1;
L = new int[N+1];
for(int i = N-1; i >= 0; i--)
if(S[i] >= 0) {
L[i] = S[i]-i+1 + L[1 + S[i]];
if(L[i] > len) {
len = L[i];
cnt = 1;
}
else if(L[i] == len)
cnt++;
}
System.out.println(len + " " + cnt);
}
void printArray(int[] arr) {
for(int elem: arr)
System.out.printf("%d ", elem);
System.out.println();
}
int b2i(boolean b) { return b ? 1 : -1; }
class Pair {
int pos, sum;
public Pair(int pos, int sum) {
this.pos = pos;
this.sum = sum;
}
boolean ok(int s) {
return s == sum-1;
}
}
public static void main(String[] args) {
new LongestRegularBracketSequence();
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 0430b7f0f128609222142c60b886a483 | train_003.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 test
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
String str=in.next();
int[] dp=new int[str.length()];
Arrays.fill(dp,-1);
Stack<Integer> stack=new Stack<Integer>();
int max=0;
int maxcount=0;
for(int i=0;i<str.length();i++)
{
char c=str.charAt(i);
if(c=='(')
{
stack.add(i);
}
else
{
if(!stack.isEmpty())
{
int m=stack.pop();
if(m>0 && dp[m-1]!=-1)
dp[i]=Math.min(dp[m-1],m);
else
dp[i]=m;
int len=i-dp[i]+1;
if(len>max)
{
max=len;
maxcount=1;
}
else if(len==max)
maxcount++;
}
}
}
if(max>0)
System.out.println(max+" "+maxcount);
else
System.out.println("0 1");
}
}
| Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 19a43764ab3e8b9a5c81772edbc6a9dc | train_003.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.*;
/**
* Created by HREN_VAM.
*/
public class C implements Runnable{
BufferedReader in;
PrintWriter out;
StringTokenizer st;
public static final String filename = "c";
public void solve() throws IOException{
String s = nextToken();
int n = s.length();
int[] level = new int[2 * n + 5];
for(int i = 0;i < 2 * n + 5;i ++){
level[i] = -1;
}
int middle = (n + 1);
int curLevel = 0;
int max = 0;
int count = 1;
for(int i = 0;i < n + 1;i ++){
if((level[middle + curLevel] != -1) && (max == i - level[middle + curLevel]))count ++;
if((level[middle + curLevel] != -1) && (max < i - level[middle + curLevel])){
max = i - level[middle + curLevel];
count = 1;
}
if(i == n)break;
if(s.charAt(i) == '('){
if(level[middle + curLevel] == -1)level[middle + curLevel] = i;
curLevel ++;
}else{
level[middle + curLevel] = -1;
curLevel --;
}
}
out.println(max + " " + count);
}
public void run(){
try{
Locale.setDefault(Locale.US);
in = new BufferedReader(new InputStreamReader(System.in));
//in = new BufferedReader(new FileReader(filename + ".in"));
out = new PrintWriter(System.out);
//out = new PrintWriter(new FileWriter(filename + ".out"));
st = new StringTokenizer("");
solve();
out.close();
} catch(IOException e){
throw new RuntimeException(e);
}
}
public static void main(String[] args){
new Thread(new C()).start();
}
public String nextToken() throws IOException{
while(!st.hasMoreTokens()){
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException{
return Integer.parseInt(nextToken());
}
public double nextDouble() throws IOException{
return Double.parseDouble(nextToken());
}
} | Java | [")((())))(()())", "))("] | 2 seconds | ["6 2", "0 1"] | null | Java 6 | 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 | 2a40d3e55b0302942e234dc909c2feaf | train_003.jsonl | 1358686800 | Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
String str=input.scanString();
ArrayList<Integer> arr_l=new ArrayList<>();
ArrayList<Integer> arr_r=new ArrayList<>();
for(int i=0;i<str.length();i++) {
if(str.charAt(i)=='l') {
arr_l.add(i+1);
}
else {
arr_r.add(i+1);
}
}
StringBuilder ans=new StringBuilder("");
for(int i=0;i<arr_r.size();i++) {
ans.append(arr_r.get(i)+" ");
}
for(int i=arr_l.size()-1;i>=0;i--) {
ans.append(arr_l.get(i)+" ");
}
ans.append("\n");
System.out.println(ans);
}
}
| Java | ["llrlr", "rrlll", "lrlrr"] | 2 seconds | ["3\n5\n4\n2\n1", "1\n2\n5\n4\n3", "2\n4\n5\n3\n1"] | NoteIn the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1. | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"two pointers"
] | 9d3c0f689ae1e6215448463def55df32 | The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either "l" or "r". | 1,200 | Output n lines — on the i-th line you should print the i-th stone's number from the left. | standard output | |
PASSED | 806e2341608aeb9acc78591ff3fcfbcc | train_003.jsonl | 1358686800 | Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
String s=bu.readLine();
int i,n=s.length();
ArrayList<Integer> l=new ArrayList<>(),r=new ArrayList<>();
for(i=0;i<n;i++)
if(s.charAt(i)=='l') l.add(i+1);
else r.add(i+1);
for(i=0;i<r.size();i++) sb.append(r.get(i)+"\n");
for(i=l.size()-1;i>=0;i--) sb.append(l.get(i)+"\n");
System.out.print(sb);
}
} | Java | ["llrlr", "rrlll", "lrlrr"] | 2 seconds | ["3\n5\n4\n2\n1", "1\n2\n5\n4\n3", "2\n4\n5\n3\n1"] | NoteIn the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1. | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"two pointers"
] | 9d3c0f689ae1e6215448463def55df32 | The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either "l" or "r". | 1,200 | Output n lines — on the i-th line you should print the i-th stone's number from the left. | standard output | |
PASSED | 3e06282631e7e642a5bbb659c328c853 | train_003.jsonl | 1358686800 | Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls. | 256 megabytes | import java.util.*;
public class CD264A {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
Scanner sc = new Scanner(System.in);
String s = sc.next();
int a[] = new int[s.length()];
int p = 0 , j = s.length() - 1;
for(int i = 0 ; i < s.length() ; i++) {
char c = s.charAt(i);
if(c == 'l') {
a[j] = i+1;
j--;
}else {
a[p] = i+1;
p++;
}
}
for(int item: a) {
sb.append(item + "\n");
}
System.out.println(sb);
sc.close();
}
}
| Java | ["llrlr", "rrlll", "lrlrr"] | 2 seconds | ["3\n5\n4\n2\n1", "1\n2\n5\n4\n3", "2\n4\n5\n3\n1"] | NoteIn the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1. | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"two pointers"
] | 9d3c0f689ae1e6215448463def55df32 | The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either "l" or "r". | 1,200 | Output n lines — on the i-th line you should print the i-th stone's number from the left. | standard output | |
PASSED | b59610ede4b88a9ee002aa1b9da91533 | train_003.jsonl | 1358686800 | Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class EscapeStones {
public static void main(String[] args) {
InputReader reader = new InputReader(System.in);
String escapeMoves = reader.nextLine();
StringBuilder res = new StringBuilder();
int length = escapeMoves.length();
int left = 0;
int right = length - 1 ;
int[] positions = new int[length];
for (int i = 0; i < length; i++) {
if (escapeMoves.charAt(i) == 'l') {
positions[right--] = i + 1;
} else {
positions[left++] = i + 1;
}
}
for (int i : positions) {
res.append(i).append("\n");
}
System.out.println(res);
}
static class InputReader {
StringTokenizer tokenizer;
BufferedReader reader;
String token;
public InputReader(InputStream stream) {
tokenizer = null;
reader = new BufferedReader(new InputStreamReader(stream));
}
public boolean hasNext() throws IOException {
if (tokenizer != null && tokenizer.hasMoreTokens()) {
return true;
}
token = reader.readLine();
return (token != null && token.length() > 0);
}
public String nextLine() {
try {
token = reader.readLine();
}
catch (IOException e) {
throw new RuntimeException(e);
}
return token;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
if (token != null) {
tokenizer = new StringTokenizer(token);
token = null;
} else {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["llrlr", "rrlll", "lrlrr"] | 2 seconds | ["3\n5\n4\n2\n1", "1\n2\n5\n4\n3", "2\n4\n5\n3\n1"] | NoteIn the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1. | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"two pointers"
] | 9d3c0f689ae1e6215448463def55df32 | The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either "l" or "r". | 1,200 | Output n lines — on the i-th line you should print the i-th stone's number from the left. | standard output | |
PASSED | 3ccd1837a8eb9c9eb6696b0bfa7a60bb | train_003.jsonl | 1358686800 | Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls. | 256 megabytes |
import java.io.BufferedReader;
import java.util.Scanner;
import java.io.BufferedWriter;
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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.math.BigInteger;
public class Escape_from_Stones
{
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
static class AnotherReader {
BufferedReader br;
StringTokenizer st;
AnotherReader() throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(System.in));
}
AnotherReader(int a) throws FileNotFoundException {
br = new BufferedReader(new FileReader("input.txt"));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
String nextLine() throws IOException {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void pn(Object o) {
out.println(o);
out.flush();
}
static void p(Object o) {
out.print(o);
out.flush();
}
static void pni(Object o) {
out.println(o);
System.out.flush();
}
static int I() throws IOException {
return sc.nextInt();
}
static long L() throws IOException {
return sc.nextLong();
}
static double D() throws IOException {
return sc.nextDouble();
}
static String S() throws IOException {
return sc.nextLine();
}
static char C() throws IOException {
return sc.next().charAt(0);
}
static int[] Ai(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = I();
return arr;
}
static String[] As(int n) throws IOException {
String s[] = new String[n];
for (int i = 0; i < n; i++)
s[i] = S();
return s;
}
static long[] Al(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = L();
return arr;
}
public static void process() throws IOException
{
String s = S();
int[] left = new int[s.length()];
int[] right = new int[s.length()];
int nl = 0;
int nr = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == 'l') {
left[nl++] = i + 1;
} else {
right[nr++] = i + 1;
}
}
for (int i = 0; i < nr; ++i) out.println(right[i]);
for (int i = nl - 1; i >= 0; --i) out.println(left[i]);
}
public static void main(String[] args)throws IOException
{
try
{
boolean oj=true;
if(oj==true)
{
AnotherReader sk=new AnotherReader();
PrintWriter out=new PrintWriter(System.out);}
else
{
AnotherReader sk=new AnotherReader(100);
out=new PrintWriter("output.txt");
}
process();
out.flush();out.close();
}
catch(Exception e)
{
return;
}
}
}
| Java | ["llrlr", "rrlll", "lrlrr"] | 2 seconds | ["3\n5\n4\n2\n1", "1\n2\n5\n4\n3", "2\n4\n5\n3\n1"] | NoteIn the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1. | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"two pointers"
] | 9d3c0f689ae1e6215448463def55df32 | The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either "l" or "r". | 1,200 | Output n lines — on the i-th line you should print the i-th stone's number from the left. | standard output | |
PASSED | b1a7e47332abea434e35ac1c454caca3 | train_003.jsonl | 1358686800 | Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls. | 256 megabytes | import java.io.*;
import java.util.*;
/**
* 06/05/20
* Created by Himanshu
**/
public class EscapeFromStones {
public static void main(String[] args) {
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
String str = s.next();
int n = str.length();
StringBuilder sb = new StringBuilder();
// List<Integer> left = new ArrayList<>();
// List<Integer> right = new ArrayList<>();
for (int i=0;i<n;i++) {
if (str.charAt(i) == 'r') sb.append(i+1 + " ");
}
for (int i=n-1;i>=0;i--) {
if (str.charAt(i) == 'l') sb.append(i+1 + " ");
}
System.out.println(sb);
// for (int i=0;i<right.size();i++) System.out.println(right.get(i));
// for (int i=left.size()-1;i>=0;i--) System.out.println(left.get(i));
}
} | Java | ["llrlr", "rrlll", "lrlrr"] | 2 seconds | ["3\n5\n4\n2\n1", "1\n2\n5\n4\n3", "2\n4\n5\n3\n1"] | NoteIn the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1. | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"two pointers"
] | 9d3c0f689ae1e6215448463def55df32 | The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either "l" or "r". | 1,200 | Output n lines — on the i-th line you should print the i-th stone's number from the left. | standard output | |
PASSED | a7e8d4cb716aba697d47c4da7f970521 | train_003.jsonl | 1358686800 | Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
StringBuilder st = new StringBuilder();
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'r') st.append(i + 1 + "\n");
for (int i = s.length() - 1; i >= 0; i--)
if(s.charAt(i) == 'l') st.append((i + 1) + "\n");
System.out.print(st);
}
} | Java | ["llrlr", "rrlll", "lrlrr"] | 2 seconds | ["3\n5\n4\n2\n1", "1\n2\n5\n4\n3", "2\n4\n5\n3\n1"] | NoteIn the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1. | Java 11 | standard input | [
"data structures",
"constructive algorithms",
"implementation",
"two pointers"
] | 9d3c0f689ae1e6215448463def55df32 | The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either "l" or "r". | 1,200 | Output n lines — on the i-th line you should print the i-th stone's number from the left. | standard output | |
PASSED | 8486967cd6a1a47980a4266558956089 | train_003.jsonl | 1494171900 | You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made y submissions, out of which x have been successful. Thus, your current success rate on Codeforces is equal to x / y.Your favorite rational number in the [0;1] range is p / q. Now you wonder: what is the smallest number of submissions you have to make if you want your success rate to be p / q? | 256 megabytes |
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner dr=new Scanner(System.in);
int Q;
Q=dr.nextInt();
while (Q!=0){
Q--;
BigInteger li=new BigInteger("0");
BigInteger x,y,p,q;
x=dr.nextBigInteger();y=dr.nextBigInteger();
p=dr.nextBigInteger();q=dr.nextBigInteger();
if (p.compareTo(q)==0){
if (x.compareTo(y)==0)System.out.println("0");
else System.out.println("-1");
continue;
}
if (p.compareTo(li)==0){
if(x.compareTo(li)==0)System.out.println("0");
else System.out.println("-1");
continue;
}
BigInteger yi=new BigInteger("1");
BigInteger er=new BigInteger("2");
BigInteger s,l,r,mid;
s=l=li;r=new BigInteger("1000000000000000000");
while (l.compareTo(r)<=0){
mid=l.add(r).divide(er);
if (q.multiply(x.add(mid)).compareTo(p.multiply(y.add(mid)))>=0){
s=mid;r=mid.subtract(yi);
}else l=mid.add(yi);
}
if (q.multiply(x.add(s)).compareTo(p.multiply(y.add(s)))==0)System.out.println(s);
else{
x=x.add(s);
BigInteger z=x.add(p).subtract(yi).divide(p);
x=z.multiply(p);
BigInteger ans=x.divide(p).multiply(q).subtract(y);
y=x.divide(p).multiply(q);
System.out.println(ans);
}
}
}
}
| Java | ["4\n3 10 1 2\n7 14 3 8\n20 70 2 7\n5 6 1 1"] | 2 seconds | ["4\n10\n0\n-1"] | NoteIn the first example, you have to make 4 successful submissions. Your success rate will be equal to 7 / 14, or 1 / 2.In the second example, you have to make 2 successful and 8 unsuccessful submissions. Your success rate will be equal to 9 / 24, or 3 / 8.In the third example, there is no need to make any new submissions. Your success rate is already equal to 20 / 70, or 2 / 7.In the fourth example, the only unsuccessful submission breaks your hopes of having the success rate equal to 1. | Java 11 | standard input | [
"binary search",
"math"
] | 589f3f7366d1e0f9185ed0926f5a10bb | The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each of the next t lines contains four integers x, y, p and q (0 ≤ x ≤ y ≤ 109; 0 ≤ p ≤ q ≤ 109; y > 0; q > 0). It is guaranteed that p / q is an irreducible fraction. Hacks. For hacks, an additional constraint of t ≤ 5 must be met. | 1,700 | For each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve. | standard output | |
PASSED | 71ca3b0d0eded88d46ef1c24bd2cd6d5 | train_003.jsonl | 1391442000 | Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.Suppose Ciel and Jiro play optimally, what is the score of the game? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.BufferedOutputStream;
import java.util.StringTokenizer;
import java.io.Closeable;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStream;
import java.io.Flushable;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Input in = new Input(inputStream);
Output out = new Output(outputStream);
CFoxAndCardGame solver = new CFoxAndCardGame();
solver.solve(1, in, out);
out.close();
}
}
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1<<29);
thread.start();
thread.join();
}
static class CFoxAndCardGame {
public CFoxAndCardGame() {
}
public void solve(int kase, Input in, Output pw) {
int n = in.nextInt();
long f = 0, s = 0;
ArrayList<Integer> middle = new ArrayList<>();
for(int i = 0; i<n; i++) {
int x = in.nextInt();
int[] arr = new int[x];
for(int j = 0; j<x; j++) {
arr[j] = in.nextInt();
}
if((x&1)==0) {
for(int j = 0; j<x >> 1; j++) {
f += arr[j];
}
for(int j = x >> 1; j<x; j++) {
s += arr[j];
}
}else {
for(int j = 0; j<x >> 1; j++) {
f += arr[j];
}
for(int j = (x >> 1)+1; j<x; j++) {
s += arr[j];
}
middle.add(arr[x >> 1]);
}
}
middle.sort(Comparator.comparingInt(o -> -o));
for(int i = 0; i<middle.size(); i++) {
if((i&1)==0) {
f += middle.get(i);
}else {
s += middle.get(i);
}
}
pw.println(f, s);
}
}
static class Output implements Closeable, Flushable {
public StringBuilder sb;
public OutputStream os;
public int BUFFER_SIZE;
public boolean autoFlush;
public String LineSeparator;
public Output(OutputStream os) {
this(os, 1<<16);
}
public Output(OutputStream os, int bs) {
BUFFER_SIZE = bs;
sb = new StringBuilder(BUFFER_SIZE);
this.os = new BufferedOutputStream(os, 1<<17);
autoFlush = false;
LineSeparator = System.lineSeparator();
}
public void print(String s) {
sb.append(s);
if(autoFlush) {
flush();
}else if(sb.length()>BUFFER_SIZE >> 1) {
flushToBuffer();
}
}
public void println(Object... o) {
for(int i = 0; i<o.length; i++) {
if(i!=0) {
print(" ");
}
print(String.valueOf(o[i]));
}
println();
}
public void println() {
sb.append(LineSeparator);
}
private void flushToBuffer() {
try {
os.write(sb.toString().getBytes());
}catch(IOException e) {
e.printStackTrace();
}
sb = new StringBuilder(BUFFER_SIZE);
}
public void flush() {
try {
flushToBuffer();
os.flush();
}catch(IOException e) {
e.printStackTrace();
}
}
public void close() {
flush();
try {
os.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
static class Input {
BufferedReader br;
StringTokenizer st;
public Input(InputStream is) {
this(is, 1<<20);
}
public Input(InputStream is, int bs) {
br = new BufferedReader(new InputStreamReader(is), bs);
st = null;
}
public boolean hasNext() {
try {
while(st==null||!st.hasMoreTokens()) {
String s = br.readLine();
if(s==null) {
return false;
}
st = new StringTokenizer(s);
}
return true;
}catch(Exception e) {
return false;
}
}
public String next() {
if(!hasNext()) {
throw new InputMismatchException();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["2\n1 100\n2 1 10", "1\n9 2 8 6 5 9 4 7 1 3", "3\n3 1 3 2\n3 5 4 6\n2 8 7", "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000"] | 1 second | ["101 10", "30 15", "18 18", "7000 7000"] | NoteIn the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | Java 11 | standard input | [
"greedy",
"sortings",
"games"
] | 21672f2906f4f821611ab1b6dfc7f081 | The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. | 2,000 | Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. | standard output | |
PASSED | f9e7be2a138bf5f9c0a0e589a4ce28c8 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
public class lol {
static long gcd(long x, long y)
{
long r=0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number
r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
long a = in.nextInt();
long b = in.nextInt();
long c = in.nextInt();
if(a==0)
{
if(b==0)
{
if(c!=0)
{
System.out.println("-1");
return;
}
else
{
System.out.println("1 1");
return;
}
}
if(Math.abs(c)%Math.abs(b)!=0)
{
System.out.println("-1");
}
else
{
System.out.println(0+" "+(-c/b));
}
return;
}
else if(b==0)
{
if(c==0)
{
System.out.println("0 1");
return;
}
else
{
if(Math.abs(c)%Math.abs(a)!=0)
{
System.out.println("-1");
}
else
{
System.out.println(0+" "+(-c/a));
}
return;
}
}
else if(c==0)
{
System.out.println(b+" "+(-a));
return;
}
long gcd = gcd(Math.abs(a),Math.abs(b));
gcd=gcd(gcd,Math.abs(c));
a/=gcd;
b/=gcd;
c/=gcd;
if(gcd(Math.abs(b),Math.abs(a))!=1)
{
System.out.println("-1");
}
else
{
if(a<0)
{
a*=-1;
b*=-1;
c*=-1;
}
BigInteger j = new BigInteger(Long.toString(a));
long temp=b;
if(b<0)
{
long hl = b/a;
b+=(hl+1)*a;
}
BigInteger m = new BigInteger(Long.toString(b));
BigInteger k = m.modInverse(j);
k=k.multiply(new BigInteger(Long.toString(-c)));
k=k.mod(j);
long ans = (-c-temp*(Integer.parseInt(k.toString())))/a;
long y=1;
for(int h=1;h<=18;h++)
y*=10;
y*=5;
if(Math.abs(ans)>y)
{
System.out.println("-1");
}
else
System.out.println(ans+" "+k.toString());
}
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | c659fb96fe7f06e80608582dc886d670 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | /**
* ******* Created on 19/9/19 3:34 AM*******
*/
import java.io.*;
import java.util.*;
public class C7 {
final static class Pair{
long a;
long b;
long c;
public Pair(long a, long b, long c){
this.a = a;
this.b =b;
this.c =c;
}
}
public static void main(String[] args) throws IOException {
try (Input input = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
long a = input.nextLong();
long b = input.nextLong();
long c = input.nextLong();
Pair gcd =extendedGCD(a,b);
if(-c% gcd.c !=0){
System.out.println("-1");
}else{
long k = (-c)/gcd.c;
System.out.println(k*gcd.a +" "+k*gcd.b);
}
//System.out.println(p.a +" "+p.b);
}
}
private static Pair extendedGCD(long a, long b) {
if (b == 0) {
return new Pair(1,0,a);
}
Pair p = extendedGCD (b, a%b);
long t = p.a;
p.a = p.b;
p.b = t - a/b*p.b;
return p;
}
interface Input extends Closeable {
String next() throws IOException;
default int nextInt() throws IOException {
return Integer.parseInt(next());
}
default long nextLong() throws IOException {
return Long.parseLong(next());
}
default double nextDouble() throws IOException {
return Double.parseDouble(next());
}
default int[] readIntArray() throws IOException {
return readIntArray(nextInt());
}
default int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextInt();
}
return array;
}
default long[] readLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextLong();
}
return array;
}
}
private static class StandardInput implements Input {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer stringTokenizer;
@Override
public void close() throws IOException {
reader.close();
}
@Override
public String next() throws IOException {
if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 3af3fdebfb5e4551922b758a2bec03b2 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(in, out);
out.close();
}
private static class TaskB {
static final long max = 1000000000000000000L;
static final double eps = 0.000001;
static final long mod = 1000000007;
static int N, M, K;
static long X, Y;
boolean V[][];
int A[][], total;
void solve(InputReader in, PrintWriter out) throws IOException {
long A, B, C;
A = in.nextInt();
B = in.nextInt();
C = in.nextInt();
long G = extendEuc(A, B);
if (C % G == 0) {
out.println(X * (-1) * (C / G) + " " + Y * (-1) * (C / G));
}
else out.println(-1);
}
long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
boolean isPrime(long n) {
if (n <= 1 || n > 3 && (n % 2 == 0 || n % 3 == 0))
return false;
for (long i = 5, j = 2; i * i <= n; i += j, j = 6 - j)
if (n % i == 0)
return false;
return true;
}
long extendEuc(long A, long B) {
long lastX = 1, lastY = 0, temp;
X = 0;
Y = 1;
while (B != 0) {
long Q = A / B;
long R = A % B;
A = B;
B = R;
temp = X;
X = lastX - Q * X;
lastX = temp;
temp = Y;
Y = lastY - Q * Y;
lastY = temp;
}
X = lastX;
Y = lastY;
return A;
}
}
private static class InputReader {
StringTokenizer st;
BufferedReader br;
public InputReader(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public InputReader(FileReader s) throws FileNotFoundException {
br = new BufferedReader(s);
}
public String next() {
while (st == null || !st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public double nextDouble() {
return Double.parseDouble(next());
}
public boolean ready() {
try {
return br.ready();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | d476c10f09aa1617ea2b11ea03a2a7a2 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes |
import java.io.IOException;
import java.io.*;
import java.io.*;
/**
* https://codeforces.com/contest/7/problem/C #math #numbertheory
*
* Solve linear diophantine equation Ax+By=-C using extended Euclid's algorithm.
*/
public class Line {
public static void main(String[] args) throws IOException {
Reader r = new Reader();
Writer w = new Writer();
long A = r.nextLong();
long B = r.nextLong();
long C = r.nextLong();
long[] sol = NumberTheory.extendedEuclid(A, B);
if (C % sol[0] == 0) {
long k = -C / sol[0];
w.println((sol[1] * k) + " " + (sol[2] * k));
} else {
w.println("-1");
}
w.close();
}
}
/**
* Custom class used for fast input.
*/
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
/**
* Custom class used for fast output.
*/
class Writer {
private final BufferedWriter bw;
public Writer() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
/**
* Placeholder for various functions and algorithms related to number theory.
*/
class NumberTheory {
/**
* Calculates the greatest common divisor of whole numbers m and n using
* Euclid's algorithm. Greatest common divisor is the largest positive whole
* number that evenly divides both m and n.
*
* Time complexity: -, Memory complexity: O(1)
*
* @param m First positive whole number.
* @param n Second positive whole number.
* @return Greatest common divisor of given numbers.
*/
public static long gcd(long m, long n) {
return n == 0L ? m : gcd(n, m % n);
}
/**
* Calculates the least common multiple of whole numbers m and n using Euclid's
* algorithm. Least common multiple is the smallest positive whole number that
* is evenly divisible by both m and n.
*
* Time complexity: -, Memory complexity: O(1)
*
* @param m First positive whole number.
* @param n Second positive whole number.
* @return Least common multiple of given numbers.
*/
public static long lcm(long m, long n) {
return (m / gcd(m, n)) * n;
}
/**
* Determines if a given positive whole number is prime using basic primality
* test.
*
* Time complexity: O(sqrt(n)), Memory complexity: O(1)
*
* @param n Positive whole number.
* @return True if given number is prime, otherwise false.
*/
public static boolean isPrime(long n) {
for (long i = 2L; i * i <= n; ++i)
if (n % i == 0)
return false;
return true;
}
/**
* Extended Euclid's algorithm for calculating the greatest common divisor d and
* factors a, b such that am + bn = d = gcd(m, n).
*
* @param m First positive whole number.
* @param n Second positive whole number.
* @return long[] with elements {d, a, b}.
*/
public static long[] extendedEuclid(long m, long n) {
long a = 0, a_ = 1, b = 1, b_ = 0, c = m, d = n;
long t;
if (n == 0) {
return new long[] { c, 1, 0 };
}
long q = c / d, r = c % d;
while (r != 0) {
c = d;
d = r;
t = a_;
a_ = a;
a = t - q * a;
t = b_;
b_ = b;
b = t - q * b;
q = c / d;
r = c % d;
}
return new long[] { d, a, b };
}
/**
* Calculates solution for system of two linear diophantine equations:
*
* x = a1 + kp1; x = a2 + kp2
*
* or expressed as system of congruences:
*
* x = a1 (mod p1); x = a2 (mod p2)
*
* Solution is expressed as x = up1a2 + vp2a1 (mod p1p2). u,v are calculated
* using Extended Euclid's algorithm as up1 + vp2 = 1. Solution exists if p1, p2
* are coprime i.e. gcd(p1, p2) = 1.
*
* @param a1 First element of first equation.
* @param p1 Second element of first equation
* @param a2 First element of second equation.
* @param p2 Second element of second equation.
* @return Positive solution to the system of equations or -1 if it doesn't
* exist.
*/
public static long smallCrt(long a1, long p1, long a2, long p2) {
long[] sol = extendedEuclid(p1, p2);
if (sol[0] != 1)
return -1L;
return ((sol[1] * p1 * a2) % (p1 * p2) + (sol[2] * p2 * a1) % (p1 * p2) + (p1 * p2)) % (p1 * p2);
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 1016348e452c3884b3c0f7e4bddb7ea2 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.Scanner;
public class Main {
static long x;
static long y;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
long d = gcdEx(a, b);
if (c%d == 0) {
System.out.println((-x*c/d)+" " +(-y*c/d));
} else {
System.out.println("-1");
}
}
private static long gcdEx(long a, long b) {
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
long gcd = gcdEx(b, a % b);
long t;
t = x;
x = y;
y = t - (a / b) * y;
return gcd;
}
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | a9c4ea470f96d006035eb74640be5960 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class R7C {
public static void main (String[] args) throws java.lang.Exception {
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
long a = in.nextLong(), b = in.nextLong(), c = in.nextLong() * -1;
long[] x = gcd(a, b);
if (c % x[0] != 0)
w.println(-1);
else {
x[1] *= c / x[0];
x[2] *= c / x[0];
w.println(x[1] + " " + x[2]);
}
w.close();
}
static long[] gcd(long p, long q) {
if (q == 0)
return new long[] { p, 1, 0 };
long[] vals = gcd(q, p % q);
long d = vals[0];
long a = vals[2];
long b = vals[1] - (p / q) * vals[2];
return new long[] { d, a, b };
}
static 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 int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public 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 | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | b4172e3550bf70ea10f716f7329581f3 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class Line implements Runnable {
private void solve() throws IOException {
long a = nextInt();
long b = nextInt();
long c = nextInt();
long x = a;
long y = b;
long xp = 1;
long xq = 0;
long yp = 0;
long yq = 1;
if (x < 0) {
x = -x;
xp = -1;
}
if (y < 0) {
y = -y;
yq = -1;
}
while (y > 0) {
long by = x / y;
long t = x - by * y;
long tp = xp - by * yp;
long tq = xq - by * yq;
x = y;
xp = yp;
xq = yq;
y = t;
yp = tp;
yq = tq;
}
if (c % x != 0) {
writer.println(-1);
} else {
long p = xp;
long q = xq;
long z;
if (b == 0) {
z = - q / a;
} else {
z = p / b;
}
p -= z * b;
q += z * a;
p *= -c / x;
q *= -c / x;
writer.println(p + " " + q);
}
}
public static void main(String[] args) {
new Line().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 8bbd0cd4b1464795daa70addfd95c8e3 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Locale;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
public class Main {
public static void main(String[] args) throws Exception {
solve(1);
}
public static void solve(int testCases) throws Exception{
long A = StdIn.readLong();
long B = StdIn.readLong();
long C = StdIn.readLong();
if(!isPossible(A,B,C)){
StdOut.println("-1");
return;
}
long g = gcd(A, B);
Point p = extendedGCD(A, B);
StdOut.println(p.x*(-C/g) + " " + p.y*(-C/g));
}
public static long gcd(long a, long b){
return (b == 0)? a : gcd(b,a%b);
}
public static boolean isPossible(long a, long b, long c){
return (c%gcd(a,b) == 0);
}
public static Point extendedGCD(long a, long b){
long x = 0, y = 1, lastx = 1, lasty = 0, temp = -1;
while(b != 0){
long q = a / b;
long r = a % b;
a = b;
b = r;
temp = x;
x = lastx - q * x;
lastx = temp;
temp = y;
y = lasty - q * y;
lasty = temp;
}
return new Point(lastx, lasty);
}
}
class Point{
long x;
long y;
public Point(long x, long y){
this.x = x;
this.y = y;
}
public String toString(){
return x + " " + y;
}
}
final class StdIn {
private StdIn() { }
private static Scanner scanner;
private static final String CHARSET_NAME = "UTF-8";
private static final Locale LOCALE = Locale.US;
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\p{javaWhitespace}+");
private static final Pattern EMPTY_PATTERN = Pattern.compile("");
private static final Pattern EVERYTHING_PATTERN = Pattern.compile("\\A");
public static boolean isEmpty() {
return !scanner.hasNext();
}
public static boolean hasNextLine() {
return scanner.hasNextLine();
}
public static boolean hasNextChar() {
scanner.useDelimiter(EMPTY_PATTERN);
boolean result = scanner.hasNext();
scanner.useDelimiter(WHITESPACE_PATTERN);
return result;
}
public static String readLine() {
String line;
try { line = scanner.nextLine(); }
catch (Exception e) { line = null; }
return line;
}
public static char readChar() {
scanner.useDelimiter(EMPTY_PATTERN);
String ch = scanner.next();
assert (ch.length() == 1) : "Internal (Std)In.readChar() error!"
+ " Please contact the authors.";
scanner.useDelimiter(WHITESPACE_PATTERN);
return ch.charAt(0);
}
public static String readAll() {
if (!scanner.hasNextLine())
return "";
String result = scanner.useDelimiter(EVERYTHING_PATTERN).next();
// not that important to reset delimeter, since now scanner is empty
scanner.useDelimiter(WHITESPACE_PATTERN); // but let's do it anyway
return result;
}
public static String readString() {
return scanner.next();
}
public static int readInt() {
return scanner.nextInt();
}
public static double readDouble() {
return scanner.nextDouble();
}
public static float readFloat() {
return scanner.nextFloat();
}
public static long readLong() {
return scanner.nextLong();
}
public static short readShort() {
return scanner.nextShort();
}
public static byte readByte() {
return scanner.nextByte();
}
public static boolean readBoolean() {
String s = readString();
if (s.equalsIgnoreCase("true")) return true;
if (s.equalsIgnoreCase("false")) return false;
if (s.equals("1")) return true;
if (s.equals("0")) return false;
throw new InputMismatchException();
}
public static String[] readAllStrings() {
// we could use readAll.trim().split(), but that's not consistent
// because trim() uses characters 0x00..0x20 as whitespace
String[] tokens = WHITESPACE_PATTERN.split(readAll());
if (tokens.length == 0 || tokens[0].length() > 0)
return tokens;
// don't include first token if it is leading whitespace
String[] decapitokens = new String[tokens.length-1];
for (int i = 0; i < tokens.length - 1; i++)
decapitokens[i] = tokens[i+1];
return decapitokens;
}
public static String[] readAllLines() {
ArrayList<String> lines = new ArrayList<String>();
while (hasNextLine()) {
lines.add(readLine());
}
return lines.toArray(new String[0]);
}
public static int[] readAllInts() {
String[] fields = readAllStrings();
int[] vals = new int[fields.length];
for (int i = 0; i < fields.length; i++)
vals[i] = Integer.parseInt(fields[i]);
return vals;
}
public static double[] readAllDoubles() {
String[] fields = readAllStrings();
double[] vals = new double[fields.length];
for (int i = 0; i < fields.length; i++)
vals[i] = Double.parseDouble(fields[i]);
return vals;
}
static {
resync();
}
private static void resync() {
setScanner(new Scanner(new java.io.BufferedInputStream(System.in), CHARSET_NAME));
}
private static void setScanner(Scanner scanner) {
StdIn.scanner = scanner;
StdIn.scanner.useLocale(LOCALE);
}
public static int[] readInts() {
return readAllInts();
}
public static double[] readDoubles() {
return readAllDoubles();
}
public static String[] readStrings() {
return readAllStrings();
}
}
final class StdOut {
private static final String CHARSET_NAME = "UTF-8";
private static final Locale LOCALE = Locale.US;
private static PrintWriter out;
static {
try {
out = new PrintWriter(new OutputStreamWriter(System.out, CHARSET_NAME), true);
}
catch (UnsupportedEncodingException e) { System.out.println(e); }
}
private StdOut() { }
public static void close() {
out.close();
}
public static void println() {
out.println();
}
public static void println(Object x) {
out.println(x);
}
public static void println(boolean x) {
out.println(x);
}
public static void println(char x) {
out.println(x);
}
public static void println(double x) {
out.println(x);
}
public static void println(float x) {
out.println(x);
}
public static void println(int x) {
out.println(x);
}
public static void println(long x) {
out.println(x);
}
public static void println(short x) {
out.println(x);
}
public static void println(byte x) {
out.println(x);
}
public static void print() {
out.flush();
}
public static void print(Object x) {
out.print(x);
out.flush();
}
public static void print(boolean x) {
out.print(x);
out.flush();
}
public static void print(char x) {
out.print(x);
out.flush();
}
public static void print(double x) {
out.print(x);
out.flush();
}
public static void print(float x) {
out.print(x);
out.flush();
}
public static void print(int x) {
out.print(x);
out.flush();
}
public static void print(long x) {
out.print(x);
out.flush();
}
public static void print(short x) {
out.print(x);
out.flush();
}
public static void print(byte x) {
out.print(x);
out.flush();
}
public static void printf(String format, Object... args) {
out.printf(LOCALE, format, args);
out.flush();
}
public static void printf(Locale locale, String format, Object... args) {
out.printf(locale, format, args);
out.flush();
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | e0fe403b25b805cdb464f0ccad612c79 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.text.*;
public class Main {
static long mod = (long)1e9 + 7;
static long mod1 = 998244353;
static FastScanner f;
static PrintWriter pw = new PrintWriter(System.out);
static Scanner S = new Scanner(System.in);
static long x0;
static long y0;
public static void solve()throws IOException {
long a = f.nextLong(); long b = f.nextLong(); long c = f.nextLong(); c *= -1;
long g = exgcd(a , b);
if(c % g != 0) pn(-1);
else {
x0 = c / g * x0;
y0 = c / g * y0;
pn(x0 + " " + y0);
}
}
public static void main(String[] args)throws IOException {
init();
int t = 1;
while(t --> 0) {solve();}
pw.flush();
pw.close();
}
/******************************END OF MAIN PROGRAM*******************************************/
public static void init()throws IOException{if(System.getProperty("ONLINE_JUDGE")==null){f=new FastScanner("");}else{f=new FastScanner(System.in);}}
public static class FastScanner {
BufferedReader br;StringTokenizer st;
FastScanner(InputStream stream){try{br=new BufferedReader(new InputStreamReader(stream));}catch(Exception e){e.printStackTrace();}}
FastScanner(String str){try{br=new BufferedReader(new FileReader("!a.txt"));}catch(Exception e){e.printStackTrace();}}
String next(){while(st==null||!st.hasMoreTokens()){try{st=new StringTokenizer(br.readLine());}catch(IOException e){e.printStackTrace();}}return st.nextToken();}
String nextLine()throws IOException{return br.readLine();}int nextInt(){return Integer.parseInt(next());}long nextLong(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());}
}
public static void pn(Object o){pw.println(o);}
public static void p(Object o){pw.print(o);}
public static void pni(Object o){pw.println(o);pw.flush();}
static class Point implements Comparator<Point>{int x;int y;Point(int x,int y){this.x=x;this.y=y;}Point(){}public int compare(Point a, Point b){if(a.x==b.x)return a.y-b.y;return a.x-b.x;}}
static int gcd(int a,int b){if(b==0)return a;else{return gcd(b,a%b);}}
static long gcd(long a,long b){if(b==0)return a;else{return gcd(b,a%b);}}
static long exgcd(long a,long b){if(b==0){x0=1;y0=0;return a;}long temp=exgcd(b,a%b);long t=x0;x0=y0;y0=t-a/b*y0;return temp;}
static long pow(long a,long b){long res=1;while(b>0){if((b&1)==1)res=res*a;b>>=1;a=a*a;}return res;}
static long mpow(long a,long b){long res=1;while(b>0){if((b&1)==1)res=((res%mod)*(a%mod))%mod;b>>=1;a=((a%mod)*(a%mod))%mod;}return res;}
static boolean isPrime(long n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(long i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
static HashSet<Long> factors(long n){HashSet<Long> hs=new HashSet<Long>();for(long i=1;i<=(long)Math.sqrt(n);i++){if(n%i==0){hs.add(i);hs.add(n/i);}}return hs;}
static int[] inpint(int n){int arr[]=new int[n];for(int i=0;i<n;i++){arr[i]=f.nextInt();}return arr;}
static long[] inplong(int n){long arr[] = new long[n];for(int i=0;i<n;i++){arr[i]=f.nextLong();}return arr;}
static boolean ise(int x){return ((x&1)==0);}static boolean ise(long x){return ((x&1)==0);}
static int gnv(char c){return Character.getNumericValue(c);}
static void sort(int[] a){ArrayList<Integer> l=new ArrayList<>();for(int i:a)l.add(i);Collections.sort(l);for(int i=0;i<a.length;++i)a[i]=l.get(i);}
static void sort(long[] a){ArrayList<Long> l=new ArrayList<>();for(long i:a)l.add(i);Collections.sort(l);for(int i=0;i<a.length;++i)a[i]=l.get(i);}
static void sort(ArrayList<Integer> a){Collections.sort(a);}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 8ce35806874548e340f5c82e99f492db | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.image.AreaAveragingScaleFilter;
import static java.lang.Math.*;
import java.lang.*;
public class Main{
static FastScanner in;
static PrintWriter out;
final static boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
public static void main(String[] args) throws FileNotFoundException {
Locale.setDefault(Locale.US);
if (ONLINE_JUDGE){
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
}else{
in = new FastScanner(new File("input.txt"));
out = new PrintWriter(new File("output.txt"));
}
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
}
class Task {
final int N = 110;
final int Inf = 1000000007;
long a,b,c;
public void solve(int testNumber, FastScanner in, PrintWriter out) {
a = in.nextLong();
b = in.nextLong();
c = in.nextLong();
long r0 = a, r1 = b;
long x0 = 1, x1 = 0;
long y0 = 0, y1 = 1;
long temp = 0;
while (r1 != 0) {
long q = r0 / r1;
temp = r1;
r1 = r0 - r1 * q;
r0 = temp;
temp = x1;
x1 = x0 - x1 * q;
x0 = temp;
temp = y1;
y1 = y0 - y1 * q;
y0 = temp;
}
if (c % r0 != 0) {
out.println(-1);
} else {
long factor = -c / r0;
out.println(x0 * factor + " " + y0 * factor);
}
}
}
class Pair implements Comparable<Pair>{
public int first, second;
Pair(){
first=second=-1;
}
Pair(int a, int b){
first = a; second=b;
}
@Override
public int compareTo(Pair b){
if (first<b.first) return 1; else
if (first>b.first) return -1; else
if (second<b.second) return 1; else return -1;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
char c = (char)42;
result = (int)(1L*first*second + first + second);
if (second!=0) result+=first % second;
if (first!=0) result+=second%first;
return result;
}
@Override
public boolean equals(Object obj) {
Pair b = (Pair)obj;
if (first==b.first && second==b.second) return true;
return false;
}
}
class PairComparator implements Comparator<Pair>{
public int compare(Pair a, Pair b) {
if (a.first<b.first) return 1; else
if (a.first>b.first) return -1; else
if (a.second<b.second) return 1; else return -1;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 88ce522801a4f14618263b64589e1708 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.Scanner;
public class Main {
public static Triplet gcd1(long a,long b) {
if(b==0) {
Triplet op=new Triplet();
op.gcd=a;
op.x=1;
op.y=0;
return op;
}
Triplet smallans=gcd1(b,a%b);
Triplet myans=new Triplet();
myans.gcd=smallans.gcd;
myans.x=smallans.y;
myans.y=smallans.x-(a/b)*smallans.y;
return myans;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
Triplet d=gcd1(a,b);
if(c%d.gcd!=0) {
System.out.println(-1);
}
else {
long ans1=d.x * (-c)/d.gcd;
long ans2=d.y * (-c)/d.gcd;
System.out.println(ans1+" "+ans2);
}
}
public static long gcd(long a,long b) {
if(b==0) {
return a;
}
else {
return gcd(b,a%b);
}
}
}
class Triplet{
long gcd;
long x;
long y;
// ax+by=gcd(a,b)
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 9ec51e94aa190b60efa53d93768c68f7 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
public static PrintWriter w = new PrintWriter(System.out);
public static void main(String args[] ) throws Exception {
Reader in = new Reader();
int a = -1*in.nextInt();
int b = -1*in.nextInt();
int c = in.nextInt();
long[] ans = gcdExtended(Math.abs(a),Math.abs(b));
if(a < 0)
ans[0] *= -1;
if(b < 0)
ans[1] *= -1;
if(c%ans[2] != 0) {
w.println("-1");
w.flush();
return;
}
w.println((ans[0]*(c/ans[2])) + " " + ans[1]*(c/ans[2]));
w.flush();
return;
}
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static long[] gcdExtended(int a, int b) {
if(a == 0)
return new long[]{0,1,b};
long[] ans = gcdExtended(b%a,a);
return new long[]{ans[1]-(b/a)*ans[0],ans[0],ans[2]};
}
}
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String nextLine() throws IOException {
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 next() throws IOException {
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 nextInt() throws IOException {
int ret = 0;
byte c = read();
while(c <= ' ')
c = read();
boolean neg = (c == '-');
if(neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public int[] nextIntArray(int n) throws IOException {
int a[] = new int[n];
for(int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public int[][] next2dIntArray(int n, int m) throws IOException {
int a[][] = new int[n][m];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
a[i][j] = nextInt();
return a;
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while(c <= ' ')
c = read();
boolean neg = (c == '-');
if(neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while(c <= ' ')
c = read();
boolean neg = (c == '-');
if(neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while((c = read()) >= '0' && c <= '9');
if (c == '.') {
while((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if(neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if(bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if(bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if(din == null)
return;
din.close();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | be054e7cc399df65ee29fbe2bc4e9696 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.*;
import java.io.*;
public class line {
static long x1, y1, x2, y2;
public void run() throws Exception {
Scanner file = new Scanner(System.in);
long a = file.nextLong(), b= file.nextLong(), c = file.nextLong();
x1 = 1; y1 = 0; x2 = 0; y2 = 1;
if (c == 0) System.out.println(0 + " " + 0);
else if (b == 0) System.out.println(-c % a == 0? (-c / a) + " 0" : -1);
else if (a == 0) System.out.println(-c % b== 0 ? 0 + " " + (-c / b) : -1);
else {
long extgcd = ext_gcd(a, b);
if (c % extgcd != 0) System.out.println(-1);
else {
//System.out.println(x2 + " " + y2);
//System.out.println(x2 + " " + y2);
long temp = -c / extgcd;
System.out.println(x2 * temp + " " + (y2 * temp));
}
}
}
public static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public static long ext_gcd(long a, long b) {
//System.out.println(a + " " + b);
if (a % b == 0) {
return b;
}
long quo = a / b, rem = a % b;
long rex = x2 * quo, rey = y2 * quo;
long rexx = x1 - rex, reyy = y1 - rey;
x1 = x2; y1 = y2;
x2 = rexx; y2 = reyy;
//System.out.println(x2 + " " + y2);
return ext_gcd(b, a % b);
}
public static void main(String[] args) throws Exception {
new line().run();
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 1fee937c69b5dcb7532b68b5b1870fca | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solver {
static long[] gcd(long a, long b, long x, long y){
if(a == 0){
x = 0; y = 1;
long q[] = new long[3];
q[0] = b;
q[1] = x;
q[2] = y;
return q;
}
long tt[] = gcd(b%a,a,x,y);
long d = tt[0];
x = tt[2] - (b/a)*tt[1];
y = tt[1];
long pp[] = new long[3];
pp[0] = d;
pp[1] = x;
pp[2] = y;
return pp;
}
public static void main(String[] args) throws IOException {
/*
Scanner in = new Scanner(new File("stack.in"));
PrintWriter out = new PrintWriter("stack.out");
*/
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
long a = in.nextInt();
long b = in.nextInt();
long c = -in.nextInt();
if(a == 0){
if(c / b == (double)(long)c/b){
System.out.println(0 + " " + c/b);
}else System.out.println(-1);
return;
}
if(b == 0){
if(c / a == (double)(long)c/a){
System.out.println(c/a + " " +0);
}else System.out.println(-1);
return;
}
long pp[] = gcd(a,b,0,0);
if(c % pp[0] != 0){
System.out.println(-1);
return;
}
System.out.println(pp[1] * c / pp[0] + " " + pp[2] * c / pp[0]);
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 2c3f4c6b414bce299e45a4ef7dc2e62a | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes |
// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// (new FileReader("input.in"));
StringBuilder out = new StringBuilder();
StringTokenizer tk;
tk = new StringTokenizer(in.readLine());
long a = parseLong(tk.nextToken()),b = parseLong(tk.nextToken()),c = -parseLong(tk.nextToken());
long d = gcd(a,b);
if(c%d!=0) {
System.out.println("-1");
return;
}
long [] A = xgcd(a/d,b/d);
long x0 = A[0],y0 = A[1];
long x = c/d*x0 + b/d;
long y = c/d*y0 - a/d;
System.out.println(x+" "+y);
}
static long gcd(long a,long b) {
return b==0 ? a : gcd(b,a%b);
}
static long[] xgcd(long a,long b) {
long [] A = new long[3];
if(b==0) {
A[0] = 1;
A[1] = 0;
A[2] = a;
return A;
}
A = xgcd(b,a%b);
long t = A[0];
A[0] = A[1];
A[1] = t-(a/b)*A[1];
return A;
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 2f6eaa9f5d96dfa1ddf9881f97ec61a6 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | /* CS104: line
*
* Author: Mitchell Roger Marshe
*
* CodeForces
* Problem: 7C, line
* Link: http://codeforces.com/contest/7/problem/C
* ID: tickey
*/
import java.util.Scanner;
public class line {
private static long x;
private static long y;
private static long r;
public static void main(String[] args) {
long a = 0;
long b = 0;
long c = 0;
x = 0;
y = 0;
r = 0;
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt();
b = scanner.nextInt();
c = scanner.nextInt();
c = -c;
gcd(a, b);
if (c % r != 0) {
System.out.println("-1");
} else {
System.out.println(c / r * x + " " + c / r * y);
}
scanner.close();
}
public static void gcd(long a, long b) {
if (b == 0) {
x = 1;
y = 0;
r = a;
return;
}
gcd(b, a % b);
long t = x;
x = y;
y = t - a / b * y;
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | b0b1674279535127812087f19d4e603e | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes |
import java.util.Scanner;
public class Open {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long A = in.nextLong();
long B = in.nextLong();
long C = -in.nextLong();
long gcd = gcd(A, B);
if (C % gcd != 0) {
System.out.println("-1");
} else {
extendGCD(A, B);
System.out.println(x * (C / gcd) + " " + y * (C / gcd));
}
}
public static long gcd(long a, long b) {
while (b != 0) {
long c = a % b;
a = b;
b = c;
}
return a;
}
static long d, x, y;
static void extendGCD(long A, long B) {
if (B == 0) {
d = A;
x = 1;
y = 0;
} else {
extendGCD(B, A % B);
long temp = x;
x = y;
y = temp - (A / B) * y;
}
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | c6ac63fa24b09500a654e4e2291b6c49 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes |
import java.util.Scanner;
public class Open {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long A = in.nextLong();
long B = in.nextLong();
long C = -in.nextLong();
long gcd = gcd(A, B);
if (C % gcd != 0) {
System.out.println("-1");
} else {
extendGCD(A, B);
System.out.println(x * (C / gcd) + " " + y * (C / gcd));
}
}
public static long gcd(long a, long b) {
while (b != 0) {
long c = a % b;
a = b;
b = c;
}
return a;
}
static long d, x, y;
static void extendGCD(long A, long B) {
if (B == 0) {
d = A;
x = 1;
y = 0;
} else {
extendGCD(B, A % B);
long temp = x;
x = y;
y = temp - (A / B) * y;
}
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | bab3c98614261d3e6241b5b8854217cb | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.*;
public class CF7C {
static long[] gcdxy(long a, long b) {
long d0 = a, d1 = b;
long x0 = 1, x1 = 0;
long y0 = 0, y1 = 1;
long t;
while (d1 != 0) {
long q = d0 / d1;
t = d0 - d1 * q;
d0 = d1;
d1 = t;
t = x0 - x1 * q;
x0 = x1;
x1 = t;
t = y0 - y1 * q;
y0 = y1;
y1 = t;
}
return new long[] {d0, x0, y0};
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
long[] dxy = gcdxy(a, b);
long d = dxy[0];
if (c % d == 0) {
long x = dxy[1];
long y = dxy[2];
x *= -c / d;
y *= -c / d;
System.out.println(x + " " + y);
} else
System.out.println(-1);
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | a92f9169d0e57b07304c7c8aaa1c6c95 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.Scanner;
public class CodeForces7C{
public static Coordinates erwGGT(long a, long b){
if(b%a==0){
return new Coordinates(1, 0);
}
else{
if(a<0){
Coordinates temp = erwGGT(-a, b);
return new Coordinates(-temp.x, temp.y);
}
if(b<0){
Coordinates temp = erwGGT(a, -b);
return new Coordinates(temp.x, -temp.y);
}
Coordinates temp = erwGGT(b%a, a);
return new Coordinates(temp.y-(b/a)*temp.x, temp.x);
}
}
public static long ggt(long a, long b){
while(b!=0){
a=a%b;
long temp = a;
a = b;
b = temp;
}
return a;
}
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
long a = read.nextLong();
long b = read.nextLong();
long c = read.nextLong();
read.close();
//normalisation a>=0
if(a<0){
a=-a;
b=-b;
c=-c;
}
//case 0: c==0
if(c==0){
System.out.println("0 0");
return;
}
//case 1: a==0
if(a==0){
if(c%b==0){
System.out.println("0 "+(-c/b));
}
else{
System.out.println("-1");
}
return;
}
//case 2: b==0
if(b==0){
if(c%a==0){
System.out.println((-c/a)+" 0");
}
else{
System.out.println("-1");
}
return;
}
long xplus=0;
long yplus=0;
long origc=c;
long x=0, y=0;
Coordinates solution;
//case 3: a>0; b>0;
if(b>0){
if(a>b){
if(c>0){
xplus=(c+a-1)/a;
}
else{
xplus=-(c/a);
}
c=-c+xplus*a;
if(c==origc){
xplus=0;
}
}
else{
if(c>0){
yplus=(c+b-1)/b;
}
else{
yplus=-(c/b);
}
c=-c+yplus*b;
if(c==origc){
yplus=0;
}
}
if(c==0){
System.out.println((-xplus)+" "+(-yplus));
}
else{
solution=erwGGT(a, b);
long ggt=ggt(a, b);
x=(solution.x*(c/ggt)-xplus);
y=(solution.y*(c/ggt)-yplus);
if(c%ggt==0){
System.out.println(x+" "+y);
}
else{
System.out.println("-1");
}
}
//System.out.println(a*x+b*y+origc);
return;
}
//case 4: a>0; b<0
if(b<0){
long modb=Math.abs(b);
if(a>=modb){
if(c>0){
xplus=(c+a-1)/a;
}
else{
xplus=-(c/a);
}
c=-c+xplus*a;
if(c==origc){
xplus=0;
}
}
else{
if(c>0){
yplus=-((c+modb-1)/modb);
}
else{
yplus=c/modb;
}
c=-c+yplus*b;
}
if(c==0){
System.out.println((-xplus)+" "+(-yplus));
}
else{
solution=erwGGT(a, b);
long ggt=ggt(a, modb);
x=(solution.x*(c/ggt)-xplus);
y=(solution.y*(c/ggt)-yplus);
if(c%ggt==0){
System.out.println(x+" "+y);
}
else{
System.out.println("-1");
}
}
//System.out.println(a*x+"+"+b*y+"+"+origc+"="+(a*x+b*y+origc));
return;
}
}
}
class Coordinates{
long x;
long y;
Coordinates(long x, long y){
this.x=x;
this.y=y;
}
public String toString(){
return this.x+" "+this.y;
}
}
/*
Tests
-957757861 308710346 45337024
587450634832960 1822535171726016
*/ | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 624e3c4dd2c18238176cc1bf8d270ba9 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
/**
* BEGIN NO SAD
* Blessed are those who suffer for doing what is right.
* The kingdom of heaven belongs to them.
* (Matthew 5:10-12)
* @author Tran Anh Tai
* @template for CP codes
* END NO SAD
*/
public class ProbC {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
// main solver
static class Task{
boolean mark[] = new boolean[31622];
ArrayList<Integer> primes = new ArrayList<>();
public void solve(InputReader in, PrintWriter out) {
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int g = gcd(a, b);
for (int i = 2; i <= 31621; i++){
if (!mark[i]){
primes.add(i);
for (int j = i * 2; j <= 31621; j += i) mark[j] = true;
}
}
if (c % g != 0){
out.println(-1);
}
else{
a /= g; b /= g; c /= g;
if (b < 0) {
a = -a; b = -b; c = -c;
}
if (b == 0){
if (-c % a == 0){
out.println(-c /a + " " + 0);
}
else{
out.println(-1);
}
return;
}
int ex = EulerTotient(b) - 1;
c = -c;
String x = (new BigInteger(binpow(a, ex, b) + "")).multiply(new BigInteger((c) +"")).toString();
String y = (new BigInteger(c + "").subtract(new BigInteger(a +"").multiply(new BigInteger(x)))).divide(new BigInteger(b +"")).toString();
out.println(x + " " + y);
}
}
private long binpow(long a, int ex, int b) {
if (ex == 0){
return 1;
}
long au = binpow(a, ex >> 1, b);
au = (au * au) % b;
if ((ex & 1) == 1){
au = (au * a) % b;
}
return au;
}
public int EulerTotient(int n){
int result = n;
for (int i = 2; i <= Math.sqrt(n); i++){
if (!mark[i] && n % i == 0){
result = result / i * (i - 1);
while (n % i == 0) n = n / i;
}
}
if (n > 1){
result = result / n * (n - 1);
}
return result;
}
public int gcd(int a, int b){
if (a == 0){
return b;
}
return gcd(b % a, a);
}
}
static class Pair{
public int x, y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
}
static class Point{
public double x, y;
public Point(double x, double y){
this.x = x; this.y = y;
}
public static Line getBisect(Point A, Point B){
double a = B.x - A.x;
double b = B.y - A.y;
double c = a * (A.x + B.x) / 2 + b * (A.y + B.y) / 2;
return new Line(a, b, c);
}
public static Point center(Point A, Point B, Point C){
Line lab = getBisect(A, B);
Line lac = getBisect(A, C);
Point intersect = Line.intersect(lab, lac);
return intersect;
}
}
static class Line{
public double a, b, c;
public Line(double a, double b, double c){
this.a = a; this.b = b; this.c = c;
}
public static Point intersect(Line l1, Line l2){
double D = l1.a * l2.b - l1.b * l2.a;
double Dy = l1.a * l2.c - l2.a * l1.c;
double Dx = l1.c * l2.b - l2.c * l1.b;
return new Point(Dx / D, Dy / D);
}
}
// fast input reader class;
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong(){
return Long.parseLong(nextToken());
}
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 810859354ba092e281c0a8da5c52ebfa | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.Scanner;
public class Problem_7C {
private static long[] extendedGCD(long a, long b) {
long[][] res = new long[][] { {1, 0, a}, {0, 1, b}, {0, 0, 0}};
while (res[1][2] != 0) {
long q = res[0][2] / res[1][2];
for (int i = 0; i < 3; i++) {
res[2][i] = res[0][i] - res[1][i] * q;
}
res[0] = res[1];
res[1] = res[2];
res[2] = new long[3];
}
return res[0];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
long[] ans = extendedGCD(a, b);
if (c % ans[2] == 0) {
System.out.println((ans[0] * (-c / ans[2])) + " " + (ans[1] * (-c / ans[2])));
} else {
System.out.println(-1);
}
sc.close();
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 335b9f3dedebb64c7391f2c994d9e5cc | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C7CV2 {
private StringTokenizer st;
private BufferedReader bf;
class Node {
public Node(int s, boolean d) {
dirty = d;
size = s;
}
boolean dirty;
int size;
}
public C7CV2() {
try {
bf = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(bf.readLine());
long A = nextLong();
long B = nextLong();
long C = nextLong();
long[] res = extendedEuclid3(A, B);
//long check = -C*res[0]/res[2]*A + -C*res[1]*B/res[2] + C;
if (C%res[2] != 0) {
System.out.println(-1);
} else {
System.out.println(-C*res[0]/res[2] + " " + (-C*res[1]/res[2]));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static long[] extendedEuclid3(long a, long b) {
long r_old = a;
long r_new = b;
long s_old = 1;
long s_new = 0;
long t_old = 0;
long t_new = 1;
long r_temp = 0;
long s_temp = 0;
long t_temp = 0;
long r_old_old = 0;
while (r_new != 0) {
r_old_old = r_old;
long q = r_old / r_new;
r_temp = r_new;
r_new = r_old - q * r_new;
r_old = r_temp;
s_temp = s_new;
s_new = s_old - q * s_new;
s_old = s_temp;
t_temp = t_new;
t_new = t_old - q * t_new;
t_old = t_temp;
}
return new long[]{s_old, t_old, r_old};
}
private long nextLong() throws IOException {
return Long.parseLong(next());
}
private int nextInt() throws IOException {
return Integer.parseInt(next());
}
private String next() throws IOException {
if (st == null || !st.hasMoreTokens())
st = new StringTokenizer(bf.readLine());
return st.nextToken();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
C7CV2 c = new C7CV2();
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | a7c9feb07937a2787c52933a9d466478 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
c7 solver = new c7();
solver.solve(1, in, out);
out.close();
}
static class c7 {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int g = gcd(Math.abs(a), Math.abs(b));
if (c % g != 0) {
out.println("-1");
return;
}
a /= g;
b /= g;
c /= g;
if (b == 0) {
int x = -c / a;
out.println(x + " 0");
return;
}
if (a == 0) {
int y = -c / b;
out.println("0 " + y);
return;
}
BigInteger x = new BigInteger(-c + "").multiply(new BigInteger(a + "").modInverse(new BigInteger(Math.abs(b) + ""))).mod(new BigInteger(Math.abs(b) + ""));
BigInteger y = new BigInteger(-c + "").subtract(new BigInteger(a + "").multiply(x)).divide(new BigInteger(b + ""));
out.println(x + " " + y);
}
int gcd(int a, int b) {
while (b != 0) {
int t = a % b;
a = b;
b = t;
}
return a;
}
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer stt;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
return null;
}
}
public String next() {
while (stt == null || !stt.hasMoreTokens()) {
stt = new StringTokenizer(nextLine());
}
return stt.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 6a7ac81eb06089b82cb2184d2cbaaf00 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.io.*;
public class CF7C {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static long x, y; // ax + by = gcd(a, b)
public static void main(String[] args) throws IOException {
String[] split = br.readLine().split(" ");
long a = Long.parseLong(split[0]);
long b = Long.parseLong(split[1]);
long c = -Long.parseLong(split[2]);
long gcd = extendedGCD(a, b);
if(c % gcd == 0) {
c /= gcd;
long i = x*c;
long j = y*c;
pw.println(i + " " + j);
}
else pw.println(-1);
pw.close();
}
public static long extendedGCD(long a, long b) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long g = extendedGCD(b, a % b);
long z = x - (a / b) * y;
x = y;
y = z;
return g;
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | db29ae01e445cf00408b268a0aebb2a3 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.util.Scanner;
public class CF_7CLine {
static long x, y;
public static int gcd(int a, int b) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
int g = gcd(b % a, a);
long x1 = y - x * (b / a);
long y1 = x;
x = x1;
y = y1;
return g;
}
static boolean findSol(int A, int B, int C) {
int g = gcd(Math.abs(A), Math.abs(B));
if (C % g != 0)
return false;
x *= (C / g);
y *= (C / g);
if (A < 0)
x *= -1;
if (B < 0)
y *= -1;
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
int B = sc.nextInt();
int C = sc.nextInt();
boolean out = findSol(-A, -B, C);
System.out.println(!out ? -1 : x + " " + y);
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 3b6c827481f570f3cc36da014363d323 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class CF7C {
public static void main(String[] args) {
int a = IN.nextInt(), b = IN.nextInt(), c = -IN.nextInt();
solve(a, b, c);
}
private static void solve(int a, int b, int c) {
long x[] = new long[1], y[] = new long[1];
long gcd = extGcd(a, b, x, y);
if (c % gcd != 0) {
System.out.println("-1");
} else {
long x1 = c / gcd * x[0];
long y1 = c / gcd * y[0];
System.out.println(x1 + " " + y1);
}
}
private static long extGcd(long a, long b, long x[], long y[]) {
if (b == 0) {
x[0] = 1;
y[0] = 0;
return a;
} else {
long ret = extGcd(b, a % b, x, y);
long tmp = x[0];
x[0] = y[0];
y[0] = tmp - (a / b) * y[0];
return ret;
}
}
static final class IN {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer st = null;
public static String readLine() throws IOException {
return br.readLine();
}
public static String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
break;
}
}
return st.nextToken();
}
public static int nextInt() {
return Integer.valueOf(next());
}
public static long nextLong() {
return Long.valueOf(next());
}
public static void close() {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
| Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | d5256f33aeab9d931da399c7dc1c65c8 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main {
static FastReader in = new FastReader("in.txt");
static PrintWriter out = new PrintWriter(System.out);
static long x, y;
public static void main(String[] args) throws IOException {
long a = in.nextInt(),
b = in.nextInt(),
c = -in.nextInt();
long d = extendedEuclid(a, b);
if(c%d != 0)
out.println(-1);
else
out.println(x * c/d + " " + y * c/d);
out.flush();
}
static long extendedEuclid(long a, long b){
if (b == 0) { x = 1; y = 0; return a; }
long d = extendedEuclid(b, a % b);
long x1 = y;
long y1 = x - (a / b) * y;
x = x1; y = y1;
return d;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(String file) {
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) { e.printStackTrace(); }
return str;
}
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | 158d4dea1a1e5411cb0d68c97ae38302 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.io.*;
import java.util.*;
public final class code
// class code
// public class Solution
{
static void solve()throws IOException
{
int a=nextInt();
int b=nextInt();
int c=-nextInt();
if(c==0)
{
out.println(0+" "+0);
return;
}
if(a==0)
{
if(Math.abs(c)%Math.abs(b)==0)
out.println(0+" "+c/b);
else
out.println(-1);
return;
}
if(b==0)
{
if(Math.abs(c)%Math.abs(a)==0)
out.println(c/a+" "+0);
else
out.println(-1);
return;
}
int g=gcd(Math.abs(a),Math.abs(b));
// debug(g);
if(Math.abs(c)%g!=0)
{
out.println(-1);
return;
}
Pair sol=solution(Math.abs(a),Math.abs(b));
long x=sol.first;
long y=sol.second;
// debug(x,y);
if(a<0)
x=-x;
if(b<0)
y=-y;
// debug(c/g);
long x1=1l*c/g*x;
long y1=1l*c/g*y;
out.println(x1+" "+y1);
}
static int gcd(int a,int b)
{
if(a==0)
return b;
else
return gcd(b%a,a);
}
static Pair solution(int a,int b)
{
if(a==0)
return new Pair(0,1);
Pair p=solution(b%a,a);
return new Pair(p.second-b/a*p.first,p.first);
}
///////////////////////////////////////////////////////////
public static void main(String args[])throws IOException
{
br=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(new BufferedOutputStream(System.out));
random=new Random();
solve();
// int t=nextInt();
// for(int i=1;i<=t;i++)
// {
// // out.print("Case #"+i+": ");
// solve();
// }
out.close();
}
static final long mod=(long)(1e9+7);
static final int inf=(int)(1e9+1);
// static final long inf=(long)(1e18);
static class Pair implements Comparable<Pair>
{
long first,second;
Pair(long a,long b)
{
first=a;
second=b;
}
public int compareTo(Pair p)
{
return first>p.first?1:-1;
}
}
static int max(int ... a)
{
int ret=a[0];
for(int i=1;i<a.length;i++)
ret=Math.max(ret,a[i]);
return ret;
}
static int min(int ... a)
{
int ret=a[0];
for(int i=1;i<a.length;i++)
ret=Math.min(ret,a[i]);
return ret;
}
static void debug(Object ... a)
{
System.out.print("> ");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
static void debug(int a[]){debuga(Arrays.stream(a).boxed().toArray());}
static void debug(long a[]){debuga(Arrays.stream(a).boxed().toArray());}
static void debuga(Object a[])
{
System.out.print("> ");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
static Random random;
static BufferedReader br;
static StringTokenizer st;
static PrintWriter out;
static String nextToken()throws IOException
{
while(st==null || !st.hasMoreTokens())
st=new StringTokenizer(br.readLine());
return st.nextToken();
}
static String nextLine()throws IOException
{
return br.readLine();
}
static int nextInt()throws IOException
{
return Integer.parseInt(nextToken());
}
static long nextLong()throws IOException
{
return Long.parseLong(nextToken());
}
static double nextDouble()throws IOException
{
return Double.parseDouble(nextToken());
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output | |
PASSED | b077cd638e3a08fd38efe8739238c302 | train_003.jsonl | 1270136700 | A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class Main {
static boolean visited[] ;
static boolean ends[] ;
static long mod = 1000000007 ;
static int lens[] ;
static int seeds[] ;
static int a[] ;
static double total ;
public static ArrayList adj[] ;
public static long x,y ;
public static void main(String[] args) throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in) ;
long A= sc.nextLong() ;
long B= sc.nextLong() ;
long C= sc.nextLong() ;
if(A==0)
{
if(Math.abs(C)<B && C!=0)System.out.println(-1);
else
if(C%B==0)
System.out.println(0+" "+(-1*(C/B)));
else
System.out.println(-1);
return ;
}
if(B==0)
{
if(Math.abs(C)<A && C!=0)System.out.println(-1);
else
if(C%A==0)
System.out.println((-1*(C/A))+" "+0);
else
System.out.println(-1);
return ;
}
if(C==0)
{
System.out.println("0 0");
return ;
}
if(C%B==0)
{
System.out.println("0 "+(-1*(C/B)));
return ;
}
if(C%A==0)
{
System.out.println((-1*(C/A)+" 0"));
return ;
}
if(A%B==0 || B%A==0)
{
System.out.println(-1);
return ;
}
// x=0 ; y=1;
long g = gcdExtended(Math.abs((int)A), Math.abs((int)B));
if(C%g!=0)
System.out.println(-1);
else
{
x*=C/g ;
y*=C/g ;
if(A>0)x*=-1 ;
if(B>0)y*=-1 ;
System.out.println(x+" "+y);
}
}
public static int gcdExtended(int a, int b)
{
// Base Case
if (a == 0)
{
x = 0;
y = 1;
return b;
}
//int x1=1, y1=1; // To store results of recursive call
int gcd = gcdExtended(b%a, a);
// Update x and y using results of recursive
// call
long x1 = y - (b/a) * x;
long y1 = x;
x = x1 ;
y = y1 ;
return gcd;
}
static int even(String x , int b )
{
for (int j = b; j>=0; j--)
{
int current = x.charAt(j)-48 ;
if(current%2==0)
return current ;
}
return -1;
}
static int odd(String x , int b )
{
for (int j = b; j>=0; j--)
{
int current = x.charAt(j)-48 ;
if(current%2!=0)
return current ;
}
return -1;
}
static long pow(long base, int exp) {
long res = 1;
while(exp > 0) {
if(exp % 2 == 1) {
res = (res * base) % mod;
}
base = (base * base) % mod;
exp /= 2;
}
return res;
}
public static long solve(int k1, long k2)
{
long x = 1l*k2*(pow(2, k1)-1) ;
return x%(1000000007) ;
}
public static long getN(long x)
{
long n = (long) Math.sqrt(x*2) ;
long y = n*(n+1)/2;
if(y==x)
return n ;
else if(y>x)
return n ;
else
for (long i = n; ; i++)
{
y = i*(i+1)/2 ;
if(y>=x)
return i ;
} }
public static void dfss(int root , int len)
{
visited[root]=true ;
if(ends[root] && root!=0) lens[root] = len ;
for (int i = 0; i < adj[root].size(); i++)
{
int c= (int) adj[root].get(i) ;
if(visited[c]==false)
dfss(c, len+1);
}
}
public static void pr(int root , int seed){
visited[root] = true ;
int dv = adj[root].size()-1 ;
if(root==0) dv++ ;
for (int i = 0; i < adj[root].size(); i++)
{
int c = (int)adj[root].get(i) ;
seeds[c]=dv*seed ;
}
for (int i = 0; i < adj[root].size() ; i++)
{
int c = (int)adj[root].get(i) ;
if(visited[c]==false)
pr(c , seeds[c]);
}
}
public static String concatinate(String s ,int n)
{
if(s.length()==n)
return s ;
else return concatinate("0"+s, n) ;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public boolean ready() throws IOException {
return br.ready();
}
}
public static long getGCD(long n1, long n2) {
if (n2 == 0) {
return n1;
}
return getGCD(n2, n1 % n2);
}
public static int cnt1(int mat[][]) //how many swaps to be a 1 matrix
{
int m = mat.length ;
int c=0 ;
for (int i = 0; i < mat.length; i++)
{
for (int j = 0; j < mat.length; j++)
{
int x = (i*m) +j ;
if(x%2==0 && mat[i][j]==0)
c++;
if(x%2!=0 && mat[i][j]==1)
c++;
}
}
return c;
}
public static int cnt0(int mat[][])
{
int m = mat.length ;
int c=0 ;
for (int i = 0; i < mat.length; i++)
{
for (int j = 0; j < mat.length; j++)
{
int x = (i*m) +j ;
if(x%2!=0 && mat[i][j]==0)
c++;
if(x%2==0 && mat[i][j]==1)
c++;
}
}
return c;
}
public static boolean canFit2(int x1, int y1 , int x2 , int y2 , int x3 , int y3){
if(x1==x2)
if(x1==x3)
return true ;
else
return false ;
else
if(x1==x3)
return false ;
else
{
long a = 1l*(y2-y1)*(x3-x2) ;
long b = 1l*(y3-y2)*(x2-x1) ;
if(a==b)
return true;
else
return false ;
}
}
public static void shuffle(long[] a2){
if(a2.length==1)
return ;
for (int i = 0; i < a2.length; i++)
{
Random rand = new Random();
int n = rand.nextInt(a2.length-1) + 0;
long temp = a2[i] ;
a2[i] = a2[n] ;
a2[n] = temp ;
}
}
public static int binary(long[]arr, int l, int r, long x) /// begin by 0 and n-1
{
if (r>=l)
{
int mid = l + (r - l)/2;
if (arr[mid]== x)
return mid;
if (arr[mid]> x)
return binary(arr, l, mid-1, x);
return binary(arr, mid+1, r, x);
}
return -1;
}
/// searching for the index of first elment greater than x
public static int binary1(long[]arr , long x) {
int low = 0, high = arr.length; // numElems is the size of the array i.e arr.size()
while (low != high) {
int mid = (low + high) / 2; // Or a fancy way to avoid int overflow
if (arr[mid] <= x) {
/* This index, and everything below it, must not be the first element
* greater than what we're looking for because this element is no greater
* than the element.
*/
low = mid + 1;
}
else {
/* This element is at least as large as the element, so anything after it can't
* be the first element that's at least as large.
*/
high = mid;
}
}
return low ; // return high ;
}
private static boolean triangle(int a, int b , int c){
if(a+b>c && a+c>b && b+c>a)
return true ;
else
return false ;
}
private static boolean segment(int a, int b , int c){
if(a+b==c || a+c==b && b+c==a)
return true ;
else
return false ;
}
private static int gcdThing(long a, long b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
public static boolean is(int i){
if(Math.log(i)/ Math.log(2) ==(int) (Math.log(i)/ Math.log(2)))
return true ;
if(Math.log(i)/ Math.log(3) ==(int) (Math.log(i)/ Math.log(3)) )
return true ;
if(Math.log(i)/ Math.log(6) ==(int) (Math.log(i)/ Math.log(6)) )
return true ;
return false;
}
public static boolean contains(int b[] , int x)
{
for (int i = 0; i < b.length; i++)
{
if(b[i]==x)
return true ;
}
return false ;
}
public static int binary(long []arr , long target , int low , long shift) {
int high = arr.length;
while (low != high) {
int mid = (low + high) / 2;
if (arr[mid]-shift <= target) {
low = mid + 1;
}
else {
high = mid;
}
}
return low ; // return high ;
}
public static boolean isLetter(char x){
if(x+0 <=122 && x+0 >=97 )
return true ;
else if (x+0 <=90 && x+0 >=65 )
return true ;
else return false;
}
public static long getPrimes(long x ){
if(x==2 || x==3 || x==1)
return 2 ;
if(isPrime(x))
return 5 ;
for (int i = 2; i*i<=x; i++)
{
if(x%i==0 && isPrime(i))
return getPrimes(x/i) ;
}
return -1;
}
public static String solve11(String x){
int n = x.length() ;
String y = "" ;
for (int i = 0; i < n-2; i+=2)
{
if(ifPalindrome(x.substring(i, i+2)))
y+= x.substring(i, i+2) ;
else
break ;
}
return y+ solve11(x.substring(y.length(),x.length())) ;
}
public static String solve1(String x){
String y = x.substring(0 , x.length()/2) ;
return "" ;
}
public static String reverse(String x){
String y ="" ;
for (int i = 0; i < x.length(); i++)
{
y = x.charAt(i) + y ;
}
return y ;
}
public static boolean ifPalindrome(String x){
int numbers[] = new int[10] ;
for (int i = 0; i < x.length(); i++)
{
int z = Integer.parseInt(x.charAt(i)+"") ;
numbers[z] ++ ;
}
for (int i = 0; i < numbers.length; i++)
{
if(numbers[i]%2!=0)
return false;
}
return true ;
}
public static int get(int n){
return n*(n+1)/2 ;
}
// public static long getSmallestDivisor( long y){
// if(isPrime(y))
// return -1;
//
// for (long i = 2; i*i <= y; i++)
// {
// if(y%i ==0)
// {
// return i;
// }
// }
// return -1;
// }
public static int lis( int[]a , int n){
int lis[] = new int[n] ;
Arrays.fill(lis,1) ;
for(int i=1;i<n;i++)
for(int j=0 ; j<i; j++)
if (a[i]>a[j] && lis[i] < lis[j]+1)
lis[i] = lis[j] + 1;
int max = lis[0];
for(int i=1; i<n ; i++)
if (max < lis[i])
max = lis[i] ;
return (max);
// ArrayList<Integer> s = new ArrayList<Integer>() ;
// for (int i = n-1; i >=0; i--)
// {
// if(lis[i]==max)
// {
// s.add(a[i].z);
// max --;
// }
// }
//
// for (int i = s.size()-1 ; i>=0 ; i--)
// {
// System.out.print(s.get(i)+" ");
// }
//
}
public static int calcDepth(Vertix node){
if(node.depth>0) return node.depth;
// meaning it has been updated before;
if(node.parent != null)
return 1+ calcDepth(node.parent);
else
return -1;
}
public static boolean isPrime (long num){
if (num < 2) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i * i <= num; i += 2)
if (num % i == 0) return false;
return true;
}
public static ArrayList<Long> getDiv(Long n)
{
ArrayList<Long> f = new ArrayList<Long>() ;
while (n%2==0)
{
if(!f.contains(2))f.add((long) 2) ;
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (long i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
if(!f.contains(i))f.add(i);
n /= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
if(!f.contains(n))f.add(n);
return f ;
}
// public static boolean dfs(Vertix v , int target){
// try{
// visited[v.i]= true ;
// } catch (NullPointerException e)
// {
// System.out.println(v.i);
// }
// if(v.i == target)
//
// return true ;
// for (int i =0 ; i< v.neighbours.size() ; i++)
// {
//
// Vertix child = v.neighbours.get(i) ;
// if(child.i == target){
// found = true ;
// }
// if(visited[child.i]==false){
// found |= dfs(child, target) ;
// }
// }
// return found;
// }
public static class Vertix{
long i ;
int depth ;
ArrayList<Vertix> neighbours ;
Vertix parent ;
Vertix child ;
public Vertix(long i){
this.i = i ;
this.neighbours = new ArrayList<Vertix> () ;
this.parent = null ;
depth =-1;
this.child = null ;
}
}
public static class pair implements Comparable<pair> {
int x ;
int y ;
public pair(int x, int y ){
this.x=x ;
this.y =y ;
}
@Override
public int compareTo(pair p) {
if(this.x > p.x)
return 1 ;
else if (this.x == p.x)
if(this.y <= p.y) return 1 ;
else if(this.y == p.y) return 0 ;
else return -1 ;
else
return -1 ;
}
}
public static class pair2 implements Comparable<pair2>{
int i ;
int j ;
int plus ;
public pair2(int i , int j , int plus){
this.i =i ;
this.j = j ;
this.plus = plus ;
}
@Override
public int compareTo(pair2 p) {
if(this.j > p.j)
return 1 ;
else if (this.j == p.j) return 0 ;
else
return -1 ;
}
}
} | Java | ["2 5 3"] | 1 second | ["6 -3"] | null | Java 8 | standard input | [
"number theory",
"math"
] | a01e1c545542c1641eca556439f0692e | The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0. | 1,800 | If the required point exists, output its coordinates, otherwise output -1. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.