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
|
23e4ac80517065477bae0e0c68188589
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
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;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
AConneRAndTheARCMarklandN solver = new AConneRAndTheARCMarklandN();
solver.solve(1, in, out);
out.close();
}
static class AConneRAndTheARCMarklandN {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int q = in.nextInt();
loop:
while (q-- > 0) {
int n = in.nextInt();
int cur = in.nextInt();
int closed = in.nextInt();
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < closed; i++)
set.add(in.nextInt());
int temp = cur;
int temp2 = cur;
while (true) {
if (temp <= n && !set.contains(temp)) {
out.println(temp - cur);
continue loop;
} else
temp++;
if (temp2 > 0 && !set.contains(temp2)) {
out.println(cur - temp2);
continue loop;
} else
temp2--;
}
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(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());
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
1653057143d385d8c856cbceb160427d
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
for(int t=1;t<=T;t++){
int N=sc.nextInt();
int S=sc.nextInt();
int K=sc.nextInt();
if(K==N){
System.out.println(-1);
continue;
}
int[] Karray=new int[K];
int startnode=0;
int flag=0;
for(int i=0;i<K;i++){
Karray[i]=sc.nextInt();
}
Arrays.sort(Karray);
for(int i=0;i<K;i++){
if(Karray[i]==S){
flag=1;
startnode=i;
}
}
//System.out.println(startnode);
if(flag==0){
System.out.println(0);
continue;
}
Arrays.sort(Karray);
//System.out.println(startnode);
int a=Integer.MAX_VALUE;
int b=Integer.MAX_VALUE;
for(int i=startnode+1;i<Karray.length;i++){
if( Karray[i]-Karray[startnode]!=i-startnode){
a=(i-startnode);
//System.out.println(startnode);
break;
}
}
for(int i=startnode-1;i>=0;i--){
if( Karray[startnode]-Karray[i]!=startnode-i){
// System.out.println(Karray[startnode]+":"+Karray[i]);
b=(startnode-i);
break;
}
}
int ans1=Math.min(a,b);
//System.out.println(ans1);
int c=Integer.MAX_VALUE;
int d=Integer.MAX_VALUE;
if(Karray[Karray.length-1]<N){
c=Karray[Karray.length-1]+1-Karray[startnode];
}
if(Karray[0]>1){
d=Karray[startnode]-(Karray[0]-1);
}
int ans2=Math.min(c,d);
// System.out.println(a+":"+b);
//System.out.println(c+";"+d);
int ans=Math.min(ans1,ans2);
System.out.println(ans);
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
4148b8c65fc56281f40fc295cf0b0486
|
train_001.jsonl
|
1579440900
|
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
|
256 megabytes
|
import java.util.*;
public class P1{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int s=sc.nextInt();
int k=sc.nextInt();
ArrayList<Integer> a=new ArrayList<Integer>();
for (int i=0;i<k;++i) {
a.add(sc.nextInt());
}
if (a.contains(s)) {
int l=s,u=s,count=0;
while(true){
++count;
if (l>1) {
--l;
if (a.contains(l)) {
}
else{
System.out.println(count);
break;
}
}
if (u<n) {
++u;
if (a.contains(u)) {
}
else{
System.out.println(count);
break;
}
}
}
}
else{
System.out.println("0");
}
}
}
}
|
Java
|
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
|
1 second
|
["2\n0\n4\n0\n2"]
|
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
|
Java 8
|
standard input
|
[
"binary search",
"implementation",
"brute force"
] |
faae9c0868b92b2355947c9adcaefb43
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
| 1,100 |
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
|
standard output
| |
PASSED
|
70b0c602fb5b191adbd31c1098f99fe7
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.*;
public class Solution{
static Scanner sc = new Scanner(System.in);
public static void main(String args[]){
int n = sc.nextInt();
int[]arr = new int[n];
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
int ptr1=0,ptr2=0;
int cur = 1,max = 1;
for(int i=0;i<n;i++){
ptr1 = ptr2 = i;
boolean flag1 = true,flag2 = true;
while(ptr1>-1||ptr2<n){
if(ptr1>0&&arr[ptr1]>=arr[ptr1-1]){
cur++;
ptr1--;
}
else
flag1 = false;
if(ptr2+1<n&&arr[ptr2]>=arr[ptr2+1]){
cur++;
ptr2++;
}
else
flag2 = false;
if(!(flag1||flag2))
break;
}
max = max<cur?cur:max;
//System.out.println(i+","+cur);
cur = 1;
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
7b1a0cb4f9d8a655062e9d4ea2a0f18b
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int[] arr, values;
static int max;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n = Integer.parseInt(br.readLine());
arr = new int[n];
values = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.fill(values, 0);
max = -1;
for (int i = 0; i < arr.length; i++) {
int total = 1;
for (int j = i - 1; j >= 0; j--) {
if (arr[j] <= arr[j + 1]) {
total++;
} else {
break;
}
}
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] <= arr[j - 1]) {
total++;
} else {
break;
}
}
values[i] = total;
max = Integer.max(max, total);
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
1a66236b2067ba3b501c85395266137c
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
int arr[] = new int[n];
int max = -1;
StringTokenizer stk = new StringTokenizer(in.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(stk.nextToken());
}
for (int i = 0; i < arr.length; i++) {
int total = 1;
int last = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] <= arr[last]) {
total++;
last = j;
} else {
break;
}
}
last = i;
for (int j = i - 1; j >= 0; j--) {
if (arr[j] <= arr[last]) {
total++;
last = j;
} else {
break;
}
}
max = Integer.max(max, total);
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
f7cbdddc85b1e857828114f6da384145
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.*;
public class judge {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int[] parts = new int[length];
int sum = 0;
int max = 0;
for (int index = 0; index < length; index++) {
int val = scanner.nextInt();
parts[index] = val;
}
for (int index = 0; index < length; index++) {
List<Integer> path = new ArrayList();
int curr = parts[index];
path.add(curr);
for (int i = index + 1; i < length; i++) {
if (parts[i] <= path.get(path.size()-1)) {
path.add(parts[i]);
} else {
break;
}
}
if (index != 0){
// System.out.println("- "+path);
Collections.reverse(path);
for (int j = index - 1; j >= 0; j--) {
if (parts[j] <= path.get(path.size()-1)) {
path.add(parts[j]);
} else {
break;
}
}}
// System.out.println(path);
if (path.size() > max) max = path.size();
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
36afa5bdb52d770680841115dc69b03e
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.*;
public class interview {
public static void main(String[] args) {
gardenRain();
}
public static void gardenRain() {
Scanner sc= new Scanner(System.in);
int n= sc.nextInt();
// System.out.println(n);
int [] levels = new int[n];
int i_reached = 1;
int left = 0;
int right = 0;
int mid = 0;
int max = 1;
boolean left_flag = true;
levels[0]=sc.nextInt();
for(int i =1;i<n;i++) {
// System.out.println(i);
if (i_reached==i) {
levels[i]=sc.nextInt();
// System.out.println(levels[i]);
i_reached++;
}
if (left_flag) {
if(levels[i]>=levels[i-1]) {
left++;
// System.out.println(levels[i]+" left");
}
else {
left_flag = false;
mid = i;
// continue;
}
}
if (!left_flag)
{
if(levels[i]<=levels[i-1]) {
right++;
// System.out.println(levels[i]+" right");
}
else {
max = Math.max(left+right+1, max);
left_flag=true;
left=0;
right=0;
i = mid;
}
}
max = Math.max(left+right+1, max);
}
System.out.println(max);
}
public static void vanye() {
Scanner sc= new Scanner(System.in);
String input1= sc.nextLine();
String input2= sc.nextLine();
String [] inputs1 = input1.split(" ");
String [] inputs2 = input2.split(" ");
int [] friends = new int[inputs2.length];
for(int i=0;i<inputs2.length;i++) {
friends[i] =Integer.parseInt(inputs2[i]);
}
int h = Integer.parseInt(inputs1[1]);
int r = 0;
for(int i = 0;i<friends.length;i++) {
if(friends[i]<=h) {
r++;
}
else {
r+=2;
}
}
System.out.println(r);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
e7fd641bd7e831bbc4a3c4f2439891a1
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class ArtificialRain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
do{
n=sc.nextInt();
}while((n<1)||(n>1000));
int tab[] = new int[n];
for(int i=0;i<tab.length;i++){
tab[i] = sc.nextInt();
}
System.out.println(max(tab));
}
public static int max(int []tab){
int max = 0;
for (int i=0;i<tab.length;i++){
int totSeq = seqLeft(i,tab)+seqRight(i,tab)+1;
if(totSeq>=max){
max = totSeq;
}
}
return max;
}
public static int seqLeft(int i,int[] tab){
int seq=0;
while((i>0)&&(tab[i]>=tab[i-1])){
seq++;
i--;
}
return seq;
}
public static int seqRight(int i,int[] tab){
int seq=0;
while((i<tab.length-1)&&(tab[i]>=tab[i+1])){
seq++;
i++;
}
return seq;
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
c6d16607c4b10fefadbde52af6e5a21a
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class Smart {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n =sc.nextInt();
int a[]=new int[n];
int x=0;
int max=1;
for (int i = 0; i < n; i++) {
a[i]=sc.nextInt();
}
for (int i = 1; i < n; i++) {
if(a[i-1]>=a[i])max++;
else break;
}
for (int i = 1; i < n; i++) {
int z=a[i];
if(z>=a[i-1])x++;
else x=0;
int y=0;
for (int j =i+1; j < n; j++) {
if(a[j-1]>=a[j])y++;
else break;
}
if(x+y+1>max)max=x+y+1;
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
96aeef6e7f730ec77aabdae085c031bd
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class NewClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int max = 1;
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
if (a.length == 1) {
System.out.println(1);
}
else {
for (int i = 0; i < n; i++) {
int count = 1;
for (int j = i + 1; j < n; j++) {
if (a[j] <= a[j - 1]) {
count++;
} else {
break;
}
}
for (int j = i - 1; j >= 0; j--) {
if (a[j] <= a[j + 1]) {
count++;
} else {
break;
}
}
if (count > max) {
max = count;
}
}
System.out.println(max);
}
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
2a2716db5956f662b84d1003936c6cf9
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Collections;
import java.util.Comparator;
import javafx.util.Pair;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
int []arr= new int[n];
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
int i1=0,i2=0,max=Integer.MIN_VALUE;
for(int i=0;i<n;i++)
{
i1=i;
while(i1>0 && arr[i1-1]<=arr[i1])
i1--;
i2=i;
while(i2<n-1 && arr[i2+1]<=arr[i2])
i2++;
int curMax = i2-i1 + 1;
if(curMax>max)
max = curMax;
}
out.println(max);
out.flush();
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
54019eb5f11184c13555f994eba72821
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
int max = 0;
for (int i = 0; i < a.length; i++)
a[i] = scan.nextInt();
for (int i = 0; i < a.length; i++) {
int count = 0;
for (int j = i; j < a.length; j++) {
if (j == a.length-1)
break;
if (a[j] >= a[j + 1])
count++;
else
break;
}
for (int j = i; j != 0; j--) {
if (a[j] >= a[j - 1])
count++;
else
break;
}
count++;
if (max < count)
max = count;
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
55bbeaadb835631f5aa32aa21854b3c8
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class B {
public static void main(String[] args) {
FastReader in = new FastReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = in.nextInt();
int ar[] = new int[n];
for(int i = 0;i<n;i++){
ar[i] = in.nextInt();
}
int max = 0;
int tempmax = 0;
int lDec = 0;int rDec = 0;
for(int i = 0;i<ar.length;i++){
for(int j = i-1;j>=0;j--){
if(ar[j]<=ar[j+1]){
lDec++;
}
else{
break;
}
}
for(int j = i+1;j<ar.length;j++){
if(ar[j]<=ar[j-1]){
rDec++;
}
else{
break;
}
}
// System.out.println("i "+i +" "+lDec+" "+rDec );
tempmax = lDec+rDec+1;
if(tempmax > max){
max = tempmax;
}
tempmax = 0;
lDec = 0;
rDec = 0;
}
System.out.println(max);
pw.close();
}
static class FastReader {
InputStream is;
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
public FastReader(InputStream is){
this.is = is;
}
public int readByte(){
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() {
int b;
while((b = readByte()) != -1 && isSpaceChar(b));
return b;
}
public String next(){
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public String nextLine(){
int b = readByte();
StringBuilder sb = new StringBuilder();
while(b != '\n' || b != '\r'){
sb.appendCodePoint(b);
}
return sb.toString();
}
public int nextInt(){
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = (num<<3) + (num<<1) + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
public long nextLong() {
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = (num<<3) + (num<<1) + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
public double nextDouble() {
return Double.parseDouble(next());
}
public char[] next(int n){
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
public char nextChar() {
return (char)skip();
}
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
06d0006d2577e13ad5bbd7c7437f045f
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
//PetyaAndCountrySide
import java.io.*;
import java.util.*;
public class PetyaAndCountrySide{
static int t,n;
static StringBuilder ans=new StringBuilder();
static int[] arr;
public static long solve(){
long max_value=0;
if(n==1){return 1;}
for(int i=0;i<n;i++){
long water=0;
int ptr1=i,ptr2=i;
while(ptr1>0&&arr[ptr1-1]<=arr[ptr1]){ptr1--;}
while(ptr2<n-1&&arr[ptr2]>=arr[ptr2+1]){ptr2++;}
water=ptr2-ptr1+1;
max_value=Math.max(max_value,water);
}
return max_value;
}
public static void main(String[] args) throws IOException{
// BufferedReader br = new BufferedReader(new
// FileReader("sampleinp.txt"));
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
arr=new int[n];
String str[]=br.readLine().split(" ");
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(str[i]);
}
ans.append(solve()+"\n");
System.out.println(ans.toString());
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
d19fc954e1810fb8beaba4d33a99f439
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
/*
Aman Agarwal
algo.java
*/
import java.util.*;
import java.io.*;
public class B66
{
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;
}
}
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static int[] nai(int N,int start)throws IOException{int[]A=new int[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
static Integer[] naI(int N,int start)throws IOException{Integer[]A=new Integer[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
static void print(int arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
static void print(long arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} // return the number of set bits.
static boolean isPrime(int number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}int sqrt = (int) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
static boolean isPrime(long number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}long sqrt = (long) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
static int power(int n,int p){if(p==0)return 1;int a = power(n,p/2);a = a*a;int b = p & 1;if(b!=0){a = n*a;}return a;}
static long power(long n,long p){if(p==0)return 1;long a = power(n,p/2);a = a*a;long b = p & 1;if(b!=0){a = n*a;}return a;}
static void reverse(int[] a) {int b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
static void reverse(long[] a) {long b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
static void swap(int a[],int i,int j){int temp = a[i];a[i] = a[j];a[j] = temp;}
static void swap(long a[],int i,int j){long temp = a[i];a[i] = a[j];a[j] = temp;}
static int count(int n){int c=0;while(n>0){c++;n = n/10;}return c;}
static int[] prefix_sum(int a[],int n){int s[] = new int[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
static long[] prefix_sum_int(int a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
static long[] prefix_sum_Integer(Integer a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
static long[] prefix_sum_long(long a[],int n){long s[] = new long[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
static boolean isPerfectSquare(double x){double sr = Math.sqrt(x);return ((sr - Math.floor(sr)) == 0);}
static ArrayList<Integer> sieve(int n) {int k=0; boolean prime[] = new boolean[n+1];ArrayList<Integer> p_arr = new ArrayList<>();for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ k=p;if(prime[p] == true) { p_arr.add(p);for(int i = p*2; i <= n; i += p) prime[i] = false; } }for(int i = k+1;i<=n;i++){if(prime[i]==true)p_arr.add(i);}return p_arr;}
static boolean[] seive_check(int n) {boolean prime[] = new boolean[n+1];for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ if(prime[p] == true) { for(int i = p*2; i <= n; i += p) prime[i] = false; } }prime[1]=false;return prime;}
static int get_bits(int n){int p=0;while(n>0){p++;n = n>>1;}return p;}
static int get_bits(long n){int p=0;while(n>0){p++;n = n>>1;}return p;}
static int get_2_power(int n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
static int get_2_power(long n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
static void close(){out.flush();}
/*-------------------------Main Code Starts(algo.java)----------------------------------*/
public static void main(String[] args) throws IOException
{
int t = 1;
//t = sc.nextInt();
while(t-- > 0)
{
int n = ni();
int arr[] = nai(n,0);
int last=0;
int ans=1;
for(int i=0;i<n;i++)
{
int x=1;
int j=i+1;
last = arr[i];
while(j<n)
{
if(arr[j]>last)
break;
last = arr[j];
j++;
x++;
}
j=i-1;
last = arr[i];
while(j>=0)
{
if(arr[j]>last)
break;
last = arr[j];
x++;
j--;
}
ans = Math.max(ans,x);
}
//out.println(last);
//ans = Math.max(ans,(n-1)-last);
out.println(ans);
}
close();
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
3dafae3684d9e0ef5d27f6541ae345a9
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] sec = new int[n];
int[] drops = new int[n];
for(int i = 0; i < n; i++){
sec[i] = in.nextInt();
}
for(int i = 0; i < n; i++){
if(i == 0)
drops[i] = right(sec, i) + 1;
else if(i == n-1)
drops[i] = left(sec, i) + 1;
else{
drops[i] = right(sec, i) + left(sec, i) + 1;
}
}
int max = drops[0];
for(int i = 1; i < drops.length; i++){
if(drops[i] > max)
max = drops[i];
}
System.out.println(max);
}
public static int left(int[] a, int index){
int s = 0;
for(int i = index; i > 0; i--){
if(a[i] >= a[i-1])
s++;
else
return s;
}
return s;
}
public static int right(int[] a, int index){
int s = 0;
for(int i = index+1; i < a.length; i++){
if(a[i-1] >= a[i])
s++;
else
return s;
}
return s;
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
89de1bc723d80dd1c3bc89d5e1383d74
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.*;
public class Solution {
static class Task{
public void solve(InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
int max = -1, maxIndex = 0;
for (int i = 0; i < n; i++){
int left = 0, right = 0, temp = a[i];
for (int j = i-1; j >= 0; j--){
if (temp >= a[j]){
left++;
temp = a[j];
}else break;
}
temp = a[i];
for (int j = i+1; j < n; j++){
if (temp >= a[j]){
right++;
temp = a[j];
}else break;
}
int tmax = left+right+1;
if (max < tmax) max = tmax;
}
System.out.println(max);
}
}
public static void main(String[] args){
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
Task task = new Task();
task.solve(in, out);
out.close();
}
public static class InputReader{
BufferedReader bufferedReader;
StringTokenizer stringTokenizer;
InputReader(InputStream inputStream){
bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 32768);
stringTokenizer = null;
}
public String next(){
while(stringTokenizer == null || !stringTokenizer.hasMoreTokens()){
try{
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
}catch (Exception e){}
}
return stringTokenizer.nextToken();
}
public String nextLine() throws IOException{
return bufferedReader.readLine();
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public float nextFloat(){
return Float.parseFloat(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
fbaa504edadadfcb8d4d310ebb2ebebe
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
private void solve() throws Exception{
int n = ri();
int[] a = ria(n);
int max = Integer.MIN_VALUE;
for(int i = 0; i < n; i++){
int area = 1;
for (int j = i-1; j >= 0; j--){
if (a[j+1] >= a[j]) area++;
else break;
}
for (int j = i+1; j < n; j++){
if (a[j-1] >= a[j]) area++;
else break;
}
if (area > max) max = area;
if (max == n) break;
}
println(""+max);
out.close();
}
public static void main(String[] args) throws Exception{
Main m = new Main();
m.solve();
}
/* Template Methods */
private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
private BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
//private PrintWriter out = new PrintWriter(System.out, true);
private StringTokenizer st;
/* console write methods */
private void print(String s) throws Exception{
out.write(s);
}
private void println(String s) throws Exception{
out.write(s);
out.newLine();
}
/* console read methods */
private String rs() throws IOException{
return in.readLine();
}
private int ri() throws IOException{
return Integer.parseInt(rs());
}
private float rf() throws IOException{
return Float.parseFloat(rs());
}
private double rd() throws IOException{
return Double.parseDouble(rs());
}
private int[] ria(int size) throws IOException{
int[] a = new int[size];
st = new StringTokenizer(rs(), " ");
int i = 0;
while(st.hasMoreTokens()){ a[i] = Integer.parseInt(st.nextToken()); i++; }
return a;
}
private long[] rla(int size) throws IOException{
long[] a = new long[size];
st = new StringTokenizer(rs(), " ");
int i = 0;
while(st.hasMoreTokens()){ a[i] = Long.parseLong(st.nextToken()); i++; }
return a;
}
private ArrayList ral() throws IOException{
ArrayList al = new ArrayList();
st = new StringTokenizer(rs(), " ");
while(st.hasMoreTokens()) al.add(st.nextToken());
return al;
}
/* MAX & MIN */
private int max(int[] a){
int max = Integer.MIN_VALUE;
for (int x: a) if (x > max) max = x;
return max;
}
private int min(int[] a){
int min = Integer.MAX_VALUE;
for (int x: a) if (x < min) min = x;
return min;
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
62b573260388d430edae3ac5f6831119
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class sandbox {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
int[] h = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
h[i] = Integer.parseInt(st.nextToken());
}
int max = 0;
for (int i = 0; i < n; i++) {
max = Math.max(max, getNumWatered(h, i));
}
pw.println(max);
pw.close();
}
private static int getNumWatered(int[] h, int i) {
int ret = 1;
// Left
if (i > 0) {
int max = h[i];
for (int j = i - 1; j >= 0; j--) {
if (h[j] <= max) {
ret++;
max = h[j];
} else {
break;
}
}
}
// Right
if (i < h.length - 1) {
int max = h[i];
for (int j = i + 1; j < h.length; j++) {
if (h[j] <= max) {
ret++;
max = h[j];
} else {
break;
}
}
}
return ret;
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
7dfa8ed569f0da0a37455646367afed6
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++) {
arr[i] = sc.nextInt();
}
int max = Integer.MIN_VALUE;
for(int i=0;i<n;i++) {
int c = arr[i];
int sum = 1;
int j = i - 1;
int p = c;
while(j >= 0 && arr[j] <= p) {
sum++;
p = arr[j];
j--;
}
j = i + 1;
p = c;
while(j < n && arr[j] <= p) {
sum++;
p = arr[j];
j++;
}
max = Math.max(max, sum);
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
7003c3711ac8dfc4f7fb82a6e44daa3e
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i=0;i<n;i++) {
arr[i] = sc.nextInt();
}
int max = Integer.MIN_VALUE;
for (int i=0;i<n;i++) {
int c = i;
int l = c - 1;
int r = c + 1;
int res = 1;
while (l >= 0 && arr[l] <= arr[c]) {
res++;
c = l;
l--;
}
c = i;
while (r < n && arr[r] <= arr[c]) {
res++;
c = r;
r++;
}
max = Math.max(max, res);
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
8fb462cdcab26a7ffc93d5d9ed88a580
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class PetyaAndCountryside {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] arr = new int[n];
int j = 0;
while (n-- > 0) {
arr[j++] = s.nextInt();
}
int globalMaxCounter = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
int leftIndex = i - 1;
int rightIndex = i + 1;
int counter = 1;
int leftIter = i;
int rightIter = i;
//left search
while (leftIndex > -1) {
if(arr[leftIndex] <= arr[leftIter]) {
counter++;
leftIter = leftIndex;
leftIndex--;
} else {
break;
}
}
//right search
while (rightIndex < arr.length) {
if(arr[rightIndex] <= arr[rightIter]) {
counter++;
rightIter = rightIndex;
rightIndex++;
} else {
break;
}
}
globalMaxCounter = Math.max(counter, globalMaxCounter);
}
System.out.println(globalMaxCounter);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
156691e4f184684cdc63bf79e1871b00
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.*;
import java.util.InputMismatchException;
public class CF66 {
static String filePath = "/home/bishoy/IdeaProjects/Arabic competetive programming/src/b/test";
public static void main(String args[]) {
InputReader inputReader = Helper.readInput(Helper.Input.STD, filePath);
OutputWriter out = new OutputWriter(System.out);
int n = inputReader.readInt();
int[] secs = new int[n];
for (int i = 0; i < n; i++) {
secs[i] = inputReader.readInt();
}
int max = 0;
for (int i = 0; i < n; i++) {
int section = secs[i];
int current = 1;
//right of section
for (int j = i + 1; j < n; j++) {
if (secs[j] <= section) {
section = secs[j];
current++;
} else
break;
}
//left of section
section = secs[i];
for (int j = i - 1; j >= 0; j--) {
if (secs[j] <= section) {
section = secs[j];
current++;
} else
break;
}
max = Math.max(max, current);
}
out.printLine(max);
out.flush();
out.close();
}
static boolean validIdx(int idx, int n) {
return idx >= 0 && idx < n;
}
static class Helper {
static InputReader inputReader = null;
static OutputWriter out = null;
public static enum Input {
FILE, STD
}
public static class Pair<K, V> {
public K key;
public V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public String toString() {
return "( " + key + " , " + value + ")";
}
}
public static InputReader readInput(Enum type, String filePath) {
if (type == Input.FILE) {
try {
inputReader = new InputReader(new FileInputStream(new File(filePath)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else
inputReader = new InputReader(System.in);
return inputReader;
}
public static boolean validIndices(int r, int c, int n, int m) {
if (r >= 0 && c >= 0 && r < n && c < m) return true;
else return false;
}
}
private static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double readDouble() {
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, readInt());
}
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, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public 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);
}
}
private static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.flush();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
writer.flush();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
ac0168e2a8c5389c6b7a9cc3b85b0c76
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int m=0,s=0,k=0,ans=0;
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
// System.out.println("m:"+m+" k:"+k);
for(int h=0;h<n;h++)
{
s=0;k=0;
for(int i=h;i>0;i--)
{
if(a[i]>=a[i-1])
{
s++;
}
else
break;
}
// System.out.println("s:"+s);
for(int i=h;i<n-1;i++)
{
if(a[i]>=a[i+1])
{
k++;
}
else
break;
}
//System.out.println("k:"+k);
int ans1=s+k;
if(ans<=ans1)
ans=ans1;
}
System.out.println(ans+1);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
a51f2017ba4bbbe3b04a322cf650374f
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class PetyaAndCountrySide {
public static void main(String[] args) throws IOException {
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
int [] h = new int[n];
String [] sp = br.readLine().split(" ");
for(int i=0;i<n;i++)
{
h[i]= Integer.parseInt(sp[i]);
}
if(n==1 || n==2)
{
System.out.println(n);
return ;
}
int count = 0;
int max =0;
boolean hitNewApex = false;
int dupCount = 0;
boolean foundDup = false;
for(int i=0;i<n-1;i++)
{
if((h[i]<=h[i+1] && !hitNewApex)|| (h[i]>=h[i+1] && hitNewApex))
{
count++;
if(h[i]==h[i+1] && foundDup)
dupCount++;
else if(h[i]!=h[i+1])
foundDup = false;
else
{
foundDup = true;
dupCount = 1;
}
}
else if(h[i]>h[i+1] && !hitNewApex)
{
foundDup = false;
hitNewApex = true;
count++;
}else
{
count++;
max = Math.max(count,max);
hitNewApex = false;
count = (foundDup)? dupCount+1:1;
foundDup= false;
}
}
if(h[n-1]>=h[n-2]&& !hitNewApex)
count++;
else if(hitNewApex && h[n-2]>=h[n-2])
count++;
else
max = Math.max(max, 2);
max = Math.max(count,max);
pw.println(max);
pw.flush();
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
1325ddd800152203b6659ca0d1ebea50
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class HelloWorld {
static Scanner sIn = new Scanner(System.in);
public static void main(String[] args) {
int n =sIn.nextInt();
int [] arr=new int[n];
int [] acc=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sIn.nextInt();
if(i>0&&arr[i]>=arr[i-1])
acc[i]=acc[i-1]+1;
else
acc[i]=1;
}
int max=-1;
for(int i=n-1;i>=0;i--)
{
if(i<n-1&&arr[i]>arr[i+1])
acc[i]+=acc[i+1];
else if(i<n-1&&arr[i]==arr[i+1])
acc[i]+=(acc[i+1]-acc[i]);
if(acc[i]>max)
max=acc[i];
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
f32e6a2d54dae944ca5d80a5655fe910
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class ProblemSolving {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int x = s.nextInt();
int arr[] = new int[x];
for (int i = 0; i < x; i++) {
int z = s.nextInt();
arr[i] = z;
}
int max = -1, counter = 0;
for (int i = 0; i < x; i++) {
for (int j = i; j < x - 1; j++) {
if (arr[j] < arr[j + 1]) {
break;
}
counter++;
}
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
break;
}
counter++;
}
if (max < counter) {
max = counter;
}
counter = 0;
}
System.out.println(max + 1);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
5594c21036a32e2b1b8b9bc3cf7f7184
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sectionsLength = scanner.nextInt();
String sectionsHeight = scanner.nextLine();
int[] arr = new int[sectionsLength];
int max=0;
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
for (int i = 0; i < arr.length; i++) {
int maxTemp = leftSection(arr, i) + rightSection(arr, i) +1;
if (maxTemp>max) max=maxTemp;
}
System.out.println(max);
}
private static int leftSection(int[] arr, int i) {
int no=0;
if (i > 0){
for (int l = i-1; l >= 0; l--) {
if((arr[l]<=arr[i]&&arr[l]<=arr[l+1])||(arr[l]==arr[i]&&Math.abs(l-i)==1)){
no++;
}
else return no;
}
return no;
}
return 0;
}
private static int rightSection(int[] arr, int i) {
int no=0;
if (i < arr.length){
for (int l = 1+i; l < arr.length; l++) {
if((arr[l]<=arr[i]&&arr[l]<=arr[l-1])||(arr[l]==arr[i]&&Math.abs(l-i)==1)){
no++;
}
else return no;
}
return no;
}
return 0;
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
703401e0193168d02ac22e406322b70d
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.*;
public class MAIN {
static Scanner in = new Scanner(System.in);
static int a[]=new int[1001];
public static void main(String[] arge) {
int n=in.nextInt();
for(int i=0;i<n;i++)
{
a[i]=in.nextInt();
}
int ans=0;
for(int i=0;i<n;i++)
{
int l,r;
r=l=i;
int c=1;
boolean t1=true,t2=true;
while(true)
{
r++;
l--;
if(r>=n&&l<0)break;
if(r<n&&a[r]<=a[r-1]&&t1)c++;
else
{
t1=false;
}
if(l>=0&&a[l]<=a[l+1]&&t2)c++;
else t2=false;
}
ans=Math.max(c, ans);
}
System.out.println(ans);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
d30af6c94fff3cbb178bf893772d439c
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class Codeforces {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int land[] = new int[size];
int solution[] = new int[size];
for(int i=0;i<size;i++)
{
land[i] = sc.nextInt();
}
/*
* int counter =1; for(int i=0;i+1<size;i++) { if(land[i] >= land[i+1])
* counter++; else break; } //System.out.println(counter); solution[0] =
* counter; counter =1; for(int i=size-1;i-1>0;i--) { if(land[i] >= land[i-1])
* counter++; else break; } //System.out.println(counter); solution[size-1] =
* counter; counter =1;
*/
for(int i=0;i<size;i++)
{
//System.out.print("value: "+ land[i]+" ");
int forward = forward(i, land);
int backword = backword(i, land);
int a = forward+backword+1;
//System.out.println(" forword: "+ forward + " backword: "+ backword);
solution[i] = a;
}
Arrays.sort(solution);
System.out.println(solution[size-1]);
}
public static int forward(int position,int []land)
{
int counter =0;
for(int i=position;i+1<land.length;i++)
{
if(land[i] >= land[i+1])
counter++;
else
break;
}
return counter;
}
public static int backword(int position,int []land)
{
int counter =0;
for(int i=position;i-1>=0;i--)
{
if(land[i] >= land[i-1])
counter++;
else
break;
}
return counter;
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
da84fc7d3b65c32d82dc4370da5d04b1
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class Codeforces {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int land[] = new int[size];
int solution[] = new int[size];
for(int i=0;i<size;i++)
{
land[i] = sc.nextInt();
}
for(int i=0;i<size;i++)
{
int forward = forward(i, land);
int backword = backword(i, land);
int a = forward+backword+1;
solution[i] = a;
}
Arrays.sort(solution);
System.out.println(solution[size-1]);
}
public static int forward(int position,int []land)
{
int counter =0;
for(int i=position;i+1<land.length;i++)
{
if(land[i] >= land[i+1])
counter++;
else
break;
}
return counter;
}
public static int backword(int position,int []land)
{
int counter =0;
for(int i=position;i-1>=0;i--)
{
if(land[i] >= land[i-1])
counter++;
else
break;
}
return counter;
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
b4a07f9fb511a28740acb7a51207b900
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.*;
public class Main {
public static int checkAfter( int index , int arr [] ){
int counter = 0;
int pointer = index;
for (int i = index; i < arr.length-1 ;i++){
if ( arr[pointer] >= arr[i+1]){
++pointer;
++counter;
}
else {
break;
}
}
return counter;
}
public static int checkBefore( int index , int arr []){
int counter=0;
int pointer = index;
for (int i = index; i > 0 ; i-- ){
if ( arr[pointer] >= arr[i-1]){
--pointer;
++counter;
}
else {
break;
}
}
return counter;
}
public static int PetyaandCountryside( int [] arr){
int max = 0;
int x ;
for (int i =0; i < arr.length;i++ ){
if ( i == 0){
x = checkAfter(i, arr);
}
else if ( i == arr.length -1 ){
x = checkBefore(i, arr);
}
else {
x= checkAfter(i , arr);
x+=checkBefore(i,arr);
}
if ( x > max ){
max = x;
}
}
return max + 1;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int length = input.nextInt();
int[] arr = new int [length];
for (int i = 0; i < arr.length; i++) {
arr[i] = input.nextInt();
}
int r = PetyaandCountryside(arr);
System.out.println(r);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
c1897f76b70abdaed9e22402342690e2
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
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);
BCF61 solver = new BCF61();
solver.solve(1, in, out);
out.close();
}
static class BCF61 {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
max = Math.max(max, 1 + less(arr, i));
}
out.println(max);
}
int less(int[] arr, int pos) {
int les = 0;
int equa = pos;
int curr = arr[pos];
for (int i = pos + 1; i < arr.length; i++) {
if (arr[i] < curr) {
les++;
curr = arr[i];
equa = i;
} else if (arr[i] == curr && i == equa + 1) {
les++;
equa++;
} else {
break;
}
}
equa = pos;
curr = arr[pos];
for (int i = pos - 1; i > -1; i--) {
if (arr[i] < curr) {
les++;
curr = arr[i];
equa = i;
} else if (arr[i] == curr && i == equa - 1) {
les++;
equa--;
} else {
break;
}
}
return les;
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
8f0a1cb4eb15bae4bf5e8764801b65fc
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static int lenbuf = 0, ptrbuf = 0;
private static byte[] inbuf = new byte[1024];
static void solve() {
int n = ni();
int[] a = na(n);
int res = 1;
for (int i = 0; i < n; i++) {
int cnt = 0;
int j = i;
while (j - 1 >= 0 && a[j - 1] <= a[j]) {
j--;
cnt++;
}
j = i;
while (j + 1 < n && a[j + 1] <= a[j]) {
j++;
cnt++;
}
res = Math.max(res, cnt + 1);
}
out.println(res);
}
static void execute() throws IOException {
solve();
}
public static void main(String[] args) throws Exception {
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
execute();
out.flush();
long G = System.currentTimeMillis();
tr(G - S + "ms");
}
private static boolean eof() {
if (lenbuf == -1) {
return true;
}
int lptr = ptrbuf;
while (lptr < lenbuf) {
if (!isSpaceChar(inbuf[lptr++])) {
return false;
}
}
try {
is.mark(1000);
while (true) {
int b = is.read();
if (b == -1) {
is.reset();
return true;
} else if (!isSpaceChar(b)) {
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static int readByte() {
if (lenbuf == -1) {
throw new InputMismatchException();
}
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) {
return -1;
}
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
// private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
private static int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
private static double nd() {
return Double.parseDouble(ns());
}
private static char nc() {
return (char) skip();
}
private static String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) {
map[i] = ns(m);
}
return map;
}
private static int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
private static int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) {
;
}
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private static long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) {
;
}
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) {
if (INPUT.length() != 0) {
System.out.println(Arrays.deepToString(o));
}
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
8567b1e70380f8f054a13197ee087889
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Scanner;
public class PetyaAndCountryside {
public static void main(String[] args) {
ArrayList<Integer> array=new ArrayList();
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int []arr=new int[n];
int c=1;
int f=0;
for (int i = 0; i < n-1; i++)
arr[i]=s.nextInt();
if(n==1)
System.out.println("1");
else{
for (int i = 0; i < n-1; i++) {
if(arr[i]<=arr[i+1]&&f==0){
c++;f=0;array.add(c);
}
else if(arr[i]>=arr[i+1]){
c++;f=1;array.add(c);
}
else{
i--;
array.add(c);c=1;f=0;
}
}
c=1;
f=0;
for (int i = n-2; i >=0; i--) {
if(arr[i]<=arr[i+1]){
c++;f=1;array.add(c);
}
else if(arr[i]>=arr[i+1]&&f==0){
c++;f=0;array.add(c);
}
else{
i++;
array.add(c);
c=1;
f=0;
}
}
array.sort(null);
System.out.println(array.get(array.size()-1));
}
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
33167ac9e46059c957782c35c5717284
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.beans.Visibility;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class CodeForces {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
int max = -1;
for (int i = 0; i < n; i++) {
int tmp = 1;
int a = i - 1;
int b = i + 1;
int tt = -1;
if (a >= 0 && arr[a] <= arr[i]) {
tt = arr[a];
while (a >= 0 && arr[a] <= tt) {
tmp++;
tt = arr[a];
a--;
}
}
tt = -1;
if (b < n && arr[b] <= arr[i]) {
tt = arr[b];
while (b < n && arr[b] <= tt) {
tmp++;
tt = arr[b];
b++;
}
}
max = Math.max(max, tmp);
}
System.out.println(max);
}
static class Pair implements Comparable<Pair> {
int x, y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return x == o.x ? y - o.y : x - o.x;
}
}
static class Edge implements Comparable<Edge> {
int node, cost;
Edge(int a, int b) {
node = a;
cost = b;
}
public int compareTo(Edge e) {
if (cost != e.cost)
return cost - e.cost;
return node - e.node;
}
public String toString() {
return node + " " + cost;
}
}
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 {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
ab4ef2f63d2390cff46e018f65e93c31
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
//package javaapplication1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import static java.lang.reflect.Array.set;
import java.math.BigInteger;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import static jdk.nashorn.internal.objects.Global.Infinity;
import static jdk.nashorn.internal.objects.NativeMath.round;
import sun.awt.HKSCS;
/*
PARTIAL AUTHOR GEORGE
MAINLY GEEKSFORGEEKS ALL RIGHTS RESERVED.
*/
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader BR=new BufferedReader(new InputStreamReader(System.in));
PrintWriter PW=new PrintWriter(new PrintStream(System.out));
In IN=new In(BR);
Out OUT=new Out(PW);
Scanner SC=new Scanner(System.in);
//DataStructure DS=new DataStructure();
//Functions F=new Functions();
//QuickSort QS=new QuickSort();
//BinarySearch BS=new BinarySearch();
//FIB fubbonanci=new FIB(93);
//Map<Character,List<Integer>>Pos=new HashMap<>();
//Set<Integer>ans=new HashSet<>();
//StringPermutationGenerator SPG=new StringPermutationGenerator();
int n=SC.nextInt();
int [] nums=new int[n];
for (int i = 0; i < n; i++) {
nums[i]=SC.nextInt();
}
//max from left
int count=1;
for (int i = 1; i < n; i++) {
if(nums[i-1]>=nums[i]){count++;}
else{break;}
}
//max from left
int counto=1;
for (int i = n-1; i > 0; i--) {
if(nums[i]>=nums[i-1]){counto++;}
else{break;}
}
//find pyramid
int tempCenter=1;
int maxCenter=1;
int center;
for (int i = 1; i < n-1; i++) {
if(nums[i]>=nums[i-1]&&nums[i]>=nums[i+1]){
tempCenter++;
for (int j = i+1; j < n-1; j++) {
//check from L-R
if(nums[j]>=nums[j+1]){tempCenter++;}
else{break;}
}
tempCenter++;
for (int j = i-1; j > 0; j--) {
//check from L-R
if(nums[j]>=nums[j-1]){tempCenter++;}
else{break;}
}
if(maxCenter<tempCenter){maxCenter=tempCenter;}
tempCenter=1;
}
}
System.out.print(Math.max(Math.max(maxCenter, counto), count));
}
protected static class In {
private BufferedReader reader;
private StringTokenizer tokenizer = new StringTokenizer("");
public In(BufferedReader reader) {
this.reader = reader;
}
public String next() throws IOException {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
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[] nextIntArray1(int n) throws IOException {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextInt();
return a;
}
public int[] nextIntArraySorted(int n) throws IOException {
int[] a = nextIntArray(n);
Random r = new Random();
for (int i = 0; i < n; i++) {
int j = i + r.nextInt(n - i);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArray1(int n) throws IOException {
long[] a = new long[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArraySorted(int n) throws IOException {
long[] a = nextLongArray(n);
Random r = new Random();
for (int i = 0; i < n; i++) {
int j = i + r.nextInt(n - i);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
return a;
}
}
protected static class Out {
private PrintWriter writer;
private static boolean local = System
.getProperty("ONLINE_JUDGE") == null;
public Out(PrintWriter writer) {
this.writer = writer;
}
public void print(char c) {
writer.print(c);
}
public void print(int a) {
writer.print(a);
}
public void println(Object a) {
writer.println(a);
}
public void println(Object[] os) {
for (int i = 0; i < os.length; i++) {
writer.print(os[i]);
writer.print(' ');
}
writer.println();
}
public void println(int[] a) {
for (int i = 0; i < a.length; i++) {
writer.print(a[i]);
writer.print(' ');
}
writer.println();
}
public void println(long[] a) {
for (int i = 0; i < a.length; i++) {
writer.print(a[i]);
writer.print(' ');
}
writer.println();
}
public static void db(Object... objects) {
if (local)
System.out.println(Arrays.deepToString(objects));
}
}
public static class QuickSort{
protected static int [] intArray;
protected static long [] longArray;
protected static double [] DoubleArray;
protected static char [] charArray;
protected static int Size;
public QuickSort() {
}
protected static int partition(int arr[],int low,int high)
{
int pivot=arr[high];
int i=low-1;
int temp=0;
for (int j = low; j < high; ++j) {
if(arr[j]<=pivot)
{
++i;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
protected static int partition(long arr[],int low,int high)
{
long pivot=arr[high];
int i=low-1;
long temp=0;
for (int j = low; j < high; ++j) {
if(arr[j]<=pivot)
{
++i;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
protected static int partition(double arr[],int low,int high)
{
double pivot=arr[high];
int i=low-1;
double temp=0;
for (int j = low; j < high; ++j) {
if(arr[j]<=pivot)
{
++i;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
protected static int partition(char arr[],int low,int high)
{
char pivot=arr[high];
int i=low-1;
char temp=0;
for (int j = low; j < high; ++j) {
if(arr[j]<=pivot)
{
++i;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
protected static void sort(int arr[],int low, int high)
{
if(low < high)
{
int pi=partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
protected static void sort(long arr[],int low, int high)
{
if(low < high)
{
int pi=partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
protected static void sort(double arr[],int low, int high)
{
if(low < high)
{
int pi=partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
protected static void sort(char arr[],int low, int high)
{
if(low < high)
{
int pi=partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
protected static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
protected static void printArray(char arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
protected static void printArray(double arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
protected static void printArray(long arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
}
public static class Functions{
public Functions() {
}
protected static boolean [] sieveOfEratosthenesPrimes(int limit){
boolean prime[] = new boolean[limit+1];
for(int i=0;i<limit;i++)
prime[i] = true;
for(int p = 2; p*p <=limit; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*2; i <= limit; i += p)
prime[i] = false;
}
}
return prime;
}
protected static boolean checkAnagram(String str1, String str2) {
int i=0;
for (char c : str1.toCharArray()) {
i = str2.indexOf(c, i) + 1;
if (i <= 0) { return false; }
}
return true;
}
protected static boolean contains(final int[] arr, final int key) {
return Arrays.stream(arr).anyMatch(i -> i == key);
}
public static boolean contains(final long[] arr, final long key) {
return Arrays.stream(arr).anyMatch(i -> i == key);
}
public static boolean contains(final double[] arr, final double key) {
return Arrays.stream(arr).anyMatch(i -> i == key);
}
protected static long NCR(long N, long R){
if(N < R)
return 0;
if(R == 0 || R == N)
return 1;
return NCR(N-1,R-1)+NCR(N-1,R);
}
protected static long NCR(int N, int R){
if(N < R)
return 0;
if(R == 0 || R == N)
return 1;
return NCR(N-1,R-1)+NCR(N-1,R);
}
protected static String changeCharInPosition(int position, char ch, String str){
char[] charArray = str.toCharArray();
charArray[position] = ch;
return new String(charArray);
}
protected static boolean isPrime(long n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
protected static long Gcd(long p, long q) {
if (q == 0) return p;
else return Gcd(q, p % q);
}
protected static boolean isPrime(int n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
int sqrtN = (int)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
}
public static class BinarySearch{
private static int middle;
private static int L;
private static int R;
public BinarySearch() {
}
protected static int Search(int [] arr,int element){
R=arr.length-1;
L=0;
return BS(arr, L, R, element);
}
protected static int Search(long [] arr,long element){
R=arr.length-1;
L=0;
return BS(arr, L, R, element);
}
protected static int Search(char [] arr,char element){
R=arr.length-1;
L=0;
return BS(arr, L, R, element);
}
protected static int Search(double [] arr,double element){
R=arr.length-1;
L=0;
return BS(arr, L, R, element);
}
protected static int BS(int [] arr,int L,int R,int element){
if(R>=L)
{
middle=L+(R-L)/2;
if(arr[middle]==element)
{
return middle;
}
else if(arr[middle]>element)
{
return BS(arr, L,middle-1, element);
}
else
{
return BS(arr, middle+1, R, element);
}
}
else
{
return -1;
}
}
protected static int BS(long [] arr,int L,int R,long element){
if(R>=L)
{
middle=L+(R-L)/2;
if(arr[middle]==element)
{
return middle;
}
else if(arr[middle]>element)
{
return BS(arr, L,middle-1, element);
}
else
{
return BS(arr, middle+1, R, element);
}
}
else
{
return -1;
}
}
protected static int BS(char [] arr,int L,int R,char element){
if(R>=L)
{
middle=L+(R-L)/2;
if(arr[middle]==element)
{
return middle;
}
else if(arr[middle]>element)
{
return BS(arr, L,middle-1, element);
}
else
{
return BS(arr, middle+1, R, element);
}
}
else
{
return -1;
}
}
protected static int BS(double [] arr,int L,int R,double element){
if(R>=L)
{
middle=L+(R-L)/2;
if(arr[middle]==element)
{
return middle;
}
else if(arr[middle]>element)
{
return BS(arr, L,middle-1, element);
}
else
{
return BS(arr, middle+1, R, element);
}
}
else
{
return -1;
}
}
}
public static class DataStructure{
public DataStructure() {
}
static void stack_push(Stack<Integer> stack)
{
for(int i = 0; i < 5; i++)
{
stack.push(i);
}
}
// Popping element from the top of the stack
static void stack_pop(Stack<Integer> stack)
{
System.out.println("Pop :");
for(int i = 0; i < 5; i++)
{
Integer y = (Integer) stack.pop();
System.out.println(y);
}
}
// Displaying element on the top of the stack
static void stack_peek(Stack<Integer> stack)
{
Integer element = (Integer) stack.peek();
System.out.println("Element on stack top : " + element);
}
// Searching element in the stack
static void stack_search(Stack<Integer> stack, int element)
{
Integer pos = (Integer) stack.search(element);
if(pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position " + pos);
}
}
public static class FIB {
protected static int MAX = 1000;
protected static long f[];
protected static int size=0;
// Returns n'th fibonacci number using
// table f[]
public FIB(int n)
{
f=new long[MAX];
this.size=n;
fib();
}
protected static void fib() {
f[0]=0;f[1]=1;
for (int i = 2; i < size; i++) {
//if(f[i-1]+f[i-2]>Long.MAX_VALUE){f[i]=0;continue;}
f[i]=f[i-1]+f[i-2];
}
}
protected static void printSeries(){
for (int i = 0; i <size; i++) {
System.out.print(f[i]+" ");
}
}
}
public static class Graph{
}
public static class StringPermutationGenerator {
// Generate all permutations of a string in Java
private Set<String> generatePermutations(String input) {
input = input.toLowerCase();
Set<String> result = new HashSet<>();
permutations("", input, result);
return result;
}
private void permutations(String prefix, String letters, Set<String> result) {
if (letters.length() == 0) {
result.add(prefix);
} else {
for (int i = 0; i < letters.length(); i++) {
String letter = letters.substring(i, i + 1);
String otherLetters = letters.substring(0, i) + letters.substring(i + 1);
permutations(prefix + letter, otherLetters, result);
}
}
}
}
}
/*
Some References for doing things
-Removing all Leading zeros in a string
s.replaceFirst("^0+(?!$)", "");
-Creating comprator
public static class CustomComparator implements Comparator<c> {
@Override
public int compare(c o1, c o2) {
return o1.score.compareTo(o2.score);
}
}
-how to write switch case
switch(x)
{
case 1:
return l;
...
default:
return m;
}
-Convert numbers of and to different bases
Integer.toString(Integer.parseInt(number, base1), base2);
-Split over white spaces:
myString.split("\\s+");
-how to know if 4 points will form a square or a rectangle:
public static class point{
int a,b;
public point(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public String toString() {
return "point{" + "a=" + a + ", b=" + b + '}';
}
}
public static int distSq(point p, point q)
{
return (p.a - q.a)*(p.a - q.a) +
(p.b - q.b)*(p.b - q.b);
}
public static boolean isSquare(point p1, point p2, point p3, point p4)
{
int d2 = distSq(p1, p2); // from p1 to p2
int d3 = distSq(p1, p3); // from p1 to p3
int d4 = distSq(p1, p4); // from p1 to p4
// If lengths if (p1, p2) and (p1, p3) are same, then
// following conditions must met to form a square.
// 1) Square of length of (p1, p4) is same as twice
// the square of (p1, p2)
// 2) p4 is at same distance from p2 and p3
if (d2 == d3 && 2*d2 == d4)
{
int d = distSq(p2, p4);
return (d == distSq(p3, p4) && d == d2);
}
// The below two cases are similar to above case
if (d3 == d4 && 2*d3 == d2)
{
int d = distSq(p2, p3);
return (d == distSq(p2, p4) && d == d3);
}
if (d2 == d4 && 2*d2 == d3)
{
int d = distSq(p2, p3);
return (d == distSq(p3, p4) && d == d2);
}
return false;
}
public static boolean isRectangle(point p1,point p2,point p3,point p4)
{
double cx,cy;
double dd1,dd2,dd3,dd4;
double x1,y1,x2,y2,x3,y3,x4,y4;
x1=p1.a;y1=p1.b;x2=p2.a;y2=p2.b;x3=p3.a;y3=p3.b;x4=p4.a;y4=p4.b;
cx=(x1+x2+x3+x4)/4;
cy=(y1+y2+y3+y4)/4;
dd1=(cx-x1)*(cx-x1)+(cy-y1)*(cy-y1);
dd2=(cx-x2)*(cx-x2)+(cy-y2)*(cy-y2);
dd3=(cx-x3)*(cx-x3)+(cy-y3)*(cy-y3);
dd4=(cx-x4)*(cx-x4)+(cy-y4)*(cy-y4);
return dd1==dd2 && dd1==dd3 && dd1==dd4;
}
*/
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
502c3e3a4e794bf1479d00aec20b6baf
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.System.in;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import static java.util.Collections.list;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import javax.xml.stream.events.Characters;
import sun.security.util.Length;
/**
*
* @author george
*/
public class main {
public static boolean isPrime(long n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
private static long f(long l) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
static public class Princess{
public int rate;public int beauty;public int intellect;public int richness;
public Princess(int sum,int a,int b,int c){
this.rate=sum;
this.beauty=a;
this.intellect=b;
this.richness=c;
}
@Override
public String toString() {
return "Princess{" + "rate=" + rate + ", beauty=" + beauty + ", intellect=" + intellect + ", richness=" + richness + '}';
}
}
public static boolean contains(final int[] arr, final int key) {
return Arrays.stream(arr).anyMatch(i -> i == key);
}
static boolean isSubSequence(String str1, String str2, int m, int n)
{
if (m == 0)
return true;
if (n == 0)
return false;
if (str1.charAt(m-1) == str2.charAt(n-1))
return isSubSequence(str1, str2, m-1, n-1);
return isSubSequence(str1, str2, m, n-1);
}
static int gcdThing(int a, int b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
public static boolean checkAnagram(String str1, String str2) {
int i=0;
for (char c : str1.toCharArray()) {
i = str2.indexOf(c, i) + 1;
if (i <= 0) { return false; }
}
return true;
}
/* Amusing Joke
String a,b,c;
a=s.next();b=s.next();c=s.next();
if((a.length()+b.length())!=c.length()){System.out.print("here");System.out.print("NO");}
else{
boolean x= true;
String agex="";
if(checkAnagram(a, c)==false){System.out.print("here1");x=false;}
else{
char [] g=a.toCharArray();
Arrays.sort(g);String ge=new String(g);a=ge;
g=b.toCharArray();Arrays.sort(g);ge=new String(b);b=ge;
g=c.toCharArray();Arrays.sort(g);ge=new String(c);c=ge;
if(isSubSequence(a, c, a.length(), c.length())){
StringBuilder sb = new StringBuilder(c);String temp="";
for (int i = 0; i < a.length(); i++) {
temp+=a.charAt(i);
c.replaceFirst(temp, "");temp="";
}
}
else{x=false;}
if(isSubSequence(a, c, a.length(), c.length())){
StringBuilder sb = new StringBuilder(c);
for (int i = 0; i < b.length(); i++) {
String temp="";
temp+=b.charAt(i);
c.replaceFirst(temp, "");temp="";
}
}
else{x=false;}
if(c.length()!=0){x=false;}
}if(x==false){System.out.print("NO");}
else{System.out.print("YES");}
}
*/
/*//t,l,r
long t,l,r;t=s.nextLong();l=s.nextLong();r=s.nextLong();
long exp=0;
// t0·f(l) + t1·f(l + 1) + ... + tr - l·f(r).
for (int i = 0; i <=r-l; i++) {
exp+=((long)(Math.pow(t, i)))*f(l+i);
}
System.out.print(exp%(1000000007));*/
/* 489C
int digits=s.nextInt();int sum=s.nextInt();
List <Integer>li=new ArrayList<Integer>();
int digitss=sum/9;
int rem=digits%9;
String z=String.join("", Collections.nCopies(digitss, "9"));
String z+=
String digit="9";
String x = String.join("", Collections.nCopies(digits, "9"));
BigInteger num=BigInteger.valueOf(Long.parseLong(x));
x=num.toString();System.out.print(x);
li.clear();
for (int i = 0; i < x.length(); i++) {
li.add(x.charAt(i)-48);
}
Collections.sort(li);
int zeros=0;
//leading zeros
String f="";
for (int i = 0; i < li.size(); i++) {
if(li.get(0)==0){zeros++;li.remove(0);}
else{f=li.get(0).toString(); break;}
}
String y="";
if(zeros!=0){
li.remove(0);
y+=String.join("", Collections.nCopies(zeros, "0"));
}
y+=li.stream().map(Object::toString).collect(Collectors.joining());
System.out.print(y);System.out.print(" ");System.out.print(x);
*/
public static List primeFactors(Long num)
{
List<Long>Prime=new ArrayList<Long>();
// Print the number of 2s that divide n
long tw=2;
while (num%2==0)
{
Prime.add(tw);
num /= 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(num); i+= 2)
{
// While i divides n, print i and divide n
while (num%i == 0)
{
Prime.add(i);
num/= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (num> 2)
Prime.add(num);
return Prime;
}
public static String Winner(int [] arr)
{
int maxsum=-1;
int temp=0;
List<Integer> L=Arrays.stream(arr).boxed().collect(Collectors.toList());
int count=0;
String winner="First";
while(L.isEmpty()!=true){
if(count%2==0){winner="First";
//take max odd interval
for (int i = 0; i < L.size(); i++) {
}
}
else{winner="Second";
}
count++;
}
return winner;
}
public static String changeCharInPosition(int position, char ch, String str){
char[] charArray = str.toCharArray();
charArray[position] = ch;
return new String(charArray);
}
/*
(Two televisions 845C)
int n=s.nextInt();
String x=s.next();
int k=s.nextInt();
int [] ind=new int[k];
char [] cho=new char[k];
String help;
for (int i = 0; i < k; i++) {
help="";
ind[i]=s.nextInt();
help=s.next();
cho[i]=help.charAt(0);
}
String substring="";
int maxcount=1;
int tempcount=0;
int occChar=0;
help="";
for (int i = 0; i < k; i++) {
substring="";
help="";
help+=cho[i];
occChar=x.length() - x.replace(help, "").length();
if((occChar+ind[i])>=n){System.out.println(n);continue;}
for (int j = 0; j < n; j++) {
if(x.charAt(j)!=cho[i]){continue;}
for (int l = j+1; l < n; l++) {
if(x.charAt(l)==cho[i]){substring=x.substring(j, l+1);System.out.println(substring);
help="";help+=cho[i];
occChar=substring.length() - substring.replace(help, "").length();
tempcount=substring.length()-occChar;
if(substring.length()>maxcount&&tempcount<=ind[i]){maxcount=tempcount;tempcount=0;}
}
}
}
// System.out.println(maxcount);maxcount=0;
}*/
/* int n=s.nextInt();
int [] l=new int[n];
int [] r=new int[n];
for (int i = 0; i< n; i++) {
l[i]=s.nextInt();
r[i]=s.nextInt();
}
boolean cond=false;
List<Integer>TV1L=new ArrayList<Integer>();
List<Integer>TV1R=new ArrayList<Integer>();
List<Integer>TV2L=new ArrayList<Integer>();
List<Integer>TV2R=new ArrayList<Integer>();
for (int i = 1; i < n; i++) {
if(TV1L.contains(l[i])==false&&TV1R.contains(r[i])==false){TV1L.add(l[i]);TV1R.add(r[i]);}
else{
if(TV2L.contains(l[i])==false&&TV2R.contains(r[i])==false){TV2L.add(l[i]);TV2R.add(r[i]);}
else{System.out.print("NO");System.exit(0);}
}
}
Collections.sort(TV1R);Collections.sort(TV1L);
//we will check everyTV interval if it is intersected or not
int countTv1=0;
for (int i = 0; i < TV1L.size()-1; i++) {
if(TV1R.get(i)-TV1L.get(i+1)>0){countTv1++;}
else if(TV1R.get(i)-TV1L.get(i+1)==0){System.out.print("NO");System.exit(0);}
}
for (int i = 0; i < TV1L.size()-1; i++) {
if(TV1L.get(i+1)-TV1R.get(i)>0){countTv1++;}
else if(TV1L.get(i+1)-TV1R.get(i)==0){System.out.print("NO");System.exit(0);}
}
if(countTv1<TV1L.size()){System.out.print("NO");System.exit(0);}
Collections.sort(TV2R);Collections.sort(TV2L);
//we will check everyTV interval if it is intersected or not
int countTv2=0;
for (int i = 0; i < TV2L.size()-1; i++) {
if(TV2R.get(i)-TV2L.get(i+1)>0){countTv2++;}
else if(TV2R.get(i)-TV2L.get(i+1)==0){System.out.print("NO");System.exit(0);}
}
for (int i = 0; i < TV2L.size()-1; i++) {
if(TV2L.get(i+1)-TV2R.get(i)>0){countTv2++;}
else if(TV2L.get(i+1)-TV2R.get(i)==0){System.out.print("NO");System.exit(0);}
}
if(countTv2<TV2L.size()){System.out.print("NO");System.exit(0);}
System.out.print("YES");
*/
public static boolean CheckCanObtainYfromX(String y,String x){
boolean cond=true;
String help="";
for (int i = 0; i < y.length(); i++) {
help="";help+=y.charAt(i);
if(y.contains(help)==false){x=x.replaceFirst(help,"A");cond=false;break;}
}
return cond;
}
public static int LongestPalindrome(String x){
int n=x.length();
boolean table[][] = new boolean[n][n];
int maxLength = 1;
for (int i = 0; i < n; ++i)
table[i][i] = true;
int start = 0;
for (int i = 0; i < n - 1; ++i) {
if (x.charAt(i) == x.charAt(i + 1)) {
table[i][i + 1] = true;
start = i;
maxLength = 2;
}
}
for (int k = 3; k <= n; ++k) {
for (int i = 0; i < n - k + 1; ++i)
{
int j = i + k - 1;
if (table[i + 1][j - 1] && x.charAt(i) == x.charAt(j)) {
table[i][j] = true;
if (k > maxLength) {
start = i;
maxLength = k;
}
}
}
}
return maxLength;
}
public static long choose(long total, long choose){
if(total < choose)
return 0;
if(choose == 0 || choose == total)
return 1;
return choose(total-1,choose-1)+choose(total-1,choose);
}
public static void main (String [] args) throws IOException
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int [] nums=new int[n];
for (int i = 0; i < n; i++) {
nums[i]=s.nextInt();
}
//max from left
int count=1;
for (int i = 1; i < n; i++) {
if(nums[i-1]>=nums[i]){count++;}
else{break;}
}
//max from left
int counto=1;
for (int i = n-1; i > 0; i--) {
if(nums[i]>=nums[i-1]){counto++;}
else{break;}
}
//find pyramid
int tempCenter=1;
int maxCenter=1;
int center;
for (int i = 1; i < n-1; i++) {
if(nums[i]>=nums[i-1]&&nums[i]>=nums[i+1]){
tempCenter++;
for (int j = i+1; j < n-1; j++) {
//check from L-R
if(nums[j]>=nums[j+1]){tempCenter++;}
else{break;}
}
tempCenter++;
for (int j = i-1; j > 0; j--) {
//check from L-R
if(nums[j]>=nums[j-1]){tempCenter++;}
else{break;}
}
if(maxCenter<tempCenter){maxCenter=tempCenter;}
tempCenter=1;
}
}
System.out.print(Math.max(Math.max(maxCenter, counto), count));
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
90dfacd195ff521bdce7dc368beb497a
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class PatyaAndCountrySide {
static int boom[];
static int n;
public static void main(String[]args) throws NumberFormatException, IOException{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(bf.readLine());
boom = new int[n];
String[]arr = bf.readLine().split(" ");
for(int i = 0 ; i < n ; i++)boom[i]=Integer.parseInt(arr[i]);
int max=1;
for(int i = 0,back,front,sum ; i < n ; i++) {
back=back(i);
front=front(i);
sum=1+back+front;
max=Math.max(max, sum);
}
System.out.println(max);
}
static int back(int i){//O(n)
int count=0;
int back=i;
i-=1;
for(;i>=0;i--){
if(boom[i]<=boom[back]){
back=i;
count++;
}
else return count;
}
return count;
}
static int front(int i){//O(n)
int front = i;
int count=0;
i+=1;
for(;i<n;i++){
if(boom[front]>=boom[i]){
front=i;
count++;
}
else return count;
}
return count;
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
5714fbf0e8e10bddff8b981ac3905563
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
public class Main {
public static void main(String[] args) {
java.util.Scanner sc=new java.util.Scanner(System.in);
int n1=sc.nextInt();
int arr[]=new int [n1];
boolean arr2 []=new boolean [n1];
for(int j=0;j<n1;j++){
arr[j]=sc.nextInt();
}
int min=min(arr);
int max=max(arr);
while(min!=max){
int maxx = 0;
int maxj=arr[max];
int maxi=arr[max];
for(int i=max;i<n1;i++){
if(arr[i]<=maxi&&arr[i]!=0){
maxi=arr[i];
maxx++;
if(arr[i]==arr[max]){
arr[max]=0;
}
}
else break;
}
for(int i=max-1;i>-1;i--){
if(arr[i]<=maxj&&arr[i]!=0){
maxj=arr[i];
maxx++;
if(arr[i]==arr[max]){
arr[max]=0;
}
}
else break;
}
arr2[maxx-1]=true;
max=max(arr);
}
boolean v=true;
for(int i=n1-1;i>-1;i--){
if(arr2[i])
{
System.out.println(i+1);
v=false;
break;
}
}
if(v){
System.out.println(n1);
}
}
public static int max(int[] arr){
int max=arr[0];
int max2=0;
for(int j=0;j<arr.length;j++){
if(arr[j]>max)
{max2=j;
max=arr[j];
}
}return max2;
}
public static int min(int[] arr){
int max=arr[0];
int max2=0;
for(int j=0;j<arr.length;j++){
if(arr[j]<max)
{max2=j;
max=arr[j];
}
}return max2;
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
b0fdfee8d78d151c4c8990227c5f86b7
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class NewClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), max = 1;
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
if (a.length == 1) {
System.out.println(1);
} else {
for (int i = 0; i < n; i++) {
int count = 1;
for (int j = i + 1; j < n; j++) {
if (a[j] <= a[j - 1]) {
count++;
} else {
break;
}
}
for (int j = i - 1; j >= 0; j--) {
if (a[j] <= a[j + 1]) {
count++;
} else {
break;
}
}
if (count > max) {
max = count;
}
}
System.out.println(max);
// sc.close();
}
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
3aa2b7547ae4bf8e01d60505af528314
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.*;
public class Rain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
int max = 0;
for (int i = 0; i < n; i++) {
int count = 1;
for (int j = i - 1; j >= 0; j--)
if (arr[j] <= arr[j + 1])
count++;
else
break;
for (int j = i + 1; j < n; j++)
if (arr[j - 1] >= arr[j])
count++;
else
break;
if (count > max) {
max = count;
}
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
b1264a4c1dce26d17e84694c90b9dce5
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class c2 {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int []arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=scn.nextInt();
}
int []left=new int[n];
int val=arr[0];
left[0]=1;
int max=1;
for(int i=0;i<=n-1;i++){
max=Math.max(mt(i, arr),max);
}
System.out.println(max);
}
public static int mt(int i,int []arr){
int val=arr[i];
int left=i-1;
int right=i+1;
int count=0;
while(left>=0){
if(arr[left]<=val){
count++;
val=arr[left];
left--;
}else{
break;
}
}
val=arr[i];
while(right<arr.length){
if(arr[right]<=val){
count++;
val=arr[right];
right++;
}else{
break;
}
}
return count+1;
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
ee46c264a6dacefb019598f0ba1a047d
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
public class Mainn {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int n= input.nextInt();
int a []=new int[n];
int b[]=new int[n];
int r,m,count;
for(int i=0;i<n;i++){
a[i]=input.nextInt();
}
for(int i=0;i<n;i++){
int j=i,k=i;
count=1;
r=a[i];
m=a[i];
while(j!=-1||k!=n){
if(j!=-1){
if(r>=a[j]){
if(i!=j)
count++;
r=a[j];
j--;
}
else
j=-1;
}
if(k!=n){
if(m>=a[k]){
if(i!=k)
count++;
m=a[k];
k++;}
else
k=n;
}
}
b[i]=count;
}
int max=0;
for(int i=0;i<n;i++){
if(b[i]>max){
max=b[i];
}
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
8cb5677886ea908bef3725b78deca3d3
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.Scanner;
/**
*
* @author Fares Abu Ali
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int max = 0;
int n = sc.nextInt();
int[] str = new int[n];
for(int i=0; i<n; i++)
str[i]=sc.nextInt();
int oldCtr = 0, ctr = 0;
for (int i = 0; i < str.length; i++) {
ctr = 0;
for (int j = i + 1; j < str.length; j++) {
if (str[j] > str[j-1]) {
break;
}
ctr++;
}
for (int k = i - 1; k >= 0; k--) {
if (str[k] > str[k+1]) {
break;
}
ctr++;
}
max = Math.max(max, ctr + 1);
}
System.out.println(max);
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
30070ce88d62fac214e6e7a90bcb506b
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.io.*;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
import static java.lang.Math.sqrt;
public class PetyaAndCountry {
public void run() throws Exception {
int numberOfHeights = Integer.parseInt(nextLine());
String[] heights = nextLine().split(" ");
int maximumSatisification = 0;
int counter = 0;
// if (numberOfHeights > 1) {
for (int i = 0; i < numberOfHeights; i++) {
counter = 1;
for (int j = i; j >= 1; j--) {
if (Integer.parseInt(heights[j]) >= Integer.parseInt(heights[j - 1])) {
++counter;
} else break;
}
for (int j = i; j < numberOfHeights - 1; j++) {
if (Integer.parseInt(heights[j]) >= Integer.parseInt(heights[j + 1])) {
++counter;
} else break;
}
if (counter > maximumSatisification) {
maximumSatisification = counter;
}
}
System.out.println(maximumSatisification);
/* } else {
System.out.println(1);
}*/
}
//
/*4, 2, 3, 3, 2*/
//8
// 1 2 1 1 1 3 3 4
private int getTheAllWateredPart(int i, String[] heights) {
int counter = 0;
for (int j = 0; j < heights.length; j++) {
if (i < j) {
if (Integer.parseInt(heights[j]) <= Integer.parseInt(heights[i]))
counter++;
}
if (j < i) {
if (Integer.parseInt(heights[j]) <= Integer.parseInt(heights[i]))
counter++;
}
}
return counter;
}
public static void main(String... args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedOutputStream(System.out));
new PetyaAndCountry().run();
br.close();
pw.close();
System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]");
long gct = 0, gcc = 0;
for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) {
gct += garbageCollectorMXBean.getCollectionTime();
gcc += garbageCollectorMXBean.getCollectionCount();
}
System.err.println("[GC time : " + gct + " ms, count = " + gcc + "]");
}
static long startTime = System.currentTimeMillis();
static BufferedReader br;
static PrintWriter pw;
StringTokenizer stok;
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) {
return null;
}
stok = new StringTokenizer(s);
}
return stok.nextToken();
}
void print(byte b) {
print("" + b);
}
void print(int i) {
print("" + i);
}
void print(long l) {
print("" + l);
}
void print(double d) {
print("" + d);
}
void print(char c) {
print("" + c);
}
void print(Object o) {
if (o instanceof int[]) {
print(Arrays.toString((int[]) o));
} else if (o instanceof long[]) {
print(Arrays.toString((long[]) o));
} else if (o instanceof char[]) {
print(Arrays.toString((char[]) o));
} else if (o instanceof byte[]) {
print(Arrays.toString((byte[]) o));
} else if (o instanceof short[]) {
print(Arrays.toString((short[]) o));
} else if (o instanceof boolean[]) {
print(Arrays.toString((boolean[]) o));
} else if (o instanceof float[]) {
print(Arrays.toString((float[]) o));
} else if (o instanceof double[]) {
print(Arrays.toString((double[]) o));
} else if (o instanceof Object[]) {
print(Arrays.toString((Object[]) o));
} else {
print("" + o);
}
}
void printsp(int[] a) {
for (int i = 0, n = a.length; i < n; print(a[i] + " "), i++)
;
}
void printsp(long[] a) {
for (int i = 0, n = a.length; i < n; print(a[i] + " "), i++)
;
}
void print(String s) {
pw.print(s);
}
void println() {
println("");
}
void println(byte b) {
println("" + b);
}
void println(int i) {
println("" + i);
}
void println(long l) {
println("" + l);
}
void println(double d) {
println("" + d);
}
void println(char c) {
println("" + c);
}
void println(Object o) {
print(o);
println();
}
void println(String s) {
pw.println(s);
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
char nextChar() throws IOException {
return (char) (br.read());
}
String next() throws IOException {
return nextToken();
}
String nextLine() throws IOException {
return br.readLine();
}
int[] readInt(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
long[] readLong(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = nextLong();
}
return array;
}
double[] readDouble(int size) throws IOException {
double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = nextDouble();
}
return array;
}
String[] readLines(int size) throws IOException {
String[] array = new String[size];
for (int i = 0; i < size; i++) {
array[i] = nextLine();
}
return array;
}
int gcd(int a, int b) {
if (a == 0)
return Math.abs(b);
if (b == 0)
return Math.abs(a);
a = Math.abs(a);
b = Math.abs(b);
int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b);
a >>>= az;
b >>>= bz;
while (a != b) {
if (a > b) {
a -= b;
a >>>= Integer.numberOfTrailingZeros(a);
} else {
b -= a;
b >>>= Integer.numberOfTrailingZeros(b);
}
}
return (a << Math.min(az, bz));
}
long gcd(long a, long b) {
if (a == 0)
return Math.abs(b);
if (b == 0)
return Math.abs(a);
a = Math.abs(a);
b = Math.abs(b);
int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b);
a >>>= az;
b >>>= bz;
while (a != b) {
if (a > b) {
a -= b;
a >>>= Long.numberOfTrailingZeros(a);
} else {
b -= a;
b >>>= Long.numberOfTrailingZeros(b);
}
}
return (a << Math.min(az, bz));
}
boolean isPrime(int v) {
if (v <= 1) {
return false;
}
if (v <= 3) {
return true;
}
if (((v & 1) == 0) || ((v % 3) == 0)) {
return false;
}
for (int m = 5, n = (int) sqrt(v); m <= n; m += 6) {
if (((v % m) == 0) || ((v % (m + 2)) == 0)) {
return false;
}
}
return true;
}
void rshuffle(int[] a) { // RANDOM shuffle
Random r = new Random();
for (int i = a.length - 1, j, t; i >= 0; j = r.nextInt(a.length), t = a[i], a[i] = a[j], a[j] = t, i--)
;
}
void qshuffle(int[] a) { // QUICK shuffle
int m = new Random().nextInt(10) + 2;
for (int i = 0, n = a.length, j = m % n, t; i < n; t = a[i], a[i] = a[j], a[j] = t, i++, j = (i * m) % n)
;
}
void shuffle(long[] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
}
void shuffle(Object[] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
Object t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int[] sort(int[] a) {
final int SHIFT = 16, MASK = (1 << SHIFT) - 1, SIZE = (1 << SHIFT) + 1;
int n = a.length, ta[] = new int[n], ai[] = new int[SIZE];
for (int i = 0; i < n; ai[(a[i] & MASK) + 1]++, i++)
;
for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++)
;
for (int i = 0; i < n; ta[ai[a[i] & MASK]++] = a[i], i++)
;
int[] t = a;
a = ta;
ta = t;
ai = new int[SIZE];
for (int i = 0; i < n; ai[(a[i] >> SHIFT) + 1]++, i++)
;
for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++)
;
for (int i = 0; i < n; ta[ai[a[i] >> SHIFT]++] = a[i], i++)
;
return ta;
}
void flush() {
pw.flush();
}
void pause() {
flush();
System.console().readLine();
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
0fc7eaa8afdd17535c628b05c1979747
|
train_001.jsonl
|
1299513600
|
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main66B
{
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=sc.nextIntArray(n);
int max=0;
for(int i=0;i<n;i++)
{
int cnt=1;
for(int j=i-1;j>=0;j--)
{
if(a[j]<=a[j+1])
cnt++;
else
break;
}
for(int j=i+1;j<n;j++)
{
if(a[j]<=a[j-1])
cnt++;
else
break;
}
if(cnt>max) max=cnt;
}
out.println(max);
out.flush();
}
static class Scanner
{
BufferedReader br;
StringTokenizer tk=new StringTokenizer("");
public Scanner(InputStream is)
{
br=new BufferedReader(new InputStreamReader(is));
}
public int nextInt() throws IOException
{
if(tk.hasMoreTokens())
return Integer.parseInt(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextInt();
}
public long nextLong() throws IOException
{
if(tk.hasMoreTokens())
return Long.parseLong(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextLong();
}
public String next() throws IOException
{
if(tk.hasMoreTokens())
return (tk.nextToken());
tk=new StringTokenizer(br.readLine());
return next();
}
public String nextLine() throws IOException
{
tk=new StringTokenizer("");
return br.readLine();
}
public double nextDouble() throws IOException
{
if(tk.hasMoreTokens())
return Double.parseDouble(tk.nextToken());
tk=new StringTokenizer(br.readLine());
return nextDouble();
}
public char nextChar() throws IOException
{
if(tk.hasMoreTokens())
return (tk.nextToken().charAt(0));
tk=new StringTokenizer(br.readLine());
return nextChar();
}
public int[] nextIntArray(int n) throws IOException
{
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=nextInt();
return a;
}
public long[] nextLongArray(int n) throws IOException
{
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=nextLong();
return a;
}
public int[] nextIntArrayOneBased(int n) throws IOException
{
int a[]=new int[n+1];
for(int i=1;i<=n;i++)
a[i]=nextInt();
return a;
}
public long[] nextLongArrayOneBased(int n) throws IOException
{
long a[]=new long[n+1];
for(int i=1;i<=n;i++)
a[i]=nextLong();
return a;
}
}
}
|
Java
|
["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"]
|
2 seconds
|
["1", "3", "6"]
| null |
Java 8
|
standard input
|
[
"implementation",
"brute force"
] |
5d11fa8528f1dc873d50b3417bef8c79
|
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
| 1,100 |
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
|
standard output
| |
PASSED
|
6765224e42cc02e91413a5e8ade15b96
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/*
*
*/
Scanner scan = new Scanner(System.in);
int n=scan.nextInt();
int m=scan.nextInt();
int[][] arr = new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
arr[i][j]=scan.nextInt();
}
}
int ans=0;
outer: for(int i=n-1;i>=0;i--) {
for(int j=m-1;j>=0;j--) {
if(arr[i][j]==0) {
int num=Math.min(arr[i][j+1], arr[i+1][j])-1;
if(num>Math.max(arr[i-1][j], arr[i][j-1])) {
arr[i][j]=num;
}else {
ans=-1;
break outer;
}
}else if(i>0 && j>0 && (arr[i][j]<=arr[i][j-1] || arr[i][j]<=arr[i-1][j])) {
ans=-1;
break outer;
}
ans+=arr[i][j];
}
}
/*
* for(int i=0;i<n;i++) { for(int j=0;j<m;j++) {
* System.out.print(arr[i][j]+" "); } System.out.println(); }
*/
System.out.println(ans);
scan.close();
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
97426386e10b0d2262f9456cd6fbe451
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.Scanner;
public class C1231 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int rows = s.nextInt();
int cols = s.nextInt();
int[][] matrix = new int[rows][cols];
int[][] original = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j]=s.nextInt();
original[i][j]=matrix[i][j];
}
}
if(maximizeRows(matrix))
{
if(isIncreasing(matrix))
{
System.out.println(sumMatrix(matrix)); return;
}
else if (adjustColumns(matrix,original) && isIncreasing(matrix))
{
System.out.println(sumMatrix(matrix)); return;
}
}
System.out.println(-1);
}
private static boolean adjustColumns(int[][] matrix,int[][] original) {
for (int j = 1; j < matrix[0].length-1; j++) {
for (int i = matrix.length-2; i > 0 ; i--) {
if(original[i][j]==0)
{
if(matrix[i][j]>=matrix[i+1][j]) // not ok !
{
// try to adjust
matrix[i][j] = matrix[i+1][j]-1;
// check
if(matrix[i][j]<=matrix[i-1][j])
{
if(original[i-1][j]==0)
matrix[i-1][j]=matrix[i][j]-1;
else
return false;
}
}
}
}
}
return true;
}
private static boolean isIncreasing(int[][] matrix) {
// check rows
for (int i = 0; i < matrix.length; i++) {
for (int j = 1; j < matrix[i].length; j++) {
if(matrix[i][j]<=matrix[i][j-1])
return false;
}
}
// check col's
for (int j = 0; j < matrix[0].length; j++) {
for (int i = 1; i < matrix.length; i++) {
if(matrix[i][j]<=matrix[i-1][j])
return false;
}
}
return true;
}
private static boolean maximizeRows(int[][] matrix) {
for (int i = 1; i < matrix.length-1; i++) {
for (int j = matrix[i].length-2; j > 0; j--) {
if(matrix[i][j]==0)
{
matrix[i][j] = matrix[i][j+1]-1; // maximal value possible
if(matrix[i][j]<=matrix[i][j-1])
return false;
}
}
}
return true;
}
private static long sumMatrix(int[][] matrix) {
long sum = 0;
for (int[] row : matrix)
for (int cell : row) sum += cell;
return sum;
}
// accessory method
private static void printMatrix(int[][] matrix)
{
for(int[] row:matrix) {
for (int cell : row) System.out.print(cell + "\t");
System.out.println();
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
d2d28abd3fa9ba7b17c19723d3878c17
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.InputMismatchException;
public class C_1231 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws NumberFormatException, IOException {
int n = nextInt();
int m = nextInt();
int[][] mat = new int[n][m];
for (int i=0; i<n; i++) {
mat[i] = readIntegerArray(m);
}
long sum = 0;
for (int i=n-1; i>=0; i--) {
for (int j=m-1; j>=0; j--) {
if (mat[i][j] == 0) {
mat[i][j] = Math.min(mat[i+1][j], mat[i][j+1]) - 1;
}
sum += mat[i][j];
}
}
boolean valid = true;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (mat[i][j] <= 0 || ((j>0 && mat[i][j] <= mat[i][j-1]) || (j<m-1 && mat[i][j] >= mat[i][j+1]) || (i>0 && mat[i][j] <= mat[i-1][j]) || (i<n-1 && mat[i][j] >= mat[i+1][j]))) {
valid = false;
break;
}
}
}
System.out.println(valid ? sum : -1);
}
static int read() throws IOException {
return br.read();
}
static int nextInt() throws IOException {
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;
}
static long nextLong() throws NumberFormatException, IOException {
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;
}
static int[] readIntegerArray(int n) throws IOException {
int[] arr = new int[n];
String s;
while(true) {
s = br.readLine();
if(s!= null && !s.equals("")) {
break;
}
}
String[] strArr = s.split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(strArr[i]);
}
return arr;
}
static long[] readlongArray(int n) throws IOException {
long[] arr = new long[n];
String[] strArr = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(strArr[i]);
}
return arr;
}
static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
cc91ca3e0dde456a1a1928fe14a159fb
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class incmat
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int m=in.nextInt();
int a[][]=new int[n][m];
int flag=1;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
a[i][j]=in.nextInt();
for(int i=n-2;i>0;i--)
{
for(int j=m-2;j>0;j--)
{
if(a[i][j]==0)
{
int x=Math.min(a[i+1][j],a[i][j+1]);
a[i][j]=x-1;
if(a[i][j]<=a[i-1][j]||a[i][j]<=a[i][j-1])
{
flag=0;
break;
}
}
}
if(flag==0)
break;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
// Out of bound condition
if (i - 1 >= 0)
{
if (a[i][j] <= a[i - 1][j])
flag=0;
}
// Out of bound condition
if (j - 1 >= 0)
{
if (a[i][j] <= a[i][j - 1])
flag=0;
}
}
}
int sum=0;
if(flag==0)
{
System.out.println("-1");
}
else
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
sum+=a[i][j];
System.out.println(sum);
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
5522c585327f59c767fbb16171df0eb7
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.Scanner; // Import the Scanner class
public final class Main{
static int[] tempList;
public static void main(String[ ] args){
Scanner sc = new Scanner(System.in);
int m = sc.nextInt(), n = sc.nextInt();
int[][] tab = new int[m][n];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++){
tab[i][j] = sc.nextInt();
}
}
int sum=0;
for(int i=m-1;i>=0;i--) for(int j=n-1;j>=0;j--){
if(i<m-1 && j<n-1 &&tab[i][j]==0)
tab[i][j] = min(tab[i+1][j],tab[i][j+1])-1;
else if((i<m-1 && tab[i][j]>=tab[i+1][j] )|| (j<n-1 && tab[i][j]>=tab[i][j+1])){
System.out.println(-1);
return;
}
sum+=tab[i][j];
}
System.out.println(sum);
sc.close();;
/*for(int i=0;i<m;i++) {
for(int j=0;j<n;j++){
System.out.print(tab[i][j] + " ");
}
System.out.print("\n");
}*/
}
static int min(int x, int y){
return x>y ? y : x;
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
ba90a417f1ac23a8251fa90900661200
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main1 {
static PrintWriter out = new PrintWriter(System.out, true);
static FastReader input = new FastReader();
void main1() throws NumberFormatException, IOException {
int n = input.nextInt();
int m = input.nextInt();
int a[][] = new int[n][m];
boolean x[][] = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = input.nextInt();
x[i][j] = a[i][j] == 0 ? true : false;
}
}
boolean good = true;
for (int i = 1; i < n - 1; i++) {
for (int j = m - 2; j > 0; j--) {
if (x[i][j]) {
a[i][j] = a[i][j + 1] - 1;
}
}
}
for (int j = 1; j < m - 1; j++) {
for (int i = n - 2; i > 0; i--) {
if (x[i][j]) {
a[i][j] = Math.min(a[i][j], a[i + 1][j] - 1);
}
}
}
long sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((i < n - 1 && a[i][j] >= a[i + 1][j] || j < m - 1 && a[i][j] >= a[i][j + 1]))
good = false;
sum += a[i][j];
}
}
out.println(good ? sum : -1);
out.flush();
}
public static void main(String args[]) throws NumberFormatException, IOException {
Main1 inst = new Main1();
inst.main1();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
// try {
// br = new BufferedReader(new FileReader(new File("hamming.in")));
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); }
return st.nextToken();
}
int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); }
long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); }
double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException {
String str = "";
str = br.readLine();
return str;
}
boolean hasNext() throws IOException { return br.ready(); }
}
static class con {
static int IINF = (int) 1e9;
static int _IINF = (int) -1e9;
static long LINF = (long) 1e15;
static long _LINF = (long) -1e15;
static double EPS = 1e-9;
}
static class Triple implements Comparable<Triple> {
int x;
int y;
int z;
Triple(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public int compareTo(Triple o) {
if (x == o.x && y == o.y)
return z - o.z;
if (x == o.x)
return y - o.y;
return x - o.x;
}
@Override
public String toString() { return "(" + x + ", " + y + ", " + z + ")"; }
}
static class Pair implements Comparable<Pair> {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
if (x == o.x)
return (int) (y - o.y);
return (int) (x - o.x);
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
static void shuffle(int[] a) {
for (int i = 0; i < a.length; i++) {
int r = i + (int) (Math.random() * (a.length - i));
int tmp = a[r];
a[r] = a[i];
a[i] = tmp;
}
}
static void shuffle(long[] a) {
for (int i = 0; i < a.length; i++) {
int r = i + (int) (Math.random() * (a.length - i));
long tmp = a[r];
a[r] = a[i];
a[i] = tmp;
}
}
static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); }
static class DSU {
int[] p, rank, setSize;
int numSets;
DSU(int n) {
p = new int[n];
rank = new int[n];
setSize = new int[n];
numSets = n;
for (int i = 0; i < n; i++) {
p[i] = i;
setSize[i] = 1;
}
}
int findSet(int n) { return p[n] = p[n] == n ? n : findSet(p[n]); }
boolean isSameSet(int n, int m) { return findSet(n) == findSet(m); }
void mergeSet(int n, int m) {
if (!isSameSet(n, m)) {
numSets--;
int p1 = findSet(n);
int p2 = findSet(m);
if (rank[p1] > rank[p2]) {
p[p2] = p1;
setSize[p1] += setSize[p2];
} else {
p[p1] = p2;
setSize[p2] += setSize[p1];
if (rank[p1] == rank[p2])
rank[p1]++;
}
}
}
int size() { return numSets; }
int setSize(int n) { return setSize[findSet(n)]; }
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[1024];
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;
}
// ddd
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9');
if (c == '.')
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
static ArrayList<Integer>[] allocateArrayList(int n) {
ArrayList<Integer> g[] = new ArrayList[n];
for (int i = 0; i < n; i++) { g[i] = new ArrayList<>(); }
return g;
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
65f10956d77a91d28a1caad72999236d
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author tarek
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
CIncreasingMatrix solver = new CIncreasingMatrix();
solver.solve(1, in, out);
out.close();
}
static class CIncreasingMatrix {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int m = in.readInt();
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = in.readInt();
}
}
int sum = 0;
for (int i = n - 2; i >= 0; --i) {
for (int j = m - 2; j >= 0; --j) {
if (a[i][j] == 0) {
a[i][j] = Math.min(a[i + 1][j], a[i][j + 1]) - 1;
}
}
}
for (int i = n - 1; i >= 0; --i) {
for (int j = m - 1; j >= 0; --j) {
sum += a[i][j];
if (i + 1 < n && a[i][j] >= a[i + 1][j] || j + 1 < m && a[i][j] >= a[i][j + 1]) {
out.printLine("-1");
return;
}
}
}
out.printLine(sum);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
6cc00ec259054991efbf84d76a27167c
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.*;
public class Codeforces1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
boolean flag=false;
int a[][]=new int[n+1][m+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a[i][j]=sc.nextInt();
}
}
int sum=0;
for(int i=n-1;i>=1;i--){
for(int j=m-1;j>=1;j--){
if(a[i][j]==0){
a[i][j]=Math.min(a[i][j+1],a[i+1][j])-1;
}
if((a[i][j]>=a[i+1][j])||(a[i][j]>=a[i][j+1])){
flag=true;
break;
}
}
if(flag==true){
break;
}
}
if(flag==false){
for(int i=1;i<m;i++){
if(a[n][i]>=a[n][i+1]){
flag=true;
break;
}
}
for(int i=1;i<n;i++){
if(a[i][m]>=a[i+1][m]){
flag=true;
break;
}
}
}
if(flag==false){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
sum+=a[i][j];
}
}
System.out.println(sum);
}
else{
System.out.println(-1);
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
399e9bb1ed3c6aa875d1f2f9ad4370f8
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
try {
Solver solver = new Solver();
solver.solve(in, out);
out.close();
}catch (Exception e){
return;
}
}
static class Solver {
public void solve(InputReader in, OutputWriter out) {
int n=in.nextInt();
int m=in.nextInt();
int[][] a=new int[n][m];
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
a[i][j]=in.nextInt();
int i=0;
while (i<m-1&&a[0][i]<a[0][i+1])
i++;
if (i!=m-1){
out.printLine("-1");
return;
}
i=0;
while (i<m-1&&a[n-1][i]<a[n-1][i+1])
i++;
if (i!=m-1){
out.printLine("-1");
return;
}
i=0;
while (i<n-1&&a[i][0]<a[i+1][0])
i++;
if (i!=n-1){
out.printLine("-1");
return;
}
i=0;
while (i<n-1&&a[i][m-1]<a[i+1][m-1])
i++;
if (i!=n-1){
out.printLine("-1");
return;
}
for (i=n-2;i>0;i--){
for (int j=m-2;j>0;j--){
if (a[i][j]==0){
a[i][j]=a[i][j+1]-1;
if (a[i][j]>=a[i+1][j])
a[i][j]=a[i+1][j]-1;
if (a[i][j]<=a[i][j-1]){
out.printLine("-1");
return;
}
if (a[i][j]<=a[i-1][j]) {
out.printLine("-1");
return;
}
}else {
if (a[i][j]>=a[i+1][j]||a[i][j]>=a[i][j+1]||a[i][j]<=a[i-1][j]||a[i][j]<=a[i][j-1]){
out.printLine("-1");
return;
}
}
}
}
int sum=0;
for (i=0;i<n;i++)
for (int j=0;j<m;j++)
sum+=a[i][j];
out.printLine(sum);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public String next() {
return nextString();
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.flush();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
writer.flush();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.nextInt();
return array;
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
fe9f88bdecc04a634f0c1579b5e97a2d
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
//package com.company;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
int a[][] = new int[n][m];
for(int i = 0; i < n ; i++)
{
String buf[] = br.readLine().split(" ");
for(int j = 0; j < m; j++)
{
a[i][j] = Integer.parseInt(buf[j]);
}
}
int sum = 0;
// suma elem inafara de ultima linie si ultima coloana
for(int i = n - 2; i > 0; i--)
{
for(int j = m - 2; j > 0; j--)
{
if(a[i][j] == 0)
{
int min = Math.min(a[i][j+1], a[i+1][j]);
int max = Math.max(a[i][j-1], a[i-1][j]);
if(min - max < 2)
{
System.out.println("-1");
return;
}
a[i][j] = min - 1;
}
else
{
if(a[i][j] >= a[i][j+1] || a[i][j] >= a[i+1][j])
{
System.out.println("-1");
return;
}
}
sum += a[i][j];
}
}
for(int i = 0; i < n - 1; i++)
{
if(a[i][0] >= a[i+1][0] || a[i][m-1] >= a[i+1][m-1])
{
System.out.println("-1");
return;
}
sum += a[i][0];
sum += a[i][m-1];
}
sum += a[n-1][0];
sum += a[n-1][m-1];
for(int i = 1; i < m - 1; i++)
{
if(a[0][i] >= a[0][i+1] || a[n-1][i] >= a[n-1][i+1])
{
System.out.println("-1");
return;
}
sum += a[0][i];
sum += a[n-1][i];
}
System.out.println(sum);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
60559921bc964951a24959ad4b25acdf
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.Scanner;
public class IncreasingMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] params = sc.nextLine().split(" ");
int n = Integer.decode(params[0]);
int m = Integer.decode(params[1]);
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++) {
String[] line = sc.nextLine().split(" ");
for (int j = 0; j < m; j++) {
matrix[i][j] = Integer.decode(line[j]);
}
}
int sum = 0;
for (int i = 0; i < n; i++) {
if (matrix[i][m-1] <= matrix[i][m-2]) {
System.out.println("-1");
return;
}
sum += matrix[i][m-1];
}
for (int j = 0; j < m; j++) {
if (matrix[n-1][j] <= matrix[n-2][j]) {
System.out.println("-1");
return;
}
sum += matrix[n-1][j];
}
sum -= matrix[n-1][m-1];
for (int i = n-2; i > 0; i--) {
for (int j = m-2; j > 0; j--) {
if (matrix[i][j] == 0) {
matrix[i][j] = Math.min(matrix[i+1][j], matrix[i][j+1]) - 1;
}
if (matrix[i][j] <= Math.max(matrix[i-1][j], matrix[i][j-1])) {
System.out.println("-1");
return;
}
sum += matrix[i][j];
}
}
for (int i = 0; i < n-1; i++) {
sum += matrix[i][0];
}
for (int j = 1; j < m-1; j++) {
sum += matrix[0][j];
}
System.out.println(sum);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
b190c6c63b147137e3f2ba73a5becc98
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static int inf = (int) (1e9 + 7);
public static void main(String[] args) throws IOException {
sc = new Scanner(System.in);
pw = new PrintWriter(System.out);
int n = sc.nextInt();
int m = sc.nextInt();
int a[][] = new int [n][m];
for(int i = 0;i < n;i++) {
for(int j = 0;j < m;j++) a[i][j] = sc.nextInt();
}
boolean bad = false;
long sum = 0;
for(int i = n - 1;i >= 0;i--) {
int last = inf;
for(int j = m - 1;j >= 0;j--) {
if (a[i][j] != 0) {
if (a[i][j] >= last) bad = true;
last = a[i][j];
}else{
int q = Math.min(a[i + 1][j], a[i][j + 1]) - 1;
if (q >= last) bad = true;
last = q;
a[i][j] = q;
}
sum += last;
}
}
for(int j = 0;j < m;j++) {
int last = -1;
for(int i = 0;i < n;i++) {
if (a[i][j] <= last) bad = true;
last = a[i][j];
}
}
if (bad) pw.println(-1);
else pw.println(sum);
pw.close();
}
static Scanner sc;
static PrintWriter pw;
static class Scanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
Scanner(InputStream in) throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(in));
}
Scanner(String in) throws FileNotFoundException {
br = new BufferedReader(new FileReader(in));
}
String next() throws IOException {
while (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
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());
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
82d465d294289ab5b6643aa0c0617e29
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.*;
import java.util.*;
// Author : Yash Shah
public class D implements Runnable {
static int fun(int x,int d)
{
return x+(int)Math.ceil(d/(float)(x+1));
}
public void run() {
InputReader sc = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=sc.nextInt();
int m=sc.nextInt();
int a[][]=new int[n][m];
for(int i[]:a)sa(i,sc);
boolean done=true;
long ans=0;
for(int i=n-1;i>=0&&done;i--)
{
for(int j=m-1;j>=0&&done;j--)
{
int put=8000;
if(i+1<n)
{
put=Math.min(put,a[i+1][j]-1);
}
if(j+1<m)
{
put=Math.min(put,a[i][j+1]-1);
}
int must=1;
if(i-1>=0)
{
must=Math.max(must,a[i-1][j]+1);
}
if(j-1>=0)
{
must=Math.max(must,a[i][j-1]+1);
}
if(must>put)
{
done=false;
}
if(a[i][j]==0)
a[i][j]=put;
else
{
if(a[i][j]<must || a[i][j]>put)
done = false;
}
//out.println(i+" "+j+" "+put);
ans+=a[i][j];
}
}
out.println(done?ans:-1);
out.close();
}
//========================================================================
static class Pair
{
int a,b;
Pair(int aa,int bb)
{
a=aa;
b=bb;
}
}
static void sa(int a[],InputReader sc)
{
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
}
static class PairSort implements Comparator<Pair>
{
public int compare(Pair a,Pair b)
{
return b.b-a.b;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new D(),"Main",1<<27).start();
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
1649467f364b515c73144fc634c83451
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class Cf182 implements Runnable
{
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception
{
new Thread(null, new Cf182(),"Main",1<<27).start();
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// array sorting by colm
public static void sortbyColumn(int arr[][], int col)
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
// gcd
public static long findGCD(long arr[], int n)
{
long result = arr[0];
for (int i = 1; i < n; i++)
result = gcd(arr[i], result);
return result;
}
// fibonaci
static int fib(int n)
{
int a = 0, b = 1, c;
if (n == 0)
return a;
for (int i = 2; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return b;
}
// sort a string
public static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
// pair function
// list.add(new Pair<>(sc.nextInt(), i + 1));
// Collections.sort(list, (a, b) -> Integer.compare(b.first, a.first));
private static class Pair<F, S> {
private F first;
private S second;
public Pair() {}
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
}
public void run()
{
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int n = sc.nextInt();
int m = sc.nextInt();
int a[][] = new int[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
a[i][j] = sc.nextInt();
}
long sum = 0;
for(int i=n-1;i>=0;i--)
{
for(int j=m-1;j>=0;j--)
{
if(a[i][j]==0)
{
int min = Math.min(a[i][j+1],a[i+1][j]);
a[i][j] = min - 1;
}
sum += a[i][j];
}
}
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<m;j++)
// w.print(a[i][j]+" ");
// w.println();
// }
int flag = 0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m-1;j++)
{
if(a[i][j]>=a[i][j+1])
{
flag = 1;
break;
}
}
if(flag==1)
break;
}
if(flag==0)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n-1;j++)
{
if(a[j][i]>=a[j+1][i])
{
flag = 1;
break;
}
}
if(flag==1)
break;
}
}
if(flag==0)
w.println(sum);
else
w.println("-1");
w.flush();
w.close();
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
f48fc8eca5dcf43668a010ad5779dae2
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.*;
import java.io.*;
public final class Solution {
static PrintWriter out = new PrintWriter(System.out);
static void flush() {
out.flush();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
static boolean isPalindrome(String str1, String str2) {
String str3 = str1+str2;
int i = 0;
int j = str3.length()-1;
while(i < j) {
char a = str3.charAt(i);
char b = str3.charAt(j);
if(a != b) return false;
i++;
j--;
}
return true;
}
static boolean isPalindrome(String str) {
int i = 0;
int j = str.length()-1;
while(i < j) {
char a = str.charAt(i);
char b = str.charAt(j);
if(a != b) return false;
i++;
j--;
}
return true;
}
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());
}
static int fact(int n) {
if(n == 1) return 1;
return n * fact(n-1);
}
public int[] readIntArray(int n) {
int[] arr = new int[n];
for(int i=0; i<n; ++i)
arr[i]=nextInt();
return arr;
}
public int[][] readIntArray(int m, int n){
int[][] arr = new int[m][n];
for(int i = 0;i<m;i++) {
for(int j = 0;j<n;j++) {
arr[i][j] = nextInt();
}
}
return arr;
}
public String[] readStringArray(int n) {
String[] arr = new String[n];
for(int i=0; i<n; ++i)
arr[i]= nextLine();
return arr;
}
static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try{
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static int solve(int[][] arr, int n, int m) {
int sum = 0;
for(int i =n-1;i >= 0;i--) {
for(int j = m-1;j >= 0;j--) {
if(arr[i][j] != 0) {
if(i != 0 && j != 0 && (arr[i-1][j] >= arr[i][j] || arr[i][j-1] >= arr[i][j])) {
return -1;
}
else {
sum += arr[i][j];
}
if(i != n-1 && j != m-1 && (arr[i][j+1] <= arr[i][j] || arr[i+1][j] <= arr[i][j])){
return -1;
}
}
else {
int c = arr[i][j+1];
int d = arr[i+1][j];
arr[i][j] = Math.min(c, d) - 1;
sum += arr[i][j];
}
}
}
return sum;
}
public static void main(String args[]) throws Exception {
FastReader sc = new FastReader();
long start = System.currentTimeMillis();
int n = sc.nextInt();
int m = sc.nextInt();
int[][] arr = sc.readIntArray(n, m);
out.println(solve(arr, n, m));
flush();
long end = System.currentTimeMillis();
NumberFormat formatter = new DecimalFormat("#0.00000");
//System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
382d4d3773dc2d3ee87421e430a18ee0
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import sun.security.jgss.TokenTracker;
public class MyCode
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,j,n,m,x,element;
Integer[][] arr = new Integer[600][600];
Integer[][] zero = new Integer[600][600];
while(sc.hasNextInt())
{
n = sc.nextInt();
m = sc.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
zero[i][j]=0;
x= sc.nextInt();
arr[i][j]=x;
if(x==0)
zero[i][j]=1;
}
}
for(j=m-2;j>0;j--)
{
for(i=n-2;i>=1;i--)
{
if(arr[i][j]==0)
{
arr[i][j]=arr[i+1][j]-1;
}
}
}
for(i=1;i<n-1;i++)
{
for(j=m-2;j>0;j--)
{
if(zero[i][j]==1)
{
element = arr[i][j];
if(arr[i][j-1]<element && element<arr[i][j+1])continue;
else
{
element = arr[i][j+1]-1;
arr[i][j]= Math.min(arr[i][j], element);
}
}
}
}
boolean ans=true;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(j>0 && arr[i][j]<=arr[i][j-1])
{
ans=false;
break;
}
if(i>0 && arr[i][j]<=arr[i-1][j])
{
ans=false;
break;
}
}
}
if(ans==false)
{
System.out.println("-1");
continue;
}
long total=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
total+=arr[i][j];
}
}
if(ans)
System.out.println(total);
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
12b5f6662de51b439e7993d948fd9fd2
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.Scanner;
public class Solution {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int rows=s.nextInt();
int cols=s.nextInt();
int[][] matrix=new int[rows][cols];
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
matrix[i][j]=s.nextInt();
}
}
printModifiedMatrix(matrix);
}
public static void printModifiedMatrix(int[][] matrix)
{
int rows=matrix.length;
int cols=matrix[0].length;
for(int i=cols-2;i>=0;i--)
{
if(matrix[rows-1][i]>=matrix[rows-1][i+1])
{
System.out.println(-1);
return;
}
}
for(int i=rows-2;i>=0;i--)
{
if(matrix[i][cols-1]>=matrix[i+1][cols-1])
{
System.out.println(-1);
return;
}
}
for(int i=rows-2;i>=0;i--)
{
for(int j=cols-2;j>=0;j--)
{
int correctValue=Math.min(matrix[i+1][j],matrix[i][j+1])-1;
if(matrix[i][j]==0)
{
matrix[i][j]=correctValue;
}
else{
if(matrix[i][j]>correctValue)
{
System.out.println(-1);
return;
}
}
}
}
int sum=0;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
sum+=matrix[i][j];
}
}
System.out.println(sum);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
dbb48bbbcab4e61a2cdbfdea0017f91e
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
//189301019.akshay
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
public class C
{
public static void main(String[] args)
{
FastReader sc=new FastReader();
StringBuffer ans=new StringBuffer();
int test=1;
while(test-->0)
{
int n=sc.nextInt();
int m=sc.nextInt();
int arr[][]=new int[n][m];
boolean flag=true;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
arr[i][j]=sc.nextInt();
for(int i=1;i<n;i++)
if((arr[i][0]<= arr[i-1][0] )||(arr[i][m-1]<=arr[i-1][m-1]))
{
flag=false;
break;
}
for(int i=1;i<m;i++)
if((arr[0][i]<= arr[0][i-1] )||(arr[n-1][i] <=arr[n-1][i-1]))
{
flag=false;
break;
}
for(int i =n-2;i>0;i--) {
for(int j=m-2;j>0;j--) {
if(arr[i][j] != 0) continue;
arr[i][j] = Math.min(arr[i+1][j], arr[i][j+1])-1;
}
}
if(!flag) {
ans.append("-1\n");
continue;
}
long sum=0l;
outer:for(int i=1;i<n-1;i++) {
for(int j=1;j<m-1;j++) {
if(arr[i][j] <0)
{
flag = false;
break outer;
}
if(arr[i][j] <= arr[i-1][j] || arr[i][j] <= arr[i][j-1]) {
flag = false;
break outer;
}
sum+=arr[i][j];
}
}
if(!flag)
ans.append("-1\n");
else {
for(int i=0;i<n;i++) sum+=arr[i][0]+arr[i][m-1];
for(int i=0;i<m;i++) sum+=arr[0][i]+arr[n-1][i];
sum -=(arr[0][0]+arr[n-1][m-1]+arr[0][m-1]+arr[n-1][0]);
ans.append(sum+"\n");
}
}
System.out.print(ans);
}
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
ab9a744e8ffaba02ec0db9f9dbe1ffd8
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Matrix {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
int a[][] = new int[n][m];
for (int i = 0; i < n; i++) {
String ss[] = br.readLine().split(" ");
for (int j = 0; j < m; j++) {
a[i][j] = Integer.parseInt(ss[j]);
}
}
for (int i = n - 2; i >= 0; i--) {
for (int j = m - 2; j >= 0; j--) {
if (a[i][j] == 0) {
a[i][j] = Math.min(a[i + 1][j], a[i][j + 1]) - 1;
}
}
}
int sum = 0;
boolean increasing = false;
for (int i = 0; i < n; i++) {
int hdiff, vdiff, prevx = 0, prevy = 0;
for (int j = 0; j < m; j++) {
if (i > 0) {
prevy = a[i - 1][j];
} else {
prevy = 0;
}
if (j > 0) {
prevx = a[i][j - 1];
} else {
prevx = 0;
}
hdiff = a[i][j] - prevx;
vdiff = a[i][j] - prevy;
if (hdiff > 0 && vdiff > 0) {
increasing = true;
sum += a[i][j];
} else {
increasing = false;
break;
}
}
if (!increasing)
break;
}
int res = increasing ? sum : -1;
System.out.println(res);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
7286f3ca94e645e22965760e83e891be
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
//Increasing Matrix
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class IncreasingMatrix
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int a[][] = new int[n][m];
long sum = 0;
boolean flag = false;
for(int i = 0; i < n; i++)
{
st = new StringTokenizer(br.readLine());
for(int j = 0; j < m; j++)
{
a[i][j] = Integer.parseInt(st.nextToken());
if(a[i][j] == 0)
flag = true;
sum = (long)(sum + a[i][j]);
}
}
if(!flag)
{
for(int i = 0; i < n; i++)
{
for(int j = 1; j < m; j++)
{
if(a[i][j] <= a[i][j-1] || a[j][i] <= a[j-1][i])
{
System.out.println("-1");
return;
}
}
}
System.out.println(sum);
return;
}
int min;
for(int i = n-2; i > 0; i--)
{
for(int j = m-2; j > 0; j--)
{
if(a[i][j] == 0)
{
min = Math.min(a[i][j+1], a[i+1][j]);
if(min-1 <= a[i][j-1] || min-1 <= a[i-1][j])
{
System.out.println("-1");
return;
}
else
{
a[i][j] = min-1;
sum = (long)(sum + min-1);
}
}
}
}
System.out.println(sum);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
2032eb528b5b292bca7bb45f64644a5f
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.*;
public class _1231c_IncreasingMatrix {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt();
int m=scn.nextInt();
int[][]array=new int[n][m];
long sum=0;
boolean b=true;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
array[i][j]=scn.nextInt();
sum+=array[i][j];
}
}
for(int i=n-1;i>0&& b;i--) {
for(int j=m-2;j>0;j--) {
if(array[i][j]==0) {
if(array[i][j+1]==0||array[i+1][j]==0)
array[i][j]=Math.max(array[i][j+1],array[i+1][j])-1;
else
array[i][j]=Math.min(array[i][j+1],array[i+1][j])-1;
if(array[i][j]==array[i-1][j] || array[i][j]==array[i][j-1]) {
System.out.println("-1");
b=false;
break;
}
sum+=array[i][j];
}
}
}
for(int i=0;i<n && b;i++) {
for(int j=0;j<m;j++) {
if(i+1<array.length && j+1<array[0].length && (array[i][j]>=array[i+1][j] || array[i][j]>=array[i][j+1]))
{
System.out.println("-1");
b=false;
break;
}
if(i-1>=0 && j-1>=0 && array[i][j-1]!=0 && (array[i][j]<=array[i-1][j] || array[i][j]<=array[i][j-1])) {
System.out.println("-1");
b=false;
break;
}
}
}
if(b)
System.out.println(sum);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
4de47d23e8574706eff1a84e0a7b3521
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class ProgEc {
public static void main(String[] args) throws Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(1, in, out);
out.close();
}
static class Solver {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int[][] a = new int[n+2][m+2];
int sum = 0;
int res = 0;
a[n+1][0] = 9000;
for (int j = 1; j <= m; j++) a[n+1][j] = a[n+1][j-1] + 10;
a[0][m+1] = 9000;
for (int i = 1; i <= n; i++) a[i][m+1] = a[i-1][m+1] + 10;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
a[i][j] = in.nextInt();
sum += a[i][j];
if (a[i][j] != 0) {
if (a[i][j] <= a[i][j-1] && a[i][j-1] != 0) { res = 1; continue; }
if (a[i][j] <= a[i-1][j] && a[i-1][j] != 0) { res = 1; continue; }
if (a[i][j] >= a[i][j+1] && a[i][j+1] != 0) { res = 1; continue; }
if (a[i][j] >= a[i+1][j] && a[i+1][j] != 0) { res = 1; continue; }
}
}
/*for (int i = 0; i <= n+1; i++) {
for (int j = 0; j <= m+1; j++) {
out.print(a[i][j] + " ");
}
}*/
if (res == 1) {
out.println(-1);
out.close();
System.exit(0);
}
for (int i = n-1; i >= 1; i--)
for (int j = m-1; j >= 1; j--) {
if (a[i][j] == 0) {
int c = Math.min(a[i+1][j],a[i][j+1]);
a[i][j] = c-1;
sum += a[i][j];
}
if (a[i][j] <= a[i][j-1] || a[i][j] <= a[i-1][j]) {
out.println(-1);
out.close();
System.exit(0);
}
}
out.println(sum);
}
}
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 float nextFloat() {
return Float.parseFloat(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
a9871cdc874c410130338de2dc29ec58
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
//Mann Shah [ DAIICT ].
//fast io
public class Main {
public static int mod = (int) (1e9 + 7);
static InputReader in;
static PrintWriter out;
public static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static void main(String args[]) {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
int n = in.nextInt();
int m = in.nextInt();
int[][] a = new int[n][m];
int[][] z = new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j]=in.nextInt();
if(a[i][j]==0) {
z[i][j]=1;
}
}
}
if(a[0][0]==0)
a[0][0] = 1;
int f=0;
for(int i=1;i<n;i++) {
if(a[i][0]==0) {
a[i][0] = a[i-1][0]+1;
}
else {
if(a[i][0] <= a[i-1][0]) {
f=1;
break;
}
}
}
for(int i=1;i<m;i++) {
if(a[0][i]==0) {
a[0][i] = a[0][i-1]+1;
}
else {
if(a[0][i] <= a[0][i-1]) {
f=1;
break;
}
}
}
for(int i=1;i<n;i++) {
for(int j=1;j<m;j++) {
if(a[i][j]==0) {
//cp
int v = Math.max(a[i-1][j], a[i][j-1]);
a[i][j]=v+1;
}
else {
if(a[i][j] <= a[i-1][j] || a[i][j]<=a[i][j-1]) {
f=1;
break;
}
}
}
}
for(int i=n-2;i>=0;i--) {
for(int j=m-2;j>=0;j--) {
if(z[i][j]==1) {
int vv = Math.min(a[i][j+1],a[i+1][j]);
a[i][j]=vv-1;
}
}
}
if(f==1) {
out.println("-1");
}
else {
int sum=0;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
sum+=a[i][j];
}
}
out.println(sum);
}
out.close();
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public long[] nextLongArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
// For Pair sorting
// Arrays.sort(arr,new Comparator<Pair>() {
// @Override public int compare(Pair p1, Pair p2)
// {
// return p1.x - p2.x;
// }
// });
// Pair arr[] = new Pair[n];
// arr[0] = new Pair(10, 20);
class Pair {
int x;
String y;
// Constructor
public Pair(int x, String y) {
this.x = x;
this.y = y;
}
}
// // Comparator to sort the pair according to first element.
// Arrays.sort(arr, new Comparator<Pair>() {
// @Override public int compare(Pair p1, Pair p2){
// return p1.x - p2.x;
// }
// });
class couple implements Comparable<couple> {
int x, y;
public couple(int m, int f) {
x = m;
y = f;
}
public int compareTo(couple o) {
return x - o.x;
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
6acfd4624f6c3f52af000e9716ffc18f
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.Scanner;
import static java.lang.Math.max;
public class Myclass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int a[][] = new int[n][m];
for (int i = 0; i <n; i++) {
for (int j = 0; j <m; j++) {
a[i][j] = scan.nextInt();
}
}
int sum = 0;
for (int i = n-1; i >=0; i--) {
for (int j = m-1; j >=0; j--) {
if (a[i][j] == 0) {
a[i][j] = Math.min(a[i + 1][j], a[i][j + 1]) - 1;
}
if(j < m-1 && a[i][j] >= a[i][j+1]){
sum = -1;
break;
}
if(i < n-1 && a[i][j] >= a[i+1][j]){
sum = -1;
break;
}
}
if(sum==-1) break;
}
// if (isInc(a)) {
// System.out.println(" hej hej");
if(sum == 0){
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
sum += a[i][j];
}
}
}
System.out.println(sum);
}
// private static boolean isInc(int[][] a) {
//
// for (int i = 0; i < a.length - 1; i++) {
// for (int j = 0; j < a[0].length - 1; ++j) {
// if (a[i][j] > a[i + 1][j] || a[i][j] > a[i][j + 1]) return false;
// }
// }
// return true;
// }
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
7723d25522b96dc9ff2d7e574b35afe2
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int[][] a = new int[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
a[i][j] = scanner.nextInt();
}
}
boolean isRising = true;
int sum = 0;
for(int i = m-2; i >= 1; i--){
for(int j = n-2; j >= 1; j--){
if(a[i][j]==0){
a[i][j] = ((a[i][j+1] - 1) <= (a[i+1][j] - 1)) ? (a[i][j+1] - 1) : (a[i+1][j] - 1);
}
}
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n-1; j++){
if(a[i][j] >= a[i][j+1]){
isRising = false;
break;
} else {
sum += a[i][j];
}
}
}
for(int i = 0; i < m-1; i++){
if(a[i][n-1] >= a[i+1][n-1]){
isRising = false;
break;
} else {
sum += a[i][n-1];
}
}
System.out.println(isRising ? sum + a[m-1][n-1] : -1);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
90c1f75f514ab48455c7d4a04d4501ef
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.Scanner;
import static java.lang.Math.max;
public class Myclass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int a[][] = new int[n][m];
for (int i = 0; i <n; i++) {
for (int j = 0; j <m; j++) {
a[i][j] = scan.nextInt();
}
}
int sum = 0;
for (int i = n-1; i >=0; i--) {
for (int j = m-1; j >=0; j--) {
if (a[i][j] == 0) {
a[i][j] = Math.min(a[i + 1][j], a[i][j + 1]) - 1;
}
if(j < m-1 && a[i][j] >= a[i][j+1]){
sum = -1;
break;
}
if(i < n-1 && a[i][j] >= a[i+1][j]){
sum = -1;
break;
}
}
if(sum==-1) break;
}
if(sum == 0){
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
sum += a[i][j];
}
}
}
System.out.println(sum);
}}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
f8b0b3b2ebb51fd6ee536f4f82173ffb
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class increasingmatrix {
public static void main(String[] args) {
FS s=new FS();
int n=s.nextInt();
int m=s.nextInt();
int[][] matrix=new int[n][m];
for(int i=0;i<n;i++)for(int j=0;j<m;j++)matrix[i][j]=s.nextInt();
for(int i=n-2;i>0;i--)for(int j=m-2;j>0;j--) {
if(matrix[i][j]!=0)continue;
int right=matrix[i][j+1];
int below=matrix[i+1][j];
matrix[i][j]=Math.min(right-1, below-1);
}
boolean bad=false;
for(int i=0;i<n-1&&!bad;i++) {
for(int j=0;j<m-1&&!bad;j++) {
if(matrix[i+1][j]<=matrix[i][j]||matrix[i][j+1]<=matrix[i][j]) {
bad=true;break;
}
}
}
if(matrix[n-1][m-1]<=matrix[n-2][m-1]||matrix[n-1][m-1]<=matrix[n-1][m-2])bad=true;
if(bad)System.out.println(-1);
else {
int sum=0;
for(int[] xarr:matrix) {
//System.out.println(Arrays.toString(xarr));
for(int x:xarr)sum+=x;
}
System.out.println(sum);
}
}
static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(Exception e) {}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
883da362156cebaa168894e82769af50
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
//package codeforces;
import java.io.*;
import java.util.InputMismatchException;
/**
* @author tainic on May 2, 2020
*/
public class P1231C {
private static boolean LOCAL;
static {
try { LOCAL = "aurel".equalsIgnoreCase(System.getenv().get("USER")); } catch (Exception e){}
}
private static final String TEST =
"3 3\n" +
"1 2 3\n" +
"2 3 4\n" +
"3 4 2";
void solve(InputReader in, PrintWriter out) {
int rows = in.nextInt();
int cols = in.nextInt();
int[][] a = new int[rows][cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
a[r][c] = in.nextInt();
}
}
long sum = 0;
for (int r = rows - 1; r >= 0; r--) {
for (int c = cols - 1; c >= 0; c--) {
if (a[r][c] == 0) {
int x = Math.min(a[r][c + 1], a[r + 1][c]) - 1;
if (x <= a[r][c - 1] || x <= a[r - 1][c]) {
sum = -1;
break;
}
a[r][c] = x;
}
sum += a[r][c];
}
if (sum == -1) break;
}
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if ((c < cols - 1 && a[r][c] >= a[r][c + 1]) || (r < rows - 1 && a[r][c] >= a[r + 1][c])) {
sum = -1;
break;
}
}
if (sum == -1) break;
}
out.println(sum);
}
//region main + fast io
public static void main(String[] args) throws Exception {
long t = System.currentTimeMillis();
try (
InputReader in = new StreamInputReader(!LOCAL ? System.in : new ByteArrayInputStream(TEST.getBytes()));
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out, 2048), false)
) {
new P1231C().solve(in, out);
}
System.err.println("time: " + (System.currentTimeMillis() - t) + "ms");
}
abstract static class InputReader implements AutoCloseable {
public abstract int read();
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
static class StreamInputReader extends InputReader {
private InputStream stream;
private byte[] buf;
private int curChar, numChars;
public StreamInputReader(InputStream stream) {
this(stream, 2048);
}
public StreamInputReader(InputStream stream, int bufSize) {
this.stream = stream;
this.buf = new byte[bufSize];
}
@Override
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++];
}
@Override
public void close() throws Exception {
stream.close();
}
}
//endregion
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
5955296d2eaedf1762af65eacb28d83c
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int sum = 0;
int ans = 0;
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++){
for (int j = 0; j <m; j++){
matrix[i][j] = sc.nextInt();
sum += matrix[i][j];
}
}
outerloop:
for (int i = n-2; i > 0; i--){
for (int j = m-2; j > 0; j--){
if (matrix[i][j] == 0){
matrix[i][j] = Math.min(matrix[i][j+1], matrix[i+1][j])-1;
sum += matrix[i][j];
if (matrix[i][j] <= matrix[i-1][j] || matrix[i][j] <= matrix[i][j-1]){
break outerloop;
}
}
ans = sum;
}
}
// verification
outerloop:
for (int i = 0; i < n-1; i++){
for (int j = 0; j < m-1; j++){
if ((i < n-1 && matrix[i][j] >= matrix[i+1][j]) || (j < m-1 && matrix[i][j] >= matrix[i][j+1])){
ans = -1;
break outerloop;
}
}
}
outerloop:
for (int i = 0; i < n-1; i++){
if ((matrix[i][0] >= matrix[i+1][0]) || (matrix[i][m-1] >= matrix[i+1][m-1])){
ans = -1;
break outerloop;
}
}
outerloop:
for (int j = 0; j < m-1; j++){
if ((matrix[0][j] >= matrix[0][j+1]) || (matrix[n-1][j] >= matrix[n-1][j+1])){
ans = -1;
break outerloop;
}
}
System.out.println(ans);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
0a832503bd91699d4ffed3d1db70b02c
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
/***
* ██████╗=====███████╗====███████╗====██████╗=
* ██╔══██╗====██╔════╝====██╔════╝====██╔══██╗
* ██║==██║====█████╗======█████╗======██████╔╝
* ██║==██║====██╔══╝======██╔══╝======██╔═══╝=
* ██████╔╝====███████╗====███████╗====██║=====
* ╚═════╝=====╚══════╝====╚══════╝====╚═╝=====
* ============================================
*/
import sun.misc.IOUtils;
import sun.nio.cs.KOI8_U;
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
public class AA implements Runnable {
public void run() {
InputReader sc = new InputReader();
PrintWriter out = new PrintWriter(System.out);
int i=0,j=0,k=0;
int t=0;
//t=sc.nextInt();
for(int testcase = 0;testcase < t; testcase++)
{
}
int rows=sc.nextInt();
int columns=sc.nextInt();
int arr[][]=new int[rows][columns];
long sum=0;
for (i=0;i<rows;i++)
{
for (j=0;j<columns;j++)
{
arr[i][j]=sc.nextInt();
sum+=arr[i][j];
}
}
boolean possible=true;
for (i=1;i<columns;i++)
if (arr[0][i-1]>=arr[0][i])
{
possible=false;
break;
}
if (possible)
for (i=1;i<columns;i++)
if (arr[rows-1][i-1]>=arr[rows-1][i])
{
possible=false;
break;
}
if (possible)
for (i=1;i<rows;i++)
if (arr[i-1][columns-1]>=arr[i][columns-1])
{
possible=false;
break;
}
if (possible)
for (i=1;i<rows;i++)
if (arr[i-1][0]>=arr[i][0])
{
possible=false;
break;
}
if (possible)
abc:for (i=rows-2;i>=0;i--)
{
for (j=columns-2;j>=0;j--)
{
if (arr[i][j]==0)
{
arr[i][j]=Math.min(arr[i+1][j],arr[i][j+1])-1;
sum+=arr[i][j];
}
if (arr[i][j]<0||arr[i][j]>=arr[i+1][j]||arr[i][j]>=arr[i][j+1])
{
possible=false;
break abc;
}
}
}
if (possible)
out.println(sum);
else
out.println("-1");
//================================================================================================================================
out.flush();
out.close();
}
//================================================================================================================================
public static int[] sa(int n,InputReader sc)
{
int inparr[]=new int[n];
for (int i=0;i<n;i++)
inparr[i]=sc.nextInt();
return inparr;
}
public static long gcd(long a,long b){
return (a%b==0l)?b:gcd(b,a%b);
}
private static long lcm(long a, long b)
{
return a * (b / gcd(a, b));
}
public int egcd(int a, int b) {
if (a == 0)
return b;
while (b != 0) {
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
public int countChar(String str, char c)
{
int count = 0;
for(int i=0; i < str.length(); i++)
{ if(str.charAt(i) == c)
count++;
}
return count;
}
static int binSearch(Integer[] inparr, int number){
int left=0,right=inparr.length-1,mid=(left+right)/2,ind=0;
while(left<=right){
if(inparr[mid]<=number){
ind=mid+1;
left=mid+1;
}
else
right=mid-1;
mid=(left+right)/2;
}
return ind;
}
static int binSearch(int[] inparr, int number){
int left=0,right=inparr.length-1,mid=(left+right)/2,ind=0;
while(left<=right){
if(inparr[mid]<=number){
ind=mid+1;
left=mid+1;
}
else
right=mid-1;
mid=(left+right)/2;
}
return ind;
}
static class Pair
{
int a,b;
Pair(int aa,int bb)
{
a=aa;
b=bb;
}
String get()
{
return a+" "+b;
}
String getrev()
{
return b+" "+a;
}
}
static boolean isPrime(long n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
static long factorial(long n)
{
if (n == 0)
return 1;
return n*factorial(n-1);
}
//================================================================================================================================
static class InputReader
{
BufferedReader br;
StringTokenizer st;
public InputReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new AA(),"Main",1<<27).start();
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
148c91f34090b887a1b888e7783a7614
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
/***
* ██████╗=====███████╗====███████╗====██████╗=
* ██╔══██╗====██╔════╝====██╔════╝====██╔══██╗
* ██║==██║====█████╗======█████╗======██████╔╝
* ██║==██║====██╔══╝======██╔══╝======██╔═══╝=
* ██████╔╝====███████╗====███████╗====██║=====
* ╚═════╝=====╚══════╝====╚══════╝====╚═╝=====
* ============================================
*/
import sun.misc.IOUtils;
import sun.nio.cs.KOI8_U;
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
public class AA implements Runnable {
public void run() {
InputReader sc = new InputReader();
PrintWriter out = new PrintWriter(System.out);
int i=0,j=0,k=0;
int t=0;
//t=sc.nextInt();
for(int testcase = 0;testcase < t; testcase++)
{
}
int rows=sc.nextInt();
int columns=sc.nextInt();
int arr[][]=new int[rows][columns];
long sum=0;
for (i=0;i<rows;i++)
{
for (j=0;j<columns;j++)
{
arr[i][j]=sc.nextInt();
sum+=arr[i][j];
}
}
boolean possible=true;
for (i=1;i<columns;i++)
if (arr[rows-1][i-1]>=arr[rows-1][i])
{
possible=false;
break;
}
if (possible)
for (i=1;i<rows;i++)
if (arr[i-1][columns-1]>=arr[i][columns-1])
{
possible=false;
break;
}
if (possible)
abc:for (i=rows-2;i>=0;i--)
{
for (j=columns-2;j>=0;j--)
{
if (arr[i][j]==0)
{
arr[i][j]=Math.min(arr[i+1][j],arr[i][j+1])-1;
sum+=arr[i][j];
}
if (arr[i][j]<0||arr[i][j]>=arr[i+1][j]||arr[i][j]>=arr[i][j+1])
{
possible=false;
break abc;
}
}
}
if (possible)
out.println(sum);
else
out.println("-1");
//================================================================================================================================
out.flush();
out.close();
}
//================================================================================================================================
public static int[] sa(int n,InputReader sc)
{
int inparr[]=new int[n];
for (int i=0;i<n;i++)
inparr[i]=sc.nextInt();
return inparr;
}
public static long gcd(long a,long b){
return (a%b==0l)?b:gcd(b,a%b);
}
private static long lcm(long a, long b)
{
return a * (b / gcd(a, b));
}
public int egcd(int a, int b) {
if (a == 0)
return b;
while (b != 0) {
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
public int countChar(String str, char c)
{
int count = 0;
for(int i=0; i < str.length(); i++)
{ if(str.charAt(i) == c)
count++;
}
return count;
}
static int binSearch(Integer[] inparr, int number){
int left=0,right=inparr.length-1,mid=(left+right)/2,ind=0;
while(left<=right){
if(inparr[mid]<=number){
ind=mid+1;
left=mid+1;
}
else
right=mid-1;
mid=(left+right)/2;
}
return ind;
}
static int binSearch(int[] inparr, int number){
int left=0,right=inparr.length-1,mid=(left+right)/2,ind=0;
while(left<=right){
if(inparr[mid]<=number){
ind=mid+1;
left=mid+1;
}
else
right=mid-1;
mid=(left+right)/2;
}
return ind;
}
static class Pair
{
int a,b;
Pair(int aa,int bb)
{
a=aa;
b=bb;
}
String get()
{
return a+" "+b;
}
String getrev()
{
return b+" "+a;
}
}
static boolean isPrime(long n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
static long factorial(long n)
{
if (n == 0)
return 1;
return n*factorial(n-1);
}
//================================================================================================================================
static class InputReader
{
BufferedReader br;
StringTokenizer st;
public InputReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new AA(),"Main",1<<27).start();
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
9fa46d004b358222a1ee51861aece5af
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
public class Sol1{
public static void main(String[] args) throws IOException {
Reader sc=new Reader();
int n=sc.nextInt();
int m=sc.nextInt();
int[][]a=new int[n][m];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
a[i][j]=sc.nextInt();
int flag=0;
outer :for(int i=n-1;i>0;i--)
{
for(int j=m-1;j>0;j--)
{
if(a[i][j]<a[i][j-1]&&a[i][j-1]!=0&&a[i][j]!=0&&a[i][j]<a[i-1][j]&&a[i-1][j]!=0&&a[i][j]!=0)
{
flag=1;
break outer;
}
if(a[i][j]==0)
{
a[i][j]=Math.min(a[i][j+1]-1, a[i+1][j]-1);
}
}
}
if(flag==1)
System.out.println(-1);
else
{
flag=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if((j<m-1&&a[i][j]>=a[i][j+1])||(j>0&&a[i][j]<=a[i][j-1])||(i<n-1&&a[i][j]>=a[i+1][j])||(i>0&&a[i][j]<=a[i-1][j]))
flag=1;
}
if(flag==1)
System.out.println(-1);
else
{
int sum=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
sum=sum+a[i][j];
System.out.println(sum);
}
}
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
9f2cdbf3f319a0682f49aee4a4f8580f
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.Scanner;
public class C1231 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int R = in.nextInt();
int C = in.nextInt();
int[][] A = new int[R][C];
for (int r=0; r<R; r++) {
for (int c=0; c<C; c++) {
A[r][c] = in.nextInt();
}
}
for (int r=R-1; r>=1; r--) {
for (int c=C-1; c>=1; c--) {
if (A[r][c] == 0) {
A[r][c] = Math.min(A[r+1][c], A[r][c+1])-1;
}
}
}
boolean ok = true;
for (int r=0; r<R; r++) {
for (int c=0; c<C; c++) {
if ((r < R-1 && A[r][c] >= A[r+1][c])
|| (c < C-1 && A[r][c] >= A[r][c+1])) {
ok = false;
}
}
}
long answer;
if (ok) {
answer = 0;
for (int r=0; r<R; r++) {
for (int c=0; c<C; c++) {
answer += A[r][c];
}
}
} else {
answer = -1;
}
System.out.println(answer);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
07ffffc094562ed793292db53df07f3b
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
//package codeforces;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Gabbar {
static boolean isPrime(long n, long i) {
// Base cases
if (n <= 2) {
return (n == 2) ? true : false;
}
if (n % i == 0) {
return false;
}
if (i * i > n) {
return true;
}
// Check for next divisor
return isPrime(n, i + 1);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int total = 0;
boolean flag = false;
int ar[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ar[i][j] = in.nextInt();
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
// if (ar[i][j] >= ar[i][j + 1] || ar[i][j] <= ar[i - 1][j]) {
// flag = true;
// break;
// }
if (ar[i][j] == 0) {
ar[i][j] = ar[i][j - 1] + 1;
while (ar[i][j] + 1 < ar[i][j + 1] && ar[i][j] + 1 < ar[i + 1][j]) {
ar[i][j] = ar[i][j] + 1;
}
}
if((i!=0&&j==m-1)&&ar[i][j]<=ar[i-1][j])
{
flag = true;
break;
}
if ((i!=0&&j!=m-1)&&(ar[i][j] >= ar[i][j + 1] || ar[i][j] <= ar[i - 1][j])) {
flag = true;
break;
}
total += ar[i][j];
//System.out.print(ar[i][j]+" ");
}
if (flag) {
break;
}
// System.out.println();
}
if (flag) {
System.out.println("-1");
} else {
System.out.println(total);
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
824815af43ecff2e7b1b400bb8b7e5bc
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s[]=bf.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1]);
int a[][]=new int[n][m];
for(int i=0;i<n;i++)
{
String s1[]=bf.readLine().split(" ");
for(int j=0;j<m;j++)
{
a[i][j]=Integer.parseInt(s1[j]);
}
}
int sum=0;
boolean sangam=true;
for(int i=0;i<n;i+=n-1)
{
for(int j=0;j<m-1;j++)
{
if(a[i][j+1]>a[i][j])
{
}
else
{
sangam=false;
break;
}
}
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<m;j+=m-1)
{
if(a[i+1][j]>a[i][j])
{
}
else
{
sangam=false;
break;
}
}
}
for(int i=n-2;i>=0;i--)
{
for(int j=m-2;j>=0;j--)
{
if(a[i][j]==0)
{
a[i][j]=(Math.min(a[i][j+1],a[i+1][j]))-1;
}
else
{
if(a[i][j]<a[i][j+1] && a[i][j]<a[i+1][j])
{
}
else
{
sangam=false;
break;
}
}
}
if(sangam==false)
{
break;
}
}
if(sangam)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
sum=sum+a[i][j];
// System.out.print(a[i][j]+" ");
}
// System.out.println();
}
System.out.println(sum);
}
else
{
System.out.println(-1);
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
c094e37ad5b34559f5a8badb1b879618
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s[]=bf.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1]);
int a[][]=new int[n][m];
for(int i=0;i<n;i++)
{
String s1[]=bf.readLine().split(" ");
for(int j=0;j<m;j++)
{
a[i][j]=Integer.parseInt(s1[j]);
}
}
int sum=0;
boolean sangam=true;
for(int i=0;i<n;i+=n-1)
{
for(int j=0;j<m-1;j++)
{
if(a[i][j+1]>a[i][j])
{
}
else
{
sangam=false;
break;
}
}
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<m;j+=m-1)
{
if(a[i+1][j]>a[i][j])
{
}
else
{
sangam=false;
break;
}
}
}
for(int i=n-2;i>=0;i--)
{
for(int j=m-2;j>=0;j--)
{
if(a[i][j]==0)
{
a[i][j]=(Math.min(a[i][j+1],a[i+1][j]))-1;
}
else
{
if(a[i][j]<a[i][j+1] && a[i][j]<a[i+1][j])
{
}
else
{
sangam=false;
break;
}
}
}
if(sangam==false)
{
break;
}
}
if(sangam)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
sum=sum+a[i][j];
// System.out.print(a[i][j]+" ");
}
// System.out.println();
}
System.out.println(sum);
}
else
{
System.out.println(-1);
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
85f6bac70b3cc279443fb18df3579e70
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n , m;
StringTokenizer st = new StringTokenizer(reader.readLine().trim());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
String line;
int arr[][] = new int[n][m];
for(int i =0;i<n;i++){
arr[i] = Arrays.stream(reader.readLine().trim().split("\\s+")).mapToInt(Integer::valueOf).toArray();
}
boolean broken = false;
// printArr(arr);
outer:
for(int i = n-1 ; i>=0 ;i--){
for(int j = m-1; j >=0 ;j--){
int current = arr[i][j];
if((singleBound(i , 0 , n-2) && current >= arr[i+1][j]) ||
(singleBound(j , 0 , m-2) && current>=arr[i][j+1])){
System.out.println("-1");
broken=true;
break outer;
}
if(inBounds(i , j ,1,n-2,1,m-2)) {
if (arr[i][j] == 0) {
int minOfRightDown = Math.min(arr[i + 1][j], arr[i][j + 1]);
int toReplace = minOfRightDown - 1;
if (toReplace <= arr[i - 1][j] || toReplace <= arr[i][j - 1]) {
System.out.println("-1");
broken = true;
break outer;
}
arr[i][j] = toReplace;
}
}
}
}
if(!broken) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += Arrays.stream(arr[i]).sum();
}
System.out.println(sum);
}
}
public static Boolean singleBound(int a , int lowerBound , int upperBound){
if(a >= lowerBound && a <= upperBound){
return true;
}
return false;
}
public static boolean inBounds(int a, int b , int rowLowerBound ,int rowUpperBound , int columnLowerBound ,int columnUpperBound){
if(a >= rowLowerBound && a<=rowUpperBound && b >= columnLowerBound && b <= columnUpperBound){
return true;
}
return false;
}
public static void printArr(int[][] arr){
for(int i = 0; i< arr.length;i++){
for(int j = 0; j< arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
fcad455aea17fb3a3eb1a7ee9c3abb0a
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.Scanner;
public class IncreasingMatrix {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt(),m=sc.nextInt();
int arr[][]=new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j]=sc.nextInt();
}
}
long sum=0;
boolean fail=false;
for(int i=n-1;i>=0;i--)
{
for (int j = m-1; j >=0; j--) {
if(arr[i][j]==0)
{
arr[i][j]= Math.min(arr[i][j+1], arr[i+1][j])-1;
}
if(j!=m-1&&arr[i][j]>= arr[i][j+1]||i!=n-1&&arr[i][j]>= arr[i+1][j])
{
fail=true;
break;
}
sum+=arr[i][j];
}
}
if(fail)System.out.println(-1);
else System.out.println(sum);
sc.close();
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
6a0f5b101466d677f4333e8ef85a2a6f
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
//static final long MOD = 998244353;
static final long MOD = 1000000007;
static boolean[] visited;
public static void main(String[] args) throws IOException {
FastScanner sc=new FastScanner();
int N = sc.nextInt();
int M = sc.nextInt();
int[][] nums = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
nums[i][j] = sc.nextInt();
}
for (int i = N-2; i >= 1; i--) {
for (int j = M-2; j >= 1; j--) {
if (nums[i][j] == 0)
nums[i][j] = Math.min(nums[i+1][j],nums[i][j+1])-1;
}
}
//Check if valid
for (int i = 0; i < N; i++) {
int n = nums[i][0];
for (int j = 1; j < M; j++) {
if (nums[i][j] > n) {
n = nums[i][j];
} else {
System.out.println(-1);
return;
}
}
}
for (int j = 0; j < M; j++) {
int m = nums[0][j];
for (int i = 1; i < N; i++) {
if (nums[i][j] > m) {
m = nums[i][j];
} else {
System.out.println(-1);
return;
}
}
}
//Valid
int sum = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
sum += nums[i][j];
}
}
System.out.println(sum);
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
else
return gcd(b,a%b);
}
public static int[][] sort(int[][] array) {
//Sort an array (immune to quicksort TLE)
Random rgen = new Random();
for (int i = 0; i < array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int[] temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
Arrays.sort(array, new Comparator<int[]>() {
@Override
public int compare(int[] arr1, int[] arr2) {
if (arr1[0] != arr2[0])
return arr1[0]-arr2[0];
else
return arr1[1]-arr2[1];
}
});
return array;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
b3a10eaeb3ade541499d807810f07567
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class x1231C
{
public static void main(String omkar[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[][] grid = new int[N][M];
for(int i=0; i < N; i++)
grid[i] = readArr(M, infile, st);
for(int r=N-2; r > 0; r--)
for(int c=M-2; c > 0; c--)
if(grid[r][c] == 0)
{
int rmin = grid[r][0];
int rmax = grid[r][0];
for(int i=1; i < M; i++)
if(grid[r][i] != 0)
{
rmin = Math.min(rmin, grid[r][i]);
rmax = Math.max(rmax, grid[r][i]);
}
int cmin = grid[0][c];
int cmax = grid[0][c];
for(int i=1; i < N; i++)
if(grid[i][c] != 0)
{
cmin = Math.min(cmin, grid[i][c]);
cmax = Math.max(cmax, grid[i][c]);
}
grid[r][c] = Math.min(grid[r+1][c], grid[r][c+1])-1;
}
//verify
for(int r=0; r < N; r++)
for(int c=0; c < M-1; c++)
if(grid[r][c] >= grid[r][c+1])
{
System.out.println(-1);
return;
}
for(int c=0; c < M; c++)
for(int r=0; r < N-1; r++)
if(grid[r][c] >= grid[r+1][c])
{
System.out.println(-1);
return;
}
long res = 0L;
for(int r=0; r < N; r++)
for(int x: grid[r])
res += x;
System.out.println(res);
}
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
a370e3e270281f53d0b69a7f5ad97884
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class x1231C
{
public static void main(String omkar[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[][] grid = new int[N][M];
for(int i=0; i < N; i++)
grid[i] = readArr(M, infile, st);
for(int r=N-2; r > 0; r--)
for(int c=M-2; c > 0; c--)
if(grid[r][c] == 0)
grid[r][c] = Math.min(grid[r+1][c], grid[r][c+1])-1;
//verify
for(int r=0; r < N; r++)
for(int c=0; c < M-1; c++)
if(grid[r][c] >= grid[r][c+1])
{
System.out.println(-1);
return;
}
for(int c=0; c < M; c++)
for(int r=0; r < N-1; r++)
if(grid[r][c] >= grid[r+1][c])
{
System.out.println(-1);
return;
}
long res = 0L;
for(int r=0; r < N; r++)
for(int x: grid[r])
res += x;
System.out.println(res);
}
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
1b7bd29523f38580d1d28956fe6e58ae
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package practice;
import java.util.Scanner;
/**
*
* @author 97156
*/
public class Practice {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int[][] array = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
array[i][j] = scan.nextInt();
}
}
int sum = 0;
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if ((array[i][j] > array[i - 1][j] && array[i][j] > array[i][j - 1]) || (array[i][j] == 0)) {
} else {
sum = -1;
}
}
}
if (sum == 0) {
for (int i = n-1; i >= 0; i--) {
for (int j = m-1; j >=0; j--) {
if (array[i][j] == 0) {
if (array[i + 1][j] > array[i][j + 1]) {
array[i][j] = array[i][j + 1] - 1;
} else {
array[i][j] = array[i + 1][j] - 1;
}
if (!(array[i][j] > array[i - 1][j] && array[i][j] > array[i][j - 1])) {
sum = -1;
break;
}
}
if (sum == -1) {
break;
} else {
sum += array[i][j];
}
}
}
}
System.out.println(sum);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
58e3a40621571227e528b4631c83acf9
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class MaxSum{
public static void main(String[] args) throws IOException {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
// System.out.println(n);
int m = scn.nextInt();
// System.out.println(m);
int[][] a = new int[n][m];
int sum = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
a[i][j] = scn.nextInt();
sum+=a[i][j];
}
}
int flag = 0;
o:
for(int i = n-2; i>=1; i--){
for(int j = m-2; j>=1; j--){
if(a[i][j] == 0){
a[i][j] = Math.min(a[i+1][j], a[i][j+1])-1;
sum+=a[i][j];
if(a[i][j] <= a[i-1][j] || a[i][j] <= a[i][j-1]){
flag = -1;
break o;
}
}
if(i == 1 && j == 1){
if(flag == 0)
flag = sum;
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(i+1 < n && a[i][j] >= a[i+1][j]){
flag = -1;
}
if(j+1 < m && a[i][j] >= a[i][j+1]){
flag = -1;
}
}
}
System.out.println(flag);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
b47e9b3202d1e56be61bfd1a365e5bfd
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
//package codeForces;
import java.util.Arrays;
import java.io.FileNotFoundException;
import java.util.*;
import java.math.BigInteger;
import java.util.Scanner;
import java.math.*;
public class Test {
static int[][] array;
static int getLeft(int x, int y) {
for (int j = y - 1; j >= 0; j--) {
if (array[x][j] != 0) {
return array[x][j];
}
}
return -1;
}
static int getRight(int x, int y) {
for (int j = y; j <= array[x].length; j++) {
if (array[x][j] != 0) {
return array[x][j];
}
}
return 8001;
}
static int getTop(int x, int y) {
for (int j = x - 1; j >= 0; j--) {
if (array[j][y] != 0) {
return array[j][y];
}
}
return -1;
}
static int getBottom(int x, int y) {
for (int j = x; j <= array.length; j++) {
if (array[j][y] != 0) {
return array[j][y];
}
}
return 8001;
}
static boolean checkVertical(int x) {
int A = 0;
int B = 0;
for (int j = 0; j < array[x].length - 1; j++) {
B = array[j + 1][x];
A = array[j][x];
if (B - A <= 0) {
return false;
}
}
return true;
}
static boolean checkHorizontal(int x) {
int A = 0;
int B = 0;
for (int j = 0; j < array[x].length - 1; j++) {
B = array[x][j + 1];
A = array[x][j];
if (B - A <= 0) {
return false;
}
}
return true;
}
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
int stackSize = 0;
int sum = 0;
int temporaryResult = 0;
int maximumOfSmallest = 0;
int minimumOfLargest = 0;
int A, B, C, D;
A = B = C = D = 0;
Integer n = input.nextInt();
Integer m = input.nextInt();
array = new int[n][m];
Stack row = new Stack();
Stack column = new Stack();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
Integer h = input.nextInt();
if (h == 0) {
A = (int) row.push(i);
B = (int) column.push(j);
}
array[i][j] = h;
}
}
if (row.isEmpty() && column.isEmpty()) {
for (int i = 0; i < n; i++) {
if (checkHorizontal(i) == false) {
sum = -1;
}
}
for (int i = 0; i < m; i++) {
if (checkVertical(i) == false) {
sum = -1;
}
}
}
stackSize = row.size();
for (int i = 0; i < stackSize; i++) {
C = (int) row.pop();
D = (int) column.pop();
maximumOfSmallest = Math.min(getRight(C, D), getBottom(C, D));
minimumOfLargest = Math.max(getLeft(C, D), getTop(C, D));
if (getRight(C, D) == getBottom(C, D)
&& getLeft(C, D) == getTop(C, D)) {
maximumOfSmallest = getBottom(C, D);
minimumOfLargest = getTop(C, D);
}
if (maximumOfSmallest - minimumOfLargest > 1) {
if (maximumOfSmallest > minimumOfLargest) {
// System.out.println("I'm in the first case");
temporaryResult = maximumOfSmallest - 1;
array[C][D] = temporaryResult;
}
// } else if (maximumOfSmallest == minimumOfLargest) {
// // System.out.println("I'm in the second case");
// temporaryResult = maximumOfSmallest - 1;
// array[C][D] = temporaryResult;
} else {
sum = -1;
break;
}
}
if (sum == -1) {
sum = -1;
} else {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
sum += array[i][j];
}
}
}
System.out.println(sum);
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
de9c3bb435f0de1231f6f53c6707665e
|
train_001.jsonl
|
1569143100
|
In this problem, a $$$n \times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
256 megabytes
|
import java.util.Scanner;
public class IncreasingMatrix {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt(),m=sc.nextInt();
int arr[][]=new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j]=sc.nextInt();
}
}
long sum=0;
boolean fail=false;
for(int i=n-1;i>=0;i--)
{
for (int j = m-1; j >=0; j--) {
if(arr[i][j]==0)
{
arr[i][j]= Math.min(arr[i][j+1], arr[i+1][j])-1;
}
if(j!=m-1&&arr[i][j]>= arr[i][j+1]||i!=n-1&&arr[i][j]>= arr[i+1][j])
{
fail=true;
break;
}
sum+=arr[i][j];
}
}
if(fail)System.out.println(-1);
else System.out.println(sum);
sc.close();
}
}
|
Java
|
["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"]
|
2 seconds
|
["144", "30", "-1", "-1"]
|
NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Java 8
|
standard input
|
[
"greedy"
] |
ebc5c1fe4b4a253647b82a608e8a084f
|
The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \dots, a_{i,m}$$$ ($$$0 \le a_{i,j} \le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.
| 1,100 |
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
standard output
| |
PASSED
|
5898a63421b373b34d4e3ad5a12bfb46
|
train_001.jsonl
|
1346081400
|
The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one.The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city a is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city a to any other city. For that some roads may have to be inversed.Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.StringTokenizer;
public class D {
// 135 div2
static int[] red, green;
static ArrayList<Integer>[] adList;
static int totalRed = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
adList = new ArrayList[n + 1];
for (int i = 0; i <= n; i++)
adList[i] = new ArrayList<Integer>();
StringTokenizer st;
int f, t;
for (int i = 0; i < n - 1; i++) {
st = new StringTokenizer(br.readLine());
f = Integer.parseInt(st.nextToken());
t = Integer.parseInt(st.nextToken());
adList[f].add(t);
adList[t].add(-f);
}
red = new int[n+1];
green = new int[n+1];
dfs(1, 0, 0, 0);
int min = Integer.MAX_VALUE;
for(int i = 1; i <= n; i++)
{
int r = (totalRed - red[i]) + green[i];
min = Math.min(min, r);
}
StringBuilder sb = new StringBuilder();
for(int i = 1; i <= n; i++)
{
int r = (totalRed - red[i]) + green[i];
if(r == min)
sb.append(' ').append(i);
}
System.out.println(min);
System.out.println(sb.substring(1));
}
private static void dfs(int curr, int last, int r, int g) {
red[curr] = r;
green[curr] = g;
for (int k : adList[curr])
if (Math.abs(k) != last) {
if (k > 0) {
dfs(k, curr, r, g + 1);
} else {
dfs(-k, curr, r + 1, g);
totalRed++;
}
}
}
}
|
Java
|
["3\n2 1\n2 3", "4\n1 4\n2 4\n3 4"]
|
3 seconds
|
["0\n2", "2\n1 2 3"]
| null |
Java 6
|
standard input
|
[
"dp",
"dfs and similar",
"trees",
"graphs"
] |
fb5c6182b9cad133d8b256f8e72e7e3b
|
The first input line contains integer n (2 ≤ n ≤ 2·105) — the number of cities in Treeland. Next n - 1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers si, ti (1 ≤ si, ti ≤ n; si ≠ ti) — the numbers of cities, connected by that road. The i-th road is oriented from city si to city ti. You can consider cities in Treeland indexed from 1 to n.
| 1,700 |
In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order.
|
standard output
| |
PASSED
|
209dc3a96b16c78f02cb4710c933c6ac
|
train_001.jsonl
|
1600526100
|
An agent called Cypher is decrypting a message, that contains a composite number $$$n$$$. All divisors of $$$n$$$, which are greater than $$$1$$$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
|
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 sort(int arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(int arr[],int l1,int r1,int l2,int r2) {
int tmp[]=new int[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(long arr[],int l1,int r1,int l2,int r2) {
long tmp[]=new long[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
static int primes[];
public static void main(String args[]) throws IOException {
Scan input=new Scan();
sieve();
StringBuilder ans=new StringBuilder("");
int test=input.scanInt();
for(int tt=1;tt<=test;tt++) {
int n=input.scanInt();
solve(n);
}
System.out.println(ans);
}
public static void solve(int n) {
ArrayList<Integer> div=new ArrayList<>();
int lim=(int)Math.sqrt(n);
for (int i=1; i<=lim; i++) {
if (n%i==0) {
if (n/i == i) {
div.add(i);
}
else {
div.add(i);
div.add(n/i);
}
}
}
div.remove(new Integer(1));
ArrayList<Integer> prime_div=new ArrayList<>();
for(int i=0;i<primes.length;i++) {
int cnt=0;
while(n%primes[i]==0) {
n/=primes[i];
cnt++;
}
if(cnt>0) {
prime_div.add(primes[i]);
}
}
if(n!=1) {
prime_div.add(n);
}
StringBuilder ans=new StringBuilder("");
if(prime_div.size()==1) {
for(int i=0;i<div.size();i++) {
ans.append(div.get(i)+" ");
}
System.out.println(ans+"\n"+0);
return;
}
boolean taken[]=new boolean[div.size()];
ArrayList<Integer> arrli1=new ArrayList<>();
for(int i=0;i<prime_div.size();i++) {
arrli1.add(prime_div.get(i));
for(int j=0;j<div.size();j++) {
if(taken[j]) {
continue;
}
if(div.get(j)%prime_div.get(i)==0 && div.get(j)%prime_div.get((i+1)%prime_div.size())==0) {
arrli1.add(div.get(j));
taken[j]=true;
break;
}
}
}
ArrayList<Integer> tmp=new ArrayList<>();
for(int i=0;i<taken.length;i++) {
if(arrli1.contains(div.get(i))) {
taken[i]=true;
}
}
for(int i=0;i<arrli1.size();i++) {
tmp.add(arrli1.get(i));
if(!prime_div.contains(arrli1.get(i))) {
continue;
}
for(int j=0;j<div.size();j++) {
if(taken[j]) {
continue;
}
if(div.get(j)%arrli1.get(i)==0) {
tmp.add(div.get(j));
taken[j]=true;
}
}
}
int cnt=0;
for(int i=0;i<tmp.size();i++) {
ans.append(tmp.get(i)+" ");
if(gcd(tmp.get(i),tmp.get((i+1)%tmp.size()))==1) {
cnt++;
}
// System.out.println(gcd(tmp.get(i),tmp.get((i+1)%tmp.size())));
}
System.out.println(ans+"\n"+cnt);
}
public static void sieve() {
boolean sieve[]=new boolean[(int)Math.sqrt(1000000000)];
primes=new int[3401];
int indx=0;
for(int i=2;i<sieve.length;i++) {
if(!sieve[i]) {
primes[indx]=i;
indx++;
for(int j=2;i*j<sieve.length;j++) {
sieve[i*j]=true;
}
}
}
// System.out.println(indx);
}
public static int gcd(int a,int n) {
int q,r1=n,r2=a,r,t1=0,t2=1,t;
while(true) {
q=r1/r2;
r=r1%r2;
t=t1-(q*t2);
r1=r2;
r2=r;
t1=t2;
t2=t;
if(r2==0) {
break;
}
}
return r1;
}
}
|
Java
|
["3\n6\n4\n30"]
|
1 second
|
["2 3 6 \n1\n2 4 \n0\n2 30 6 3 15 5 10 \n0"]
|
NoteIn the first test case $$$6$$$ has three divisors, which are greater than $$$1$$$: $$$2, 3, 6$$$. Regardless of the initial order, numbers $$$2$$$ and $$$3$$$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $$$2, 6, 3, 6$$$, and every two adjacent numbers are not coprime.In the second test case $$$4$$$ has two divisors greater than $$$1$$$: $$$2, 4$$$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.In the third test case all divisors of $$$30$$$ greater than $$$1$$$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation",
"number theory",
"math"
] |
406f8f662d2013d87b36dacca663bef5
|
The first line contains an integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. Next $$$t$$$ lines describe each test case. In a single line of each test case description, there is a single composite number $$$n$$$ $$$(4 \le n \le 10^9)$$$ — the number from the message. It's guaranteed that the total number of divisors of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.
| 2,100 |
For each test case in the first line output the initial order of divisors, which are greater than $$$1$$$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message. If there are different possible orders with a correct answer, print any of them.
|
standard output
| |
PASSED
|
f8a6dbd451f14af02f6a50fdc953b16e
|
train_001.jsonl
|
1600526100
|
An agent called Cypher is decrypting a message, that contains a composite number $$$n$$$. All divisors of $$$n$$$, which are greater than $$$1$$$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args) {
Scanner inp;
try {
inp = new Scanner(new File("input.txt"));
} catch (FileNotFoundException e){
inp = new Scanner(System.in);
}
int Tests = inp.nextInt();
while (Tests-- > 0) {
int n = inp.nextInt();
int m = n;
ArrayList<Integer> primes = new ArrayList<Integer>();
ArrayList<Integer> divisors = new ArrayList<Integer>();
for (int i = 1; i <= Math.sqrt(m); i++) {
if (m % i != 0) {
continue;
} else {
divisors.add(i);
if (i*i != m) {
divisors.add(m/i);
}
}
}
Collections.sort(divisors);
divisors.remove(0);
int sumCount = 0;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
primes.add(i);
sumCount += cnt;
}
}
if (n > 1) {
primes.add(n);
sumCount++;
}
if (primes.size() == 2 && sumCount == 2) {
int p1 = primes.get(0);
int p2 = primes.get(1);
System.out.println(p1 + " " + p2 + " " + (p1*p2));
System.out.println(1);
} else {
if (primes.size() == 1) {
int p = primes.get(0);
for (int i = p; i <= m; i *= p) {
System.out.print(i + " ");
if (i == m) break;
}
} else if (primes.size() == 2) {
int p1 = primes.get(0);
for (int i = 0; i < divisors.size(); i++) {
if (divisors.get(i) != m && divisors.get(i) % p1 == 0) {
System.out.print(divisors.get(i) + " ");
}
}
for (int i = 0; i < divisors.size(); i++) {
if (divisors.get(i) != m && divisors.get(i) % p1 != 0) {
System.out.print(divisors.get(i) + " ");
}
}
System.out.print(m);
} else {
ArrayList<Integer> mulPrimes = new ArrayList<Integer>();
int sz = primes.size();
for (int i = 0; i < sz; i++) {
mulPrimes.add(primes.get(i));
mulPrimes.add(primes.get(i)*primes.get((i + 1) % sz));
}
ArrayList<ArrayList<Integer> > res =
new ArrayList<ArrayList<Integer> >();
for (int i = 0; i < sz*2; i++) {
res.add(new ArrayList<Integer>());
}
for (int i = 1; i < sz*2; i += 2) {
int p = mulPrimes.get(i);
for (int j = divisors.size() - 1; j >= 0; j--) {
if (divisors.get(j) % p == 0) {
res.get(i).add(divisors.get(j));
divisors.remove(j);
}
}
}
for (int i = 0; i < sz*2; i+=2) {
int p = mulPrimes.get(i);
for (int j = divisors.size() - 1; j >= 0; j--) {
if (divisors.get(j) % p == 0) {
res.get(i).add(divisors.get(j));
divisors.remove(j);
}
}
}
for (ArrayList<Integer> arrayList : res) {
for (int x : arrayList) {
System.out.print(x + " ");
}
}
}
System.out.println("\n" + 0);
}
}
}
}
|
Java
|
["3\n6\n4\n30"]
|
1 second
|
["2 3 6 \n1\n2 4 \n0\n2 30 6 3 15 5 10 \n0"]
|
NoteIn the first test case $$$6$$$ has three divisors, which are greater than $$$1$$$: $$$2, 3, 6$$$. Regardless of the initial order, numbers $$$2$$$ and $$$3$$$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $$$2, 6, 3, 6$$$, and every two adjacent numbers are not coprime.In the second test case $$$4$$$ has two divisors greater than $$$1$$$: $$$2, 4$$$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.In the third test case all divisors of $$$30$$$ greater than $$$1$$$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation",
"number theory",
"math"
] |
406f8f662d2013d87b36dacca663bef5
|
The first line contains an integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. Next $$$t$$$ lines describe each test case. In a single line of each test case description, there is a single composite number $$$n$$$ $$$(4 \le n \le 10^9)$$$ — the number from the message. It's guaranteed that the total number of divisors of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.
| 2,100 |
For each test case in the first line output the initial order of divisors, which are greater than $$$1$$$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message. If there are different possible orders with a correct answer, print any of them.
|
standard output
| |
PASSED
|
ef565d46a439978821f8f8e52016501d
|
train_001.jsonl
|
1600526100
|
An agent called Cypher is decrypting a message, that contains a composite number $$$n$$$. All divisors of $$$n$$$, which are greater than $$$1$$$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class SolutionE extends Thread {
private static final FastReader scanner = new FastReader();
private static final PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
new Thread(null, new SolutionE(), "Main", 1 << 26).start();
}
public static List<Integer> getDivisorsOfN(int n) {
List<Integer> result = new ArrayList<>();
result.add(n);
for (int i = 2; ; i++) {
if ((long) i * i > n) {
break;
}
if (n % i == 0) {
result.add(i);
if (n / i != i) {
result.add(n / i);
}
}
}
return result;
}
public static List<Integer> getPrimeFactorsOfNumber(int n) {
List<Integer> result = new ArrayList<>();
for (int i = 2; ; i++) {
if ((long) i * i > n) {
break;
}
if (n % i == 0) {
result.add(i);
}
while (n % i == 0) {
n /= i;
}
}
if (n != 1) {
result.add(n);
}
return result;
}
private static void solve() {
int n = scanner.nextInt();
List<Integer> divisors = getDivisorsOfN(n);
List<Integer> primeFactors = getPrimeFactorsOfNumber(n);
if (primeFactors.size() == 1) {
for (Integer divisor : divisors) {
out.print(divisor + " ");
}
out.println();
out.println("0");
return;
} else if (primeFactors.size() == 2 && divisors.size() == 3) {
for (int divisor: divisors) {
out.print(divisor + " ");
}
out.println();
out.println("1");
return;
}
boolean[] marked = new boolean[divisors.size()];
for (int i = 0; i < primeFactors.size(); i++) {
int prevEdge = primeFactors.get(i) * primeFactors.get((i+primeFactors.size() - 1) % primeFactors.size());
int nextEdge = primeFactors.get(i) * primeFactors.get((i+1) % primeFactors.size());
int nextIndex = -1;
for (int j = 0; j < divisors.size(); j++) {
int divisor = divisors.get(j);
if (!marked[j] && divisor != nextEdge && divisor != prevEdge && divisor % primeFactors.get(i) == 0) {
out.print(divisor + " ");
marked[j] = true;
}
if (divisor == nextEdge) {
nextIndex = j;
}
}
if (i < primeFactors.size() - 1 || primeFactors.size() > 2) {
out.print(nextEdge + " ");
marked[nextIndex] = true;
}
}
out.println();
out.println(0);
}
public void run() {
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
solve();
}
out.close();
}
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;
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
}
}
|
Java
|
["3\n6\n4\n30"]
|
1 second
|
["2 3 6 \n1\n2 4 \n0\n2 30 6 3 15 5 10 \n0"]
|
NoteIn the first test case $$$6$$$ has three divisors, which are greater than $$$1$$$: $$$2, 3, 6$$$. Regardless of the initial order, numbers $$$2$$$ and $$$3$$$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $$$2, 6, 3, 6$$$, and every two adjacent numbers are not coprime.In the second test case $$$4$$$ has two divisors greater than $$$1$$$: $$$2, 4$$$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.In the third test case all divisors of $$$30$$$ greater than $$$1$$$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation",
"number theory",
"math"
] |
406f8f662d2013d87b36dacca663bef5
|
The first line contains an integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. Next $$$t$$$ lines describe each test case. In a single line of each test case description, there is a single composite number $$$n$$$ $$$(4 \le n \le 10^9)$$$ — the number from the message. It's guaranteed that the total number of divisors of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.
| 2,100 |
For each test case in the first line output the initial order of divisors, which are greater than $$$1$$$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message. If there are different possible orders with a correct answer, print any of them.
|
standard output
| |
PASSED
|
66530df3c4d77f3786f0953f851b8966
|
train_001.jsonl
|
1600526100
|
An agent called Cypher is decrypting a message, that contains a composite number $$$n$$$. All divisors of $$$n$$$, which are greater than $$$1$$$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.Vector;
/*
2 1
1 2 0
*/
public class E {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int t=fs.nextInt();
outer: while(t-->0)
{
int n=fs.nextInt();
Vector<Integer> d=new Vector<>();
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
d.add(i);
if(i!=n/i)
d.add(n / i);
}
}
d.add(n);
Collections.sort(d);
if (d.size() == 3 && isprime(d.get(0)) && isprime(d.get(1))) {
for (Integer x : d) out.print(x+" ");
out.println();
out.println(1);
continue;
}
HashMap<Integer, Boolean> used=new HashMap<>();
Vector<Integer> primes=new Vector<>();
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
primes.add(i);
while (n % i == 0) n /= i;
}
}
if (n > 1) primes.add(n);
Vector<Integer> connect=new Vector<>(primes.size());
for (int i = 0; i < (int)primes.size(); ++i) {
int p = primes.get(i), q = primes.get((i + 1)% primes.size());
for (int j = 0; j < (int)d.size(); ++j) {
if (!used.containsKey(d.get(j))&& d.get(j) % p == 0 && d.get(j)% q == 0) {
used.put(d.get(j), true);
connect.add(i,d.get(j));
break;
}
}
}
for (int i = 0; i < (int)primes.size(); ++i) {
int p = primes.get(i);
used.put(p, true);
out.print(p+" ");
for (int j = 0; j < (int)d.size(); ++j) {
if (!used.containsKey(d.get(j))&& d.get(j) % p == 0) {
used.put(d.get(j), true);
out.print(d.get(j)+" ");
}
}
if (primes.size() > 1) {
out.print(connect.get(i)+" ");
}
}
out.println();
out.println(0);
}
out.close();
}
static HashMap<Integer,ArrayList<Integer>> sieveOfEratosthenes(int n,ArrayList<Integer> primefac)
{
HashSet<Integer> used = new HashSet<>();
HashMap<Integer,ArrayList<Integer>> fac=new HashMap<>();
for (Integer b : primefac) {
fac.put(b, new ArrayList<>());
}
for(Integer p: primefac)
{
for(int i = p*p; i <= n; i += p)
{
if(!used.contains(i)&&n%i==0)
fac.get(p).add(i);
used.add(i);
}
}
return fac;
}
private static ArrayList<Integer> primefactors(int n) {
ArrayList<Integer> ans=new ArrayList<Integer>();
for (int i = 2; i <=Math.sqrt(n); i++) {
if(n%i==0&&isprime(i))
ans.add(i);
if(n%i==0&&isprime(n/i)&&i!=n/i)
ans.add(n/i);
}
return ans;
}
private static boolean isprime(int i) {
for (int j = 2; j <= Math.sqrt(i); j++) {
if(i%j==0)
return false;
}
return true;
}
static class Node {
}
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["3\n6\n4\n30"]
|
1 second
|
["2 3 6 \n1\n2 4 \n0\n2 30 6 3 15 5 10 \n0"]
|
NoteIn the first test case $$$6$$$ has three divisors, which are greater than $$$1$$$: $$$2, 3, 6$$$. Regardless of the initial order, numbers $$$2$$$ and $$$3$$$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $$$2, 6, 3, 6$$$, and every two adjacent numbers are not coprime.In the second test case $$$4$$$ has two divisors greater than $$$1$$$: $$$2, 4$$$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.In the third test case all divisors of $$$30$$$ greater than $$$1$$$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation",
"number theory",
"math"
] |
406f8f662d2013d87b36dacca663bef5
|
The first line contains an integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. Next $$$t$$$ lines describe each test case. In a single line of each test case description, there is a single composite number $$$n$$$ $$$(4 \le n \le 10^9)$$$ — the number from the message. It's guaranteed that the total number of divisors of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.
| 2,100 |
For each test case in the first line output the initial order of divisors, which are greater than $$$1$$$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message. If there are different possible orders with a correct answer, print any of them.
|
standard output
| |
PASSED
|
a8f3a3eb6747e72e14ef21e5e29e2356
|
train_001.jsonl
|
1600526100
|
An agent called Cypher is decrypting a message, that contains a composite number $$$n$$$. All divisors of $$$n$$$, which are greater than $$$1$$$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
|
256 megabytes
|
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Test1419E {
static final BufferedInputStream IN = new BufferedInputStream(System.in);
static final PrintWriter OUT = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args) throws IOException {
int t = nextInt();
while (t > 0) {
int n = nextInt();
Map<Integer, Integer> primes = getPrimes(n);
//System.out.println(primes);
int k = 0;
int[][] arr = new int[primes.size()][];
for (Map.Entry<Integer, Integer> entry : primes.entrySet()) {
arr[k] = new int[entry.getValue() + 1];
arr[k][0] = 1;
for (int i = 1; i <= entry.getValue(); i++) {
arr[k][i] = entry.getKey() * arr[k][i - 1];
}
k++;
}
int[] counters = new int[primes.size()];
int p = 0, last = 0;
while (p < counters.length) {
while (next(counters, p, arr)) {
int divider = getDivider(counters, arr);
if (last == 0 && counters[0] == 1 && counters[counters.length - 1] == 1) {
last = divider;
} else {
OUT.print(divider);
OUT.print(' ');
}
}
p++;
Arrays.fill(counters, 0);
}
OUT.println(last);
OUT.println(primes.size() == 2 && primes.values().stream().mapToInt(value -> value).sum() == 2 ? 1 : 0);
t--;
}
OUT.flush();
}
static boolean next(int[] counters, int p, int[][] arr) {
int k = p;
while (k < counters.length) {
if (counters[k] + 1 < arr[k].length) {
counters[k]++;
return true;
} else {
counters[k] = (k == p) ? 1 : 0;
k++;
}
}
return false;
}
private static int getDivider(int[] counters, int[][] arr) {
int result = 1;
for (int i = 0; i < counters.length; i++) {
result *= arr[i][counters[i]];
}
return result;
}
private static Map<Integer, Integer> getPrimes(int n) {
Map<Integer, Integer> dividers = new HashMap<>();
int[] basis = {2, 3, 5};
int[] inc = {4, 2, 4, 2, 4, 6, 2, 6};
for (int b : basis) {
int count = 0;
while (n % b == 0) {
count++;
n /= b;
}
if (count > 0) {
dividers.put(b, count);
}
}
int k = 7, i = 0, count = 0;
while (k * k <= n) {
if (n % k == 0) {
count++;
n /= k;
} else {
if (count > 0) {
dividers.put(k, count);
count = 0;
}
k += inc[i];
if (i < 7) i++;
else i = 0;
}
}
if (count > 0) {
if (n == k) {
count++;
n = 0;
}
dividers.put(k, count);
}
if (n > 1) {
dividers.put(n, 1);
}
return dividers;
}
static int nextInt() throws IOException {
int result;
int sign = 1;
int c = IN.read();
while (c != '-' && (c < '0' || c > '9')) {
c = (char) IN.read();
}
if (c == '-') {
sign = -1;
c = (char) IN.read();
}
result = c - '0';
c = (char) IN.read();
while (c >= '0' && c <= '9') {
result = result * 10 + c - '0';
c = (char) IN.read();
}
return result * sign;
}
}
|
Java
|
["3\n6\n4\n30"]
|
1 second
|
["2 3 6 \n1\n2 4 \n0\n2 30 6 3 15 5 10 \n0"]
|
NoteIn the first test case $$$6$$$ has three divisors, which are greater than $$$1$$$: $$$2, 3, 6$$$. Regardless of the initial order, numbers $$$2$$$ and $$$3$$$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $$$2, 6, 3, 6$$$, and every two adjacent numbers are not coprime.In the second test case $$$4$$$ has two divisors greater than $$$1$$$: $$$2, 4$$$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.In the third test case all divisors of $$$30$$$ greater than $$$1$$$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation",
"number theory",
"math"
] |
406f8f662d2013d87b36dacca663bef5
|
The first line contains an integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. Next $$$t$$$ lines describe each test case. In a single line of each test case description, there is a single composite number $$$n$$$ $$$(4 \le n \le 10^9)$$$ — the number from the message. It's guaranteed that the total number of divisors of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.
| 2,100 |
For each test case in the first line output the initial order of divisors, which are greater than $$$1$$$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message. If there are different possible orders with a correct answer, print any of them.
|
standard output
| |
PASSED
|
7cbe136876bcb1542c5c109824174f29
|
train_001.jsonl
|
1600526100
|
An agent called Cypher is decrypting a message, that contains a composite number $$$n$$$. All divisors of $$$n$$$, which are greater than $$$1$$$, are placed in a circle. Cypher can choose the initial order of numbers in the circle.In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.util.LinkedList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Asgar Javadov
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
EDecryption solver = new EDecryption();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class EDecryption {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
List<IntPair> primeFactors = NumberTheory.primeFactorization(n);
LinkedList<Integer> list = new LinkedList<>();
for (int i = primeFactors.size() - 1; i >= 0; --i) {
int current = primeFactors.get(i).first;
int next = (i < primeFactors.size() - 1) ? primeFactors.get(i + 1).first : -1;
LinkedList<Integer> currentList = new LinkedList<>();
if (next != -1) {
int product = current * next;
if (product < n) {
currentList.add(product);
}
}
for (int e : list) {
for (int j = 1, currentPower = 1; j <= primeFactors.get(i).second; ++j) {
currentPower *= current;
if (e == next && j == 1) continue;
int product = e * currentPower;
if (product < n) {
currentList.add(product);
}
}
}
for (int j = 1, currentPower = 1; j <= primeFactors.get(i).second; ++j) {
currentPower *= current;
if (currentPower < n) {
currentList.add(currentPower);
}
}
list.addAll(currentList);
}
list.addFirst(n);
out.println(list);
if (primeFactors.size() == 2 && primeFactors.get(0).second == 1 && primeFactors.get(1).second == 1) {
out.one();
} else {
out.zero();
}
}
}
static class NumberTheory {
public static List<IntPair> primeFactorization(int n) {
List<IntPair> result = new ArrayList<>();
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
IntPair pair = IntPair.of(i, 0);
while (n % i == 0) {
pair.second++;
n /= i;
}
result.add(pair);
}
}
if (n > 1) {
result.add(IntPair.of(n, 1));
}
return result;
}
}
static class OutputWriter extends PrintWriter {
public OutputWriter(OutputStream outputStream) {
super(outputStream);
}
public OutputWriter(Writer writer) {
super(writer);
}
public OutputWriter(String filename) throws FileNotFoundException {
super(filename);
}
public void println(Iterable<?> collection) {
Iterator<?> iter = collection.iterator();
if (iter.hasNext())
print(iter.next());
while (iter.hasNext()) {
print(' ');
print(iter.next());
}
println();
}
public void close() {
super.close();
}
public void zero() {
println(0);
}
public void one() {
println(1);
}
}
static class IntPair implements Comparable<IntPair> {
public int first;
public int second;
public IntPair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(IntPair o) {
if (first != o.first)
return Integer.compare(first, o.first);
return Integer.compare(second, o.second);
}
public String toString() {
return first + " " + second;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof IntPair)) return false;
IntPair intPair = (IntPair) o;
return first == intPair.first &&
second == intPair.second;
}
public int hashCode() {
return (31 * first + second);
}
public IntPair clone() {
return of(first, second);
}
public static IntPair of(int first, int second) {
return new IntPair(first, second);
}
}
static class InputReader extends BufferedReader {
StringTokenizer tokenizer;
public InputReader(InputStream inputStream) {
super(new InputStreamReader(inputStream), 32768);
}
public InputReader(String filename) {
super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
String line = readLine();
if (line == null) continue;
tokenizer = new StringTokenizer(line);
} catch (IOException e) {
throw new RuntimeException();
}
}
return tokenizer.nextToken();
}
public Integer nextInt() {
return Integer.valueOf(next());
}
}
}
|
Java
|
["3\n6\n4\n30"]
|
1 second
|
["2 3 6 \n1\n2 4 \n0\n2 30 6 3 15 5 10 \n0"]
|
NoteIn the first test case $$$6$$$ has three divisors, which are greater than $$$1$$$: $$$2, 3, 6$$$. Regardless of the initial order, numbers $$$2$$$ and $$$3$$$ are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes $$$2, 6, 3, 6$$$, and every two adjacent numbers are not coprime.In the second test case $$$4$$$ has two divisors greater than $$$1$$$: $$$2, 4$$$, and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.In the third test case all divisors of $$$30$$$ greater than $$$1$$$ can be placed in some order so that there are no two adjacent numbers that are coprime.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"implementation",
"number theory",
"math"
] |
406f8f662d2013d87b36dacca663bef5
|
The first line contains an integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. Next $$$t$$$ lines describe each test case. In a single line of each test case description, there is a single composite number $$$n$$$ $$$(4 \le n \le 10^9)$$$ — the number from the message. It's guaranteed that the total number of divisors of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$.
| 2,100 |
For each test case in the first line output the initial order of divisors, which are greater than $$$1$$$, in the circle. In the second line output, the minimal number of moves needed to decrypt the message. If there are different possible orders with a correct answer, print any of them.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.