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
|
8a2aeb8f0acf65ead1bac4e19f788015
|
train_001.jsonl
|
1432658100
|
While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.
|
256 megabytes
|
import java.awt.List;
import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class solver implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
//in = new BufferedReader(new FileReader("input.txt"));
//out = new PrintWriter("output.txt");
}
String readString() {
try {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine());
} catch (Exception e) {
return null;
}
}
return tok.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
int readInt() {
return Integer.parseInt(readString());
}
long readLong() {
return Long.parseLong(readString());
}
double readDouble() {
return Double.parseDouble(readString());
}
public static void main(String[] args) {
new Thread(null, new solver(), "", 256 * (1L << 20)).start();
}
long timeBegin, timeEnd;
void time() {
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
public void run() {
try {
timeBegin = System.currentTimeMillis();
init();
solve();
out.close();
time();
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
int pow(int x, int p) {
return (int)Math.pow(x, p);
}
boolean contains(char[][] a, int i, int j, char c) {
for (int x=i;x<=i+1;x++) {
for (int y=j;y<=j+1;y++){
if (a[x][y] == c) return true;
}
}
return false;
}
void solve() {
String s = readString();
int k = readInt();
if (s.length() % k != 0) {
out.println("NO");
return;
}
int len = s.length() / k;
for (int sl=0;sl<k;sl++) {
int l = sl * len;
int r = (sl + 1) * len - 1;
while (r - l > 0) {
if (s.charAt(l) != s.charAt(r)) {
out.println("NO");
return;
}
l++;
r--;
}
}
out.println("YES");
}
}
|
Java
|
["saba\n2", "saddastavvat\n2"]
|
1 second
|
["NO", "YES"]
|
NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".
|
Java 7
|
standard input
|
[
"implementation",
"brute force",
"strings"
] |
43bb8fec6b0636d88ce30f23b61be39f
|
The first line of input contains string s containing lowercase English letters (1ββ€β|s|ββ€β1000). The second line contains integer k (1ββ€βkββ€β1000).
| 1,100 |
Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.
|
standard output
| |
PASSED
|
7091f384f21c2eb9691bcc651c890f6c
|
train_001.jsonl
|
1432658100
|
While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.
|
256 megabytes
|
import java.util.Scanner;
public class A305 {
public static boolean isPalindrome(String string)
{
StringBuilder sb=new StringBuilder();
for(int i=string.length()-1;i>=0;i--)
sb.append(string.charAt(i));
return string.equals(sb.toString());
}
public static void main(String[] args) {
String input;
int k;
Scanner kb=new Scanner(System.in);
input=kb.nextLine();
k=kb.nextInt();
int length=input.length()/k;
if(length*k!=input.length())
{
System.out.println("NO");
return;
}
for(int i=0;i<input.length();i+=length)
{
if(i+length<=input.length() && !isPalindrome(input.substring(i,i+length)))
{
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
}
|
Java
|
["saba\n2", "saddastavvat\n2"]
|
1 second
|
["NO", "YES"]
|
NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".
|
Java 7
|
standard input
|
[
"implementation",
"brute force",
"strings"
] |
43bb8fec6b0636d88ce30f23b61be39f
|
The first line of input contains string s containing lowercase English letters (1ββ€β|s|ββ€β1000). The second line contains integer k (1ββ€βkββ€β1000).
| 1,100 |
Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.
|
standard output
| |
PASSED
|
d695a132ff77cd1d50e27ce48ce250d8
|
train_001.jsonl
|
1432658100
|
While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.
|
256 megabytes
|
import java.util.Scanner;
public class main
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
String x = input.nextLine();
char a[] = new char[x.length()];
for(int i=0;i<x.length();i++)
{
a[i] = x.charAt(i);
}
float k = input.nextFloat();
boolean z = true;
float lu = x.length()/k;
if(lu != (int)lu)
{
System.out.println("NO");
System.exit(0);
}
int l = (int)lu;
int j = 0;
int y = 0;
int f = l;
// System.out.println(isPallindrome(a,0,1));
for(int i=0;i<k;i++)
{
if(z && isPallindrome(a,j,l-1))
{
j = l;
l += f;
}
else
{
y =1;
break;
}
}
if(y == 1)
{
System.out.println("NO");
}
else
{
System.out.println("YES");
}
}
public static boolean isPallindrome(char arr[],int i,int j)
{
while(i<=j)
{
if(arr[i] != arr[j])
{
return false;
}
i++;
j--;
}
return true;
}
}
|
Java
|
["saba\n2", "saddastavvat\n2"]
|
1 second
|
["NO", "YES"]
|
NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".
|
Java 7
|
standard input
|
[
"implementation",
"brute force",
"strings"
] |
43bb8fec6b0636d88ce30f23b61be39f
|
The first line of input contains string s containing lowercase English letters (1ββ€β|s|ββ€β1000). The second line contains integer k (1ββ€βkββ€β1000).
| 1,100 |
Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.
|
standard output
| |
PASSED
|
b1a9fa9509830fd6d4af51bea1ffb502
|
train_001.jsonl
|
1432658100
|
While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.
|
256 megabytes
|
import java.util.*;
public class Fek {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int x = Integer.valueOf(sc.nextLine());
if (s.length() % x != 0)
System.out.println("NO");
else {
int div = s.length() / x;
boolean b = true;
int k = 0;
while (k < x) {
String pal = s.substring(k * div, k * div + div);
String rev = new StringBuilder(pal).reverse().toString();
// System.out.println(pal+" "+rev);
if (!pal.equals(rev)) {
b = false;
break;
}
k++;
}
if (b)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
|
Java
|
["saba\n2", "saddastavvat\n2"]
|
1 second
|
["NO", "YES"]
|
NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".
|
Java 7
|
standard input
|
[
"implementation",
"brute force",
"strings"
] |
43bb8fec6b0636d88ce30f23b61be39f
|
The first line of input contains string s containing lowercase English letters (1ββ€β|s|ββ€β1000). The second line contains integer k (1ββ€βkββ€β1000).
| 1,100 |
Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.
|
standard output
| |
PASSED
|
3bea54bb771995603a5b66a84e64d48b
|
train_001.jsonl
|
1432658100
|
While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.
|
256 megabytes
|
/*
* Author - Chinmaya Kr. Patanaik
* University - Charles University Prague
*
*/
import java.util.Scanner;
import java.util.*;
public class MikeAndFox_548A
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String line = scan.next();
int size = scan.nextInt();
if((line.length()%size) != 0)
System.out.println("NO");
else
{
int cut = line.length()/size;
boolean val = true;
for(int i=0, j=cut; j<=line.length(); i += cut, j += cut)
{
if(!isPallindrome(line.substring(i, j)))
val = false;
}
if(val)
System.out.println("YES");
else
System.out.println("NO");
}
}
public static boolean isPallindrome(String str)
{
for(int i=0, j=str.length()-1; i<=j; i++, j--)
{
if(str.charAt(i) != str.charAt(j))
return false;
}
return true;
}
}
|
Java
|
["saba\n2", "saddastavvat\n2"]
|
1 second
|
["NO", "YES"]
|
NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".
|
Java 7
|
standard input
|
[
"implementation",
"brute force",
"strings"
] |
43bb8fec6b0636d88ce30f23b61be39f
|
The first line of input contains string s containing lowercase English letters (1ββ€β|s|ββ€β1000). The second line contains integer k (1ββ€βkββ€β1000).
| 1,100 |
Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.
|
standard output
| |
PASSED
|
11cf8b4605268574c0c9a74b95db3fe3
|
train_001.jsonl
|
1432658100
|
While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
public class Main {
static void solve(InputReader reader, OutputWriter out) throws IOException {
// Scanner in = new Scanner(new BufferedReader(new
// FileReader("input.txt")));
// PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
String k = reader.readLine();
String nn = reader.readLine();
int n = Integer.parseInt(nn);
StringBuilder a = new StringBuilder();
//StringBuilder b = new StringBuilder();
if (k.length() % n == 0) {
boolean check =true;
int beg = 0, end = k.length() / n;
int dif = end - beg;
for(int i = 0; i < n; ++i)
{
// out.println(beg + " " + end);
for (int j = beg; j < end; ++j) {
a.append(k.charAt(j));
}
if(!isPol(a))
{
check = false;
break;
}
//out.println(a);
beg = end;
end += dif;
a = new StringBuilder();
}
if(check)
out.println("YES");
else
out.println("NO");
} else {
out.println("NO");
}
}
static boolean isPol(StringBuilder x) {
boolean c = true;
//StringBuilder a = new StringBuilder();
for (int i = 0, j = x.length() - 1; i < x.length(); ++i, --j) {
if (x.charAt(j) != x.charAt(i)) {
c = false;
break;
}
}
return c;
}
public static void main(String[] args) throws Exception {
InputReader reader = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
try {
solve(reader, out);
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
out.close();
}
}
}
class InputReader extends BufferedReader {
public InputReader(InputStream in) {
super(new InputStreamReader(in));
}
}
class OutputWriter extends PrintWriter {
public OutputWriter(PrintStream out) {
super(new BufferedWriter(new OutputStreamWriter(out)));
}
}
/*
* 6 [email protected] [email protected] [email protected] [email protected]
* [email protected] [email protected]
*
* 4 2 [email protected] [email protected] 2 [email protected] [email protected] 1
* [email protected] 1 [email protected]
*/
|
Java
|
["saba\n2", "saddastavvat\n2"]
|
1 second
|
["NO", "YES"]
|
NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".
|
Java 7
|
standard input
|
[
"implementation",
"brute force",
"strings"
] |
43bb8fec6b0636d88ce30f23b61be39f
|
The first line of input contains string s containing lowercase English letters (1ββ€β|s|ββ€β1000). The second line contains integer k (1ββ€βkββ€β1000).
| 1,100 |
Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.
|
standard output
| |
PASSED
|
a5df77f5422432789678bd40600b39e2
|
train_001.jsonl
|
1432658100
|
While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.
|
256 megabytes
|
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean ans = true;
String s = in.next();
int k = in.nextInt();
String t = "";
int w = s.length() / k;
int start = 0;
while (w != 0) {
if (start + w > s.length())
break;
t = s.substring(start, start + w);
if (!pal(t))
ans = false;
start += w;
}
if (!ans || s.length() % k != 0)
System.out.println("NO");
else
System.out.println("YES");
}
private static boolean pal(String t) {
int length = t.length();
String reverse = "";
for (int i = length - 1; i >= 0; i--)
reverse = reverse + t.charAt(i);
if (t.equals(reverse))
return true;
else
return false;
}
}
|
Java
|
["saba\n2", "saddastavvat\n2"]
|
1 second
|
["NO", "YES"]
|
NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".
|
Java 7
|
standard input
|
[
"implementation",
"brute force",
"strings"
] |
43bb8fec6b0636d88ce30f23b61be39f
|
The first line of input contains string s containing lowercase English letters (1ββ€β|s|ββ€β1000). The second line contains integer k (1ββ€βkββ€β1000).
| 1,100 |
Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.
|
standard output
| |
PASSED
|
038864a4d625ab57fcf77456ff74e830
|
train_001.jsonl
|
1432658100
|
While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.
|
256 megabytes
|
import java.util.Scanner;
public class A {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String s = in.next();
int k = in.nextInt();
int n = s.length();
int a = n/k;
int m = 0 ;
int l = m+a-1;
int count = 0 ;
if(a*k!=n){
System.out.println("NO");
return;
}
for(int i = 0 ; i < k ; i++){
int te1 = m , te2 = l;
//System.out.println("te1 = "+te1+" te2 = "+te2);
boolean c = true;
if(te1==te2&&te1<n&&te2<n&&te1>=0&&te2>=0){
count++;
}else if(te1<n&&te2<n&&te1>=0&&te2>=0){
while(te1<te2){
if(s.charAt(te1)!=s.charAt(te2)){
c = false;
}
te1++;
te2--;
if(te1<te2&&s.charAt(te1)!=s.charAt(te2)){
c = false;
}
}
if(c)
count++;
}
m+=a;
l+=a;
}
//System.out.println(count);
if(count == k)
System.out.println("YES");
else
System.out.println("NO");
}
}
|
Java
|
["saba\n2", "saddastavvat\n2"]
|
1 second
|
["NO", "YES"]
|
NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".
|
Java 7
|
standard input
|
[
"implementation",
"brute force",
"strings"
] |
43bb8fec6b0636d88ce30f23b61be39f
|
The first line of input contains string s containing lowercase English letters (1ββ€β|s|ββ€β1000). The second line contains integer k (1ββ€βkββ€β1000).
| 1,100 |
Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.
|
standard output
| |
PASSED
|
d08d9b039f463249e5875a452b4f0e44
|
train_001.jsonl
|
1481992500
|
Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries.The world can be modeled as an undirected graph with n nodes and m edges. k of the nodes are home to the governments of the k countries that make up the world.There is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions is stable.Hongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class A
{
static ArrayList<Integer> g[];
static int[] vis,cc;
public static void main(String[] args)throws IOException
{
FastReader f=new FastReader();
StringBuffer sb = new StringBuffer();
int n=f.nextInt();
int m=f.nextInt();
int k=f.nextInt();
Set<Integer> cap=new HashSet<>();
for(int i=0;i<k;i++)
cap.add(f.nextInt());
g=new ArrayList[n+1];
for(int i=0;i<=n;i++)
g[i]=new ArrayList<>();
vis=new int[n+1];
cc=new int[n+1];
List<String> list=new ArrayList<>();
for(int i=0;i<m;i++)
{
int x=f.nextInt();
int y=f.nextInt();
g[x].add(y);
g[y].add(x);
list.add(x+" "+y);
}
int c=1;
for(int x : cap)
{
dfs(x,c);
c++;
}
// size of cc no of edges
int[] size=new int[c],edge=new int[c];
for(int i=1;i<=n;i++)
size[cc[i]]++;
long max=0;
for(int i=1;i<c;i++)
max=Math.max(max, size[i]);
for(String str : list)
{
String s[]=str.split(" ");
int x=Integer.parseInt(s[0]);
int y=Integer.parseInt(s[1]);
if(cc[x]==cc[y])
edge[cc[x]]++;
}
long sum=0;
for(int i=1;i<c;i++)
{
long calc = ((long)size[i]*(long)(size[i]-1)/2 - (long)edge[i]);
sum+=calc;
}
long sz=max+size[0];
long calc=sz*(sz-1)/2 - (edge[0]+(max*(max-1)/2));
sum+=calc;
System.out.println(sum);
}
static void dfs(int n,int c)
{
cc[n]=c;
vis[n] = 1;
for (int child : g[n]) {
if (vis[child] == 0)
dfs(child,c);
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try{
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["4 1 2\n1 3\n1 2", "3 3 1\n2\n1 2\n1 3\n2 3"]
|
2 seconds
|
["2", "0"]
|
NoteFor the first sample test, the graph looks like this: Vertices 1 and 3 are special. The optimal solution is to connect vertex 4 to vertices 1 and 2. This adds a total of 2 edges. We cannot add any more edges, since vertices 1 and 3 cannot have any path between them.For the second sample test, the graph looks like this: We cannot add any more edges to this graph. Note that we are not allowed to add self-loops, and the graph must be simple.
|
Java 8
|
standard input
|
[
"dfs and similar",
"graphs"
] |
6cf43241b14e4d41ad5b36572f3b3663
|
The first line of input will contain three integers n, m and k (1ββ€βnββ€β1β000, 0ββ€βmββ€β100β000, 1ββ€βkββ€βn)Β β the number of vertices and edges in the graph, and the number of vertices that are homes of the government. The next line of input will contain k integers c1,βc2,β...,βck (1ββ€βciββ€βn). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world. The following m lines of input will contain two integers ui and vi (1ββ€βui,βviββ€βn). This denotes an undirected edge between nodes ui and vi. It is guaranteed that the graph described by the input is stable.
| 1,500 |
Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.
|
standard output
| |
PASSED
|
aec3f9b6ed25ba5ee584b5a338029c27
|
train_001.jsonl
|
1334934300
|
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an nβΓβm rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.lang.Math.*;
public class crocA
{
public static void main(String args[])throws Exception
{
Scanner in=new Scanner(System.in);
//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//BufferedReader br = new BufferedReader(new FileReader("crocA.in"));
//PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("crocA.out")));
PrintWriter pw = new PrintWriter(System.out);
//StringTokenizer st;
int n=in.nextInt();
int m=in.nextInt();
int a[]=new int[3];
int b[]=new int[3];
int i,j,k=0,l;
for(i=0;i<n;i++)
{
String s=in.next();
for(j=0;j<m;j++)
{
if(s.charAt(j)=='*')
{
a[k]=i+1;
b[k]=j+1;
k++;
}
}
}
if(a[0]!=a[1] && a[0]!=a[2])
pw.print(a[0]);
else if(a[1]!=a[0] && a[1]!=a[2])
pw.print(a[1]);
else
pw.print(a[2]);
pw.print(" ");
if(b[0]!=b[1] && b[0]!=b[2])
pw.print(b[0]);
else if(b[1]!=b[0] && b[1]!=b[2])
pw.print(b[1]);
else
pw.print(b[2]);
pw.flush();
}
}
|
Java
|
["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."]
|
2 seconds
|
["1 1", "2 3"]
| null |
Java 6
|
standard input
|
[
"implementation",
"geometry",
"brute force"
] |
e344de8280fb607c556671db9cfbaa9e
|
The first line contains two space-separated integers n and m (2ββ€βn,βmββ€β100) β the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.
| 800 |
Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
|
standard output
| |
PASSED
|
29b732612e1d65faaa6f0b85c37fc659
|
train_001.jsonl
|
1334934300
|
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an nβΓβm rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.lang.Math.*;
public class crocA
{
public static void main(String args[])throws Exception
{
Scanner in=new Scanner(System.in);
//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//BufferedReader br = new BufferedReader(new FileReader("crocA.in"));
//PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("crocA.out")));
PrintWriter pw = new PrintWriter(System.out);
//StringTokenizer st;
int n=in.nextInt();
int m=in.nextInt();
int a[][]=new int[3][2];
int i,j,k=0,l;
for(i=0;i<n;i++)
{
String s=in.next();
for(j=0;j<m;j++)
{
if(s.charAt(j)=='*')
{
a[k][0]=i+1;
a[k][1]=j+1;
k++;
}
}
}
int max=a[0][0],min=a[0][0],mincount=0,maxcount=0;
for(i=0;i<3;i++)
{
if(a[i][0]>=max)
{
max=a[i][0];
}
else if(a[i][0]<=min)
{
min=a[i][0];
}
}
for(i=0;i<3;i++)
{
if(a[i][0]==max)
{
maxcount++;
}
else if(a[i][0]==min)
{
mincount++;
}
}
if(mincount>1)
pw.print(max);
else
pw.print(min);
max=a[0][1];min=a[0][1];mincount=0;maxcount=0;
for(i=0;i<3;i++)
{
if(a[i][1]>=max)
{
max=a[i][1];
}
else if(a[i][1]<=min)
{
min=a[i][1];
}
}
for(i=0;i<3;i++)
{
if(a[i][1]==max)
{
maxcount++;
}
else if(a[i][1]==min)
{
mincount++;
}
}
if(mincount>1)
pw.print(" "+max);
else
pw.print(" "+min);
pw.println();
pw.flush();
}
}
|
Java
|
["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."]
|
2 seconds
|
["1 1", "2 3"]
| null |
Java 6
|
standard input
|
[
"implementation",
"geometry",
"brute force"
] |
e344de8280fb607c556671db9cfbaa9e
|
The first line contains two space-separated integers n and m (2ββ€βn,βmββ€β100) β the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.
| 800 |
Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
|
standard output
| |
PASSED
|
ec6a3ec7021774ea12954af77cb4d157
|
train_001.jsonl
|
1334934300
|
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an nβΓβm rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
new Main().run();
}
StringTokenizer str = null;
BufferedReader in;
PrintWriter out;
void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
out.flush();
out.close();
}
String nextString() throws IOException {
return in.readLine();
}
String nextToken() throws IOException {
if (str == null || !str.hasMoreElements()) {
str = new StringTokenizer(nextString());
}
return str.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
Set<String> set = new HashSet<String>();
void solve() throws IOException {
int n = nextInt();
int m = nextInt();
int maxX = -1;
int maxY = -1;
int minX = 1000;
int minY = 1000;
for (int i = 0; i < n; i++) {
String s = nextString();
for (int j = 0; j < m; j++) {
if (s.charAt(j) == '*') {
if (j > maxX) {
maxX = j;
}
if (j < minX) {
minX = j;
}
if (i > maxY) {
maxY = i;
}
if (i < minY) {
minY = i;
}
set.add(j + "#" + i);
}
}
}
out(minX, minY);
out(minX, maxY);
out(maxX, minY);
out(maxX, maxY);
}
private void out(int x, int y) {
if (!set.contains(x + "#" + y)) {
out.println((y + 1) + " " + (x + 1));
}
}
}
|
Java
|
["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."]
|
2 seconds
|
["1 1", "2 3"]
| null |
Java 6
|
standard input
|
[
"implementation",
"geometry",
"brute force"
] |
e344de8280fb607c556671db9cfbaa9e
|
The first line contains two space-separated integers n and m (2ββ€βn,βmββ€β100) β the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.
| 800 |
Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
|
standard output
| |
PASSED
|
58f048554e5ff91c83e74679acf970ea
|
train_001.jsonl
|
1334934300
|
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an nβΓβm rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
char[][] c = new char[n][m];
int i1 = -1, i2 = -1;
for (int i = 0; i < n; i++) {
String s = in.readLine();
int cnt = 0;
for (int j = 0; j < m; j++) {
c[i][j] = s.charAt(j);
if (c[i][j] == '*')
cnt++;
}
if (cnt == 1)
i1 = i;
else if (cnt == 2)
i2 = i;
}
int l = -1, r = m - 1;
for (int j = 0; j < m; j++)
if (c[i2][j] == '*')
if (l == -1)
l = j;
else
r = j;
int pos = -1;
for (int j = 0; j < m; j++)
if (c[i1][j] == '*')
pos = j;
int ansI = i1 + 1, ansJ = 1 + (pos == r ? l : r);
out.println(ansI + " " + ansJ);
out.close();
}
}
|
Java
|
["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."]
|
2 seconds
|
["1 1", "2 3"]
| null |
Java 6
|
standard input
|
[
"implementation",
"geometry",
"brute force"
] |
e344de8280fb607c556671db9cfbaa9e
|
The first line contains two space-separated integers n and m (2ββ€βn,βmββ€β100) β the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.
| 800 |
Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
|
standard output
| |
PASSED
|
d35564924d05f26d79c68add895cf548
|
train_001.jsonl
|
1334934300
|
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an nβΓβm rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.
|
256 megabytes
|
import java.awt.Point;
import java.io.*;
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
try {
String[] data = br.readLine().split(" ");
int n = Integer.parseInt(data[0]);
Point[] coords = new Point[4];
int offset = 0;
for(int i = 0; i < n && offset < 4; i++) {
String line = br.readLine();
int li = line.lastIndexOf("*");
int si = line.indexOf("*");
if(si != -1 && li !=-1) {
coords[offset++] = new Point(i, si);
if(li != si)
coords[offset++] = new Point(i, li);
}
}
int valueX = 0, valueY = 0;
for(int i = 0; i < 3; i++) {
Point first = coords[i];
boolean existsX = false, existsY = false;
for(int j = i + 1; j < 3; j++) {
if(coords[j].x == first.x) {
existsX = true;
coords[j].x = -1;
}
if(coords[j].y == first.y) {
existsY = true;
coords[j].y = -1;
}
}
if(!existsX && first.x != -1)
valueX = first.x;
if(!existsY && first.y != -1)
valueY = first.y;
}
valueX++;
valueY++;
System.out.println(valueX+ " " + valueY);
}
catch(Exception ex) {
System.out.println("Something went wrong!");
}
}
}
|
Java
|
["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."]
|
2 seconds
|
["1 1", "2 3"]
| null |
Java 6
|
standard input
|
[
"implementation",
"geometry",
"brute force"
] |
e344de8280fb607c556671db9cfbaa9e
|
The first line contains two space-separated integers n and m (2ββ€βn,βmββ€β100) β the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.
| 800 |
Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
|
standard output
| |
PASSED
|
509c09522547efe2862384a71e8c9b20
|
train_001.jsonl
|
1334934300
|
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an nβΓβm rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.
|
256 megabytes
|
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n,m;
Scanner sc = new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
char mas [][] = new char[n][m];
for(int i=0;i<n;i++)
{
String S = sc.next();
char arr[] = S.toCharArray();
System.arraycopy(arr, 0, mas[i], 0, m);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mas[i][j]!='*')
{
for(int k=0;k<m;k++)
{
if(k!=j && mas[i][k]=='*')
{
for(int l=0;l<n;l++)
{
if(l!=i && mas[l][j]=='*')
{
if(mas[l][k]=='*') System.out.print((i+1)+" "+(j+1));
}
}
}
}
}
}
}
}
}
|
Java
|
["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."]
|
2 seconds
|
["1 1", "2 3"]
| null |
Java 6
|
standard input
|
[
"implementation",
"geometry",
"brute force"
] |
e344de8280fb607c556671db9cfbaa9e
|
The first line contains two space-separated integers n and m (2ββ€βn,βmββ€β100) β the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.
| 800 |
Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
|
standard output
| |
PASSED
|
7ac7080b3855b8b03bd4af43b62e6f27
|
train_001.jsonl
|
1334934300
|
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an nβΓβm rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.
|
256 megabytes
|
import java.util.Scanner;
public class KK {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int f = 0;
Point[] ans = new Point[4];
for (int i = 0; i < x; i++) {
String s = sc.next();
for (int j = 0 ; j < y;j++) {
if (s.charAt(j) == '*') {
ans[f] = new Point(j,i);
f++;
}
}
}
int xx = 0;
int yy = 0;
if (ans[0].x == ans[1].x) {
xx = ans[2].x;
}
else if (ans[1].x == ans[2].x) {
xx = ans[0].x;
}
else if (ans[0].x == ans[2].x) {
xx = ans[1].x;
}
if (ans[0].y == ans[1].y) {
yy = ans[2].y;
}
else if (ans[1].y == ans[2].y) {
yy = ans[0].y;
}
else if (ans[0].y == ans[2].y) {
yy = ans[1].y;
}
xx++;
yy++;
System.out.println (yy + " " + xx);
}
}
class Point{
Point(int x, int y){
this.x = x;
this.y = y;
}
int x;
int y;
}
|
Java
|
["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."]
|
2 seconds
|
["1 1", "2 3"]
| null |
Java 6
|
standard input
|
[
"implementation",
"geometry",
"brute force"
] |
e344de8280fb607c556671db9cfbaa9e
|
The first line contains two space-separated integers n and m (2ββ€βn,βmββ€β100) β the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.
| 800 |
Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
|
standard output
| |
PASSED
|
82f9f080ffa42429962b06b53b0bc3fc
|
train_001.jsonl
|
1334934300
|
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an nβΓβm rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public void solve( ) throws Throwable {
int rows = in.nextInt( ), cols = in.nextInt( );
char mat[ ][ ] = new char[ rows ][ ];
for ( int i = 0; i < mat.length; ++i ) {
mat[ i ] = in.next( ).toCharArray( );
}
for ( int i = 0; i < rows; ++i ) {
for ( int j = 0; j < cols; ++j ) {
if ( mat[ i ][ j ] == '.' ) {
int r = 0, c = 0;
for ( int k = 0; k < cols; ++k ) {
if ( mat[ i ][ k ] == '*' ) {
r++;
}
}
for ( int k = 0; k < rows; ++k ) {
if ( mat[ k ][ j ] == '*' ) {
++c;
}
}
if ( r == 1 && c == 1 ) {
out.println( ( i + 1 ) + " " + ( j + 1 ) );
return;
}
}
}
}
}
public void run( ) {
in = new FastScanner( System.in );
out = new PrintWriter( new PrintStream( System.out ), true );
try {
solve( );
out.close( );
System.exit( 0 );
} catch( Throwable e ) {
e.printStackTrace( );
System.exit( -1 );
}
}
public void debug( Object...os ) {
System.err.println( Arrays.deepToString( os ) );
}
public static void main( String[ ] args ) {
( new Main( ) ).run( );
}
private FastScanner in;
private PrintWriter out;
private static class FastScanner {
private int charsRead;
private int currentRead;
private byte buffer[ ] = new byte[ 0x1000 ];
private InputStream reader;
public FastScanner( InputStream in ) {
reader = in;
}
public int read( ) {
if ( charsRead == -1 ) {
throw new InputMismatchException( );
}
if ( currentRead >= charsRead ) {
currentRead = 0;
try {
charsRead = reader.read( buffer );
} catch( IOException e ) {
throw new InputMismatchException( );
}
if ( charsRead <= 0 ) {
return -1;
}
}
return buffer[ currentRead++ ];
}
public int nextInt( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( sign == -1 ) {
c = read( );
}
if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int ans = 0;
while ( !isWhitespace( c ) && c != -1 ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10 + num;
c = read( );
}
return ans * sign;
}
public long nextLong( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( sign == -1 ) {
c = read( );
}
if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
long ans = 0;
while ( !isWhitespace( c ) && c != -1 ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10 + num;
c = read( );
}
return ans * sign;
}
public boolean isWhitespace( int c ) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == -1;
}
public String next( ) {
int c = read( );
StringBuffer ans = new StringBuffer( );
while ( isWhitespace( c ) && c != -1 ) {
c = read( );
}
if ( c == -1 ) {
return null;
}
while ( !isWhitespace( c ) && c != -1 ) {
ans.appendCodePoint( c );
c = read( );
}
return ans.toString( );
}
public String nextLine( ) {
String ans = nextLine0( );
while ( ans.trim( ).length( ) == 0 ) {
ans = nextLine0( );
}
return ans;
}
private String nextLine0( ) {
int c = read( );
if ( c == -1 ) {
return null;
}
StringBuffer ans = new StringBuffer( );
while ( c != '\n' && c != '\r' && c != -1 ) {
ans.appendCodePoint( c );
c = read( );
}
return ans.toString( );
}
public double nextDouble( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '.' && c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( c == '-' ) {
c = read( );
}
double ans = 0;
while ( c != -1 && c != '.' && !isWhitespace( c ) ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10.0 + num;
c = read( );
}
if ( !isWhitespace( c ) && c != -1 && c != '.' ) {
throw new InputMismatchException( );
}
double pow10 = 1.0;
if ( c == '.' ) {
c = read( );
}
while ( !isWhitespace( c ) && c != -1 ) {
pow10 *= 10.0;
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10.0 + num;
c = read( );
}
return ans * sign / pow10;
}
}
}
|
Java
|
["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."]
|
2 seconds
|
["1 1", "2 3"]
| null |
Java 6
|
standard input
|
[
"implementation",
"geometry",
"brute force"
] |
e344de8280fb607c556671db9cfbaa9e
|
The first line contains two space-separated integers n and m (2ββ€βn,βmββ€β100) β the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.
| 800 |
Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
|
standard output
| |
PASSED
|
fa2a7a6973a6e2627cf4825b9589c479
|
train_001.jsonl
|
1334934300
|
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an nβΓβm rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.
|
256 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
public class Main {
public static void main(String[] args) throws IOException
{
(new Main()).solve();
}
public void Main() {
}
void solve() throws IOException {
//BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//Scanner in = new Scanner(System.in);
MyReader in = new MyReader();
PrintWriter out = new PrintWriter(System.out);
// Scanner in = new Scanner(new FileReader("input.txt"));
// PrintWriter out = new PrintWriter("output.txt");
int n = in.nextInt();
int m = in.nextInt();
int x1 = 0, x2 = 0, x3 = 0, y1 = 0, y2 = 0, y3 = 0, x4, y4;
for (int i = 0; i < n; i++) {
String s = in.readLine();
for (int j = 0; j < m; j++) {
if (s.charAt(j) == '*') {
if (x1 == 0) {
x1 = i + 1;
y1 = j + 1;
} else if (x2 == 0) {
x2 = i + 1;
y2 = j + 1;
} else {
x3 = i + 1;
y3 = j + 1;
}
}
}
}
if (y1 == y3) {
if (x2 == x3) {
x4 = x1;
y4 = y2;
} else {
x4 = x3;
y4 = y2;
}
} else {
if (y1 == y2) {
x4 = x1;
y4 = y3;
} else {
x4 = x3;
y4 = y1;
}
}
out.println(x4 + " " + y4);
out.close();
}
};
class MyReader {
private BufferedReader in;
String[] parsed;
int index = 0;
public MyReader() {
in = new BufferedReader(new InputStreamReader(System.in));
}
public int nextInt() throws NumberFormatException, IOException {
if (parsed == null || parsed.length == index) {
read();
}
return Integer.parseInt(parsed[index++]);
}
public long nextLong() throws NumberFormatException, IOException {
if (parsed == null || parsed.length == index) {
read();
}
return Long.parseLong(parsed[index++]);
}
public String nextString() throws IOException {
if (parsed == null || parsed.length == index) {
read();
}
return parsed[index++];
}
private void read() throws IOException {
parsed = in.readLine().split(" ");
index = 0;
}
public String readLine() throws IOException {
return in.readLine();
}
};
|
Java
|
["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."]
|
2 seconds
|
["1 1", "2 3"]
| null |
Java 6
|
standard input
|
[
"implementation",
"geometry",
"brute force"
] |
e344de8280fb607c556671db9cfbaa9e
|
The first line contains two space-separated integers n and m (2ββ€βn,βmββ€β100) β the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.
| 800 |
Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
|
standard output
| |
PASSED
|
8081d8da35a979e8ebee8991ed5084a2
|
train_001.jsonl
|
1334934300
|
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an nβΓβm rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.
|
256 megabytes
|
import java.util.Scanner;
public class CROC_1012_A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int x[] = new int[3];
int y[] = new int[3];
int k = 0;
for (int i = 0; i < n; i++) {
String s = sc.next(); sc.nextLine();
// System.out.println(i+" "+s);
for (int j = 0; j < m; j++) {
if(s.substring(j , j+1).equals("*")){
x[k] = i;
y[k] = j;
k++;
}
}
}
int i1=x[0], i2=y[0];
if(x[0]!=x[1] && x[0]!=x[2]) i1 = x[0];
if(x[2]!=x[1] && x[2]!=x[0]) i1 = x[2];
if(x[1]!=x[0] && x[1]!=x[2]) i1 = x[1];
if(y[0]!=y[1] && y[0]!=y[2]) i2 = y[0];
if(y[2]!=y[1] && y[2]!=y[0]) i2 = y[2];
if(y[1]!=y[0] && y[1]!=y[2]) i2 = y[1];
i1++; i2++;
System.out.println(i1+" "+i2);
}
}
|
Java
|
["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."]
|
2 seconds
|
["1 1", "2 3"]
| null |
Java 6
|
standard input
|
[
"implementation",
"geometry",
"brute force"
] |
e344de8280fb607c556671db9cfbaa9e
|
The first line contains two space-separated integers n and m (2ββ€βn,βmββ€β100) β the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.
| 800 |
Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
|
standard output
| |
PASSED
|
99455ed6bdb3b8cf090970d486b38a94
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Solution{
static int id = 1;
public static void dfs(int curr,List<List<Integer>> adj, int[] ids, int[] lows,Stack<Integer> stack, int[] onStack, List<List<Integer>> sccs){
ids[curr] = Solution.id;
lows[curr] = Solution.id;
stack.push(curr);
onStack[curr] = 1;
Solution.id += 1;
for(int neighbour: adj.get(curr)){
if(ids[neighbour]!=0){
if(onStack[neighbour]==1){
lows[curr] = Math.min(lows[curr],lows[neighbour]);
}
}
else{
dfs(neighbour,adj,ids,lows,stack,onStack,sccs);
lows[curr] = Math.min(lows[curr],lows[neighbour]);
}
}
if(ids[curr]==lows[curr]){
List<Integer> scc = new ArrayList<>();
int val = stack.pop();
scc.add(val);
onStack[val] = 0;
while(val!=curr){
val = stack.pop();
onStack[val] = 0;
scc.add(val);
}
sccs.add(scc);
}
}
public static List<List<Integer>> tarjan(List<List<Integer>> adjList){
int[] ids = new int[adjList.size()];
int[] lowLinks = new int[adjList.size()];
int[] onStack = new int[adjList.size()];
Stack<Integer> stack = new Stack();
List<List<Integer>> sccs = new ArrayList<>();
for(int i=0;i<adjList.size();i++){
if(ids[i]!=0) continue;
dfs(i,adjList,ids,lowLinks,stack, onStack, sccs);
}
return sccs;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int nbVertices = sc.nextInt();
int[] prices = IntStream.range(0,nbVertices).map(i->sc.nextInt()).toArray();
int nbEdges = sc.nextInt();
ArrayList<List<Integer>> adj = new ArrayList<>();
for(int i=0;i<nbVertices;i++){
adj.add(new ArrayList<>());
}
for(int i=0;i<nbEdges;i++){
int u = sc.nextInt();
int v = sc.nextInt();
adj.get(u-1).add(v-1);
}
List<List<Integer>> sccs = tarjan(adj);
long totalPrice = 0;
long differentWays = 1;
int modulo = 1000000007;
for(List<Integer> scc : sccs){
int min = scc.stream().mapToInt(i->prices[i]).min().getAsInt();
long nbMin = scc.stream().filter(i->prices[i]==min).count();
totalPrice += min;
differentWays = (differentWays * nbMin) % modulo;
}
System.out.println(String.valueOf(totalPrice)+" "+String.valueOf(differentWays));
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
fc75ca89aea7e096451ec03430c51122
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Main {
static ArrayList<Integer> a[] = new ArrayList[100005];
static int []cost = new int[100005];// = new int[26];
static int []low = new int[100005];
static int []time = new int[100005];
static int []stack = new int[100005];
static int []instack = new int[100005];
static int []comp = new int[100005];
static int []mini = new int[100005];
static int []cnt = new int[100005];
static int top = 0;
static int comps = 0;
static int t = 0;
static void tarjan(int v)
{
low[v] = t;
time[v] = t++;
stack[top++] = v;
instack[v] = 1;
for (Integer u:a[v]) {
if (time[u] < 0) {
tarjan(u);
low[v] = Math.min(low[v], low[u]);
} else if (instack[u] == 1) {
low[v] = Math.min(low[v], time[u]);
}
}
if (low[v] == time[v]) {
comps += 1;
int tmp = -1;
do {
tmp = stack[--top];
instack[tmp] = 0;
comp[tmp] = comps;
} while (tmp != v);
}
}
public static void main(String[] args) {
try {
MScanner in = new MScanner(System.in);
int n = in.nextInt();
for (int i = 0; i <= n; i++) a[i] = new ArrayList<>();
for (int i = 1;i <= n;++i) cost[i] = in.nextInt();
int m = in.nextInt();
for (int i = 0;i < m;++i) {
int x = in.nextInt();
int y = in.nextInt();
a[x].add(y);
}
for (int i = 1;i <= n;++i) time[i] = -1;
for (int i = 1;i <= n;++i) {
if (time[i] < 0) {
tarjan(i);
//System.out.println("=======tarjan"+i);
//for (int j = 1;j <= n;++j) {
// System.out.println("j:"+comp[j]);
//}
}
}
for(int i = 1; i <= comps; i++) mini[i]=2000000000;
for (int i = 1;i <= n;++i) {
int c = comp[i];
int w = cost[i];
if(mini[c] > w) {
mini[c] = w;
cnt[c]=1;
}else if(mini[c] == w) cnt[c]++;
}
long a = 0, b=1;
for(int i = 1; i <= comps; i++){
a += mini[i];
b =b * (long)cnt[i];
b %= 1000000007;
}
System.out.println(a + " " + b);
} catch (Exception e) {
e.printStackTrace();
}
}
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int[] takearr(int n) throws IOException {
int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
return in;
}
public long[] takearrl(int n) throws IOException {
long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
return in;
}
public Integer[] takearrobj(int n) throws IOException {
Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt();
return in;
}
public Long[] takearrlobj(int n) throws IOException {
Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong();
return in;
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
82d6cb53dec17a57138a36617954864d
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
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.PrintStream;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Stack;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
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);
OutputWriter out = new OutputWriter(outputStream);
CCheckposts solver = new CCheckposts();
solver.solve(1, in, out);
out.close();
}
static class CCheckposts {
int cnt;
int ans = 1;
long all = 0;
Stack<Vertex> stack;
static int mod = 1000000007;
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
Vertex[] vertices = new Vertex[n];
stack = new Stack<>();
for (int i = 0; i < n; ++i) {
vertices[i] = new Vertex();
vertices[i].adj = new ArrayList<>();
vertices[i].cost = in.nextInt();
}
int m = in.nextInt();
for (int i = 0; i < m; ++i) {
int u = in.nextInt();
int v = in.nextInt();
--u;
--v;
vertices[u].adj.add(vertices[v]);
}
for (int i = 0; i < n; ++i) {
if (vertices[i].dead == false) vertices[i].dfs();
}
out.println(all + " " + ans);
}
class Vertex {
boolean dead;
int low;
int num;
int cost;
ArrayList<Vertex> adj;
void dfs() {
low = num = ++cnt;
stack.push(this);
for (Vertex nxt : adj) {
if (nxt.dead == true) continue;
if (nxt.num > 0) {
low = Math.min(low, nxt.num);
} else {
nxt.dfs();
low = Math.min(low, nxt.low);
}
}
if (low >= num) {
int best = Integer.MAX_VALUE, way = 0;
while (true) {
Vertex now = stack.pop();
if (now.cost == best) ++way;
else if (now.cost < best) {
best = now.cost;
way = 1;
}
now.dead = true;
if (now == this) break;
}
ans = (int) ((long) ans * (long) way % mod);
all = all + best;
}
}
}
//
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
this.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) {
this.writer.print(' ');
}
this.writer.print(objects[i]);
}
}
public void println(Object... objects) {
this.print(objects);
this.writer.println();
}
public void close() {
this.writer.close();
}
}
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 (this.numChars == -1) {
throw new InputMismatchException();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException var2) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for (c = this.read(); this.isSpaceChar(c); c = this.read()) {
}
int sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) {
res *= 10;
res += c - 48;
c = this.read();
if (this.isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public boolean isSpaceChar(int c) {
return this.filter != null ? this.filter.isSpaceChar(c) : isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
}
public interface SpaceCharFilter {
boolean isSpaceChar(int var1);
}
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
089e2c09e5082afb97c394d14336794d
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
// 21:49.030
public class cf302218b {
public static void main(String[] args) throws IOException {
int n = ri(), cost[] = ria(n), m = ri();
Graph g = graph(n, m);
List<List<Integer>> scc = scc(g);
int k = scc.size(), ans_n = 1;
long c = 0;
for (int i = 0; i < k; ++i) {
int min = IBIG, cnt = 0;
for (int j : scc.get(i)) {
if (cost[j] < min) {
min = cost[j];
cnt = 1;
} else if (cost[j] == min) {
++cnt;
}
}
c += min;
ans_n = mmul(ans_n, cnt);
}
prln(c, ans_n);
close();
}
static int mmod = 1000000007;
static int madd(int a, int b) {
return (a + b) % mmod;
}
static int madd(int... a) {
int ans = a[0];
for (int i = 1; i < a.length; ++i) {
ans = madd(ans, a[i]);
}
return ans;
}
static int msub(int a, int b) {
return (a - b + mmod) % mmod;
}
static int mmul(int a, int b) {
return (int) ((long) a * b % mmod);
}
static int mmul(int... a) {
int ans = a[0];
for (int i = 1; i < a.length; ++i) {
ans = mmul(ans, a[i]);
}
return ans;
}
static int minv(int x) {
return mpow(x, mmod - 2);
}
static int mpow(int a, int b) {
if (a == 0) {
return 0;
}
int ans = 1;
while (b > 0) {
if ((b & 1) > 0) {
ans = mmul(ans, a);
}
a = mmul(a, a);
b >>= 1;
}
return ans;
}
static List<List<Integer>> scc(Graph g) {
List<Integer> ordered = new ArrayList<>();
int n = g.size();
boolean[] vis = new boolean[n];
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
dfs(g, i, vis, ordered);
}
}
Graph transpose = graph(n);
for (int i = 0; i < n; ++i) {
for (int j : g.get(i)) {
transpose.cto(j, i);
}
}
for (int i = 0; i < n / 2; ++i) {
int __swap = ordered.get(i);
ordered.set(i, ordered.get(n - i - 1));
ordered.set(n - i - 1, __swap);
}
List<List<Integer>> scc = new ArrayList<>();
fill(vis, false);
for (int i : ordered) {
if (!vis[i]) {
List<Integer> comp = new ArrayList<>();
dfs(transpose, i, vis, comp);
scc.add(comp);
}
}
return scc;
}
static void dfs(Graph g, int i, boolean vis[], List<Integer> ordered) {
vis[i] = true;
for (int j : g.get(i)) {
if (!vis[j]) {
dfs(g, j, vis, ordered);
}
}
ordered.add(i);
}
static Graph graph(int n) {
Graph g = new Graph();
for (int i = 0; i < n; ++i) {
g.add(new ArrayList<>());
}
return g;
}
static Graph graph(int n, int m) throws IOException {
Graph g = graph(n);
for (int i = 0; i < m; ++i) {
g.cto(rni() - 1, ni() - 1);
}
return g;
}
static Graph tree(int n) throws IOException {
return graph(n, n - 1);
}
static class Graph extends ArrayList<List<Integer>> {
void cto(int u, int v) {
get(u).add(v);
}
void c(int u, int v) {
cto(u, v);
cto(v, u);
}
boolean is_leaf(int i) {
return get(i).size() == 1;
}
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __rand = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int) d;}
static int cei(double d) {return (int) ceil(d);}
static long fll(double d) {return (long) d;}
static long cel(double d) {return (long) ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int lcm(int a, int b) {return a * b / gcf(a, b);}
static long lcm(long a, long b) {return a * b / gcf(a, b);}
static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static double rd() throws IOException {return Double.parseDouble(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
static char[] rcha() throws IOException {return rline().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {prln("yes");}
static void pry() {prln("Yes");}
static void prY() {prln("YES");}
static void prno() {prln("no");}
static void prn() {prln("No");}
static void prN() {prln("NO");}
static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
static void h() {prln("hlfd"); flush();}
static void flush() {__out.flush();}
static void close() {__out.close();}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
00ca9e43ec85363e081771702c0119ae
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main
{
int n,cost[],time;
Pair exit[];
ArrayList<Integer> list[],revlist[];
boolean visited[];
long ans,cnt;
final long mod=(long)(1e9+7);
private void solve()throws IOException
{
n=nextInt();
cost=new int[n+1];
for(int i=1;i<=n;i++)
cost[i]=nextInt();
int m=nextInt();
list=new ArrayList[n+1];
for(int i=1;i<=n;i++)
list[i]=new ArrayList<>();
revlist=new ArrayList[n+1];
for(int i=1;i<=n;i++)
revlist[i]=new ArrayList<>();
while(m-->0)
{
int u=nextInt();
int v=nextInt();
list[u].add(v);
revlist[v].add(u);
}
exit=new Pair[n+1];
time=1;
visited=new boolean[n+1];
for(int i=1;i<=n;i++)
{
if(!visited[i])
dfs(i);
}
Arrays.sort(exit,1,n+1,new Comparator<Pair>(){
public int compare(Pair a,Pair b){
return a.second-b.second;
}
});
ans=0;
cnt=1;
visited=new boolean[n+1];
for(int i=n;i>=1;i--)
{
int v=exit[i].first;
if(visited[v])
continue;
Pair p=dfs2(v);
ans=ans+p.first;
cnt=(cnt*p.second)%mod;
}
out.println(ans+" "+cnt);
}
void dfs(int v)
{
visited[v]=true;
for(int vv:list[v])
if(!visited[vv])
dfs(vv);
exit[v]=new Pair(v,time++);
}
Pair dfs2(int v)
{
Pair ret=new Pair(cost[v],1);
visited[v]=true;
for(int vv:revlist[v])
if(!visited[vv])
{
Pair p=dfs2(vv);
if(p.first<ret.first)
ret=p;
else if(p.first==ret.first)
ret.second+=p.second;
}
return ret;
}
class Pair{
int first,second;
Pair(int a,int b){
first=a;
second=b;
}
}
///////////////////////////////////////////////////////////
public void run()throws IOException
{
br=new BufferedReader(new InputStreamReader(System.in));
st=null;
out=new PrintWriter(System.out);
solve();
br.close();
out.close();
}
public static void main(String args[])throws IOException{
new Main().run();
}
BufferedReader br;
StringTokenizer st;
PrintWriter out;
String nextToken()throws IOException{
while(st==null || !st.hasMoreTokens())
st=new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine()throws IOException{
return br.readLine();
}
int nextInt()throws IOException{
return Integer.parseInt(nextToken());
}
long nextLong()throws IOException{
return Long.parseLong(nextToken());
}
double nextDouble()throws IOException{
return Double.parseDouble(nextToken());
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
59d8dcb5972f72d39ec13d2377457794
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Stack;
public class Solution {
public static void topodfs(int src , HashMap<Integer , ArrayList<Integer>> map , Stack<Integer> st , HashSet<Integer> vis){
vis.add(src);
for(int i : map.get(src)){
if(!vis.contains(i)){
topodfs(i , map , st , vis);
}
}
st.push(src);
}
public static void dfs(int src , HashMap<Integer , ArrayList<Integer>> map , HashSet<Integer> vis , long a[] , long arr[]){
vis.add(src);
if(arr[src] < a[0]){
a[0] = arr[src];
a[1] = 1;
}
else if(arr[src] == a[0]){
a[1]++;
a[1] %= 1000000007;
}
for(int i : map.get(src)){
if(!vis.contains(i)){
dfs(i , map , vis , a , arr);
}
}
}
public static void main(String args[]){
Scanner scn = new Scanner(System.in);
HashMap<Integer , ArrayList<Integer>> graph = new HashMap<>();
int n = scn.nextInt();
long arr[] = new long[n + 1];
for(int i = 0 ; i < n ; i++){
arr[i + 1] = scn.nextLong();
graph.putIfAbsent(i+1 , new ArrayList<>());
}
int m = scn.nextInt();
for(int i = 0 ; i < m ; i++){
int a = scn.nextInt();
int b = scn.nextInt();
graph.get(a).add(b);
}
// starting with scc
//topo dfs
Stack<Integer> st = new Stack<>();
HashSet<Integer> vis = new HashSet<>();
for(int i = 1 ; i <= n ; i++){
if(!vis.contains(i)){
topodfs(i, graph, st, vis);
}
}
// reversing the graph
HashMap<Integer , ArrayList<Integer>> reverse = new HashMap<>();
for(int i = 0 ; i < n ; i++){
reverse.putIfAbsent(i+1 , new ArrayList<>());
}
for(int i : graph.keySet()){
for(int j : graph.get(i)){
reverse.get(j).add(i);
}
}
vis = new HashSet<>();
long mincost = 0;
long ways = 1;
while(st.size() != 0){
int a = st.peek();
if(vis.contains(a)){
st.pop();
continue;
}
long b[] = new long[2];
b[0] = Long.MAX_VALUE;
b[1] = 0;
dfs(a , reverse , vis , b , arr);
mincost += b[0];
// mincost %= 1000000007;
ways*= b[1]%1000000007;
ways %= 1000000007;
}
System.out.println(mincost + " " + ways);
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
11d0661925ef0ff9f3238ac754db2f60
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
public static void main(String sp[])throws IOException{
//Scanner sc = new Scanner(System.in);
FastReader sc = new FastReader();
int n = sc.nextInt();
long weight[] = new long[n+1];
for(int i=1;i<=n;i++){
weight[i] = sc.nextLong();
}
int q = sc.nextInt();
ArrayList<Integer> tree[] = new ArrayList[n+1];
ArrayList<Integer> revtree[] = new ArrayList[n+1];
for(int i=1;i<=n;i++){
tree[i] = new ArrayList<Integer>();
revtree[i] = new ArrayList<Integer>();
}
for(int i=1;i<=q;i++){
int u = sc.nextInt();
int v = sc.nextInt();
tree[u].add(v);
revtree[v].add(u);
}
for(int i=1;i<=n;i++){
if(!visited.containsKey(i))
dfs(i, tree);
}
visited = new HashMap<Integer,Integer>();
long ways = 1;
long total = 0;
long mod = (long)1e9+7;
while(stack.size()!=0){
int val = stack.pop();
if(!visited.containsKey(val)){
visited.put(val,0);
dfs2(val, revtree, weight);
total += max;
ways = (ways%mod * times%mod)%mod;
}
max = Long.MAX_VALUE;
times = 0;
}
System.out.println(total+" "+ways);
}
static Stack<Integer> stack = new Stack<>();
static HashMap<Integer,Integer> visited = new HashMap<>();
static long max = Long.MAX_VALUE;
static long times = 0;
static void dfs2(int root, ArrayList<Integer> revtree[], long weight[]){
if(weight[root]<max){
max = weight[root];
times=1;
}else if(weight[root]==max){
++times;
}
ArrayList<Integer> al = revtree[root];
int siz = al.size();
for(int i=0;i<siz;i++){
int child = al.get(i);
if(!visited.containsKey(child)){
visited.put(child,0);
dfs2(child, revtree, weight);
}
}
}
static void dfs(int root, ArrayList<Integer> tree[]){
ArrayList<Integer> al = tree[root];
int siz = al.size();
visited.put(root,0);
for(int i=0;i<siz;i++){
int child = al.get(i);
if(!visited.containsKey(child)){
dfs(child, tree);
}
}
stack.push(root);
return;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
/*public static class comp implements Comparator<pair>{
public int compare(pair o1, pair o2){
return Long.valueOf(o1.diff).compareTo(Long.valueOf(o2.diff));
}
}*/
static class FastReader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public FastReader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastReader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
7147cada5b6a24f625f93ad1c79f73bd
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
public static void main(String sp[])throws IOException{
//Scanner sc = new Scanner(System.in);
FastReader sc = new FastReader();
int n = sc.nextInt();
long weight[] = new long[n+1];
for(int i=1;i<=n;i++){
weight[i] = sc.nextLong();
}
int q = sc.nextInt();
ArrayList<Integer> tree[] = new ArrayList[n+1];
ArrayList<Integer> revtree[] = new ArrayList[n+1];
for(int i=1;i<=n;i++){
tree[i] = new ArrayList<Integer>();
revtree[i] = new ArrayList<Integer>();
}
for(int i=1;i<=q;i++){
int u = sc.nextInt();
int v = sc.nextInt();
tree[u].add(v);
revtree[v].add(u);
}
for(int i=1;i<=n;i++){
if(!visited.containsKey(i))
dfs(i, tree);
}
visited = new HashMap<Integer,Integer>();
long ways = 1;
long total = 0;
long mod = (long)1e9+7;
while(stack.size()!=0){
int val = stack.pop();
if(!visited.containsKey(val)){
visited.put(val,0);
dfs2(val, revtree, weight);
total += max;
ways = (ways%mod * times%mod)%mod;
}
max = Long.MAX_VALUE;
times = 0;
}
System.out.println(total+" "+ways);
}
static Stack<Integer> stack = new Stack<>();
static HashMap<Integer,Integer> visited = new HashMap<>();
static long max = Long.MAX_VALUE;
static long times = 0;
static void dfs2(int root, ArrayList<Integer> revtree[], long weight[]){
if(weight[root]<max){
max = weight[root];
times=1;
}else if(weight[root]==max){
++times;
}
ArrayList<Integer> al = revtree[root];
int siz = al.size();
for(int i=0;i<siz;i++){
int child = al.get(i);
if(!visited.containsKey(child)){
visited.put(child,0);
dfs2(child, revtree, weight);
}
}
}
static void dfs(int root, ArrayList<Integer> tree[]){
ArrayList<Integer> al = tree[root];
int siz = al.size();
visited.put(root,0);
for(int i=0;i<siz;i++){
int child = al.get(i);
if(!visited.containsKey(child)){
dfs(child, tree);
}
}
stack.push(root);
return;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
/*public static class comp implements Comparator<pair>{
public int compare(pair o1, pair o2){
return Long.valueOf(o1.diff).compareTo(Long.valueOf(o2.diff));
}
}*/
static class FastReader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public FastReader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastReader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
95b0f9360971ce50518c7d7e1a3d7fc1
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.util.LinkedList;
// This class represents a directed graph using adjacency list
// representation
public class Graph
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; //Adjacency List
//Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
//Function to add an edge into the graph
void addEdge(int v, int w) { adj[v].add(w); }
// A recursive function to print DFS starting from v
void DFSUtil(int v,boolean visited[],ArrayList<Integer> ls)
{
// Mark the current node as visited and print it
visited[v] = true;
//System.out.print(v + " ");
ls.add(v);
int n;
// Recur for all the vertices adjacent to this vertex
Iterator<Integer> i =adj[v].iterator();
while (i.hasNext())
{
n = i.next();
if (!visited[n])
DFSUtil(n,visited,ls);
}
}
// Function that returns reverse (or transpose) of this graph
Graph getTranspose()
{
Graph g = new Graph(V);
for (int v = 0; v < V; v++)
{
// Recur for all the vertices adjacent to this vertex
Iterator<Integer> i =adj[v].listIterator();
while(i.hasNext())
g.adj[i.next()].add(v);
}
return g;
}
void fillOrder(int v, boolean visited[], Stack stack)
{
// Mark the current node as visited and print it
visited[v] = true;
// Recur for all the vertices adjacent to this vertex
Iterator<Integer> i = adj[v].iterator();
while (i.hasNext())
{
int n = i.next();
if(!visited[n])
fillOrder(n, visited, stack);
}
// All vertices reachable from v are processed by now,
// push v to Stack
stack.push(new Integer(v));
}
// The main function that finds and prints all strongly
// connected components
void printSCCs(ArrayList<Integer>[] compos, int currShit)
{
Stack stack = new Stack();
// Mark all the vertices as not visited (For first DFS)
boolean visited[] = new boolean[V];
for(int i = 0; i < V; i++)
visited[i] = false;
// Fill vertices in stack according to their finishing
// times
for (int i = 0; i < V; i++)
if (visited[i] == false)
fillOrder(i, visited, stack);
// Create a reversed graph
Graph gr = getTranspose();
// Mark all the vertices as not visited (For second DFS)
for (int i = 0; i < V; i++)
visited[i] = false;
// Now process all vertices in order defined by Stack
while (stack.empty() == false)
{
// Pop a vertex from stack
int v = (int)stack.pop();
// Print Strongly connected component of the popped vertex
if (visited[v] == false)
{
gr.DFSUtil(v, visited, compos[currShit]);
//System.out.println();
currShit++;
}
}
}
public static void main(String omkar[]) throws Exception
{
//BufferedReader infile = new BufferedReader(new FileReader("cowdate.in"));
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
Graph g = new Graph(N);
int M = Integer.parseInt(infile.readLine());
for(int i=0; i < M; i++)
{
st = new StringTokenizer(infile.readLine());
int a = Integer.parseInt(st.nextToken())-1;
int b = Integer.parseInt(st.nextToken())-1;
g.addEdge(a, b);
}
ArrayList<Integer>[] comps = new ArrayList[N+1];
for(int i=0; i <= N; i++)
comps[i] = new ArrayList<Integer>();
g.printSCCs(comps, 0);
//solve
long MOD = 1000000007L;
long res = 0L;
long cntR = 1L;
for(int a=0; a <= N; a++)
if(comps[a].size() > 0)
{
ArrayList<Integer> ls = comps[a];
int min = arr[ls.get(0)];
int cnt = 1;
for(int i=1; i < ls.size(); i++)
{
if(arr[ls.get(i)] < min)
{
min = arr[ls.get(i)];
cnt = 1;
}
else if(arr[ls.get(i)] == min)
cnt++;
}
res += min;
cntR *= cnt;
cntR %= MOD;
}
System.out.println(res+" "+cntR);
}
}
// This code is contributed by Aakash Hasija
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
58caadc2dcbcca59a6dfd20d753122a4
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
//package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner in = new Scanner(System.in);
int t1=1;
while(t1>0){
int n = in.nextInt();
int[] cost = new int[n];
for(int i=0;i<n;i++) {
cost[i] = in.nextInt();
}
int m = in.nextInt();
graph g = new graph(n);
g.cost = cost;
for(int i=0;i<m;i++){
int x1 = in.nextInt();
int y1 = in.nextInt();
g.addEdge(x1,y1);
}
int flag=0;
for(int i=0;i<n;i++){
if(g.visit[i]==0)
g.dfs(i);
}
g.visit = new int[n];
long cst=0;
long ways = 1;
while(g.s.size()!=0){
int cur = g.s.pop();
if(g.visit[cur]==0){
g.wg = 0;
cst = cst+g.df(cur,cost[cur]);
ways= mod(mod(ways)*mod(g.wg));
}
}
System.out.println(cst+" "+(ways));
t1--;
}
}
public static long mod(long x){
return x%1000000007;
}
}
class graph {
int n;
int[] visit;
int[] cost;
LinkedList<Integer> Edges[];
LinkedList<Integer> rev[];
Stack<Integer> s;
public graph(int ni) {
s = new Stack<>();
n = ni;
Edges = new LinkedList[n];
rev = new LinkedList[n];
visit = new int[n];
for (int i = 0; i < n; i++){
Edges[i] = new LinkedList<>();
rev[i] = new LinkedList<>();
}
}
public void addEdge(int x, int y) {
Edges[x - 1].add(y - 1);
rev[y-1].add(x-1);
}
long wg;
public int df(int i,int mi) {
visit[i] = 1;
if(mi==cost[i]){
wg+= 1;
}
else if(mi>cost[i]){
wg=1;
mi = cost[i];
}
Iterator<Integer> it = rev[i].iterator();
while(it.hasNext()){
int cur = it.next();
if(visit[cur]==0) mi = df(cur,mi);
}
return mi;
}
public void dfs(int i) {
visit[i] = 1;
Iterator<Integer> it = Edges[i].iterator();
while(it.hasNext()){
int cur = it.next();
if(visit[cur]==0) dfs(cur);
}
s.push(i);
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
46583a0d22b777bb7ea24a13bb1ceb35
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
//package com.company;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.sql.SQLOutput;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(System.in);
StringBuilder sb=new StringBuilder();
//BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb2 = new StringBuilder();//int n=Integer.parseInt(s.readLine());
int n=s.nextInt();
long[] c=new long[n+1];
for(int i=1;i<=n;i++){
c[i]=s.nextLong();
}
int m=s.nextInt();
ArrayList<Integer>[] adj=new ArrayList[n+1];
ArrayList<Integer>[] radj=new ArrayList[n+1];
vis=new int[n+1];
for(int i=0;i<m;i++){
int u=s.nextInt();int v=s.nextInt();
if(adj[u]==null) adj[u]=new ArrayList<Integer>();
adj[u].add(v);
if(radj[v]==null) radj[v]=new ArrayList<Integer>();
radj[v].add(u);
}long ans=0;
st=new Stack<Integer>();
for(int i=1;i<=n;i++){
if(vis[i]==0)
dfs(i,adj,vis);
}
vis=new int[n+1];long a2=1;
while(!st.isEmpty()){
while(!st.isEmpty()&&vis[st.peek()]==1)
st.pop();
if(st.isEmpty()) break;
int i=st.pop();
// System.out.println(i);
count=0;min=c[i];
dfs1(c,c[i],i,radj,vis);
ans+=min;
a2=(a2%1000000007*count%1000000007)%1000000007;
}
System.out.println(ans+" "+a2);
}static Stack<Integer> st;
static int[] vis;
static long min;static long count;
static void dfs1(long[] c,long min1,int i,ArrayList<Integer>[] radj,int[] vis1){
vis[i]=1;
if(c[i]==min) count++;
else if(c[i]<min){
count=1;
}
min=Math.min(c[i],min);
// if(adj[i]==null) return;
if(radj[i]!=null){
for(int j:radj[i]){
if(vis[j]==0){
dfs1(c,min,j,radj,vis);
}
}
}}
static void dfs(int i,ArrayList<Integer>[] adj,int[] vis){
vis[i]=1;
if(adj[i]!=null){for(int j:adj[i]){
if(vis[j]==0){
dfs(j,adj,vis);
}
}
}
st.push(i);
}
static void computeLPSArray(String pat, int M, int lps[])
{
int len = 0;
int i = 1;
lps[0] = 0;
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
if (len != 0) {
len = lps[len - 1];
}
else // if (len == 0)
{
lps[i] = len;
i++;
}
}
}
}
static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static double power(double x, long y, int p) {
double res = 1;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static void fracion(double x) {
String a = "" + x;
String spilts[] = a.split("\\."); // split using decimal
int b = spilts[1].length(); // find the decimal length
int denominator = (int) Math.pow(10, b); // calculate the denominator
int numerator = (int) (x * denominator); // calculate the nerumrator Ex
// 1.2*10 = 12
int gcd = (int)gcd((long)numerator, denominator); // Find the greatest common
// divisor bw them
String fraction = "" + numerator / gcd + "/" + denominator / gcd;
// System.out.println((denominator/gcd));
long x1=modInverse(denominator / gcd,998244353 );
// System.out.println(x1);
System.out.println((((numerator / gcd )%998244353 *(x1%998244353 ))%998244353 ) );
}static int anst=Integer.MAX_VALUE;
static StringBuilder sb1=new StringBuilder();
public static int bfs(int i1, ArrayList<Integer>[] h, int[] vis, int n,int val1) {
Queue<Integer> q = new LinkedList<Integer>();
q.add(i1);Queue<Integer> aq=new LinkedList<Integer>();
aq.add(0);
while(!q.isEmpty()){
int i=q.poll();
int val=aq.poll();
if(i==n){
return val;
}
if(h[i]!=null){
for(Integer j:h[i]){
if(vis[j]==0){
q.add(j);vis[j]=1;
aq.add(val+1);}
}
}
}return -1;
}
static int i(String a) {
return Integer.parseInt(a);
}
static long l(String a) {
return Long.parseLong(a);
}
static String[] inp() throws IOException {
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
return s.readLine().trim().split("\\s+");
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long modInverse(int a, int m)
{
{
// If a and m are relatively prime, then modulo inverse
// is a^(m-2) mode m
return (power(a, m - 2, m));
}
}
// To compute x^y under modulo m
static long power(int x, int y, int m)
{
if (y == 0)
return 1;
long p = power(x, y / 2, m) % m;
p = (p * p) % m;
if (y % 2 == 0)
return p;
else
return (x * p) % m;
}
// Function to return gcd of a and b
}
class Student
{
int l;int r;
public Student(int l, int r) {
this.l = l;
this.r = r;
}
// Constructor
// Used to print student details in main()
public String toString()
{
return this.l+" ";
}
}
class Sortbyroll implements Comparator<Student>
{
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b){
if(a.l==b.l) return a.r-b.r;
return b.l-a.l; }
} class Sortbyroll1 implements Comparator<Student>
{
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b){
if(a.r==b.r) return a.l-b.l;
return b.l-a.l;
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
2ae7e88cfdb47887596599a8c7892c6d
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Checkposts {
private int time;
ArrayList<Integer> stack;
public static void main(String[] args) throws IOException{
new Checkposts().solve(new BufferedReader(new InputStreamReader(System.in)));
}
public void solve(BufferedReader f) throws IOException {
int n = Integer.parseInt(f.readLine());
int[] cost = new int[n];
int mod = 1000000007;
StringTokenizer tokenizer = new StringTokenizer(f.readLine());
for (int i = 0; i < n; i++) {
cost[i] = Integer.parseInt(tokenizer.nextToken());
}
ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>();
int m = Integer.parseInt(f.readLine());
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<Integer>());
}
for (int i = 0; i < m; i++) {
tokenizer = new StringTokenizer(f.readLine());
int a = Integer.parseInt(tokenizer.nextToken()) - 1;
int b = Integer.parseInt(tokenizer.nextToken()) - 1;
adj.get(a).add(b);
}
int[] inTime = new int[n];
int[] upTime = new int[n];
Arrays.fill(inTime, -1);
ArrayList<ArrayList<Integer>> components = new ArrayList<ArrayList<Integer>>();
boolean[] visited = new boolean[n];
stack = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
this.time = 0;
if (inTime[i] == -1) {
tarjan(i, components, adj, inTime, upTime, visited);
}
}
long ways = 1;
long sumOfCosts = 0;
for (ArrayList<Integer> component : components) {
int minCost = Integer.MAX_VALUE;
int amountWithMinCost = 0;
for (int i : component) {
if (cost[i] < minCost) {
minCost = cost[i];
amountWithMinCost = 0;
}
if (cost[i] == minCost) {
amountWithMinCost++;
}
}
sumOfCosts += minCost;
ways *= amountWithMinCost;
ways %= mod;
}
System.out.println(sumOfCosts + " " + ways);
}
private void tarjan(int node, ArrayList<ArrayList<Integer>> components, ArrayList<ArrayList<Integer>> adj, int[] inTime, int[] upTime, boolean[] visited) {
stack.add(node);
time++;
inTime[node] = time;
upTime[node] = time;
for (int v : adj.get(node)) {
if (visited[v]) {
continue;
}
if (inTime[v] == -1) {
tarjan(v, components, adj, inTime, upTime, visited);
upTime[node] = Math.min(upTime[node], upTime[v]);
}
upTime[node] = Math.min(upTime[node], upTime[v]);
}
if (inTime[node] == upTime[node]) {
ArrayList<Integer> component = new ArrayList<Integer>();
while (stack.get(stack.size() - 1) != node) {
component.add(stack.get(stack.size() - 1));
visited[stack.get(stack.size() - 1)] = true;
stack.remove(stack.size() - 1);
}
component.add(stack.get(stack.size() - 1));
visited[stack.get(stack.size() - 1)] = true;
stack.remove(stack.size() - 1);
components.add(component);
}
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
b1a03830159c05bcdebdb5245a9f21b3
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Checkposts {
private int time;
ArrayList<Integer> stack;
public static void main(String[] args) throws IOException{
new Checkposts().solve(new BufferedReader(new InputStreamReader(System.in)));
}
public void solve(BufferedReader f) throws IOException {
int n = Integer.parseInt(f.readLine());
int[] cost = new int[n];
int mod = 1000000007;
StringTokenizer tokenizer = new StringTokenizer(f.readLine());
for (int i = 0; i < n; i++) {
cost[i] = Integer.parseInt(tokenizer.nextToken());
}
ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>();
int m = Integer.parseInt(f.readLine());
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<Integer>());
}
for (int i = 0; i < m; i++) {
tokenizer = new StringTokenizer(f.readLine());
int a = Integer.parseInt(tokenizer.nextToken()) - 1;
int b = Integer.parseInt(tokenizer.nextToken()) - 1;
adj.get(a).add(b);
}
int[] inTime = new int[n];
int[] upTime = new int[n];
Arrays.fill(inTime, -1);
ArrayList<ArrayList<Integer>> components = new ArrayList<ArrayList<Integer>>();
boolean[] visited = new boolean[n];
stack = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
this.time = 0;
if (inTime[i] == -1) {
tarjan(i, components, adj, inTime, upTime, visited);
}
}
long ways = 1;
long sumOfCosts = 0;
for (ArrayList<Integer> component : components) {
int minCost = Integer.MAX_VALUE;
int amountWithMinCost = 0;
for (int i : component) {
if (cost[i] < minCost) {
minCost = cost[i];
amountWithMinCost = 0;
}
if (cost[i] == minCost) {
amountWithMinCost++;
}
}
sumOfCosts += minCost;
ways *= amountWithMinCost;
ways %= mod;
}
System.out.println(sumOfCosts + " " + ways);
}
private void tarjan(int node, ArrayList<ArrayList<Integer>> components, ArrayList<ArrayList<Integer>> adj, int[] inTime, int[] upTime, boolean[] visited) {
stack.add(node);
time++;
inTime[node] = time;
upTime[node] = time;
for (int v : adj.get(node)) {
if (visited[v]) {
continue;
}
if (inTime[v] == -1) {
tarjan(v, components, adj, inTime, upTime, visited);
upTime[node] = Math.min(upTime[node], upTime[v]);
}
upTime[node] = Math.min(upTime[node], upTime[v]);
}
if (inTime[node] == upTime[node]) {
ArrayList<Integer> component = new ArrayList<Integer>();
while (stack.get(stack.size() - 1) != node) {
component.add(stack.get(stack.size() - 1));
visited[stack.get(stack.size() - 1)] = true;
stack.remove(stack.size() - 1);
}
component.add(stack.get(stack.size() - 1));
visited[stack.get(stack.size() - 1)] = true;
stack.remove(stack.size() - 1);
components.add(component);
}
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
39bb4d8e18ad998e308a61b97bdfa9b8
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Checkposts {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(f.readLine());
int[] cost = new int[n];
int mod = 1000000007;
StringTokenizer tokenizer = new StringTokenizer(f.readLine());
for (int i = 0; i < n; i++) {
cost[i] = Integer.parseInt(tokenizer.nextToken());
}
ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>();
int m = Integer.parseInt(f.readLine());
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<Integer>());
}
for (int i = 0; i < m; i++) {
tokenizer = new StringTokenizer(f.readLine());
int a = Integer.parseInt(tokenizer.nextToken()) - 1;
int b = Integer.parseInt(tokenizer.nextToken()) - 1;
adj.get(a).add(b);
}
int[] inTime = new int[n];
int[] upTime = new int[n];
Arrays.fill(inTime, -1);
ArrayList<ArrayList<Integer>> components = new ArrayList<ArrayList<Integer>>();
boolean[] visited = new boolean[n];
for (int i = 0; i < n; i++) {
if (inTime[i] == -1) {
tarjan(0, i, components, adj, new ArrayDeque<Integer>(), inTime, upTime, visited);
}
}
long ways = 1;
long sumOfCosts = 0;
for (ArrayList<Integer> component : components) {
int minCost = Integer.MAX_VALUE;
int amountWithMinCost = 0;
for (int i : component) {
if (cost[i] < minCost) {
minCost = cost[i];
amountWithMinCost = 0;
}
if (cost[i] == minCost) {
amountWithMinCost++;
}
}
sumOfCosts += minCost;
ways *= amountWithMinCost;
ways %= mod;
}
System.out.println(sumOfCosts + " " + ways);
}
private static int tarjan(int time, int node, ArrayList<ArrayList<Integer>> components, ArrayList<ArrayList<Integer>> adj, ArrayDeque<Integer> stack, int[] inTime, int[] upTime, boolean[] visited) {
inTime[node] = time;
upTime[node] = time;
stack.push(node);
for (int v : adj.get(node)) {
if (visited[v]) {
continue;
}
if (inTime[v] == -1) {
time = tarjan(time + 1, v, components, adj, stack, inTime, upTime, visited) + 1;
upTime[node] = Math.min(upTime[node], upTime[v]);
}
upTime[node] = Math.min(upTime[node], upTime[v]);
}
if (inTime[node] == upTime[node]) {
int part = -1;
ArrayList<Integer> component = new ArrayList<Integer>();
while (part != node) {
part = stack.poll();
visited[part] = true;
component.add(part);
}
components.add(component);
}
return time;
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
5c7a3b62f27fa09f76c88aa900c2c595
|
train_001.jsonl
|
1399044600
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either iβ=βj or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Set;
import java.util.InputMismatchException;
import java.util.HashMap;
import java.io.IOException;
import java.util.Stack;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Vector;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CCheckposts solver = new CCheckposts();
solver.solve(1, in, out);
out.close();
}
static class CCheckposts {
public void solve(int testNumber, FastReader s, PrintWriter out) {
int n = s.nextInt();
long[] arr = s.nextLongArray(n);
int m = s.nextInt();
CCheckposts.Graph g = new CCheckposts.Graph(n, true);
g.takeInput(m, s);
ArrayList<ArrayList<Integer>> ans = g.stronglyConnectedComponents();
// out.println(ans);
long ans1 = 1L;
long ans2 = 0L;
long mod = 1000000007L;
for (ArrayList<Integer> a : ans) {
ArrayList<Long> prices = new ArrayList<>();
for (int i = 0; i < a.size(); i++) {
prices.add(arr[a.get(i)]);
}
Collections.sort(prices);
int count = 0;
for (int i = 0; i < prices.size(); i++) {
if (prices.get(i).equals(prices.get(0))) {
count++;
}
}
ans2 += prices.get(0);
ans1 = ((ans1 % mod) * (count % mod)) % mod;
}
out.println(ans2 + " " + ans1);
}
private static class Graph {
HashMap<Integer, HashSet<Integer>> neighbours;
int numVertices;
boolean isDirected;
public Graph(int numVertices, boolean isDirected) {
neighbours = new HashMap<>();
this.numVertices = numVertices;
this.isDirected = isDirected;
}
public void takeInput(int n, FastReader s) {
for (int i = 0; i < n; i++) {
int src = s.nextInt() - 1;
int dest = s.nextInt() - 1;
addEdge(src, dest);
}
}
private void addEdge(int src, int dest) {
HashSet<Integer> list = neighbours.getOrDefault(src, new HashSet<>());
list.add(dest);
neighbours.put(src, list);
if (!this.isDirected) {
list = neighbours.getOrDefault(dest, new HashSet<>());
list.add(src);
neighbours.put(dest, list);
}
}
private void dfsHelper(boolean[] visited, int src, ArrayList<Integer> list) {
// System.out.print(src + " ");
list.add(src);
visited[src] = true;
ArrayList<Integer> set = new ArrayList<>(this.neighbours.getOrDefault(src, new HashSet<>()));
for (Integer next : set) {
if (!visited[next]) {
dfsHelper(visited, next, list);
}
}
}
private Stack<Integer> topologicalSort() {
Stack<Integer> stack = new Stack<>();
boolean[] visited = new boolean[numVertices];
for (int i = 0; i < numVertices; i++) {
if (!visited[i]) {
topologicalSortHelper(stack, visited, i);
}
}
return stack;
}
private void topologicalSortHelper(Stack<Integer> stack, boolean[] visited, int src) {
visited[src] = true;
ArrayList<Integer> list = new ArrayList<>(neighbours.getOrDefault(src, new HashSet<>()));
for (int i = 0; i < list.size(); i++) {
if (!visited[list.get(i)]) {
topologicalSortHelper(stack, visited, list.get(i));
}
}
stack.add(src);
}
private ArrayList<ArrayList<Integer>> stronglyConnectedComponents() {
Stack<Integer> stack = topologicalSort();
CCheckposts.Graph g1 = reverseGraph();
boolean[] visited = new boolean[numVertices];
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
while (!stack.isEmpty()) {
int curr = stack.pop();
if (!visited[curr]) {
ArrayList<Integer> list1 = new ArrayList<>();
g1.dfsHelper(visited, curr, list1);
list.add(list1);
// System.out.println();
}
}
return list;
}
private CCheckposts.Graph reverseGraph() {
CCheckposts.Graph g1 = new CCheckposts.Graph(this.numVertices, this.isDirected);
Iterator<Integer> iter = neighbours.keySet().iterator();
HashMap<Integer, HashSet<Integer>> map = g1.neighbours;
while (iter.hasNext()) {
int curr = iter.next();
HashSet<Integer> map1 = neighbours.get(curr);
ArrayList<Integer> list = new ArrayList<>(map1);
for (int a : list) {
HashSet<Integer> list1 = map.getOrDefault(a, new HashSet<>());
list1.add(curr);
map.put(a, list1);
}
}
return g1;
}
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public 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 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 long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
return array;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
Java
|
["3\n1 2 3\n3\n1 2\n2 3\n3 2", "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1", "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9", "2\n7 91\n2\n1 2\n2 1"]
|
2 seconds
|
["3 1", "8 2", "15 6", "7 1"]
| null |
Java 11
|
standard input
|
[
"two pointers",
"dfs and similar",
"graphs"
] |
ee576fd64305ab99b2e0442aad6199d8
|
In the first line, you will be given an integer n, number of junctions (1ββ€βnββ€β105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109). The next line will contain an integer mΒ (0ββ€βmββ€β3Β·105). And each of the next m lines contains two integers ui and viΒ (1ββ€βui,βviββ€βn;Β uββ βv). A pair ui,βvi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
| 1,700 |
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109β+β7).
|
standard output
| |
PASSED
|
4b85e344bc6721980af21e04aa6846f3
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
//package q1;
import java.util.*;
public final class Q1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n],max=Integer.MIN_VALUE,index=0,b_r=0,b_l=0,l,r;
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
if(a[i]>max)
{
max=a[i];
index=i;
}
}
l=index-1;
r=index+1;
//System.out.println("index is "+index+" l is "+l+" r is "+r);
if(index==0)
{
for(int i=1;i<n;i++)
{
if(a[i]>a[i-1]){b_l=1;b_r=1;break;}
}
}
else if(index==n-1)
{
for(int i=n-2;i>=0;i--)
{
if(a[i]>a[i+1]){{b_l=1;b_r=1;break;}}
}
}
else{
for(int i=l;i>=0;i--)
{
// System.out.println(" in fgfgfgf "+i);
if(a[i]>a[i+1]){b_l=1;break;}
}
for(int i=r;i<n;i++)
{
//System.out.println(" r is "+i);
if(a[i]>a[i-1]) {b_r=1;break;}
}
}
//System.out.println("b_l "+b_l+" b_r "+b_r);
if(b_l==1)System.out.println("NO");
else if(b_r==1)System.out.println("NO");
else System.out.println("YES");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
3cf6386611bacd586f5053fceaa5f8bd
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.Scanner;
public class Pillars {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int i;
int[] ar= new int[n];
for(i=0;i<n;i++)
ar[i]=in.nextInt();
int d = n/2;
// d--;
// if(n%2!=0)
// d++;
//
int c=1;
if(n%2!=0)
c=0;
int as = ar[0];
for(i=0;i<n;i++) {
if(as<=ar[i]) {
as=ar[i];
d=i;
}
}
// System.out.println(d);
boolean yes = true;
for(i=1;i<n;i++) {
if(i<=d) {
if(ar[i]-ar[i-1]>0) {
c++;
}
else {
yes=false;
break;
}
}else {
if(ar[i]-ar[i-1]<0) {
c++;
}else {
yes=false;
break;
}
}
}
// System.out.println(c);
if(yes)
System.out.println("YES");
else
System.out.println("NO");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
e60ccf765c47725cf60a22e4a4da2c44
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Ladder {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
int i ,j=-1, n, m;
m= Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] ar = new int[m];
int large=Integer.MIN_VALUE;
for(i=0;i<m;i++){
ar[i]=Integer.parseInt(st.nextToken());
if(ar[i]>large)
{
large=ar[i];
j=i;
}
}
for(i=0;i<j;i++)
{
if(ar[i]>ar[i+1]){
out.print("NO");
out.flush();
out.close();
}
}
for(i=m-1;i>j;i--)
{
if(ar[i]>ar[i-1]){
out.print("NO");
out.flush();
out.close();
}
}
out.print("YES");
out.flush();
out.close();
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
5a239700b15030a32c198f68a0c8d4d4
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Pillars
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int []pil=new int[n];
HashMap<Integer,Integer> hashMap=new HashMap<>();
for (int i = 0; i <n; i++)
pil[i]=scanner.nextInt()-1;
for (int i = 0; i <n ; i++)
{
hashMap.put(pil[i],i);
}
int next=n-2;
int front=hashMap.get(next+1)+1;
int back=hashMap.get(next+1)-1;
while (next>=0)
{
if(hashMap.get(next)==back)
{
back--;
hashMap.remove(next+1);
next--;
}
else if(hashMap.get(next)==front)
{
front++;
hashMap.remove(next+1);
next--;
}
else
{
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
6fab8e46c77d72b67edcb3fd8c3b9e23
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
public class Main
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
int i, max=0, x=-1;
for(i=0;i<n;i++){
a[i]=in.nextInt();
if(a[i]>max){
max=a[i];
x=i;
}
}
boolean flag = true;
for(i=0;i<x;i++){
if(a[i]>=a[i+1]){
flag = false;
break;
}
}
for(i=x+1;i<n;i++){
if(a[i]>=a[i-1]){
flag = false;
break;
}
}
if(flag)
System.out.println("YES");
else
System.out.println("NO");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
789b34549cfed6a57be5386fa9f81e67
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.Scanner;
public class pre21
{
public static void main(String args[])
{
Scanner obj = new Scanner(System.in);
int n = obj.nextInt();
int[] arr = new int[n];
int max = Integer.MIN_VALUE,index=-1;
for(int i=0;i<n;i++)
{
arr[i] = obj.nextInt();
if(arr[i]>max)
{
max = arr[i];
index=i;
}
}
boolean flag = true;
for(int i=0;i<index;i++)
flag &= (arr[i+1]>arr[i]);
for(int i=index+1;i<n;i++)
flag &= (arr[i-1]>arr[i]);
System.out.println(flag?"YES":"NO");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
62c446a70c71f3ad22d8ab3fdef62f7e
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Jenish
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BPillars solver = new BPillars();
solver.solve(1, in, out);
out.close();
}
static class BPillars {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int n = in.scanInt();
int arr[] = new int[n];
in.scanInt(arr);
int index = -1;
int max = -1;
for (int i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
index = i;
}
}
int p1 = index + 1;
int p2 = index - 1;
int top = max;
boolean f = true;
while (p1 < n || p2 >= 0) {
if (p1 < n && p2 >= 0) {
if (arr[p1] > arr[p2]) {
if (top > arr[p1]) {
top = arr[p1];
p1++;
} else {
f = false;
break;
}
} else {
if (top > arr[p2]) {
top = arr[p2];
p2--;
} else {
f = false;
break;
}
}
continue;
}
if (p1 < n) {
if (top > arr[p1]) {
top = arr[p1];
p1++;
} else {
f = false;
break;
}
}
if (p2 >= 0) {
if (top > arr[p2]) {
top = arr[p2];
p2--;
} else {
f = false;
break;
}
}
}
if (f) {
out.println("YES");
} else {
out.println("NO");
}
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int INDEX;
private BufferedInputStream in;
private int TOTAL;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (INDEX >= TOTAL) {
INDEX = 0;
try {
TOTAL = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (TOTAL <= 0) return -1;
}
return buf[INDEX++];
}
public int scanInt() {
int I = 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') {
I *= 10;
I += n - '0';
n = scan();
}
}
return neg * I;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
public void scanInt(int[] A) {
for (int i = 0; i < A.length; i++) A[i] = scanInt();
}
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
f9442a39173fc85b8a4cde6bcaff42c0
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Exam {
public static long mod = (long) Math.pow(10, 9)+7 ;
public static InputReader sc = new InputReader(System.in);
public static PrintWriter pw = new PrintWriter(System.out);
public static int countSetBits(int n)
{
int count = 0;
while (n > 0)
{
count += n & 1;
n >>= 1;
}
return count;
}
public static boolean [] sieveOfEratosthenes(int n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
boolean prime[] = new boolean[n+1];
Arrays.fill(prime, true);
for(int p = 2; p*p <=n; 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*p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
public static void main(String[] args) {
// Code starts..
int n=sc.nextInt();
int a[]=scanArray(n);
int flag=0;
int m=0;
int l[]=new int[n];
int r[]=new int[n];
for(int i=0;i<n-1;i++)
r[i]=i+1;
for(int i=1;i<n;i++)
l[i]=i-1;
for(int i=0;i<n;i++){
if(a[i]>a[m])
m=i;
}
for(int i=0;i<n;i++){
if(m>0&&a[m]-1==a[l[m]]){
r[l[m]]=r[m];
m=l[m];
}
else if(m<n-1&&a[m]-1==a[r[m]]){
l[r[m]]=l[m];
m=r[m];
}
else{
flag=1;
break;
}
if(a[m]==1)
break;
}
if(flag==1){
pw.println("NO");
}
else{
pw.println("Yes");
}
// Code ends...
pw.flush();
pw.close();
}
public static String reverseString(String s){
StringBuilder input1 = new StringBuilder();
input1.append(s);
input1 = input1.reverse();
return input1.toString();
}
public static int[] scanArray(int n){
int a[]=new int [n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
return a;
}
public static long[] scanLongArray(int n){
long a[]=new long [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLong();
return a;
}
public static String [] scanStrings(int n){
String a[]=new String [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLine();
return a;
}
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 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);
}
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
7534c30c6d81591208dc95efd6efcf73
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.zip.Inflater;
public class Main {
private static AWriter writer = new AWriter(System.out);
private static AReader reader = new AReader(System.in);
private static final int INF=0X3F3F3F3F;
private static int n,x,y;
private static int a[];
public static void main(String[] args) throws IOException {
int n = reader.nextInt();
a = new int[n];
int max=-INF;
int mInd=0;
for (int i = 0; i < n; i++) {
a[i] = reader.nextInt();
if(a[i]>max){
max=a[i];
mInd=i;
}
}
boolean flag=true;
for(int i=mInd+1;i<n;i++){
if(a[i]>a[i-1]) {
flag = false;
break;
}
}
for(int i=mInd-1;i>=0;i--){
if(a[i]>a[i+1]) {
flag = false;
break;
}
}
if(flag)
writer.println("YES");
else
writer.println("NO");
writer.close();
reader.close();
}
}
class AReader implements Closeable {
private BufferedReader reader;
private StringTokenizer tokenizer;
public AReader(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
tokenizer = new StringTokenizer("");
}
private String innerNextLine() {
try {
return reader.readLine();
} catch (IOException ex) {
return null;
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String nextLine = innerNextLine();
if (nextLine == null) {
return false;
}
tokenizer = new StringTokenizer(nextLine);
}
return true;
}
public String nextLine() {
tokenizer = new StringTokenizer("");
return innerNextLine();
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.valueOf(next());
}
public long nextLong() {
return Long.valueOf(next());
}
public double nextDouble() {
return Double.valueOf(next());
}
public BigDecimal nextBigDecimal(){ return new BigDecimal(next()); }
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
@Override
public void close() throws IOException {
reader.close();
}
}
// Fast writer for ACM By Azure99
class AWriter implements Closeable {
private BufferedWriter writer;
public AWriter(OutputStream outputStream) {
writer = new BufferedWriter(new OutputStreamWriter(outputStream));
}
public void print(Object object) throws IOException {
writer.write(object.toString());
}
public void println(Object object) throws IOException {
writer.write(object.toString());
writer.write("\n");
}
@Override
public void close() throws IOException {
writer.close();
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
20c7f606e49e606ef7710b595241b48d
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class tr1 {
static PrintWriter out;
static StringBuilder sb;
static int inf = (int) 1e9;
static int mod = (int) 1e9 + 7;
static int[] si;
static ArrayList<Integer> primes;
static HashSet<Integer> pr;
static int n, k, m;
static boolean[] in;
static HashMap<Integer, Integer> factors;
static HashSet<Integer> f;
static long[] fac;
static int[] l, r;
static long[][] memo;
static int[] a;
static ArrayList<Integer>[] ad;
static HashMap<Integer, Integer> hm;
static pair[] ed;
static ArrayList<Integer>[] np;
static TreeMap<pair, Integer> tm;
static int[] ans,h1,h2;
static char []h;
static int []po;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int n=sc.nextInt();
int []a=new int [n];
int max=0;
int id=0;
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
if(a[i]>max) {
max=a[i];
id=i;
}
}
int i=id-1;
int j=id+1;
boolean f=true;
int l=a[id];
for(int u=id;u<n-1;u++) {
if(a[u+1]>a[u])
f=false;
}
for(int u=id;u>0;u--) {
if(a[u-1]>a[u])
f=false;
}
if(f)
out.print("YES");
else
out.print("NO");
out.flush();
}
static long dp(int i,int st) {
// System.out.println(st+" th "+i);
if(i==h.length) {
//st*=inver(10);
// st%=13;
// System.out.println("HEY "+st);
if(st%13==5)
return 1;
return 0;
}
// System.out.println("hshsh "+(h.length-i)+" "+i);
// st*=po[h.length-i];
st%=13;
if(memo[i][st]!=-1)
return memo[i][st];
long ans=0;
if(h[i]=='?') {
for(int u='0';u<='9';u++) {
// System.out.println("utuuu "+((Integer.parseInt((u-'0')+""))*po[h.length-i-1]));
ans=(ans+dp(i+1,((st+((Integer.parseInt((u-'0')+""))*po[h.length-i-1]))%13)))%mod;
}
}
else {
ans=dp(i+1,((st+((Integer.parseInt((h[i]-'0')+""))*po[h.length-i-1]))%13));
}
return memo[i][st]=ans;
}
static int c = 0;
static boolean ff;
static Queue<StringBuilder> sq;
static void sol(int i) {
// System.out.println(i+" i");
if (i == 0) {
if (ff) {
sol(i + 1);
}
} else if (i == n) {
sb = new StringBuilder();
for (int l : ans)
sb.append(l + "");
c++;
// System.out.println(c);
sq.add(sb);
if (c == k)
ff = false;
} else {
for (int y = 0; y < np[i].size(); y++) {
ans[np[i].get(y)] = 1;
if (ff) {
sol(i + 1);
ans[np[i].get(y)] = 0;
}
if (!ff)
return;
}
}
}
static class pair {
int x;
int y;
int num;
pair(int f, int t,int n) {
x = f;
y = t;
num=n;
}
public String toString() {
return x+" "+y+" "+num;
}
}
public int[] merge(int[] d, int st, int e) {
if (st > e)
return null;
if (e == st) {
int[] ans = { d[e] };
return ans;
}
int mid = (st + e) / 2;
int[] ans = new int[e - st + 1];
int[] ll = merge(d, st, mid);
int[] rr = merge(d, mid + 1, e);
if (ll == null)
return rr;
if (rr == null)
return ll;
int iver = 0;
int idxl = 0;
int idxr = 0;
for (int i = st; i <= e; i++) {
if (ll[idxl] < rr[idxr]) {
}
}
return ans;
}
public static class pair1 implements Comparable<pair1> {
int a;
int idx;
pair1(int a, int i) {
this.a = a;
idx = i;
}
public String toString() {
return a + " " + idx;
}
@Override
public int compareTo(pair1 o) {
return o.a - a;
}
}
public static class pair2 implements Comparable<pair2> {
int a;
int idx;
pair2(int a, int i) {
this.a = a;
idx = i;
}
public String toString() {
return a + " " + idx;
}
@Override
public int compareTo(pair2 o) {
// TODO Auto-generated method stub
return idx - o.idx;
}
}
static int inver(long x) {
int a = (int) x;
int e = (13 - 2);
int res = 1;
while (e > 0) {
if ((e & 1) == 1) {
// System.out.println(res*a);
res = (int) ((1l * res * a) % 13);
}
a = (int) ((1l * a * a) % 13);
e >>= 1;
}
// out.println(res+" "+x);
return res % 13;
}
static int atMostSum(int arr[], int n, int k) {
int sum = 0;
int cnt = 0, maxcnt = 0;
for (int i = 0; i < n; i++) {
// If adding current element doesn't
// cross limit add it to current window
if ((sum + arr[i]) <= k) {
sum += arr[i];
cnt++;
}
// Else, remove first element of current
// window and add the current element
else if (sum != 0) {
sum = sum - arr[i - cnt] + arr[i];
}
// keep track of max length.
maxcnt = Math.max(cnt, maxcnt);
}
return maxcnt;
}
public static int[] longestSubarray(int[] inp) {
// array containing prefix sums up to a certain index i
int[] p = new int[inp.length];
p[0] = inp[0];
for (int i = 1; i < inp.length; i++) {
p[i] = p[i - 1] + inp[i];
}
// array Q from the description below
int[] q = new int[inp.length];
q[inp.length - 1] = p[inp.length - 1];
for (int i = inp.length - 2; i >= 0; i--) {
q[i] = Math.max(q[i + 1], p[i]);
}
int a = 0;
int b = 0;
int maxLen = 0;
int curr;
int[] res = new int[] { -1, -1 };
while (b < inp.length) {
curr = a > 0 ? q[b] - p[a - 1] : q[b];
if (curr >= 0) {
if (b - a > maxLen) {
maxLen = b - a;
res = new int[] { a, b };
}
b++;
} else {
a++;
}
}
return res;
}
static void factor(int n) {
if (si[n] == n) {
f.add(n);
return;
}
f.add(si[n]);
factor(n / si[n]);
}
static void seive() {
si = new int[1000001];
primes = new ArrayList<>();
int N = 1000001;
pr = new HashSet<>();
si[1] = 1;
for (int i = 2; i < N; i++) {
if (si[i] == 0) {
si[i] = i;
primes.add(i);
pr.add(i);
}
for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++)
si[primes.get(j) * i] = primes.get(j);
}
}
static public class SegmentTree { // 1-based DS, OOP
int N; // the number of elements in the array as a power of 2 (i.e. after padding)
int[] array, sTree, lazy;
SegmentTree(int[] in) {
array = in;
N = in.length - 1;
sTree = new int[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero
lazy = new int[N << 1];
build(1, 1, N);
}
public String toString() {
return Arrays.toString(sTree);
}
void build(int node, int b, int e) // O(n)
{
if (b == e)
sTree[node] = array[b];
else {
int mid = b + e >> 1;
build(node << 1, b, mid);
build(node << 1 | 1, mid + 1, e);
sTree[node] = gcd(sTree[node << 1], sTree[node << 1 | 1]);
}
}
void update_point(int index, int val) // O(log n)
{
index += N - 1;
sTree[index] += val;
while (index > 1) {
index >>= 1;
sTree[index] = sTree[index << 1] + sTree[index << 1 | 1];
}
}
void update_range(int i, int j, int val) // O(log n)
{
update_range(1, 1, N, i, j, val);
}
void update_range(int node, int b, int e, int i, int j, int val) {
if (i > e || j < b)
return;
if (b >= i && e <= j) {
sTree[node] += (e - b + 1) * val;
lazy[node] += val;
} else {
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node << 1, b, mid, i, j, val);
update_range(node << 1 | 1, mid + 1, e, i, j, val);
sTree[node] = sTree[node << 1] + sTree[node << 1 | 1];
}
}
void propagate(int node, int b, int mid, int e) {
lazy[node << 1] += lazy[node];
lazy[node << 1 | 1] += lazy[node];
sTree[node << 1] += (mid - b + 1) * lazy[node];
sTree[node << 1 | 1] += (e - mid) * lazy[node];
lazy[node] = 0;
}
int query(int i, int j) {
return query(1, 1, N, i, j);
}
int query(int node, int b, int e, int i, int j) // O(log n)
{
if (i > e || j < b)
return 0;
if (b >= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
// propagate(node, b, mid, e);
int q1 = query(node << 1, b, mid, i, j);
int q2 = query(node << 1 | 1, mid + 1, e, i, j);
return gcd(q1, q2);
}
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static class unionfind {
int[] p;
int[] size;
unionfind(int n) {
p = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
p[i] = i;
}
Arrays.fill(size, 1);
}
int findSet(int v) {
if (v == p[v])
return v;
return p[v] = findSet(p[v]);
}
boolean sameSet(int a, int b) {
a = findSet(a);
b = findSet(b);
if (a == b)
return true;
return false;
}
int max() {
int max = 0;
for (int i = 0; i < size.length; i++)
if (size[i] > max)
max = size[i];
return max;
}
void combine(int a, int b) {
a = findSet(a);
b = findSet(b);
if (a == b)
return;
if (size[a] > size[b]) {
p[b] = a;
size[a] += size[b];
} else {
p[a] = b;
size[b] += size[a];
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
84d1f7b7140f1545a9fa4d40b2e92342
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
public class A{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n+1];
int j=0,max=0;
for(int i=1;i<n+1;i++){
int num=sc.nextInt();
a[num]=i;
}
int left=0,right=0;
boolean flag=true;
j=a[n];
//System.out.println(j);
for(int i=n-1;i>0 && flag==true;i--){
if(j>a[i]){
if(left==j-a[i]-1){
left++;
}
else{
flag=false;
//System.out.println("else: "+i+" "+left+" "+(j-a[i]-1));
}
}
else if(j<a[i]){
if(right==a[i]-j-1){
right++;
}
else{
flag=false;
///System.out.println("else1: "+i);
}
}
}
if(flag==true)
System.out.println("YES");
else
System.out.println("NO");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
bfb71600c689c945927d6743a592359f
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
public class A{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n+1];
int j=0,max=0;
for(int i=1;i<n+1;i++){
a[sc.nextInt()]=i;
}
int left=0,right=0;
boolean flag=true;
j=a[n];
//System.out.println(j);
for(int i=n-1;i>0 && flag==true;i--){
if(j>a[i]){
if(left==j-a[i]-1){
left++;
}
else{
flag=false;
//System.out.println("else: "+i+" "+left+" "+(j-a[i]-1));
}
}
else if(j<a[i]){
if(right==a[i]-j-1){
right++;
}
else{
flag=false;
///System.out.println("else1: "+i);
}
}
}
if(flag==true)
System.out.println("YES");
else
System.out.println("NO");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
44fcbc226945f8d19a4f622fc9d3a2da
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
//@Manan Parmar
public class Solution2 implements Runnable {
public void run() {
InputReader sc = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=sc.nextInt();
boolean b=true;
ArrayList<Integer> a=new ArrayList<Integer>();
for(int i=0;i<n;i++)
a.add(sc.nextInt());
int x=a.indexOf(Collections.max(a));
for(int i=x;i<n-1;i++)
{
if(a.get(i)<=a.get(i+1))
{
b=false;
break;
}
}
for(int i=x;i>0;i--)
{
if(!b)
break;
if(a.get(i)<=a.get(i-1))
{
b=false;
break;
}
}
if(b)
out.println("YES");
else
out.println("NO");
out.close();
}
//========================================================================
boolean check(char a,char b)
{
if(a==b)
return true;
else
return false;
}
long binarySearch(long arr[], long x)
{
int l = 0, r = arr.length - 1,q=0;
while (l<=r) {
int m = l + (r - l) / 2;
if (arr[l] >=x)
{
q=1;
return l;
}
if (arr[m] < x)
l=m+1;
else
r=m;
}
return -1;
}
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 Solution2(),"Main",1<<27).start();
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
aeb8b612d726a8b0a1a34e244de12df9
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class cf2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int[] arr1 = new int[n];
int[] arr2 = new int[n];
int[] arr3 = new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
arr1[i]=i;
}
for(int i=0;i<n;i++) {
arr2[i]=arr[i];
}
mergeSort(arr,arr1,arr3,0,n-1);
int flag =0;
for(int i=0;i<arr1[n-1];i++) {
if(arr2[i]>arr2[i+1]) {
flag=1;
}
}
for(int i=arr1[n-1]+1;i<n-1;i++) {
if(arr1[n-1]+1<n) {
if(arr2[i]<arr2[i+1]) {
flag=1;
}
}
}
if(flag==0) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
static void merge(int arr[], int arr1[],int arr2[],int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[]=new int[n1];
int R[]=new int[n2];
int L1[]=new int[n1];
int R1[]=new int[n2];
int L2[]=new int[n1];
int R2[]=new int[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++) {
L[i] = arr[l + i];
L1[i] = arr1[l + i];
L2[i] = arr2[l + i];
}
for (j = 0; j < n2; j++) {
R[j] = arr[m + 1+ j];
R1[j] = arr1[m + 1+ j];
R2[j] = arr2[m + 1+ j];
}
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
arr1[k] = L1[i];
arr2[k] = L2[i];
i++;
}
else
{
arr[k] = R[j];
arr1[k]=R1[j];
arr2[k]=R2[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
arr1[k] = L1[i];
arr2[k] = L2[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
arr1[k]=R1[j];
arr2[k]=R2[j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
static void mergeSort(int arr[],int arr1[],int arr2[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr,arr1,arr2,l, m);
mergeSort(arr,arr1,arr2, m+1, r);
merge(arr,arr1,arr2,l, m, r);
}
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
b229df1d6eb1caa6d6aea02c3dc0230c
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Contest {
//private static List<Integer> results = new ArrayList<>();
//private static Map<Integer, List<Integer>> map = new HashMap<>();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//String line = sc.nextLine();
//int n = Integer.valueOf(line);
String line = sc.nextLine();
/*String[] split = line.split(" ");
int w = Integer.valueOf(split[0]);
int h = Integer.valueOf(split[1]);*/
int n = Integer.valueOf(line);
line = sc.nextLine();
String[] split = line.split(" ");
int[] radiuses = new int[split.length];
for (int i = 0; i < split.length; i++) {
radiuses[i] = Integer.valueOf(split[i]);
}
solve(n, radiuses);
}
private static void solve(int n, int[] radiuses) {
int maxIndex = 0;
for (int i = 0; i < radiuses.length; i++) {
if (radiuses[i] > radiuses[maxIndex]) {
maxIndex = i;
}
}
int left = maxIndex - 1;
int right = maxIndex + 1;
int max = radiuses[maxIndex];
for (int i = 0; i < radiuses.length; i++) {
if (right >= radiuses.length) {
if (left < 0) {
System.out.println("YES");
return;
}
if (radiuses[left] >= max) {
System.out.println("NO");
return;
}
max = radiuses[left];
left--;
} else if (left < 0) {
if (right >= radiuses.length) {
System.out.println("YES");
return;
}
if (radiuses[right] >= max) {
System.out.println("NO");
return;
}
max = radiuses[right];
right++;
} else {
int prevMax = max;
max = Math.max(radiuses[left], radiuses[right]);
if (max >= prevMax) {
System.out.println("NO");
return;
}
if (max == radiuses[left]) {
left--;
} else {
right++;
}
}
}
}
public static boolean isPrime(int num) {
if (num > 2 && num % 2 == 0) {
//System.out.println(num + " is not prime");
return false;
}
int top = (int) Math.sqrt(num) + 1;
for (int i = 3; i < top; i += 2) {
if (num % i == 0) {
//System.out.println(num + " is not prime");
return false;
}
}
//System.out.println(num + " is prime");
return true;
}
private static void doneList(List<Integer> a) {
StringBuilder builder = new StringBuilder("");
for (int i = 0; i < a.size(); i++) {
builder.append(a.get(i) + 1).append(" ");
}
System.out.println(builder.toString().substring(0, builder.toString().length() - 1));
}
private static void doneArray(Integer[] a) {
StringBuilder builder = new StringBuilder("");
for (int i = 2; i < a.length; i++) {
builder.append(a[i]).append(" ");
}
System.out.println(builder.toString().substring(0, builder.toString().length() - 1));
}
private static void done() {
System.out.println("-1");
}
private static int lowerBound(int[] a, int low, int high, int element) {
while (low < high) {
int middle = low + (high - low) / 2;
if (element > a[middle]) {
low = middle + 1;
} else {
high = middle;
}
}
return low;
}
private static int upperBound(int[] a, int low, int high, int element) {
while (low < high) {
int middle = low + (high - low) / 2;
if (a[middle] > element) {
high = middle;
} else {
low = middle + 1;
}
}
return low;
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
669982c598b1d9b61768878718e1c04c
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
public class B {
static Scanner sc = new Scanner(System.in);
static int[] inx;
static int[] a;
public static void main(String[] args) {
int n = sc.nextInt();
a = new int[n];
int max = 0, maxInx = 0;
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
// System.out.println(a[i]);
if (max < a[i]) {
max = a[i];
maxInx = i;
}
}
boolean f = true;
for (int i = maxInx - 1; i >= 0; i--) {
if (a[i] > a[i + 1]) {
f = false;
// System.out.println(i + "!");
}
}
// System.out.println(maxInx);
for (int i = maxInx + 1; i < n - 1; i++) {
if (a[i] < a[i + 1])
f = false;
// System.out.println(i + "?");
}
System.out.println(f ? "YES" : "NO");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
fe5219097743cdc1b50bf91c86b27c8c
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
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[] a = new int[n];
int pp = -1;
for(int i = 0; i<n ; i++) {
a[i] = sc.nextInt();
if(a[i] == n) pp = i;
}
int lg = pp-1;
int pg = pp+1;
for(int sl = n-1; sl>0; sl--){
if((lg>-1) && (sl == a[lg])){
lg--;
continue;
}
if((pg<n) && (sl == a[pg])){
pg++;
continue;
}
System.out.print("NO");
return;
}
System.out.print("YES");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
705d22230f4c1091bc8fb5ff2f8d135b
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc= new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
boolean flag = false;
boolean ok = true;
int prev = -1;
for(int i = 0; i<n; i++){
int a = sc.nextInt();
if(a == prev)ok = false;
if(flag){
if(a >= prev)ok = false;
}
else{
if(a <= prev)flag = true;
}
prev = a;
}
if(ok)out.println("YES");
else out.println("NO");
out.flush();
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
86012d826524dff6aedf7bc4b1fbd083
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Main {
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
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[] nextSArray() {
String sr[] = null;
try {
sr = br.readLine().trim().split(" ");
} catch (IOException e) {
e.printStackTrace();
}
return sr;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//static ArrayList<Integer>al=new ArrayList<>();
// static int gcd(int a,int b)
// {
// if(a==0)
// return b;
// return gcd(b%a,a);
// }
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
// int t1=sc.nextInt();
// for(int t=0;t<t1;++t) {
int n = sc.nextInt();
int arr[]=new int[n];
int maxindex=0,max=Integer.MIN_VALUE;
for (int i = 0; i < n; ++i)
{
arr[i]=sc.nextInt();
if(max<arr[i])
{
max=arr[i];
maxindex=i;
}
}
int a=maxindex,b=maxindex;
int k=1;
while(k<n)
{
int aa=-1,bb=-1;
if(a-1>=0)
aa=arr[a-1];
if(b+1<n)
bb=arr[b+1];
if(aa>bb)
{
if(max<aa)
{
System.out.println("NO");
return;
}
max=aa;
a=a-1;
}
else
{
if(max<bb)
{
System.out.println("NO");
return;
}
max=bb;
b=b+1;
}
++k;
}
System.out.println("YES");
out.flush();
out.close();
}
}
// out.println(al.toString().replaceAll("[\\[|\\]|,]",""));
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
33807166cd7da3d1d7eb5d2f25dd709a
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader() {
br = new BufferedReader(new InputStreamReader(System.in)); }
String next() {
while (st == null || !st.hasMoreElements()) {
try{ st = new StringTokenizer(br.readLine()); }
catch (IOException e) { e.printStackTrace(); } }
return st.nextToken(); }
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try{ str = br.readLine(); }
catch (IOException e) { e.printStackTrace(); }
return str; }
}
public static void main(String[] args) {
FastReader ip = new FastReader();
OutputStream output = System.out;
PrintWriter out = new PrintWriter(output);
int n=ip.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=ip.nextInt();
}
boolean inc=false;
boolean dec=false;
boolean ans=true;
for(int i=1;i<n;i++){
if(arr[i]>arr[i-1]){
if(dec){
ans=false;
break;
}
inc=true;
}
if(arr[i]<arr[i-1]){
dec=true;
}
}
if(ans){
out.println("YES");
}else{
out.println("NO");
}
out.close();
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
4a24df5184440c39db91666a4771b494
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader() {
br = new BufferedReader(new InputStreamReader(System.in)); }
String next() {
while (st == null || !st.hasMoreElements()) {
try{ st = new StringTokenizer(br.readLine()); }
catch (IOException e) { e.printStackTrace(); } }
return st.nextToken(); }
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try{ str = br.readLine(); }
catch (IOException e) { e.printStackTrace(); }
return str; }
}
public static void main(String[] args) {
FastReader ip = new FastReader();
OutputStream output = System.out;
PrintWriter out = new PrintWriter(output);
int n=ip.nextInt();
int arr[]=new int[n];
HashSet<Integer> hs=new HashSet<>();
for(int i=0;i<n;i++){
arr[i]=ip.nextInt();
hs.add(arr[i]);
}
if(hs.size()!=n){
out.println("NO");
}else {
boolean inc = false;
boolean dec = false;
boolean ans = true;
for (int i = 1; i < n; i++) {
if (arr[i] > arr[i - 1]) {
if (dec) {
ans = false;
break;
}
inc = true;
}
if (arr[i] < arr[i - 1]) {
dec = true;
}
}
if (ans) {
out.println("YES");
} else {
out.println("NO");
}
}
out.close();
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
2b2b0c1f9488ccd9831a175a84bebe3d
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.*;
import static java.lang.Double.max;
import static java.lang.Math.pow;
import java.util.*;
/**
*
* @author Mohammed M Elkady
*/
public class Mamo {
static long ans,sum,sum2,max;
static StringBuilder Sd=new StringBuilder();
static FastReader in=new FastReader();
static long n,m;
public static void main(String[] args) {
//Ψ§ΨΩΨ§Ω
Ψ§ΩΨ¨Ψ΄Ψ± ΩΨ§ ΨΨ―ΩΨ― ΩΩΨ§
int n=in.nextInt(),a[]=new int[n+2],l=0;
for(int i=1;i<=n;i++){a[i]=in.nextInt();if(a[i]>max){max=a[i];l=i;}}
for(int i=l+1,j=l-1;i<=n||j>0;){
if(a[i]>a[j]){
if(a[i]<max){max=a[i];i++;}
else {System.out.println("NO");return;}
}
else if(a[j]>a[i]){
if(a[j]<max){max=a[j];j--;}
else {System.out.println("NO");return;}
}
else {System.out.println("NO");return;}
}
System.out.println("YES");
}}
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;
}
}
class Sorting{
public static int[] bucketSort(int[] array, int bucketCount) {
if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count");
if (array.length <= 1) return array; //trivially sorted
int high = array[0];
int low = array[0];
for (int i = 1; i < array.length; i++) { //find the range of input elements
if (array[i] > high) high = array[i];
if (array[i] < low) low = array[i];
}
double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket
ArrayList<Integer> buckets[] = new ArrayList[bucketCount];
for (int i = 0; i < bucketCount; i++) { //initialize buckets
buckets[i] = new ArrayList();
}
for (int i = 0; i < array.length; i++) { //partition the input array
buckets[(int)((array[i] - low)/interval)].add(array[i]);
}
int pointer = 0;
for (int i = 0; i < buckets.length; i++) {
Collections.sort(buckets[i]); //mergeSort
for (int j = 0; j < buckets[i].size(); j++) { //merge the buckets
array[pointer] = buckets[i].get(j);
pointer++;
}
}
return array;
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
b1ea81cf74d2161cdacc0daa1b20aff7
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.*;
import static java.lang.Double.max;
import static java.lang.Math.pow;
import java.util.*;
/**
*
* @author Mohammed M Elkady
*/
public class Mamo {
static long ans,sum,sum2,max;
static StringBuilder Sd=new StringBuilder();
static FastReader in=new FastReader();
static long n,m;
public static void main(String[] args) {
//Ψ§ΨΩΨ§Ω
Ψ§ΩΨ¨Ψ΄Ψ± ΩΨ§ ΨΨ―ΩΨ― ΩΩΨ§
int n=in.nextInt(),a[]=new int[n+2],l=0;
for(int i=1;i<=n;i++){a[i]=in.nextInt();if(a[i]>max){max=a[i];l=i;}}
for(int i=l+1,j=l-1;i<=n||j>0;){
if(a[i]>a[j]){
if(a[i]<max){max=a[i];i++;}
else {System.out.println("NO");return;}
}
else if(a[j]>a[i]){
if(a[j]<max){max=a[j];j--;}
else {System.out.println("NO");return;}
}
else {System.out.println("NO");return;}
}
System.out.println("YES");
}}
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;
}
}
class Sorting{
public static int[] bucketSort(int[] array, int bucketCount) {
if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count");
if (array.length <= 1) return array; //trivially sorted
int high = array[0];
int low = array[0];
for (int i = 1; i < array.length; i++) { //find the range of input elements
if (array[i] > high) high = array[i];
if (array[i] < low) low = array[i];
}
double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket
ArrayList<Integer> buckets[] = new ArrayList[bucketCount];
for (int i = 0; i < bucketCount; i++) { //initialize buckets
buckets[i] = new ArrayList();
}
for (int i = 0; i < array.length; i++) { //partition the input array
buckets[(int)((array[i] - low)/interval)].add(array[i]);
}
int pointer = 0;
for (int i = 0; i < buckets.length; i++) {
Collections.sort(buckets[i]); //mergeSort
for (int j = 0; j < buckets[i].size(); j++) { //merge the buckets
array[pointer] = buckets[i].get(j);
pointer++;
}
}
return array;
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
b5ef4efc9b058d5f7a91f255dca15eaf
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class pillars {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = Integer.parseInt(in.readLine());
int[] nums = new int[n];
StringTokenizer st = new StringTokenizer(in.readLine());
for (int i = 0; i < n; i++) {
int num = Integer.parseInt(st.nextToken());
nums[i] = num;
}
boolean f = true;
boolean up = true;
for (int i = 1; i < n; i++) {
if(up) {
if(nums[i] <= nums[i-1]) {
up = false;
}
} else {
if(nums[i] >= nums[i-1]) {
f = false;
break;
}
}
}
out.println(f ? "YES" : "NO");
in.close();
out.close();
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
22a920c06883960760fdab8d06b79a48
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class A {
public static void main(String args[]) {
FastScanner in = new FastScanner();
int n=in.nextInt();
int a[]=in.nextArray(n);
int ctr=0;
int i=0,j=n-1;
for(;i<n-1;i++) {
if(a[i]>a[i+1]) {
break;
}
}
for(;j>0;j--) {
if(a[j]>a[j-1]) {
break;
}
}
if(i==j)
System.out.println("YES");
else
System.out.println("NO");
}
///////////////////////////
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[] nextArray(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
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
413d3e2f654fa18e14af0619de37aa25
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
public class test
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int n=s.nextInt();
int arr1[]=new int[n];
int arr2[]=new int[n+1];
int max_index=0;
for(int i=0;i<n+1;i++)
{
arr2[i]=0;
}
for(int i=0;i<n;i++)
{
arr1[i]=s.nextInt();
arr2[arr1[i]]=i;
}
for(int i=n-1;i>=1;i--)
{
if(Math.abs(arr2[n]-arr2[i])<=(n-i))
{
}
else
{
System.out.println("NO");
System.exit(0);
}
}
int a=0;
int b=n-1;
for(int i=1;i<=n;i++)
{
if(arr2[i]!=a && arr2[i]!=b)
{
System.out.println("NO");
System.exit(0);
}
if(arr2[i]==a)
{
a++;
}
else if(arr2[i]==b)
{
b--;
}
}
System.out.println("YES");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
9d042b7b4975692c8add6eef97c8eacc
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class b2{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int A[]=new int[n];
int g=0,m=-1;
for(int i=0;i<n;i++){
A[i]=sc.nextInt();
if(A[i]>g){
g=A[i];
m=i;
}
}
int l,r;
int t=1;
for(int i=m;i<n-1;i++){
if(A[i]<A[i+1]){
t=2;
break;
}
}
for(int i=m;i>=1;i--){
if(A[i]<A[i-1]){
t=2;
break;
}
}
if(t==2)
System.out.println("NO");
else
System.out.println("YES");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
616d653102c4f2062c565ba94318f355
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class CF1197B {
public static void main(String[] args) {
FastReader input = new FastReader();
int n = input.nextInt();
int[] arr = new int[n];
int max = 0;
int maxIndex = 0;
for(int i = 0;i < n;i++){
arr[i] = input.nextInt();
if(arr[i] > max){
max = arr[i];
maxIndex = i;
}
}
int j = maxIndex - 1;
int k = maxIndex + 1;
while (j >= 0 && k < n){
if(arr[j] > arr[k]){
if(arr[j] < max){
max = arr[j];
j--;
}
else {
System.out.println("NO");
return;
}
}
else{
if(arr[k] < max){
max = arr[k];
k++;
}
else{
System.out.println("NO");
return;
}
}
}
if(j >= 0){
while (j >= 0){
if(arr[j] < max){
max = arr[j];
j--;
}
else{
System.out.println("NO");
return;
}
}
}
if(k < n){
while (k < n){
if(arr[k] < max){
max = arr[k];
k++;
}
else{
System.out.println("NO");
return;
}
}
}
if(j == -1 && k == n){
System.out.println("YES");
}
}
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\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
7ee5370d28b44b2321f482009199d4f0
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.Scanner;
public class codeforces {
public static void solution(int [] arr) {
int max=0;
int index=0;
for(int i=0;i<arr.length;i++) {
if(arr[i]>max) {
max=arr[i];
index=i;
}
}
boolean isSorted=true;
for(int i=0;i<index;i++) {
if(arr[i]>=arr[i+1]) {
isSorted=false;
break;
}
}
for(int i=index;i<arr.length-1;i++) {
if(arr[i]<=arr[i+1]) {
isSorted=false;
break;
}
}
if(isSorted) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
public static void main(String args []) {
Scanner obj = new Scanner(System.in);
int n = obj.nextInt();
int [] a = new int[n];
for(int i=0;i<a.length;i++) {
a[i] = obj.nextInt();
}
solution(a);
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
31e1a7dddf1c0a64e709603e101f687c
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class a{
static int[] count,count1;
static long[] arr;
static char[] ch,ch1;
static long[] darr;
static long[][] mat,mat1;
static long x;
static long maxl;
static double dec;
static String s;
// static long minl;
static int mx = (int)1e6;
static long mod = 998244353l;
// static int minl = -1;
// static long n;
static int n,n1,n2;
static long a,g;
static long b;
static long c;
static long d;
static long y;
static int m;
static long k;
static int q;
static String[] str,str1;
static Set<Character> set,set1;
static List<Long> list;
static Map<Long,Integer> map;
static StringBuilder sb;
public static boolean solve(){
int index = -1;
for(int i = 0 ; i < n ;i++){
if(arr[i] == n){
index = i;
break;
}
}
for(int i = 1 ; i <= index ; i++){
if(arr[i] < arr[i-1])
return false;
}
for(int i = index+1 ; i < n ; i++){
if(arr[i] > arr[i-1])
return false;
}
return true;
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
// Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
int t = 1;
while(t > 0){
// x = sc.nextLong();
// y = sc.nextLong();
// a = sc.nextLong();
// b = sc.nextLong();
// c = sc.nextLong();
// d = sc.nextLong();
n = sc.nextInt();
// a = sc.nextLong();
// b = sc.nextLong();
// n1 = sc.nextInt();
// n = sc.nextLong();
// k = sc.nextLong();
// ch = sc.next().toCharArray();
// ch1 = sc.next().toCharArray();
// m = sc.nextInt();
// k = sc.nextLong();
arr = new long[n];
for(int i = 0; i < n ; i++){
arr[i] = sc.nextLong();
}
// n1 = sc.nextInt();
// darr = new long[n1];
// for(int i = 0; i < n1 ; i++){
// darr[i] = sc.nextLong();
// }
// mat = new long[n][m];
// for(int i = 0 ; i < n ; i++){
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = sc.nextLong();
// }
// }
System.out.println(solve()?"YES":"NO");
// solve();
// System.out.println(solve());
// sb = new StringBuilder();
// sb.append(str);
t -= 1;
}
}
public static int log(long n){
if(n == 0 || n == 1)
return 0;
if(n == 2)
return 1;
double num = Math.log(n);
double den = Math.log(2);
if(den == 0)
return 0;
return (int)(num/den);
}
public static long gcd(long a,long b){
if(b%a == 0)
return a;
return gcd(b%a,a);
}
// public static void swap(int i,int j){
// long temp = arr[j];
// arr[j] = arr[i];
// arr[i] = temp;
// }
static final Random random=new Random();
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 Node{
int first;
int second;
Node(int f,int s){
this.first = f;
this.second = s;
}
}
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
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
5c0ea1f93fad2427f1c413e7fe8859aa
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
/*
9 2
1 1 1 1 1 0 0 0 0
1 2
1 5
5 6
5 7
2 3
2 4
3 8
3 9
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Question {
static long[] arr1;
static long[] arr2;
static int[] arr;
static int[][] dp;
static int n;
static long MOD = 998244353 ;
static long ans = 0;
static String str1 ;
static String str2 ;
static int[] primes = new int[]{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
static ArrayList<Integer> list;
static int aa;
public static void main (String[] args) throws IOException {
Reader.init(System.in);
int n = Reader.nextInt();
int[] arr = new int[n];
int max = -1;
int index = -1;
for (int i = 0 ; i < n ; i++){
arr[i] = Reader.nextInt();
if (arr[i]>max){
max = Math.max(max,arr[i]);
index = i;
}
}
//System.out.println(index);
for (int i = 0 ; i <= index-1 ; i++){
//System.out.println(arr[i] + " " + arr[i+1]);
if (arr[i]>=arr[i+1]){
System.out.println("NO");
System.exit(0);
}
}
for (int i = index ; i < n-1; i++){
if (arr[i]<=arr[i+1]){
System.out.println("NO");
System.exit(0);
}
}
System.out.println("YES");
}
static long count(String a, String b)
{
int m = a.length();
int n = b.length();
// Create a table to store
// results of sub-problems
long lookup[][] = new long[m + 1][n + 1];
// If first string is empty
for (int i = 0; i <= n; ++i)
lookup[0][i] = 0;
// If second string is empty
for (int i = 0; i <= m; ++i)
lookup[i][0] = 1;
// Fill lookup[][] in
// bottom up manner
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
// If last characters are
// same, we have two options -
// 1. consider last characters
// of both strings in solution
// 2. ignore last character
// of first string
if (a.charAt(i - 1) == b.charAt(j - 1))
lookup[i][j] = lookup[i - 1][j - 1] +
lookup[i - 1][j];
else
// If last character are
// different, ignore last
// character of first string
lookup[i][j] = lookup[i - 1][j];
}
}
return lookup[m][n];
}
public static void sortbyColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
static int fun(int i , int mask){
if (i < n){
//System.out.println(i + " " + Integer.toBinaryString(mask));
}
if (i==n){
return 0;
}
else if (dp[i][mask]!=-1){
return dp[i][mask];
}
else{
int flag = 0;
for (int j = 0 ; j < 16 ; j++) {
if (((mask>>j) &1 ) == 1 && ((list.get(i)>>j) &1 ) == 1) {
flag =1;
}
}
if (flag==0){
dp[i][mask] =Math.max(1 + fun(i + 1, (mask | (list.get(i)))), fun(i + 1, mask));
}
else{
dp[i][mask] = fun(i + 1, mask);
}
return dp[i][mask];
}
}
/*static int lcs(int i , int j){
if (i ==0 || j==0){
return 0;
}
else if (dp[i][j]!=-1){
return dp[i][j];
}
else if (str1.charAt(i-1)==str2.charAt(j-1)){
return dp[i][j] = 1 + lcs(i-1,j-1);
}
else {
return dp[i][j] = Math.max(lcs(i-1,j),lcs(i,j-1));
}
}
*/
/*static long fun(int i , int cnt){
if (i >= n){
return 0;
}
if (dp[i][cnt%2]!=-1){
return dp[i][cnt%2];
}
else{
if (cnt%2==0){
return dp[i][cnt%2] = arr1[i] + Math.max(fun(i+1,cnt+1),fun(i+2,cnt+1));
}
else{
return dp[i][cnt%2] = arr2[i] + Math.max(fun(i+1,cnt+1),fun(i+2,cnt+1));
}
}
}*/
/*static long grid(int i , int j){
//System.out.println(i + " " + j);
if (i>= n || i < 0 || j >= m || j < 0){
return 0;
}
else if (arr[i][j]==-1){
return 0;
}
else if (i==n-1 && j==m-1){
return 1;
}
else if (dp[i][j]!=-1){
//System.out.println(dp[i][j]);
return dp[i][j];
}
else{
long tmp =0;
tmp+=grid(i+1 , j);
tmp%=MOD;
tmp+=grid(i,j+1);
tmp%=MOD;
return dp[i][j] = tmp;
}
}*/
/*static int dfs(int n){
//System.out.println(n +" " + dp[n]);
if (dp[n]!=-1){
return dp[n];
}
else{
int max = 1;
Iterator i = adj[n].iterator();
while (i.hasNext()){
int j = (int)i.next();
max = Math.max(max,1+dfs(j));
}
return dp[n] = max;
}
}*/
static boolean isSubSequence(String str1, String str2, int m, int n)
{
// Base Cases
if (m == 0)
return true;
if (n == 0)
return false;
// If last characters of two strings are matching
if (str1.charAt(m-1) == str2.charAt(n-1))
return isSubSequence(str1, str2, m-1, n-1);
// If last characters are not matching
return isSubSequence(str1, str2, m, n-1);
}
}
class Tnode{
int lSub;
int rSub;
int left;
int right;
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
/*class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
*//* Create temp arrays *//*
int L[] = new int [n1];
int R[] = new int [n2];
*//*Copy data to temp arrays*//*
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
*//* Merge the temp arrays *//*
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
*//* Copy remaining elements of L[] if any *//*
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
*//* Copy remaining elements of R[] if any *//*
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
*//* A utility function to print array of size n *//*
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
}
class Node{
int data;
int freq;
public Node(int data, int freq) {
this.data = data;
this.freq = freq;
}
}
class GFG {
// limit for array size
static int N = 1000000;
static int n; // array size
// Max size of tree
static int []tree = new int[2 * N];
// function to build the tree
static void build( char []arr,int n)
{
// insert leaf nodes in tree
for (int i = 0; i < n-1; i++) {
if (arr[i]!=arr[i+1]) {
System.out.println(i);
tree[n + i] = 1;
}
}
// build the tree by calculating
// parents
for (int i = n - 1; i > 0; --i)
tree[i] = tree[i << 1] +
tree[i << 1 | 1];
}
static int query(int l, int r)
{
int res = 0;
// loop to find the sum in the range
for (l += n, r += n; l < r;
l >>= 1, r >>= 1)
{
if ((l & 1) > 0)
res += tree[l++];
if ((r & 1) > 0)
res += tree[--r];
}
return res;
}
// driver program to test the
// above function
}
class SegmentTree{
int[] arr ;
int[] tree;
SegmentTree(int[] arr ,int n){
this.arr = arr;
tree = new int[2*n];
}
void updateRangeUtil(int si, int ss, int se, int us,
int ue, int val)
{
if (ss>se || ss>ue || se<us)
return ;
if (ss==se)
{
tree[si] |= val;
return;
}
int mid = (ss+se)/2;
updateRangeUtil(si*2+1, ss, mid, us, ue, val);
updateRangeUtil(si*2+2, mid+1, se, us, ue, val);
tree[si] = tree[si*2+1] + tree[si*2+2];
}
void constructSTUtil(int ss, int se, int si)
{
// out of range as ss can never be greater than se
if (ss > se)
return ;
// If there is one element in array, store it in
// current node of segment tree and return
if (ss == se)
{
//tree[si] = arr[ss];
System.out.println(tree[si]);
return;
}
// If there are more than one elements, then recur
// for left and right subtrees and store the sum
// of values in this node
int mid = (ss + se)/2;
constructSTUtil(ss, mid, si*2+1);
constructSTUtil(mid+1, se, si*2+2);
//tree[si] = tree[si*2 + 1] + tree[si*2 + 2];
}
}
class Heap{
int[] Heap;
int size;
int maxSize;
public Heap(int maxSize){
this.maxSize = maxSize;
this.size = 0;
Heap = new int[maxSize + 1];
Heap[0] = Integer.MIN_VALUE;
}
int parent(int pos){
return pos/2;
}
int left(int pos){
return 2*pos;
}
int right(int pos){
return 2*pos + 1;
}
boolean isLeaf(int pos){
if (pos>=size/2 && pos<=size){
return true;
}
return false;
}
void swap(int i , int j){
int temp = Heap[i];
Heap[i] = Heap[j];
Heap[j]= temp;
}
void minHeapify(int pos){
if (!isLeaf(pos)){
if (Heap[pos]>Heap[left(pos)] || Heap[pos] > Heap[right(pos)]){
if (Heap[left(pos)] < Heap[right(pos)]) {
swap(pos, left(pos));
minHeapify(left(pos));
}
// Swap with the right child and heapify
// the right child
else {
swap(pos, right(pos));
minHeapify(right(pos));
}
}
}
}
}
class Graph{
}
class DijkstraNode implements Comparator<DijkstraNode>{
public int node;
public int cost;
public int[] visited;
public boolean[] settled;
public DijkstraNode(int node, int cost,int vertices) {
this.node = node;
this.cost = cost;
visited = new int[vertices];
settled = new boolean[vertices];
}
@Override
public int compare(DijkstraNode node1 , DijkstraNode node2){
if (node1.cost < node2.cost){
return -1;
}
if (node1.cost > node2.cost){
return 1;
}
return 0;
}
ArrayList<Integer> list = new ArrayList<>(10000);
}*/
/*
public class DPQ {
private int dist[];
private Set<Integer> settled;
private PriorityQueue<Node> pq;
private int V; // Number of vertices
List<List<Node> > adj;
public DPQ(int V)
{
this.V = V;
dist = new int[V];
settled = new HashSet<Integer>();
pq = new PriorityQueue<Node>(V, new Node());
}
// Function for Dijkstra's Algorithm
public void dijkstra(List<List<Node> > adj, int src)
{
this.adj = adj;
for (int i = 0; i < V; i++)
dist[i] = Integer.MAX_VALUE;
// Add source node to the priority queue
pq.add(new Node(src, 0));
// Distance to the source is 0
dist[src] = 0;
while (settled.size() != V) {
// remove the minimum distance node
// from the priority queue
int u = pq.remove().node;
// adding the node whose distance is
// finalized
settled.add(u);
e_Neighbours(u);
}
}
// Function to process all the neighbours
// of the passed node
private void e_Neighbours(int u)
{
int edgeDistance = -1;
int newDistance = -1;
// All the neighbors of v
for (int i = 0; i < adj.get(u).size(); i++) {
Node v = adj.get(u).get(i);
// If current node hasn't already been processed
if (!settled.contains(v.node)) {
edgeDistance = v.cost;
newDistance = dist[u] + edgeDistance;
// If new distance is cheaper in cost
if (newDistance < dist[v.node])
dist[v.node] = newDistance;
// Add the current node to the queue
pq.add(new Node(v.node, dist[v.node]));
}
}
}
// Driver code
public static void main(String arg[])
{
int V = 5;
int source = 0;
// Adjacency list representation of the
// connected edges
List<List<Node> > adj = new ArrayList<List<Node> >();
// Initialize list for every node
for (int i = 0; i < V; i++) {
List<Node> item = new ArrayList<Node>();
adj.add(item);
}
// Inputs for the DPQ graph
adj.get(0).add(new Node(1, 9));
adj.get(0).add(new Node(2, 6));
adj.get(0).add(new Node(3, 5));
adj.get(0).add(new Node(4, 3));
adj.get(2).add(new Node(1, 2));
adj.get(2).add(new Node(3, 4));
// Calculate the single source shortest path
DPQ dpq = new DPQ(V);
dpq.dijkstra(adj, source);
// Print the shortest path to all the nodes
// from the source node
System.out.println("The shorted path from node :");
for (int i = 0; i < dpq.dist.length; i++)
System.out.println(source + " to " + i + " is "
+ dpq.dist[i]);
}
}
*/
// Class to represent a node in the graph
/*class BinarySearchTree {
*//* Class containing left and right child of current node and key value*//*
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
// Root of BST
Node root;
// Constructor
BinarySearchTree() {
root = null;
}
// This method mainly calls insertRec()
void insert(int key) {
root = insertRec(root, key);
}
*//* A recursive function to insert a new key in BST *//*
Node insertRec(Node root, int key) {
*//* If the tree is empty, return a new node *//*
if (root == null) {
root = new Node(key);
return root;
}
*//* Otherwise, recur down the tree *//*
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
*//* return the (unchanged) node pointer *//*
return root;
}
// This method mainly calls InorderRec()
void inorder() {
inorderRec(root);
}
// A utility function to do inorder traversal of BST
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}
// Driver Program to test above functions
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
*//* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 *//*
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
// print inorder traversal of the BST
tree.inorder();
}
} */
/*int maxPathSumUtil(Node node, Res res) {
// Base cases
if (node == null)
return 0;
if (node.left == null && node.right == null)
return node.data;
// Find maximum sum in left and right subtree. Also
// find maximum root to leaf sums in left and right
// subtrees and store them in ls and rs
int ls = maxPathSumUtil(node.left, res);
int rs = maxPathSumUtil(node.right, res);
// If both left and right children exist
if (node.left != null && node.right != null) {
// Update result if needed
res.val = Math.max(res.val, ls + rs + node.data);
// Return maxium possible value for root being
// on one side
return Math.max(ls, rs) + node.data;
}
// If any of the two children is empty, return
// root sum for root being on one side
return (node.left == null) ? rs + node.data
: ls + node.data;
} */
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
07a1ef9ad240aab9dcbce129ffa968b4
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
/*
* @Author: Kaustav Vats
*/
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
public class PB {
public static void main(String[] args) throws IOException{
Reader.init(System.in);
int N = Reader.nextInt();
Node Arr = new Node(Reader.nextInt());
Node ptr = Arr;
for (int i=1; i<N; i++) {
Node tmp = new Node(Reader.nextInt());
ptr.next = tmp;
tmp.prev = ptr;
ptr = ptr.next;
}
// System.out.println("HESS");
ptr = Arr;
Node max = ptr;
ptr = ptr.next;
for (int i=1; i<N; i++) {
if (ptr.Radius > max.Radius) {
max = ptr;
}
ptr = ptr.next;
}
boolean Flag = true;
ptr = max;
for (int i=0; i<N-1; i++) {
if (ptr.next != null && ptr.prev != null) {
if (ptr.next.Radius < ptr.Radius && ptr.Radius > ptr.prev.Radius) {
if (ptr.prev.Radius < ptr.next.Radius) {
ptr.Radius = ptr.next.Radius;
Node temp = ptr.next.next;
ptr.next = temp;
}
else {
ptr.Radius = ptr.prev.Radius;
Node temp = ptr.prev.prev;
ptr.prev = temp;
}
}
else {
Flag = false;
break;
}
}
else if (ptr.next != null) {
if (ptr.Radius > ptr.next.Radius) {
ptr.Radius = ptr.next.Radius;
Node temp = ptr.next.next;
ptr.next = temp;
}
else {
Flag = false;
break;
}
}
else if (ptr.prev != null) {
if (ptr.Radius > ptr.prev.Radius) {
ptr.Radius = ptr.prev.Radius;
Node temp = ptr.prev.prev;
ptr.prev = temp;
}
else {
Flag = false;
break;
}
}
else {
break;
}
// Node temp = Arr;
// while(temp.next != null) {
// System.out.print(temp.Radius + " ");
// temp = temp.next;
// }
// System.out.println();
}
if (Flag) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
class Node {
int Radius;
Node next;
Node prev;
public Node (int r) {
this.Radius = r;
this.next = null;
this.prev = null;
}
}
/** Class for buffered reading int and double values */
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
6f55d33a85e2eb518918397c03634b60
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
/**
* for competitive programming
* @author BrolyCode
* @version 1.0.0
*/
public class CP {
static Random random;
private static void mySort(int[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
int t = s[i];
s[i] = s[j];
s[j] = t;
}
Arrays.sort(s);
}
static class Pair{
int x;
int y;
public Pair(int x, int y) {
this.x=x;
this.y=y;
}
}
public static void sortPairX(Pair[] p) {
Arrays.sort(p, new Comparator<Pair>() {
public int compare(Pair o1, Pair o2) {
return o1.x-o2.x;
}
});
}
public static void sortPairY(Pair[] p) {
Arrays.sort(p, new Comparator<Pair>() {
public int compare(Pair o1, Pair o2) {
return o1.y-o2.y;
}
});
}
public static void main(String[] args) {
random = new Random(543534151132L + System.currentTimeMillis());
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
task(in,out);
out.close();
}
private static void task(InputReader in, PrintWriter out) {
int n=in.nextInt();
int[] T=new int[n];
int max=0;
for(int i=0;i<n;i++) {
T[i]=in.nextInt();
if(T[max]<T[i])max=i;
}
int[] T1=Arrays.copyOf(T, max);
int[] T2=Arrays.copyOfRange(T, max+1,T.length);
if(isSorted(T1) && isDec(T2))out.println("YES");
else out.println("NO");
}
private static boolean isDec(int[] a) {
for(int i = 0; i < a.length-1; i ++){
if (a[i] < a[i+1]) {
return false;
}
}
return true;
}
public static boolean isSorted(int[] a)
{
for(int i = 0; i < a.length-1; i ++){
if (a[i] > a[i+1]) {
return false;
}
}
return true;
}
public static int gcd(int a, int b)
{
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
public static long gcd(long a, long b)
{
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.longValue();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
c1c5365e9b41d9e4bdea316d9adcd712
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Scanner;
public class Pillars {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
ArrayList<Integer> arrayList = new ArrayList<>(n);
int max = Integer.MIN_VALUE, maxIndex = 0;
for (int i = 0; i < n; i++) {
arrayList.add(scanner.nextInt());
if (arrayList.get(i) > max) {
max = arrayList.get(i);
maxIndex = i;
}
}
for (int i = maxIndex - 1; i > 0; i--) {
if (arrayList.get(i) < arrayList.get(i - 1)) {
System.out.println("NO");
return;
}
}
for (int i = maxIndex + 1; i < arrayList.size() - 1; i++) {
if (arrayList.get(i) < arrayList.get(i + 1)) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
3c398e7bdf57ea1503bfb3a8b06657ed
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class pillars {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
diskSet[] arr = new diskSet[n];
diskSet[] unsortedArr = new diskSet[n];
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
arr[i] = new diskSet(i, a);
unsortedArr[i] = new diskSet(i, a);
}
Arrays.sort(arr, new IntervalComparator());
int maxPos1 = arr[arr.length - 1].diskpos;
for (int i = maxPos1; i >= 1; i--) {
if (unsortedArr[i].diskval < unsortedArr[i-1].diskval) {
System.out.println("NO");
System.exit(0);
}
}
for (int i = maxPos1; i < n-1; i++) {
if (unsortedArr[i+1].diskval > unsortedArr[i].diskval) {
System.out.println("NO");
System.exit(0);
}
}
/*
int maxPos2 = arr.length - arr[arr.length - 1].diskpos - 1;
int maxIterate = Math.min(maxPos1, maxPos2);
int iterateVal = arr[arr.length-1].diskval;
for (int i = 1; i < maxIterate + 1; i++) {
int a = unsortedArr[maxPos1 + i].diskval;
int b = unsortedArr[maxPos1 - i].diskval;
if (a >= iterateVal - 2 && b >= iterateVal - 2) {
iterateVal -= 2;
}
else {
System.out.println("NO");
System.exit(0);
}
}
int nextLeft = maxPos1 - (maxIterate + 1);
int nextRight = maxPos1 + (maxIterate + 1);
if (outOfBounds(nextLeft, n) && outOfBounds(nextRight, n)) {
System.out.println("YES");
System.exit(0);
}
if (outOfBounds(nextLeft, n)) {
for (int i = nextRight; i < n - 1; i++) {
if (unsortedArr[i].diskval - unsortedArr[i+1].diskval != 1) {
System.out.println("NO");
System.exit(0);
}
}
}
if (outOfBounds(nextRight, n)) {
for (int i = nextLeft; i >= 1; i--) {
if (unsortedArr[i].diskval - unsortedArr[i-1].diskval != 1) {
System.out.println("NO");
System.exit(0);
}
}
}
*/
System.out.println("YES");
}
public static boolean outOfBounds(int pos, int n) {
if (pos < 0)
return true;
if (pos >= n)
return true;
return false;
}
}
class diskSet {
int diskpos;
int diskval;
public diskSet(int p, int v) {
diskpos = p;
diskval = v;
}
}
class IntervalComparator implements Comparator<diskSet>
{
public int compare(diskSet i1, diskSet i2)
{
return i1.diskval- i2.diskval;
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
0a7418225050af1824b43ef73c5f8c89
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import static java.lang.Math.*;
public class Main {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader br = new BufferedReader(new FileReader("input.txt"));
StringTokenizer st = new StringTokenizer("");
PrintWriter pw = new PrintWriter(System.out);
public Main() throws FileNotFoundException {
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
String next() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
public static void main(String[] args) throws IOException {
new Main().run();
}
void run() throws IOException {
int n = nextInt();
int[] a = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = nextInt();
}
int index = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > a[index]) index = i;
}
int l = max(0, index - 1);
int r = min(a.length - 1, index + 1);
int last = a[index];
int count = 1;
TreeSet<Integer> ts = new TreeSet<>();
ts.add(index);
while (count < a.length) {
if (l == 0 && ts.contains(0)) {
if (a[r] < last) {
last = a[r];
ts.add(r);
r = min(a.length - 1, r + 1);
count++;
continue;
}
} else if (r == a.length - 1 && ts.contains(r)) {
if (a[l] < last) {
last = a[l];
ts.add(l);
l = max(0, l - 1);
count++;
continue;
}
} else {
if (a[l] < last && a[r] < last) {
if (a[l] > a[r]) {
last = a[l];
ts.add(l);
l = max(0, l - 1);
count++;
continue;
} else {
last = a[r];
ts.add(r);
r = min(a.length - 1, r + 1);
count++;
continue;
}
}
}
pw.println("NO");
pw.close();
return;
}
pw.print("YES");
pw.close();
}
class Pair {
int x, y;
public Pair(int a, int b) {
x = a;
y = b;
}
}
class PairComp implements Comparator<Pair> {
@Override
public int compare(Pair o1, Pair o2) {
if (o1.x == o2.x) return Integer.compare(o1.y, o2.y);
return Integer.compare(o1.x, o2.x);
}
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
997012f836ffd2d3a8f303a2667c1c45
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
Scanner obj= new Scanner(System.in);
int n= obj.nextInt();
int[] list= new int[n];
int highestv=0;
int highest=0;
for(int i=0;i<n;i++){
int current=obj.nextInt();
list[i]=current;
if(current>highestv){
highestv=current;
highest=i;
}
}
boolean able=true;
int prev=0;
for(int i=0;i<n;i++){
int current=list[i];
if(i<highest){
if(prev>current){
able=false;
break;
}
}
else if(i>highest){
if(current>prev&&i!=0){
able=false;
break;
}
}
prev=current;
}
if(able) System.out.println("YES");
else System.out.println("NO");
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
708c007056ace8d2498b4b68c6b407af
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
256 megabytes
|
import java.io.InputStreamReader;
import java.util.Scanner;
public class Pillars {
public static void main(String[] args) {
Scanner sc = new Scanner(new InputStreamReader(System.in));
int numPillars = sc.nextInt();
int[] length = new int[numPillars];
int maxIndex = 0;
int max = 0;
for(int pil =0; pil < numPillars; pil++){
length[pil] = sc.nextInt();
if(max<length[pil]){
max = length[pil];
maxIndex = pil;
}
}
boolean isDone = false;
for(int i=0; i<maxIndex-1; i++){
//increasing
if(length[i+1] - length[i] < 0){
isDone = true;
}
}
for(int i=maxIndex; i<numPillars-1; i++){
//decreasing
if(length[i+1] - length[i] > 0){
isDone = true;
}
}
if(isDone){
System.out.println("No");
}else{
System.out.println("Yes");
}
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
9e6975c0740ff8701ef350b9b1888065
|
train_001.jsonl
|
1563806100
|
There are $$$n$$$ pillars aligned in a row and numbered from $$$1$$$ to $$$n$$$.Initially each pillar contains exactly one disk. The $$$i$$$-th pillar contains a disk having radius $$$a_i$$$.You can move these disks from one pillar to another. You can take a disk from pillar $$$i$$$ and place it on top of pillar $$$j$$$ if all these conditions are met: there is no other pillar between pillars $$$i$$$ and $$$j$$$. Formally, it means that $$$|i - j| = 1$$$; pillar $$$i$$$ contains exactly one disk; either pillar $$$j$$$ contains no disks, or the topmost disk on pillar $$$j$$$ has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all $$$n$$$ disks on the same pillar simultaneously?
|
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 apred = sc.nextInt();
if (apred == n) {
for (int j = 1; j < n; j++) {
int a = sc.nextInt();
if (a > apred) {
System.out.println("NO");
return;
}
apred = a;
}
System.out.println("YES");
return;
}
for (int i = 1; i < n; i++) {
int a = sc.nextInt();
if (a < apred) {
System.out.println("NO");
return;
} else if (a == n) {
if (i == n - 1) {
System.out.println("YES");
return;
}
apred = sc.nextInt();
for (int j = i + 2; j < n; j++) {
a = sc.nextInt();
if (a > apred) {
System.out.println("NO");
return;
}
apred = a;
}
System.out.println("YES");
return;
}
apred = a;
}
}
}
|
Java
|
["4\n1 3 4 2", "3\n3 1 2"]
|
1.5 seconds
|
["YES", "NO"]
|
NoteIn the first case it is possible to place all disks on pillar $$$3$$$ using the following sequence of actions: take the disk with radius $$$3$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$1$$$ and place it on top of pillar $$$2$$$; take the disk with radius $$$2$$$ from pillar $$$4$$$ and place it on top of pillar $$$3$$$; take the disk with radius $$$1$$$ from pillar $$$2$$$ and place it on top of pillar $$$3$$$.
|
Java 8
|
standard input
|
[
"implementation",
"greedy"
] |
12abfbe7118868a0b1237489b5c92760
|
The first line contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) β the number of pillars. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_i$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the radius of the disk initially placed on the $$$i$$$-th pillar. All numbers $$$a_i$$$ are distinct.
| 1,000 |
Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
standard output
| |
PASSED
|
71ad1b681622d56389ea90a72470fb18
|
train_001.jsonl
|
1335614400
|
Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%. An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares). An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares). An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class E implements Runnable {
private MyScanner in;
private PrintWriter out;
final int[] dx = { -1, 0, 1, 0 };
final int[] dy = { 0, -1, 0, 1 };
int n;
int[][] map;
boolean[][] seen;
Deque<Integer> queue;
boolean valid(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < n;
}
int bfs(int i, int j) {
queue.addLast(i);
queue.addLast(j);
int ans = 0;
seen[i][j] = true;
while (!queue.isEmpty()) {
++ans;
int x = queue.removeFirst();
int y = queue.removeFirst();
for (int dir = 0; dir < 4; ++dir) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (valid(nx, ny) && !seen[nx][ny] && map[nx][ny] == 1) {
seen[nx][ny] = true;
queue.addLast(nx);
queue.addLast(ny);
}
}
}
return ans;
}
private void solve() {
n = in.nextInt();
map = new int[n][n];
for (int i = 0; i < n; ++i) {
String temp = in.nextLine();
for (int j = 0; j < n; ++j) {
map[i][j] = temp.charAt(2 * j) - '0';
}
}
int[][] toRight = new int[n][];
for (int i = 0; i < n; ++i) {
int[] cur = toRight[i] = new int[n + 1];
cur[n] = 0;
for (int j = n - 1; j >= 0; --j) {
if (map[i][j] == 0) {
cur[j] = 0;
} else {
cur[j] = cur[j + 1] + 1;
}
}
}
queue = new ArrayDeque<Integer>();
seen = new boolean[n][n];
int circles = 0, squares = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (seen[i][j]) {
continue;
}
if (toRight[i][j] == 0) {
seen[i][j] = true;
continue;
}
bfs(i, j);
if (toRight[i][j] < 15) {
++circles;
} else {
++squares;
}
}
}
out.println(circles + " " + squares);
}
@Override
public void run() {
in = new MyScanner();
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
public static void main(String[] args) {
new E().run();
}
class MyScanner {
private BufferedReader br;
private StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean hasNext() {
while (st == null || !st.hasMoreTokens()) {
try {
String s = br.readLine();
if (s == null) {
return false;
}
st = new StringTokenizer(s);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return st != null && st.hasMoreTokens();
}
private String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String s = br.readLine();
if (s == null) {
return null;
}
st = new StringTokenizer(s);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return st.nextToken();
}
public String nextLine() {
try {
st = null;
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
Java
|
[]
|
5 seconds
|
[]
|
NoteYou are given a sample of original data for each difficulty level. The samples are available at http://codeforces.ru/static/materials/contests/178/e-samples.zip .
|
Java 6
|
standard input
|
[] |
06c7699523a9f8036330857660c0687e
|
The first input line contains a single integer n (1000ββ€βnββ€β2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0ββ€βaijββ€β1), separated by spaces. Value of aijβ=β0 corresponds to a white pixel and aijβ=β1 corresponds to a black one. It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50. The input limitations for getting 20 points are: These test cases have no noise and the sides of the squares are parallel to the coordinate axes. The input limitations for getting 50 points are: These test cases have no noise, but the squares are rotated arbitrarily. The input limitations for getting 100 points are: These test cases have noise and the squares are rotated arbitrarily.
| 1,900 |
Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly.
|
standard output
| |
PASSED
|
3a7dbcff9cba159636c48a462184a8fd
|
train_001.jsonl
|
1335614400
|
Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%. An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares). An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares). An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Solution implements Runnable {
private StringTokenizer st;
private BufferedReader in;
private PrintWriter out;
long count;
long sumx;
long sumy;
double mind, maxd;
private void cDfs(int i, int j, boolean[][] b, boolean[][] col) {
if (i < 0 || i >= b.length || j < 0 || j >= b.length || !b[i][j] || col[i][j]) {
return;
}
col[i][j] = true;
count++;
sumx += i;
sumy += j;
for (int d = 0; d < 4; ++d) {
cDfs(i + dx[d], j + dy[d], b, col);
}
}
private void dDfs(int i, int j, boolean[][] b, boolean[][] col) {
double x = (double)(i * count - sumx) / count;
double y = (double)(j * count - sumy) / count;
double dist = Math.sqrt(x * x + y * y);
boolean out = i < 0 || i >= b.length || j < 0 || j >= b.length;
if (!out && b[i][j]) {
b[i][j] = false;
maxd = Math.max(maxd, dist);
for (int d = 0; d < 4; ++d) {
dDfs(i + dx[d], j + dy[d], b, col);
}
} else {
if (out || !col[i][j]) {
mind = Math.min(mind, dist);
}
}
}
public void solve() throws IOException {
int n = nextInt();
boolean[][] a = new boolean[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
a[i][j] = nextInt() == 1;
}
}
boolean[][] b = new boolean[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int count = 0, total = 0;
for (int dx = -2; dx <= 2; ++dx) {
for (int dy = -2; dy <= 2; ++dy) {
if (i + dx >= 0 && i + dx < n && j + dy >= 0 && j + dy < n) {
total++;
if (a[i + dx][j + dy]) {
count++;
}
}
}
}
b[i][j] = count * 5 >= total * 3;
}
}
a = b;
b = new boolean[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int count = 0, total = 0;
for (int dx = -2; dx <= 2; ++dx) {
for (int dy = -2; dy <= 2; ++dy) {
if (i + dx >= 0 && i + dx < n && j + dy >= 0 && j + dy < n) {
total++;
if (a[i + dx][j + dy]) {
count++;
}
}
}
}
b[i][j] = a[i][j] || count * 3 >= total;
}
}
boolean[][] col = new boolean[n][n];
int ans1 = 0, ans2 = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (b[i][j]) {
count = 0;
sumx = 0;
sumy = 0;
cDfs(i, j, b, col);
if (count < 100) {
continue;
}
mind = Double.POSITIVE_INFINITY;
maxd = 0.;
dDfs(i, j, b, col);
// System.err.println(mind + " " + maxd);
if (maxd / mind < (Math.sqrt(2.) + 1) / 2.) {
ans1++;
} else {
ans2++;
}
}
}
}
out.println(ans1 + " " + ans2);
}
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
// in = new BufferedReader(new FileReader("hard_1.in"));
out = new PrintWriter(System.out);
eat("");
solve();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
void eat(String s) {
st = new StringTokenizer(s);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
new Thread(null, new Solution(), "vasya", 64000000).start();
}
}
|
Java
|
[]
|
5 seconds
|
[]
|
NoteYou are given a sample of original data for each difficulty level. The samples are available at http://codeforces.ru/static/materials/contests/178/e-samples.zip .
|
Java 6
|
standard input
|
[] |
06c7699523a9f8036330857660c0687e
|
The first input line contains a single integer n (1000ββ€βnββ€β2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0ββ€βaijββ€β1), separated by spaces. Value of aijβ=β0 corresponds to a white pixel and aijβ=β1 corresponds to a black one. It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50. The input limitations for getting 20 points are: These test cases have no noise and the sides of the squares are parallel to the coordinate axes. The input limitations for getting 50 points are: These test cases have no noise, but the squares are rotated arbitrarily. The input limitations for getting 100 points are: These test cases have noise and the squares are rotated arbitrarily.
| 1,900 |
Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly.
|
standard output
| |
PASSED
|
f33733cf179d351072da3dfc437c325d
|
train_001.jsonl
|
1335614400
|
Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%. An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares). An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares). An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Solution implements Runnable {
private StringTokenizer st;
private BufferedReader in;
private PrintWriter out;
long count;
long sumx;
long sumy;
double mind, maxd;
private void cDfs(int i, int j, boolean[][] b, boolean[][] col) {
if (i < 0 || i >= b.length || j < 0 || j >= b.length || !b[i][j] || col[i][j]) {
return;
}
col[i][j] = true;
count++;
sumx += i;
sumy += j;
for (int d = 0; d < 4; ++d) {
cDfs(i + dx[d], j + dy[d], b, col);
}
}
private void dDfs(int i, int j, boolean[][] b, boolean[][] col) {
double x = (double)(i * count - sumx) / count;
double y = (double)(j * count - sumy) / count;
double dist = Math.sqrt(x * x + y * y);
boolean out = i < 0 || i >= b.length || j < 0 || j >= b.length;
if (!out && b[i][j]) {
b[i][j] = false;
maxd = Math.max(maxd, dist);
for (int d = 0; d < 4; ++d) {
dDfs(i + dx[d], j + dy[d], b, col);
}
} else {
if (out || !col[i][j]) {
mind = Math.min(mind, dist);
}
}
}
public void solve() throws IOException {
int n = nextInt();
boolean[][] a = new boolean[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
a[i][j] = nextInt() == 1;
}
}
boolean[][] b = new boolean[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int count = 0, total = 0;
for (int dx = -2; dx <= 2; ++dx) {
for (int dy = -2; dy <= 2; ++dy) {
if (i + dx >= 0 && i + dx < n && j + dy >= 0 && j + dy < n) {
total++;
if (a[i + dx][j + dy]) {
count++;
}
}
}
}
b[i][j] = count * 5 >= total * 3;
}
}
boolean[][] col = new boolean[n][n];
int ans1 = 0, ans2 = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (b[i][j]) {
count = 0;
sumx = 0;
sumy = 0;
cDfs(i, j, b, col);
if (count < 100) {
continue;
}
mind = Double.POSITIVE_INFINITY;
maxd = 0.;
dDfs(i, j, b, col);
// System.err.println(mind + " " + maxd);
if (maxd / mind < (Math.sqrt(2.) + 1) / 2.) {
ans1++;
} else {
ans2++;
}
}
}
}
out.println(ans1 + " " + ans2);
}
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
// in = new BufferedReader(new FileReader("hard_1.in"));
out = new PrintWriter(System.out);
eat("");
solve();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
void eat(String s) {
st = new StringTokenizer(s);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
new Thread(null, new Solution(), "vasya", 64000000).start();
}
}
|
Java
|
[]
|
5 seconds
|
[]
|
NoteYou are given a sample of original data for each difficulty level. The samples are available at http://codeforces.ru/static/materials/contests/178/e-samples.zip .
|
Java 6
|
standard input
|
[] |
06c7699523a9f8036330857660c0687e
|
The first input line contains a single integer n (1000ββ€βnββ€β2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0ββ€βaijββ€β1), separated by spaces. Value of aijβ=β0 corresponds to a white pixel and aijβ=β1 corresponds to a black one. It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50. The input limitations for getting 20 points are: These test cases have no noise and the sides of the squares are parallel to the coordinate axes. The input limitations for getting 50 points are: These test cases have no noise, but the squares are rotated arbitrarily. The input limitations for getting 100 points are: These test cases have noise and the squares are rotated arbitrarily.
| 1,900 |
Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly.
|
standard output
| |
PASSED
|
a28385b3bf9e98baadfca6a4365ca98c
|
train_001.jsonl
|
1335614400
|
Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%. An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares). An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares). An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Solution implements Runnable {
private StringTokenizer st;
private BufferedReader in;
private PrintWriter out;
long count;
long sumx;
long sumy;
double mind, maxd;
private void cDfs(int i, int j, boolean[][] b, boolean[][] col) {
if (i < 0 || i >= b.length || j < 0 || j >= b.length || !b[i][j] || col[i][j]) {
return;
}
col[i][j] = true;
count++;
sumx += i;
sumy += j;
for (int d = 0; d < 4; ++d) {
cDfs(i + dx[d], j + dy[d], b, col);
}
}
private void dDfs(int i, int j, boolean[][] b, boolean[][] col) {
if (i < 0 || i >= b.length || j < 0 || j >= b.length) {
return;
}
double x = (double)(i * count - sumx) / count;
double y = (double)(j * count - sumy) / count;
double dist = Math.sqrt(x * x + y * y);
if (b[i][j]) {
b[i][j] = false;
maxd = Math.max(maxd, dist);
for (int d = 0; d < 4; ++d) {
dDfs(i + dx[d], j + dy[d], b, col);
}
} else {
if (!col[i][j]) {
mind = Math.min(mind, dist);
}
}
}
public void solve() throws IOException {
int n = nextInt();
boolean[][] a = new boolean[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
a[i][j] = nextInt() == 1;
}
}
boolean[][] b = new boolean[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int count = 0, total = 0;
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
if (i + dx >= 0 && i+ dx < n && j + dy >= 0 && j + dy < n) {
total++;
if (a[i + dx][j + dy]) {
count++;
}
}
}
}
b[i][j] = count * 3 >= total;
}
}
boolean[][] col = new boolean[n][n];
int ans1 = 0, ans2 = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (b[i][j]) {
count = 0;
sumx = 0;
sumy = 0;
cDfs(i, j, b, col);
if (count < 100) {
continue;
}
mind = Double.POSITIVE_INFINITY;
maxd = 0.;
dDfs(i, j, b, col);
// System.err.println(mind + " "+ maxd);
if (maxd / mind < (Math.sqrt(2.) + 1) / 2.) {
ans1++;
} else {
ans2++;
}
}
}
}
out.println(ans1 + " " + ans2);
}
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
// in = new BufferedReader(new FileReader("easy_2.in"));
out = new PrintWriter(System.out);
eat("");
solve();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
void eat(String s) {
st = new StringTokenizer(s);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
new Thread(null, new Solution(), "vasya", 64000000).start();
}
}
|
Java
|
[]
|
5 seconds
|
[]
|
NoteYou are given a sample of original data for each difficulty level. The samples are available at http://codeforces.ru/static/materials/contests/178/e-samples.zip .
|
Java 6
|
standard input
|
[] |
06c7699523a9f8036330857660c0687e
|
The first input line contains a single integer n (1000ββ€βnββ€β2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0ββ€βaijββ€β1), separated by spaces. Value of aijβ=β0 corresponds to a white pixel and aijβ=β1 corresponds to a black one. It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50. The input limitations for getting 20 points are: These test cases have no noise and the sides of the squares are parallel to the coordinate axes. The input limitations for getting 50 points are: These test cases have no noise, but the squares are rotated arbitrarily. The input limitations for getting 100 points are: These test cases have noise and the squares are rotated arbitrarily.
| 1,900 |
Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly.
|
standard output
| |
PASSED
|
d67f99e510ad23ded3a1fd4efbfbf3cf
|
train_001.jsonl
|
1335614400
|
Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%. An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares). An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares). An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).
|
256 megabytes
|
import java.io.*;
import java.util.*;
/*
*
* E.java
* Written by Andy Huang: 9:57:38 AM Apr 28, 2012
*/
public class E {
static boolean img[][];
static final int d[][] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
static void solve() {
int n = in.nInt();
img = new boolean[n + 2][n + 2];
int sq = 0;
int cir = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n ; j++)
if (in.nInt() == 1)
img[i][j] = true;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (!img[i][j])
continue;
int rowlen = 0;
int temp = j;
while (img[i][temp]){
temp++;
rowlen++;
}
temp = i;
int collen = 0;
while (img[temp][j]){
temp++;
collen++;
}
//pln(rowlen+","+collen);
if (rowlen == collen){
sq++;
for (int k = i; k < i + collen; k++)
for (int l = j; l < j + rowlen; l++)
img[k][l] = false;
}
else{
cir++;
int start = j - (collen >> 1);
for (int k = i; k < i + collen; k++)
for (int l = start; l < start + collen; l++)
img[k][l] = false;
}
}
}
out.append(cir);
out.append(' ');
out.appendln(sq);
}
public static void main(String[] args) {
in = new Input();
out = new Output();
solve();
out.print();
}
static Output out;
static Input in;
static void pln(Object o) {
System.out.println(o);
}
static void pf(Object o) {
System.out.print(o);
}
}
final class Point extends java.awt.Point {
private static final long serialVersionUID = -5276940640259749850L;
public Point(int a, int b) {
super(a, b);
}
public Point(Point p) {
super(p);
}
public Point() {
super();
}
public String toString() {
return "(" + x + "," + y + ")";
}
}
final class Input {
private java.io.BufferedReader reader;
private java.util.StringTokenizer tokenizer;
public Input() {
reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
// try {
// reader = new BufferedReader(new FileReader("input.txt"));
// }
// catch (FileNotFoundException e) {
// }
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new java.util.StringTokenizer(reader.readLine());
}
catch (java.io.IOException e) {
throw new RuntimeException("Input Error");
}
}
return tokenizer.nextToken();
}
public String nLine() {
try {
return reader.readLine();
}
catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
public long nLong() {
return Long.parseLong(next());
}
public int nInt() {
return Integer.parseInt(next());
}
public double nDouble() {
return Double.parseDouble(next());
}
}
final class Output {
public StringBuilder buffer;
Output() {
buffer = new StringBuilder();
}
Output(int size) {
buffer = new StringBuilder(size);
}
void print() {
System.out.print(buffer.toString());
}
void flush() {
System.out.flush();
}
<T> void append(T obj) {
buffer.append(obj);
}
<T> void appendln(T obj) {
append(obj);
append('\n');
}
void delete(int index) {
buffer.deleteCharAt(index);
}
}
|
Java
|
[]
|
5 seconds
|
[]
|
NoteYou are given a sample of original data for each difficulty level. The samples are available at http://codeforces.ru/static/materials/contests/178/e-samples.zip .
|
Java 6
|
standard input
|
[] |
06c7699523a9f8036330857660c0687e
|
The first input line contains a single integer n (1000ββ€βnββ€β2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0ββ€βaijββ€β1), separated by spaces. Value of aijβ=β0 corresponds to a white pixel and aijβ=β1 corresponds to a black one. It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50. The input limitations for getting 20 points are: These test cases have no noise and the sides of the squares are parallel to the coordinate axes. The input limitations for getting 50 points are: These test cases have no noise, but the squares are rotated arbitrarily. The input limitations for getting 100 points are: These test cases have noise and the squares are rotated arbitrarily.
| 1,900 |
Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly.
|
standard output
| |
PASSED
|
5524cb1608e7adf03976e57c3230fff2
|
train_001.jsonl
|
1335614400
|
Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%. An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares). An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares). An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).
|
256 megabytes
|
//package abbyy2.hard;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ES2 {
BufferedReader br;
PrintWriter out;
String INPUT = "";
void solve() throws Exception
{
int n = Integer.parseInt(br.readLine());
DJSet ds = new DJSet(n*n);
// long S = System.currentTimeMillis();
int[][] p = new int[n][n];
for(int i = 0;i < n;i++){
String line = br.readLine();
for(int j = 0;j < n;j++){
p[i][j] = line.charAt(j*2)-'0';
}
}
// tr(System.currentTimeMillis() - S);
for(int i = 0;i < n;i++){
for(int j = 0;j < n-1;j++){
if(p[i][j] == 1 && p[i][j+1] == 1){
ds.union(i*n+j, i*n+(j+1));
}
}
}
for(int i = 0;i < n-1;i++){
for(int j = 0;j < n;j++){
if(p[i][j] == 1 && p[i+1][j] == 1){
ds.union(i*n+j, (i+1)*n+j);
}
}
}
double[][] cent = new double[n*n][2];
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
int clus = ds.root(i*n+j);
cent[clus][0] += i;
cent[clus][1] += j;
}
}
// tr(System.currentTimeMillis() - S);
Map<Integer, Integer> ics = new HashMap<Integer, Integer>();
int[] nums = new int[100];
int q = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
if(ds.upper[i*n+j] < -50){
int num = -ds.upper[i*n+j];
int clus = i*n+j;
cent[clus][0] /= num;
cent[clus][1] /= num;
nums[q] = num;
ics.put(clus, q++);
}
}
}
double[] max = new double[q];
double[] v = new double[q];
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
if(ds.upper[ds.root(i*n+j)] < -50){
int clus = ds.root(i*n+j);
int id = ics.get(clus);
double D = (i-cent[clus][0])*(i-cent[clus][0])+(j-cent[clus][1])*(j-cent[clus][1]);
max[id] = Math.max(max[id], D);
v[id] += D;
}
}
}
int cir = 0, sq = 0;
for(int i = 0;i < q;i++){
v[i] /= nums[i];
if(v[i] > 5*max[i]/12){
cir++;
}else{
sq++;
}
}
out.println(cir + " " + sq);
}
public static class DJSet { public int[] upper; public DJSet(int n){ upper = new int[n]; Arrays.fill(upper, -1);} public int root(int x){ return upper[x] < 0 ? x : (upper[x] = root(upper[x]));} public boolean equiv(int x, int y){ return root(x) == root(y);} public void union(int x, int y){ x = root(x);y = root(y);if(x != y) { if(upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x;}} public int count(){ int ct = 0; for(int i = 0;i < upper.length;i++){ if(upper[i] < 0)ct++; } return ct; }}
void run() throws Exception
{
long S = System.currentTimeMillis();
br = new BufferedReader(INPUT.isEmpty() ? new InputStreamReader(System.in) : new StringReader(INPUT));
// br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("x:\\easy_1.in"))));
out = new PrintWriter(System.out);
solve();
out.flush();
long G = System.currentTimeMillis();
tr(G-S+"ms");
}
public static void main(String[] args) throws Exception
{
new ES2().run();
}
void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
|
Java
|
[]
|
5 seconds
|
[]
|
NoteYou are given a sample of original data for each difficulty level. The samples are available at http://codeforces.ru/static/materials/contests/178/e-samples.zip .
|
Java 6
|
standard input
|
[] |
06c7699523a9f8036330857660c0687e
|
The first input line contains a single integer n (1000ββ€βnββ€β2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0ββ€βaijββ€β1), separated by spaces. Value of aijβ=β0 corresponds to a white pixel and aijβ=β1 corresponds to a black one. It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50. The input limitations for getting 20 points are: These test cases have no noise and the sides of the squares are parallel to the coordinate axes. The input limitations for getting 50 points are: These test cases have no noise, but the squares are rotated arbitrarily. The input limitations for getting 100 points are: These test cases have noise and the squares are rotated arbitrarily.
| 1,900 |
Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly.
|
standard output
| |
PASSED
|
f444f01da6280fe0c1a6cb305460974d
|
train_001.jsonl
|
1335614400
|
Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%. An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares). An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares). An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).
|
256 megabytes
|
import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class E implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Thread(null, new E(), "", 256 * (1L << 20)).start();
}
public void run() {
try {
long t1 = System.currentTimeMillis();
if (System.getProperty("ONLINE_JUDGE") != null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("src/input.txt"));
out = new PrintWriter(System.out);
}
Locale.setDefault(Locale.US);
solve();
in.close();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
// solution
int squares;
int circles;
int[][] a;
boolean[][] visited;
int size;
int minX;
int maxX;
int minY;
int maxY;
boolean square()
{
return (maxX - minX + 1) * (maxY - minY + 1) == size;
}
boolean circle()
{
return true;
}
void solve() throws IOException {
int n = readInt();
a = new int[n][n];
visited = new boolean[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = readInt();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!visited[i][j] && a[i][j] == 1) {
size = 0;
minX = maxX = i;
minY = maxY = j;
dfs(i, j);
if (square()) {
squares++;
} else if (circle()) {
circles++;
}
}
}
}
out.println(circles + " " + squares);
}
private void dfs(int x, int y) {
if (x < 0 || y < 0 || x >= a.length || y >= a[0].length) {
return;
}
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
visited[x][y] = true;
size++;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (!visited[x + i][y + j] && a[x + i][y + j] == 1) {
dfs(x + i, y + j);
}
}
}
}
}
|
Java
|
[]
|
5 seconds
|
[]
|
NoteYou are given a sample of original data for each difficulty level. The samples are available at http://codeforces.ru/static/materials/contests/178/e-samples.zip .
|
Java 6
|
standard input
|
[] |
06c7699523a9f8036330857660c0687e
|
The first input line contains a single integer n (1000ββ€βnββ€β2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0ββ€βaijββ€β1), separated by spaces. Value of aijβ=β0 corresponds to a white pixel and aijβ=β1 corresponds to a black one. It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50. The input limitations for getting 20 points are: These test cases have no noise and the sides of the squares are parallel to the coordinate axes. The input limitations for getting 50 points are: These test cases have no noise, but the squares are rotated arbitrarily. The input limitations for getting 100 points are: These test cases have noise and the squares are rotated arbitrarily.
| 1,900 |
Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly.
|
standard output
| |
PASSED
|
f7cb0ae1fc56209d27e6ba92b2b7e1e2
|
train_001.jsonl
|
1335614400
|
Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%. An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares). An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares). An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
import java.util.Stack;
import java.util.StringTokenizer;
public class E {
private void solve() throws IOException {
int n = nextInt();
int[][] a = new int[n][n];
byte[][] used = new byte[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = nextInt();
}
}
int square = 0;
int circle = 0;
int[] qx = new int[n * n];
int[] qy = new int[n * n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 1 && used[i][j] == 0) {
leftX = upX = i;
leftY = upY = j;
qx[0] = i;
qy[0] = j;
int l = 0;
int r = 1;
while (l != r) {
int x = qx[l];
int y = qy[l];
l++;
if (x < upX && x == upX && y < upY) {
upX = x;
upY = y;
}
if (y < leftY || y == leftY && x < leftX) {
leftX = x;
leftY = y;
}
if (x > 0 && a[x - 1][y] == 1 && used[x - 1][y] == 0) {
used[x - 1][y] = 1;
qx[r] = x - 1;
qy[r] = y;
r++;
}
if (x < n - 1 && a[x + 1][y] == 1 && used[x + 1][y] == 0) {
used[x + 1][y] = 1;
qx[r] = x + 1;
qy[r] = y;
r++;
}
if (y > 0 && a[x][y - 1] == 1 && used[x][y - 1] == 0) {
used[x][y - 1] = 1;
qx[r] = x;
qy[r] = y - 1;
r++;
}
if (y < n - 1 && a[x][y + 1] == 1 && used[x][y + 1] == 0) {
used[x][y + 1] = 1;
qx[r] = x;
qy[r] = y + 1;
r++;
}
}
if (upX == leftX) {
square++;
} else {
boolean dy = false;
int y = upY;
for (int x = upX; x <= leftX; x++) {
int k = 0;
while (y >= 0 && a[x][y] == 1) {
y--;
k++;
}
y++;
k--;
if (k > 1) {
dy = true;
}
}
boolean dx = false;
int x = leftX;
for (y = leftY; y <= upY; y++) {
int k = 0;
while (x >= 0 && a[x][y] == 1) {
x--;
k++;
}
x++;
k--;
if (k > 1) {
dx = true;
}
}
if (dx && dy) {
circle++;
} else {
square++;
}
}
}
}
}
println(circle + " " + square);
}
int leftX;
int leftY;
int upX;
int upY;
private String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
private int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
private double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private void print(Object o) {
writer.print(o);
}
private void println(Object o) {
writer.println(o);
}
private void printf(String format, Object... o) {
writer.printf(format, o);
}
public static void main(String[] args) {
long time = System.currentTimeMillis();
Locale.setDefault(Locale.US);
new E().run();
System.err.printf("%.3f\n", 1e-3 * (System.currentTimeMillis() - time));
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
private void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
// reader = new BufferedReader(new FileReader("medium_2.in"));
solve();
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(13);
}
}
}
|
Java
|
[]
|
5 seconds
|
[]
|
NoteYou are given a sample of original data for each difficulty level. The samples are available at http://codeforces.ru/static/materials/contests/178/e-samples.zip .
|
Java 6
|
standard input
|
[] |
06c7699523a9f8036330857660c0687e
|
The first input line contains a single integer n (1000ββ€βnββ€β2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0ββ€βaijββ€β1), separated by spaces. Value of aijβ=β0 corresponds to a white pixel and aijβ=β1 corresponds to a black one. It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50. The input limitations for getting 20 points are: These test cases have no noise and the sides of the squares are parallel to the coordinate axes. The input limitations for getting 50 points are: These test cases have no noise, but the squares are rotated arbitrarily. The input limitations for getting 100 points are: These test cases have noise and the squares are rotated arbitrarily.
| 1,900 |
Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly.
|
standard output
| |
PASSED
|
596ccbbf89d985c60dfcfa901b98552e
|
train_001.jsonl
|
1335614400
|
Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%. An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares). An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares). An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class E2{
public static void main(String ar[]){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
//BufferedReader in=new BufferedReader(new FileReader("e-samples\\easy_2.in"));
int n=Integer.parseInt(in.readLine());
int q=0;
int p=0;
char row[]=new char[n];
for(int i=0;i<n;i++){
boolean f=true;
String s=in.readLine();
for(int j=0;j<n;j++){
char c=s.charAt(j<<1);
char pr=(j==0 || row[j-1]==0)?0:row[j-1];
if(c=='1'){
if(row[j]==0){
if(j==0){row[j]='s';p++;}
else if(pr==0)row[j]='n';
else row[j]=pr;
}else if(row[j]=='n'){
if(pr=='n'){
q++;
int k=j;
while(row[k]=='n')row[k--]='q';
}else if(pr==0){row[j]='s';p++;}
else row[j]=pr;
}else if(row[j]=='q' && pr=='n'){
int k=j-1;
while(row[k]=='n')row[k--]='q';
//System.out.println(new String(row)+'-');
}
}else row[j]=0;
}
//if(i>400 && i<500)System.out.println(new String(row));
}
System.out.println(""+q+' '+p);
}catch(Exception e){e.printStackTrace();}
}
}
|
Java
|
[]
|
5 seconds
|
[]
|
NoteYou are given a sample of original data for each difficulty level. The samples are available at http://codeforces.ru/static/materials/contests/178/e-samples.zip .
|
Java 6
|
standard input
|
[] |
06c7699523a9f8036330857660c0687e
|
The first input line contains a single integer n (1000ββ€βnββ€β2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0ββ€βaijββ€β1), separated by spaces. Value of aijβ=β0 corresponds to a white pixel and aijβ=β1 corresponds to a black one. It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50. The input limitations for getting 20 points are: These test cases have no noise and the sides of the squares are parallel to the coordinate axes. The input limitations for getting 50 points are: These test cases have no noise, but the squares are rotated arbitrarily. The input limitations for getting 100 points are: These test cases have noise and the squares are rotated arbitrarily.
| 1,900 |
Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly.
|
standard output
| |
PASSED
|
ac3fb0ca67e4ca60f47c42a1a2b414af
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main implements Runnable {
static final int MOD = (int) 1e9 + 7;
static final int MI = (int) 1e9;
static final long ML = (long) 1e18;
static final Reader in = new Reader();
static final PrintWriter out = new PrintWriter(System.out);
StringBuilder answer = new StringBuilder();
public static void main(String[] args) {
new Thread(null, new Main(), "persefone", 1 << 32).start();
}
@Override
public void run() {
solve();
printf();
flush();
}
void solve() {
char[] a = in.next().toCharArray();
char[] b = in.next().toCharArray();
int na = 0;
for (char c : a) if (c == '1') na++;
int nb = 0;
for (char c : b) if (c == '1') nb++;
if (nb > na + 1 || na + 1 == nb && na % 2 == 0) {
printf("NO");
} else {
printf("YES");
}
}
void printf() {
out.print(answer);
}
void close() {
out.close();
}
void flush() {
out.flush();
}
void printf(Stream<?> str) {
str.forEach(o -> add(o, " "));
add("\n");
}
void printf(Object... obj) {
printf(false, obj);
}
void printfWithDescription(Object... obj) {
printf(true, obj);
}
private void printf(boolean b, Object... obj) {
if (obj.length > 1) {
for (int i = 0; i < obj.length; i++) {
if (b)
add(obj[i].getClass().getSimpleName(), " - ");
if (obj[i] instanceof Collection<?>) {
printf((Collection<?>) obj[i]);
} else if (obj[i] instanceof int[][]) {
printf((int[][]) obj[i]);
} else if (obj[i] instanceof long[][]) {
printf((long[][]) obj[i]);
} else if (obj[i] instanceof double[][]) {
printf((double[][]) obj[i]);
} else
printf(obj[i]);
}
return;
}
if (b)
add(obj[0].getClass().getSimpleName(), " - ");
printf(obj[0]);
}
void printf(Object o) {
if (o instanceof int[])
printf(Arrays.stream((int[]) o).boxed());
else if (o instanceof char[])
printf(new String((char[]) o));
else if (o instanceof long[])
printf(Arrays.stream((long[]) o).boxed());
else if (o instanceof double[])
printf(Arrays.stream((double[]) o).boxed());
else if (o instanceof boolean[]) {
for (boolean b : (boolean[]) o)
add(b, " ");
add("\n");
} else
add(o, "\n");
}
void printf(int[]... obj) {
for (int i = 0; i < obj.length; i++)
printf(obj[i]);
}
void printf(long[]... obj) {
for (int i = 0; i < obj.length; i++)
printf(obj[i]);
}
void printf(double[]... obj) {
for (int i = 0; i < obj.length; i++)
printf(obj[i]);
}
void printf(boolean[]... obj) {
for (int i = 0; i < obj.length; i++)
printf(obj[i]);
}
void printf(Collection<?> col) {
printf(col.stream());
}
<T, K> void add(T t, K k) {
if (t instanceof Collection<?>) {
((Collection<?>) t).forEach(i -> add(i, " "));
} else if (t instanceof Object[]) {
Arrays.stream((Object[]) t).forEach(i -> add(i, " "));
} else
add(t);
add(k);
}
<T> void add(T t) {
answer.append(t);
}
static class Reader {
private BufferedReader br;
private StringTokenizer st;
Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
Reader(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
}
boolean isReady() throws IOException {
return br.ready();
}
String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
ae0d219a9c69612c0e5b549e0ececd71
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
String a = in.n();
String b = in.n();
int countA = 0;
int countB = 0;
int len1 = a.length();
int len2 = b.length();
for (int i = 0; i < len1; ++i)
countA += (a.charAt(i) == '1' ? 1 : 0);
for (int i = 0; i < len2; ++i)
countB += (b.charAt(i) == '1' ? 1 : 0);
if (countA % 2 == 1)
++countA;
out.println(countA >= countB ? "YES" : "NO");
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream inputStream) {
br = new BufferedReader(new InputStreamReader(inputStream));
}
public String n() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
f903cb59d84a95279bef72985bb98645
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class P297A {
private static void solve() {
char[] c1 = next().toCharArray();
char[] c2 = next().toCharArray();
int cnt1 = 0;
for (int i = 0; i < c1.length; i++) {
if (c1[i] == '1') cnt1++;
}
int cnt2 = 0;
for (int i = 0; i < c2.length; i++) {
if (c2[i] == '1') cnt2++;
}
if ((cnt1 % 2 == 0 && cnt2 > cnt1) || (cnt1 % 2 != 0 && cnt2 > cnt1 + 1)) {
System.out.println("NO");
} else {
System.out.println("YES");
}
}
private static void run() {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
private static StringTokenizer st;
private static BufferedReader br;
private static PrintWriter out;
private static String next() {
while (st == null || !st.hasMoreElements()) {
String s;
try {
s = br.readLine();
} catch (IOException e) {
return null;
}
st = new StringTokenizer(s);
}
return st.nextToken();
}
private static int nextInt() {
return Integer.parseInt(next());
}
private static long nextLong() {
return Long.parseLong(next());
}
public static void main(String[] args) {
run();
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
f9335a486ed34c202298b85ffcfdf1c1
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.StringTokenizer;
// knuth-moris-pattrn string matching algorithm
public class Main {
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
String a=br.readLine();
String b=br.readLine();
int count=0;
int count1=0;
for(int i=0;i<a.length();i++)
if(a.charAt(i)=='1')
count++;
for(int i=0;i<b.length();i++)
if(b.charAt(i)=='1')
count1++;
if(count%2==1)
count++;
if(count>=count1)
System.out.print("YES");
else System.out.print("NO");
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
bef57912ae43c5d2826f291cfd43b079
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main extends PrintWriter {
BufferedReader in;
StringTokenizer stok;
final Random rand = new Random(31);
final int inf = (int) 1e9;
final long linf = (long) 1e18;
final static String IO = "_std";
int k1(char[] s) {
int res = 0;
for (char c : s) {
if (c == '1') {
res++;
}
}
return res;
}
void solve() throws IOException {
char[] s = next().toCharArray();
char[] t = next().toCharArray();
println(k1(s) + k1(s) % 2 >= k1(t) ? "YES" : "NO");
}
Main() {
super(System.out);
in = new BufferedReader(new InputStreamReader(System.in));
}
Main(String fileIn, String fileOut) throws IOException {
super(fileOut);
in = new BufferedReader(new FileReader(fileIn));
}
public static void main(String[] args) throws IOException {
Main main;
if ("_std".equals(IO)) {
main = new Main();
} else if ("_iotxt".equals(IO)) {
main = new Main("input.txt", "output.txt");
} else {
main = new Main(IO + ".in", IO + ".out");
}
main.solve();
main.close();
}
String next() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = in.readLine();
if (s == null) {
return null;
}
stok = new StringTokenizer(s);
}
return stok.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());
}
int[] nextIntArray(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = nextInt();
}
return a;
}
int[] nextIntArraySorted(int len) throws IOException {
int[] a = nextIntArray(len);
shuffle(a);
Arrays.sort(a);
return a;
}
void shuffle(int[] a) {
for (int i = 1; i < a.length; i++) {
int x = rand.nextInt(i + 1);
int _ = a[i];
a[i] = a[x];
a[x] = _;
}
}
void shuffleAndSort(int[] a) {
shuffle(a);
Arrays.sort(a);
}
boolean nextPermutation(int[] p) {
for (int a = p.length - 2; a >= 0; --a) {
if (p[a] < p[a + 1])
for (int b = p.length - 1; ; --b)
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
}
return false;
}
<T> List<T>[] createAdjacencyList(int n) {
List<T>[] res = new List[n];
for (int i = 0; i < n; i++) {
res[i] = new ArrayList<>();
}
return res;
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
dd83d7f1b629741461dec4b380660573
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
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.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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
String a = in.next();
int aCount = count(a);
String b = in.next();
int bCount = count(b);
if (aCount + (aCount % 2) >= bCount)
out.println("YES");
else out.println("NO");
}
int count(String s) {
int res = 0;
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == '1') res++;
return res;
}
}
static class InputReader {
private StringTokenizer tokenizer;
private BufferedReader reader;
public InputReader(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
private void fillTokenizer() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public String next() {
fillTokenizer();
return tokenizer.nextToken();
}
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
d35ab5eae108cdaa4c837faace1495c1
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.util.Scanner;
public class A297 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = 0;
char[] s = in.next().toCharArray();
char[] n = in.next().toCharArray();
for (int i = 0; i < s.length; ++i) {
count += s[i] == '1' ? 1 : 0;
}
count += count % 2 == 1 ? 1 : 0;
for (int i = 0; i < n.length; ++i) {
count -= n[i] == '1' ? 1 : 0;
}
if (count >= 0) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
f38497877b2109422d731a48b30969ce
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Template implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
try {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
} catch (Exception e) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
}
class GraphBuilder {
int n, m;
int[] x, y;
int index;
int[] size;
GraphBuilder(int n, int m) {
this.n = n;
this.m = m;
x = new int[m];
y = new int[m];
size = new int[n];
}
void add(int u, int v) {
x[index] = u;
y[index] = v;
size[u]++;
size[v]++;
index++;
}
int[][] build() {
int[][] graph = new int[n][];
for (int i = 0; i < n; i++) {
graph[i] = new int[size[i]];
}
for (int i = index - 1; i >= 0; i--) {
int u = x[i];
int v = y[i];
graph[u][--size[u]] = v;
graph[v][--size[v]] = u;
}
return graph;
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine());
} catch (Exception e) {
return null;
}
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException {
int[] res = new int[size];
for (int i = 0; i < size; i++) {
res[i] = readInt();
}
return res;
}
long[] readLongArray(int size) throws IOException {
long[] res = new long[size];
for (int i = 0; i < size; i++) {
res[i] = readLong();
}
return res;
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
<T> List<T>[] createGraphList(int size) {
List<T>[] list = new List[size];
for (int i = 0; i < size; i++) {
list[i] = new ArrayList<>();
}
return list;
}
public static void main(String[] args) {
new Template().run();
// new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start();
}
long timeBegin, timeEnd;
void time() {
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
long memoryTotal, memoryFree;
void memory() {
memoryFree = Runtime.getRuntime().freeMemory();
System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
+ " KB");
}
public void run() {
try {
timeBegin = System.currentTimeMillis();
memoryTotal = Runtime.getRuntime().freeMemory();
init();
solve();
out.close();
if (System.getProperty("ONLINE_JUDGE") == null) {
time();
memory();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
void solve() throws IOException {
String s = readString();
String t = readString();
out.println(fullSolve(s, t) ? "YES" : "NO");
//out.println(recur(s, t, 0) ? "YES" : "NO");
// while (true) {
// s = gen();
// t = gen();
// if (fullSolve(s, t) != recur(s, t, 0)) {
// throw new RuntimeException(s + " " + t);
// }
// }
}
Random rnd = new Random();
String gen() {
int len = rnd.nextInt(4) + 1;
String s = "";
while (len --> 0) {
s += rnd.nextInt(2);
}
return s;
}
boolean recur(String s, String t, int it) {
if (it > 20) return false;
if (s.equals(t)) return true;
if (s.length() > 0 && recur(s.substring(1), t, it + 1)) return true;
int ones = 0;
for (char x : s.toCharArray()) {
if (x == '1') ones++;
}
ones %= 2;
s = s + ones;
return recur(s, t, it + 1);
}
boolean fullSolve(String s, String t) {
if (solve(s, t)) return true;
int ones = 0;
for (char x : s.toCharArray()) {
if (x == '1') {
ones++;
}
}
s += ones % 2;
return solve(s, t);
}
boolean solve(String s, String t) {
char[] a = s.toCharArray();
char[] b = t.toCharArray();
int ones = 0;
for (char x : a) {
if (x == '1') {
ones++;
}
}
int sones = ones;
for (int sufflen = 0; sufflen <= a.length; sufflen++) {
ones = sones;
if (sufflen > b.length) continue;
boolean prefEq = true;
for (int i = sufflen - 1; i >= 0; i--) {
if (b[i] != a[a.length - sufflen + i]) {
prefEq = false;
break;
}
}
if (!prefEq) continue;
ArrayDeque<Character> deque = new ArrayDeque<>();
for (int i = 0; i < a.length - sufflen; i++) {
deque.add(a[i]);
}
boolean failed = false;
for (int i = sufflen; i < b.length; i++) {
while (true) {
if ((b[i] - '0') == ones % 2) {
if (b[i] == '1') {
ones++;
}
break;
} else {
if (deque.size() == 0) {
failed = true;
break;
} else {
char x = deque.pollFirst();
if (x == '1') {
ones--;
}
}
}
}
}
if (!failed) {
return true;
}
}
return false;
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
76375779748dcc261b884cc4edaf0061
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author mthai
*/
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);
CF_180A solver = new CF_180A();
solver.solve(1, in, out);
out.close();
}
static class CF_180A {
public void solve(int testNumber, Scanner in, PrintWriter out) {
char[] a = in.next().toCharArray();
char[] b = in.next().toCharArray();
int n = count1(a), m = count1(b);
boolean ans = false;
n += (n % 2 != 0) ? 1 : 0;
if (m <= n)
ans = true;
out.println(convert(ans));
}
String convert(boolean b) {
return (b == true) ? "YES" : "NO";
}
int count1(char[] a) {
int one = 0;
for (char c : a)
one += (c == '1') ? 1 : 0;
return one;
}
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
c26a650681b6cb2d105b996492edca07
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Alex
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out){
char[] first = in.next().toCharArray(), second = in.next().toCharArray();
int firstones = 0, secondones = 0;
for(int i = 0; i < first.length; i++) if (first[i] == '1') firstones++;
for(int i = 0; i < second.length; i++) if (second[i] == '1') secondones++;
if (firstones % 2 == 0 && secondones <= firstones){
out.print("YES");
return;
}
if (firstones % 2 == 1 && secondones <= firstones + 1){
out.print("YES");
return;
}
out.print("NO");
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void close() {
writer.close();
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
624df19f69ededb4857f9442c79f3667
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.util.Scanner;
public class ParityGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] a = sc.next().toCharArray();
char[] b = sc.next().toCharArray();
int count = 0;
for (int i = 0; i < a.length; ++i) {
count += a[i] == '1' ? 1 : 0;
}
count += count % 2 == 1 ? 1 : 0;
for (int i = 0; i < b.length; ++i) {
count -= b[i] == '1' ? 1 : 0;
}
if (count >= 0) {
System.out.println("YES");
} else {
System.out.println("NO");
}
sc.close();
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
0e76dc8e3b16e900fbac50e6954a6a34
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.math.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Input in = new Input(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
}
class Task {
static final long MOD = 1000000007;
int[] primes;
public void solve(int testNumber, Input in, PrintWriter out) {
char[] a = in.next().toCharArray();
char[] b = in.next().toCharArray();
int oneA = 0;
int oneB = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == '1')
oneA++;
}
for (int i = 0; i < b.length; i++) {
if (b[i] == '1')
oneB++;
}
if ((oneA & 1) == 1) {
oneA++;
}
if (oneA < oneB)
out.println("NO");
else
out.println("YES");
}
// didn't test
static class BIT {
int[] MAX;
int[] MIN;
int[] SUM;
int n;
public BIT(int n) {
MAX = new int[n + 1];
MIN = new int[n + 1];
SUM = new int[n + 1];
this.n = n;
}
public void setSum(int pos, int val) {
for (int i = pos; i < n; i += (i & -i)) {
SUM[i] += val;
}
}
public int getSum(int to) {
int sum = 0;
for (int i = to; i > 0; i -= (i & -i)) {
sum += SUM[i];
}
return sum;
}
public int getSum(int from, int to) {
return getSum(to) - getSum(from);
}
public void setMax(int pos, int val) {
for (int i = pos; i < n; i += (i & -i)) {
MAX[i] = Math.max(val, MAX[i]);
}
}
public int getMax(int to) {
int max = 0;
for (int i = to; i > 0; i -= (i & -i)) {
max = Math.max(max, MAX[i]);
}
return max;
}
public void setMin(int pos, int val) {
for (int i = pos; i < n; i += (i & -i)) {
MIN[i] = Math.min(val, MIN[i]);
}
}
public int getMin(int to) {
int min = 0;
for (int i = to; i > 0; i -= (i & -i)) {
min = Math.min(min, MIN[i]);
}
return min;
}
}
// didn't test
static class DisjointSet {
int[] set;
public DisjointSet(int n) {
set = new int[n];
for (int i = 0; i < n; i++) {
set[i] = i;
}
}
public int findSet(int a) {
if (set[a] == a)
return a;
return findSet(set[a]);
}
public void unionSet(int a, int b) {
set[findSet(a)] = findSet(b);
}
public boolean isSameSet(int a, int b) {
return findSet(a) == findSet(b);
}
}
static class SegmentTree {
int[] MAX;
int[] MIN;
int[] SUM;
int[] data;
int n;
public SegmentTree(int[] data) {
this.n = data.length;
this.data = data;
MAX = new int[4 * n + 10];
MIN = new int[4 * n + 10];
SUM = new int[4 * n + 10];
buildTree(1, 0, n - 1);
}
public int left(int root) {
return root << 1;
}
public int right(int root) {
return (root << 1) + 1;
}
private void buildTree(int root, int rl, int rr) {
if (rr == rl) {
MAX[root] = MIN[root] = SUM[root] = data[rr];
} else {
int rm = (rl + rr) >> 1;
buildTree(left(root), rl, rm);
buildTree(right(root), rm + 1, rr);
MAX[root] = Math.max(MAX[left(root)], MAX[right(root)]);
MIN[root] = Math.min(MIN[left(root)], MIN[right(root)]);
SUM[root] = SUM[left(root)] + SUM[right(root)];
}
}
public int getMax(int from, int to) {
return getMax(1, 0, n - 1, from, to);
}
private int getMax(int root, int rl, int rr, int from, int to) {
if (from > rr || to < rl || rl > rr) {
return -1;
}
if (from == rl && to == rr) {
return MAX[root];
}
int rm = (rr + rl) >> 1;
int m1 = getMax(left(root), rl, rm, from, Math.min(to, rm));
int m2 = getMax(right(root), rm + 1, rr, Math.max(from, rm + 1), to);
return Math.max(m1, m2);
}
public int getMin(int from, int to) {
return getMin(1, 0, n - 1, from, to);
}
private int getMin(int root, int rl, int rr, int from, int to) {
if (from > rr || to < rl || rl > rr) {
return Integer.MAX_VALUE;
}
if (from == rl && to == rr) {
return MIN[root];
}
int rm = (rr + rl) >> 1;
int m1 = getMin(left(root), rl, rm, from, Math.min(to, rm));
int m2 = getMin(right(root), rm + 1, rr, Math.max(from, rm + 1), to);
return Math.min(m1, m2);
}
public int getSum(int from, int to) {
return getSum(1, 0, n - 1, from, to);
}
private int getSum(int root, int rl, int rr, int from, int to) {
if (from > rr || to < rl || rl > rr) {
return 0;
}
if (from == rl && to == rr) {
return SUM[root];
}
int rm = (rr + rl) >> 1;
int m1 = getSum(left(root), rl, rm, from, Math.min(to, rm));
int m2 = getSum(right(root), rm + 1, rr, Math.max(from, rm + 1), to);
return m1 + m2;
}
}
private String lcs(String s1, String s2) {
int n = s1.length();
int m = s2.length();
int[][] dp = new int[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
StringBuilder sb = new StringBuilder();
int i = n;
int j = m;
while (i > 0 && j > 0) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
sb.append(s1.charAt(i - 1));
i--;
j--;
} else {
if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}
}
return sb.reverse().toString();
}
private boolean isPrime(int n) {
if (n <= 3)
return n > 1;
else if (n % 3 == 0 || n % 2 == 0)
return false;
for (long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
private int[] getPrimes(int n) {
int len = n + 1;
boolean[] isPrimes = new boolean[len];
int[] temps = new int[len];
int index = 0;
Arrays.fill(isPrimes, true);
isPrimes[0] = isPrimes[1] = false;
for (int i = 2; i * i < len; i++) {
if (isPrimes[i]) {
for (int j = i * i; j < len; j += i) {
isPrimes[j] = false;
}
}
}
for (int i = 0; i < len; i++) {
if (isPrimes[i])
temps[index++] = i;
}
int[] primes = Arrays.copyOf(temps, index);
return primes;
}
static void swap(char[] arrays, int pos1, int pos2) {
char tmp = arrays[pos1];
arrays[pos1] = arrays[pos2];
arrays[pos2] = tmp;
}
static void swap(int[] arrays, int pos1, int pos2) {
int tmp = arrays[pos1];
arrays[pos1] = arrays[pos2];
arrays[pos2] = tmp;
}
static long modPow(long n, long ex) {
long res = 1;
while (ex > 0) {
if ((ex & 1) == 1)
res = res * n % MOD;
n = n * n % MOD;
ex >>= 1;
}
return res;
}
static long ModInverse(long n) {
return modPow(n, MOD - 2);
}
static long nCr(int n, int r) {
if (r > n)
return -1;
// return (f[n] * ModInverse((f[r] * f[n - r]) % MOD)) % MOD;
return 0;
}
}
class Input {
public BufferedReader reader;
public StringTokenizer tokenizer;
public Input(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
d1b62c4156aae8f1e906ba0792f54a60
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
FastScanner in;
PrintWriter out;
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = null;
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine() throws IOException {
if (st == null || !st.hasMoreTokens())
return br.readLine();
StringBuilder result = new StringBuilder(st.nextToken());
while (st.hasMoreTokens()) {
result.append(" ");
result.append(st.nextToken());
}
return result.toString();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
void run() throws IOException {
in = new FastScanner(System.in);
out = new PrintWriter(System.out, false);
solve();
out.close();
}
public static void main(String[] args) throws IOException{
new Main().run();
}
public void printArr(int[] arr){
for(int i = 0; i < arr.length; i++){
out.print(arr[i] + " ");
}
out.println();
}
public long gcd(long a, long b){
if(a == 0) return b;
return gcd(b % a, a);
}
public boolean isPrime(long num){
if(num == 0 || num == 1){
return false;
}
for(int i = 2; i * i <= num; i++){
if(num % i == 0){
return false;
}
}
return true;
}
public void solve() throws IOException{
String a = in.next();
String b = in.next();
int n1 = 0, n2 = 0;
for(int i = 0; i < a.length(); i++){
if(a.charAt(i) == '1'){
n1++;
}
}
for(int i = 0; i < b.length(); i++){
if(b.charAt(i) == '1'){
n2++;
}
}
n1 += (n1 % 2);
if(n1 >= n2){
out.println("YES");
}else{
out.println("NO");
}
return;
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
ccc3ff702980a252bf1d4d625b80b1b8
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputReader in=new InputReader(System.in);
String a=in.next();
String b=in.next();
int cA=count1in(a);
int cB=count1in(b);
if(cA>=cB){
System.out.print("YES");
}else if(cB-cA==1){
if(cA%2!=0){
System.out.print("YES");
}else{
System.out.print("NO");
}
}else{
System.out.print("NO");
}
}
private static int count1in(String a) {
int c=0;
for(int i=0; i<a.length(); i++){
if(a.charAt(i)=='1'){
c++;
}
}
return c;
}
}
class InputReader {
StringTokenizer tokenizer;
BufferedReader reader;
public InputReader(InputStream stream) {
tokenizer = null;
reader = new BufferedReader(new InputStreamReader(stream), 32768);
}
public String nextLine() throws IOException {
return reader.readLine();
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
b479dd18146683b53915283047e0762f
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* #
* @author pttrung
*/
public class A_Round_180_Div1 {
public static long MOD = 998244353;
static long[][][]dp;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
String a = in.next();
String b = in.next();
int c = 0, d = 0;
for(int i = 0; i < a.length(); i++){
if(a.charAt(i) == '1'){
c++;
}
}
for(int i = 0; i < b.length(); i++){
if(b.charAt(i) == '1'){
d++;
}
}
if(c + (c %2)>= d){
out.println("YES");
}else {
out.println("NO");
}
out.close();
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point implements Comparable<Point> {
int x, y;
public Point(int start, int end) {
this.x = start;
this.y = end;
}
@Override
public int hashCode() {
int hash = 5;
hash = 47 * hash + this.x;
hash = 47 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Point other = (Point) obj;
if (this.x != other.x) {
return false;
}
if (this.y != other.y) {
return false;
}
return true;
}
@Override
public int compareTo(Point o) {
return Integer.compare(x, o.x);
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, long b, long MOD) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2, MOD);
if (b % 2 == 0) {
return val * val % MOD;
} else {
return val * (val * a % MOD) % MOD;
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
2a57095490f3d5a08fa7951a71333b12
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class codeforces
{
static class Student{
int x,y;
Student(int x,int y){
this.x=x;
this.y=y;
//this.z=z;
}
}
static int prime[];
static void sieveOfEratosthenes(int n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
int pos=0;
prime= new int[n+1];
for(int p = 2; p*p <=n; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == 0)
{
// Update all multiples of p
prime[p]=p;
for(int i = p*p; i <= n; i += p)
if(prime[i]==0)
prime[i] = p;
}
}
}
static class Sortbyroll implements Comparator<Student>
{
// Used for sorting in ascending order of
// roll number
public int compare(Student c, Student b)
{
return c.y-b.y;
}
}
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 class Edge{
int a,b;
Edge(int a,int b){
this.a=a;
this.b=b;
}
}
static class Trie{
HashMap<Character,Trie>map;
int c;
Trie(){
map=new HashMap<>();
//z=null;
//o=null;
c=0;
}
}
//static long ans;
static int parent[];
static int rank[];
// static int b[][];
static int bo[];
static int ho[];
static int seg[];
//static int pos;
static long mod=1000000007;
//static int dp[][];
static HashMap<Integer,Integer>map;
static PriorityQueue<Student>q=new PriorityQueue<>();
//static Stack<Integer>st;
// static ArrayList<Character>ans;
static ArrayList<ArrayList<Integer>>adj;
//static long ans;
static int pos;
static Trie root;
static long fac[];
static int gw,gb;
//static long mod=(long)(998244353);
//static int ans;
static void solve()throws IOException{
FastReader sc=new FastReader();
int i,c=0,c2=0;
String s1,s2;
s1=sc.nextLine();
s2=sc.nextLine();
for(i=0;i<s1.length();i++){
if(s1.charAt(i)=='1')
++c;
}
if(c%2!=0){
c+=1;
}
for(i=0;i<s2.length();i++){
if(s2.charAt(i)=='1')
++c2;
}
if(c2>c)
System.out.println("NO");
else
System.out.println("YES");
}
static long fac(long x,long y){
if(y==0||x==0)
return 1;
return x * fac(x-1,y-1);
}
static long nCr(long n, long r,
long p)
{
return (fac[(int)n]* modInverse(fac[(int)r], p)
% p * modInverse(fac[(int)(n-r)], p)
% p) % p;
}
//static int prime[];
//static int dp[];
public static void main(String[] args){
//long sum=0;
try {
codeforces.solve();
} catch (Exception e) {
e.printStackTrace();
}
}
static long modInverse(long n, long p)
{
return power(n, p-(long)2,p);
}
static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y %(long)2!=0)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res%p;
}
/*public static long power(long x,long a) {
if(a==0) return 1;
if(a%2==1)return (x*1L*power(x,a-1))%mod;
return (power((int)((x*1L*x)%mod),a/2))%mod;
}*/
static int find(int x)
{
// Finds the representative of the set
// that x is an element of
while(parent[x]!=x)
{
// if x is not the parent of itself
// Then x is not the representative of
// his set,
x=parent[x];
// so we recursively call Find on its parent
// and move i's node directly under the
// representative of this set
}
return x;
}
static void union(int x, int y)
{
// Find representatives of two sets
int xRoot = find(x), yRoot = find(y);
// Elements are in the same set, no need
// to unite anything.
if (xRoot == yRoot)
return;
// If x's rank is less than y's rank
if (rank[xRoot] < rank[yRoot])
// Then move x under y so that depth
// of tree remains less
parent[xRoot] = yRoot;
// Else if y's rank is less than x's rank
else if (rank[yRoot] < rank[xRoot])
// Then move y under x so that depth of
// tree remains less
parent[yRoot] = xRoot;
else // if ranks are the same
{
// Then move y under x (doesn't matter
// which one goes where)
parent[yRoot] = xRoot;
// And increment the the result tree's
// rank by 1
rank[xRoot] = rank[xRoot] + 1;
}
}
static long gcd(long a, long b)
{
if (a == 0){
//ans+=b;
return b;
}
return gcd(b % a, a);
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
1affe848581d58649f3c10b502ea2f5c
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
void solve() {
String a = in.next();
String b = in.next();
int cnt = 0;
for (int i = 0; i < a.length(); ++i) {
if (a.charAt(i) == '1') ++cnt;
}
if (cnt % 2 == 1) ++cnt;
for (int i = 0; i < b.length(); ++i) {
if (b.charAt(i) == '1') --cnt;
}
out.println(cnt >= 0 ? "YES" : "NO");
}
void run() {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
InputReader in;
PrintWriter out;
class InputReader {
BufferedReader br;
StringTokenizer st;
InputReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
st = null;
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
public static void main(String[] args) {
new Main().run();
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
| |
PASSED
|
663f1ffb42b55d893c559e15c5600b7a
|
train_001.jsonl
|
1366385400
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
|
256 megabytes
|
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main{
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner =new Scanner(System.in);
// Scanner scanner =new Scanner(new FileInputStream("src/in.txt"));
int ans1,ans2;
ans1=ans2=0;
String s1=scanner.next();
String s2=scanner.next();
int len1 =s1.length();
int len2 =s2.length();
for(int i=0;i<len1;i++){
if(s1.charAt(i)=='1'){
ans1++;
}
} for(int i=0;i<len2;i++){
if(s2.charAt(i)=='1'){
ans2++;
}
}
if(ans1>=ans2||(ans1+1==ans2&&ans1%2==1)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
|
Java
|
["01011\n0110", "0011\n1110"]
|
1 second
|
["YES", "NO"]
|
NoteIn the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110
|
Java 8
|
standard input
|
[
"constructive algorithms"
] |
cf86add6c92fa8a72b8e23efbdb38613
|
The first line contains the string a and the second line contains the string b (1ββ€β|a|,β|b|ββ€β1000). Both strings contain only the characters "0" and "1". Here |x| denotes the length of the string x.
| 1,700 |
Print "YES" (without quotes) if it is possible to turn a into b, and "NO" (without quotes) otherwise.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.