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 | 292928e601a44c3d72c1ee11d033c51d | train_003.jsonl | 1547044500 | You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.It is guaranteed that the answer exists. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
import java.util.stream.*;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static java.lang.Math.max;
import static java.lang.Math.sqrt;
import static java.lang.Integer.signum;
@SuppressWarnings("unchecked")
public class P1102D {
int a [];
Deque<Integer> [] dp = new ArrayDeque [3];
void swap(int i, int j) {
int p = (i < j) ? dp[i].pollLast() : dp[i].pollFirst();
dp[j].add(p);
a[p] = j;
}
public void run() throws Exception {
int n = nextInt(), n3 = n / 3;
a = new int [n];
dp[0] = new ArrayDeque();
dp[1] = new ArrayDeque();
dp[2] = new ArrayDeque();
String s = next();
for (int i = 0; i < n; a[i] = s.charAt(i) - '0', dp[a[i]].add(i), i++);
while ((dp[2].size() > n3) && (dp[0].size() < n3)) { swap(2, 0); }
while ((dp[1].size() > n3) && (dp[0].size() < n3)) { swap(1, 0); }
while ((dp[2].size() > n3) && (dp[1].size() < n3)) { swap(2, 1); }
while ((dp[1].size() > n3) && (dp[2].size() < n3)) { swap(1, 2); }
while ((dp[0].size() > n3) && (dp[2].size() < n3)) { swap(0, 2); }
while ((dp[0].size() > n3) && (dp[1].size() < n3)) { swap(0, 1); }
IntStream.of(a).forEach(this::print);
}
public static void main(String... args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedOutputStream(System.out));
new P1102D().run();
br.close();
pw.close();
System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]");
}
static long startTime = System.currentTimeMillis();
static BufferedReader br;
static PrintWriter pw;
StringTokenizer stok;
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) { return null; }
stok = new StringTokenizer(s);
}
return stok.nextToken();
}
void print(byte b) { print("" + b); }
void print(int i) { print("" + i); }
void print(long l) { print("" + l); }
void print(double d) { print("" + d); }
void print(char c) { print("" + c); }
void print(Object o) {
if (o instanceof int[]) { print(Arrays.toString((int [])o));
} else if (o instanceof long[]) { print(Arrays.toString((long [])o));
} else if (o instanceof char[]) { print(Arrays.toString((char [])o));
} else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
} else if (o instanceof short[]) { print(Arrays.toString((short [])o));
} else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
} else if (o instanceof float[]) { print(Arrays.toString((float [])o));
} else if (o instanceof double[]) { print(Arrays.toString((double [])o));
} else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
} else { print("" + o); }
}
void print(String s) { pw.print(s); }
void println() { println(""); }
void println(byte b) { println("" + b); }
void println(int i) { println("" + i); }
void println(long l) { println("" + l); }
void println(double d) { println("" + d); }
void println(char c) { println("" + c); }
void println(Object o) { print(o); println(); }
void println(String s) { pw.println(s); }
int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
long nextLong() throws IOException { return Long.parseLong(nextToken()); }
double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
char nextChar() throws IOException { return (char) (br.read()); }
String next() throws IOException { return nextToken(); }
String nextLine() throws IOException { return br.readLine(); }
int [] readInt(int size) throws IOException {
int [] array = new int [size];
for (int i = 0; i < size; i++) { array[i] = nextInt(); }
return array;
}
long [] readLong(int size) throws IOException {
long [] array = new long [size];
for (int i = 0; i < size; i++) { array[i] = nextLong(); }
return array;
}
double [] readDouble(int size) throws IOException {
double [] array = new double [size];
for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
return array;
}
String [] readLines(int size) throws IOException {
String [] array = new String [size];
for (int i = 0; i < size; i++) { array[i] = nextLine(); }
return array;
}
int gcd(int a, int b) {
if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a);
a = Math.abs(a); b = Math.abs(b);
int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b);
a >>>= az; b >>>= bz;
while (a != b) {
if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); }
else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); }
}
return (a << Math.min(az, bz));
}
long gcd(long a, long b) {
if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a);
a = Math.abs(a); b = Math.abs(b);
int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b);
a >>>= az; b >>>= bz;
while (a != b) {
if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); }
else { b -= a; b >>>= Long.numberOfTrailingZeros(b); }
}
return (a << Math.min(az, bz));
}
void shuffle(int [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
int t = a[i]; a[i] = a[j]; a[j] = t;
}
}
void shuffle(long [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
long t = a[i]; a[i] = a[j]; a[j] = t;
}
}
void shuffle(Object [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
Object t = a[i]; a[i] = a[j]; a[j] = t;
}
}
void flush() {
pw.flush();
}
void pause() {
flush();
System.console().readLine();
}
} | Java | ["3\n121", "6\n000000", "6\n211200", "6\n120110"] | 2 seconds | ["021", "001122", "211200", "120120"] | null | Java 8 | standard input | [
"greedy",
"strings"
] | cb852bf0b62d72b3088969ede314f176 | The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) — the number of characters in $$$s$$$. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'. | 1,500 | Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer. | standard output | |
PASSED | 92332ccb53885848da8cfc214578c7eb | train_003.jsonl | 1547044500 | You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.It is guaranteed that the answer exists. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
import java.util.stream.*;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static java.lang.Math.max;
import static java.lang.Math.sqrt;
import static java.lang.Integer.signum;
@SuppressWarnings("unchecked")
public class P1102D {
int [] a, k = new int [3];
Deque<Integer> [] dp = new ArrayDeque [3];
void swap(int i, int j) {
a[(i < j) ? dp[i].pollLast() : dp[i].pollFirst()] = j;
k[i]--;
k[j]++;
}
public void run() throws Exception {
int n = nextInt(), n3 = n / 3;
a = new int [n];
dp[0] = new ArrayDeque(n);
dp[1] = new ArrayDeque(n);
dp[2] = new ArrayDeque(n);
String s = next();
for (int i = 0; i < n; a[i] = s.charAt(i) - '0', dp[a[i]].add(i), k[a[i]]++, i++);
while ((k[2] > n3) && (k[0] < n3)) { swap(2, 0); }
while ((k[1] > n3) && (k[0] < n3)) { swap(1, 0); }
while ((k[2] > n3) && (k[1] < n3)) { swap(2, 1); }
while ((k[1] > n3) && (k[2] < n3)) { swap(1, 2); }
while ((k[0] > n3) && (k[2] < n3)) { swap(0, 2); }
while ((k[0] > n3) && (k[1] < n3)) { swap(0, 1); }
IntStream.of(a).forEach(this::print);
}
public static void main(String... args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedOutputStream(System.out));
new P1102D().run();
br.close();
pw.close();
System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]");
}
static long startTime = System.currentTimeMillis();
static BufferedReader br;
static PrintWriter pw;
StringTokenizer stok;
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) { return null; }
stok = new StringTokenizer(s);
}
return stok.nextToken();
}
void print(byte b) { print("" + b); }
void print(int i) { print("" + i); }
void print(long l) { print("" + l); }
void print(double d) { print("" + d); }
void print(char c) { print("" + c); }
void print(Object o) {
if (o instanceof int[]) { print(Arrays.toString((int [])o));
} else if (o instanceof long[]) { print(Arrays.toString((long [])o));
} else if (o instanceof char[]) { print(Arrays.toString((char [])o));
} else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
} else if (o instanceof short[]) { print(Arrays.toString((short [])o));
} else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
} else if (o instanceof float[]) { print(Arrays.toString((float [])o));
} else if (o instanceof double[]) { print(Arrays.toString((double [])o));
} else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
} else { print("" + o); }
}
void print(String s) { pw.print(s); }
void println() { println(""); }
void println(byte b) { println("" + b); }
void println(int i) { println("" + i); }
void println(long l) { println("" + l); }
void println(double d) { println("" + d); }
void println(char c) { println("" + c); }
void println(Object o) { print(o); println(); }
void println(String s) { pw.println(s); }
int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
long nextLong() throws IOException { return Long.parseLong(nextToken()); }
double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
char nextChar() throws IOException { return (char) (br.read()); }
String next() throws IOException { return nextToken(); }
String nextLine() throws IOException { return br.readLine(); }
int [] readInt(int size) throws IOException {
int [] array = new int [size];
for (int i = 0; i < size; i++) { array[i] = nextInt(); }
return array;
}
long [] readLong(int size) throws IOException {
long [] array = new long [size];
for (int i = 0; i < size; i++) { array[i] = nextLong(); }
return array;
}
double [] readDouble(int size) throws IOException {
double [] array = new double [size];
for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
return array;
}
String [] readLines(int size) throws IOException {
String [] array = new String [size];
for (int i = 0; i < size; i++) { array[i] = nextLine(); }
return array;
}
int gcd(int a, int b) {
if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a);
a = Math.abs(a); b = Math.abs(b);
int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b);
a >>>= az; b >>>= bz;
while (a != b) {
if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); }
else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); }
}
return (a << Math.min(az, bz));
}
long gcd(long a, long b) {
if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a);
a = Math.abs(a); b = Math.abs(b);
int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b);
a >>>= az; b >>>= bz;
while (a != b) {
if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); }
else { b -= a; b >>>= Long.numberOfTrailingZeros(b); }
}
return (a << Math.min(az, bz));
}
void shuffle(int [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
int t = a[i]; a[i] = a[j]; a[j] = t;
}
}
void shuffle(long [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
long t = a[i]; a[i] = a[j]; a[j] = t;
}
}
void shuffle(Object [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
Object t = a[i]; a[i] = a[j]; a[j] = t;
}
}
void flush() {
pw.flush();
}
void pause() {
flush();
System.console().readLine();
}
} | Java | ["3\n121", "6\n000000", "6\n211200", "6\n120110"] | 2 seconds | ["021", "001122", "211200", "120120"] | null | Java 8 | standard input | [
"greedy",
"strings"
] | cb852bf0b62d72b3088969ede314f176 | The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) — the number of characters in $$$s$$$. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'. | 1,500 | Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer. | standard output | |
PASSED | abed7d9cbd092f7316e2b3953b97b33e | train_003.jsonl | 1547044500 | You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.It is guaranteed that the answer exists. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Puneet //FastRead class is taken from different online Sources
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
int n;
char[] str;
int[] arr;
int freq;
public void solve(int testNumber, FastInput in, PrintWriter out) {
n = in.ni();
str = in.ns().toCharArray();
arr = new int[3];
freq = n / 3;
for (char ch : str) {
arr[ch - '0']++;
}
if (arr[0] < freq)
add0();
if (arr[2] < freq)
add2();
if (arr[1] < freq)
add1();
for (int i = 0; i < n; i++) {
out.print(str[i]);
}
}
private void add0() {
for (int j = 0; j < n; j++) {
if (arr[0] == freq)
break;
if (arr[str[j] - '0'] > freq) {
arr[str[j] - '0']--;
arr[0]++;
str[j] = '0';
}
}
}
private void add2() {
for (int i = n - 1; i >= 0; i--) {
if (arr[2] == freq)
break;
if (arr[str[i] - '0'] > freq) {
arr[str[i] - '0']--;
arr[2]++;
str[i] = '2';
}
}
}
private void add1() {
for (int i = 0; i < n; i++) {
if (arr[2] == freq || arr[1] == freq)
break;
if (str[i] == '2') {
arr[2]--;
arr[1]++;
str[i] = '1';
}
}
for (int i = n - 1; i >= 0; i--) {
if (arr[0] == freq || arr[1] == freq)
break;
if (str[i] == '0') {
arr[0]--;
arr[1]++;
str[i] = '1';
}
}
// int[] arr2=new int[3];
//
// for(int i=0;i<n;i++){
// if(str[i])
// }
}
}
static class FastInput {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastInput.SpaceCharFilter filter;
public FastInput(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 ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String ns() {
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 interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
} | Java | ["3\n121", "6\n000000", "6\n211200", "6\n120110"] | 2 seconds | ["021", "001122", "211200", "120120"] | null | Java 8 | standard input | [
"greedy",
"strings"
] | cb852bf0b62d72b3088969ede314f176 | The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) — the number of characters in $$$s$$$. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'. | 1,500 | Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer. | standard output | |
PASSED | 5cf0b32ac4c791cf585fd9f172927d18 | train_003.jsonl | 1547044500 | You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.It is guaranteed that the answer exists. | 256 megabytes | import java.util.*;
import java.io.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int l=n/3;
char c[]=br.readLine().toCharArray();
int a[]=new int[n];
int b[]=new int[3];
for(int i=0;i<n;i++)
{
a[i]=c[i]-48;
b[a[i]]++;
}
for(int i=n-1;i>=0;i--)
{
if(a[i]!=2 && b[a[i]]>l && b[2]<l)
{
b[2]++; b[a[i]]--; a[i]=2;
}
}
for(int i=0;i<n-1;i++)
{
if(a[i]!=0 && b[a[i]]>l && b[0]<l)
{
b[a[i]]--;
b[0]++;
a[i]=0;
}
}
// System.out.println(b[0]+" "+b[1]+" "+b[2]+" "+l);
if(b[0]>l && b[1]<l)
{
int zero=0;
int j=0;
while(j<n && zero<l)
{ if(a[j]==0)
{ zero++; }
j++;
}
//System.out.println(j);
for(int i=j;i<n;i++)
if(a[i]==0 && b[1]<l && b[0]>l)
{ a[i]=1; b[0]--; b[1]++; }
}
if(b[2]>l && b[1]<l)
{
int two=0;
int j=n-1;
while(j>=0 && two<l)
{ if(a[j]==2)
{ two++; }
j--;
}
for(int i=j;i>=0;i--)
if(a[i]==2 && b[1]<l && b[2]>l)
{ a[i]=1; b[2]--; b[1]++; }
}
for(int i=0;i<n;i++)
System.out.print(a[i]);
}
} | Java | ["3\n121", "6\n000000", "6\n211200", "6\n120110"] | 2 seconds | ["021", "001122", "211200", "120120"] | null | Java 8 | standard input | [
"greedy",
"strings"
] | cb852bf0b62d72b3088969ede314f176 | The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) — the number of characters in $$$s$$$. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'. | 1,500 | Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer. | standard output | |
PASSED | b469c25738db04d34ed82a663027e38a | train_003.jsonl | 1547044500 | You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.It is guaranteed that the answer exists. | 256 megabytes | import java.util.*;
public class D1102_BalancedTernaryString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
sc.close();
char str[] = s.toCharArray();
int[] arr = new int[3];
for (int i=0; i<n; i++) {
if (str[i] == '0')
arr[0]++;
else if (str[i] == '1')
arr[1]++;
else
arr[2]++;
}
if (arr[0] < n/3) {
for (int i=0; i<n; i++) {
if (arr[0] == n/3)
break;
if (str[i] == '1' && arr[1] > n/3) {
str[i] = '0';
arr[0]++;
arr[1]--;
}
if (str[i] == '2' && arr[2] > n/3) {
str[i] = '0';
arr[0]++;
arr[2]--;
}
}
}
if (arr[2] < n/3) {
for(int i=n-1; i>=0; i--) {
if (arr[2] == n/3)
break;
if (str[i]=='1' && arr[1] > n/3) {
str[i] = '2';
arr[1]--;
arr[2]++;
}
if (str[i]=='0' && arr[0] > n/3) {
str[i] = '2';
arr[0]--;
arr[2]++;
}
}
}
if(arr[1] < n/3) {
for (int i=0; i<n; i++) {
if (str[i]=='2' && arr[2] > n/3) {
str[i] = '1';
arr[2]--;
arr[1]++;
}
}
for(int i=n-1; i>=0; i--) {
if(str[i]=='0' && arr[0] > n/3) {
str[i] = '1';
arr[0]--;
arr[1]++;
}
}
}
System.out.println(new String(str));
}
} | Java | ["3\n121", "6\n000000", "6\n211200", "6\n120110"] | 2 seconds | ["021", "001122", "211200", "120120"] | null | Java 8 | standard input | [
"greedy",
"strings"
] | cb852bf0b62d72b3088969ede314f176 | The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) — the number of characters in $$$s$$$. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'. | 1,500 | Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer. | standard output | |
PASSED | 7673bde211a5c25e43e59cacdcb9b350 | train_003.jsonl | 1547044500 | You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.It is guaranteed that the answer exists. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.lang.Math.*;
/* 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
{ int i,j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
int n=Integer.parseInt(br.readLine());
String s=br.readLine();
int temp0=0,temp1=0,temp2=0;
char p[]=s.toCharArray();
for(i=0;i<n;i++)
{ if(s.charAt(i)=='0')temp0++;
if(s.charAt(i)=='1')temp1++;
if(s.charAt(i)=='2')temp2++;
}
int l=n/3,flag0=0,flag1=0;
for(i=0;i<n;i++)
{ if(p[i]=='0')
{ if(temp0>l)
{ if(temp1<l && flag0==l)
{p[i]='1';temp1++;temp0--;}
else if(temp2<l && flag0==l)
{p[i]='2';temp2++;temp0--;}
else flag0++;
}
}
if(p[i]=='1')
{ if(temp1>l)
{ if(temp0<l)
{p[i]='0';temp0++;temp1--;}
else if(temp2<l && flag1==l)
{p[i]='2';temp2++;temp1--;}
else flag1++;
}
}
if(p[i]=='2')
{
if(temp2>l)
{ if(temp0<l)
{p[i]='0';temp0++;}
else if(temp1<l)
{p[i]='1';temp1++;}
temp2--;
}
}
}
for(i=0;i<n;i++)
out.print(p[i]);
out.close();
}
}
| Java | ["3\n121", "6\n000000", "6\n211200", "6\n120110"] | 2 seconds | ["021", "001122", "211200", "120120"] | null | Java 8 | standard input | [
"greedy",
"strings"
] | cb852bf0b62d72b3088969ede314f176 | The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) — the number of characters in $$$s$$$. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'. | 1,500 | Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer. | standard output | |
PASSED | bd24cd79e7f58dc34b5296eb63627d1d | train_003.jsonl | 1547044500 | You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.It is guaranteed that the answer exists. | 256 megabytes | import java.io.*;
import java.util.*;
public class timepass {
public static void main(String args[] ) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
//StringTokenizer st = new StringTokenizer(br.readLine());
String s = br.readLine();
int c0 = 0;
int c1 = 0;
int c2 = 0;
for(int i = 0;i<s.length();i++){
if(s.charAt(i) == '0') c0++;
else if(s.charAt(i) == '1') c1++;
else c2++;
}
if(c0 == c1 && c1 == c2){
System.out.println(s);
return;
}
else{
int req = n/3;
char [] crr = new char[n];
for(int i = 0;i<n;i++){
crr[i] = s.charAt(i);
}
//System.out.println(c1 + " "+ c2 + " " + c0 + " " + req);
if(c0 < req && c1>= req && c2 >= req){
int c10 = c1 - req;
int c20 = c2 - req;
//System.out.println("Hello");
for(int i=0;i<n;i++){
if(crr[i] == '2' && c20>0) {
crr[i] = '0';
c20--;
}
}
//System.out.println(crr);
//System.out.println(c10 + " " + c20 + " ") ;
for(int i = 0;i<n;i++){
if(crr[i] == '1' && c10 >0){
crr[i] = '0';
//System.out.println("Mahin");
c10--;
}
}
}
else if(c1 < req && c0>=req && c2>=req){
int c01 = c0 - req;
int c21 = c2 - req;
for(int i=0;i<n;i++){
if(crr[i] == '2' && c21>0) {
crr[i] = '1';
c21--;
}
}
for(int i = n-1;i>=0;i--){
if(crr[i] == '0' && c01 >0){
crr[i] = '1';
c01--;
}
}
}
else if(c2 < req && c0>=req && c1>=req){
int c02 = c0 - req;
int c12 = c1 - req;
for(int i=n-1;i>=0;i--){
if(crr[i] == '1' && c12>0) {
crr[i] = '2';
c12--;
}
}
for(int i = n-1;i>=0;i--){
if(crr[i] == '0' && c02 >0){
crr[i] = '2';
c02--;
}
}
}
else if( c0 < req && c1 < req && c2>= req){
int req0 = req - c0;
int req1 = req - c1;
for(int i = 0;i<n;i++){
if(crr[i] == '2' && req0>0){
crr[i] = '0';
req0--;
}
}
for(int i = 0;i<n;i++){
if(crr[i] == '2' && req1>0){
crr[i] = '1';
req1--;
}
}
}
else if( c0 < req && c2 < req && c1>= req){
int req0 = req - c0;
int req2 = req - c2;
for(int i = 0;i<n;i++){
if(crr[i] == '1' && req0>0){
crr[i] = '0';
req0--;
}
}
for(int i = n-1;i>=0;i--){
if(crr[i] == '1' && req2>0){
crr[i] = '2';
req2--;
}
}
}
else if( c1 < req && c2 < req && c0>= req){
int req1 = req - c1;
int req2 = req - c2;
for(int i = n-1;i>=0;i--){
if(crr[i] == '0' && req2>0){
crr[i] = '2';
req2--;
}
}
for(int i = n-1;i>=0;i--){
if(crr[i] == '0' && req1>0){
crr[i] = '1';
req1--;
}
}
}
System.out.println(crr);
}
//System.out.println();
}
}
| Java | ["3\n121", "6\n000000", "6\n211200", "6\n120110"] | 2 seconds | ["021", "001122", "211200", "120120"] | null | Java 8 | standard input | [
"greedy",
"strings"
] | cb852bf0b62d72b3088969ede314f176 | The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) — the number of characters in $$$s$$$. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'. | 1,500 | Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer. | standard output | |
PASSED | 9d1d9dd33e56228311cdcd93b997ae65 | train_003.jsonl | 1547044500 | You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.It is guaranteed that the answer exists. | 256 megabytes | //Code by Sounak, IIEST
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Arrays;
public class Test1{
public static void main(String args[])throws IOException{
FastReader sc = new FastReader();
StringBuilder sb = new StringBuilder();
long mod=1000000007l;
int n=sc.nextInt();
char ch[]=sc.next().toCharArray();
int i,one=0,zero=0,two=0,zc=0;
for(i=0;i<n;i++)
{
if(ch[i]=='1')
one++;
else if(ch[i]=='2')
two++;
else
zero++;
}
one-=n/3;
two-=n/3;
zero-=n/3;
i=0;
while(zero<0 && i<n)
{
if(ch[i]=='1')
{
if(one>0)
{
one--;
zero++;
ch[i]='0';
}
}
if(ch[i]=='2')
{
if(two>0)
{
two--;
zero++;
ch[i]='0';
}
}
i++;
}
i=n-1;
while(two<0 && i>=0)
{
if(ch[i]=='1')
{
if(one>0)
{
one--;
two++;
ch[i]='2';
}
}
if(ch[i]=='0')
{
if(zero>0)
{
zero--;
two++;
ch[i]='2';
}
}
i--;
}
i=0;
while(one<0 && i<n)
{
if(ch[i]=='2')
{
if(two>0)
{
two--;
one++;
ch[i]='1';
}
}
if(ch[i]=='0')
{
if(zero>0 && zc>=n/3)
{
zero--;
one++;
ch[i]='1';
}
zc++;
}
i++;
}
for(i=0;i<n;i++)
System.out.print(ch[i]);
System.out.println();
}
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); }
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
} | Java | ["3\n121", "6\n000000", "6\n211200", "6\n120110"] | 2 seconds | ["021", "001122", "211200", "120120"] | null | Java 8 | standard input | [
"greedy",
"strings"
] | cb852bf0b62d72b3088969ede314f176 | The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) — the number of characters in $$$s$$$. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'. | 1,500 | Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer. | standard output | |
PASSED | 1583ec94bf860fc82734ba0dca3e76ef | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
InputStream inputStream;
OutputStream outputStream;
if(System.getProperty("ONLINE_JUDGE") == null) {
File input = new File("test.inp");
inputStream = new FileInputStream(input);
}
else {
inputStream = System.in;
}
outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
static class Task {
public void solve(InputReader in, PrintWriter out) {
int n,m;
n = in.nextInt();
m = in.nextInt();
int x = Math.min(n,m);
out.println(x + 1);
for(int i = 0;i <= x;++i){
out.println(i + " " + (x - i));
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
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());
}
String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | f367b92ced3741e6e1203b7682c77360 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution
{
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
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 Ele implements Comparable<Ele>
{
public int x,y;
Ele(int x,int y)
{
this.x=x;this.y=y;
}
public int compareTo(Ele ob) {
if(ob.x!=x)return x-ob.x;
return this.y-ob.y;
}
}
int a[][];int b[][];
int solve(int i,int j)// 0-none, 1-true,2-false
{
//System.out.println(i+" "+j);
if (i==a.length-1 || j==a.length-1) return 1;
else {
if (b[i][j]!=0) return b[i][j];
b[i][j]=2;
if (a[i+1][j]==1)
b[i][j]=solve(i+1,j);
if (a[i][j+1]==1)
b[i][j]=Math.min(b[i][j],solve(i,j+1));
return b[i][j];
}
}
long gcd(long a,long b)
{
long min=Math.min(a,b);
long max=Math.max(a,b);
while (max%min!=0)
{
a=max%min;
max=min;min=a;
}
return min;
}
public static void main(String[] args) throws IOException
{
Reader sc=new Reader();Solution G=new Solution();
PrintWriter o = new PrintWriter(System.out);
//int t=sc.nextInt();
int t=1;
int x,y;
//long l;long a[];
//HashSet<Integer> ob1=new HashSet<>();
while (t-->0)
{
x=sc.nextInt();y=sc.nextInt();
x=Math.min(x,y);
o.println(x+1);
for (int i=0;i<=x;i++)
{
o.println(i+" "+(x-i));
}
}
//o.println(l);
o.flush();
o.close();
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 2f5a3de3f8cd1187ddcea1def25bac17 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.util.*;
import java.io.*;
public class C268 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
StringBuilder sb = new StringBuilder();
int cnt = 0;
int min = Math.min(n,m);
for (int i = 0; i <= min; i++) {
sb.append(i + " " + (min - i)).append("\n");
cnt++;
}
System.out.println(cnt + "\n" + sb.toString());
}
}
| Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 3b51342ae068207594918ab503f002bb | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main implements Runnable {
static boolean use_n_tests = false;
static int stack_size = 1 << 27;
void solve(FastScanner in, PrintWriter out, int testNumber) {
int n,m;
n = in.nextInt();
m = in.nextInt();
int ans = Math.min(n, m) + 1;
out.println(ans);
int x = 0, y = m;
for (int i = 0; i < ans; i++) {
out.println(x + " " + y);
x++;
y--;
}
}
// ****************************** template code ***********
class Coeff {
long mod;
long[][] C;
long[] fact;
boolean cycleWay = false;
Coeff(int n, long mod) {
this.mod = mod;
fact = new long[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = i;
fact[i] %= mod;
fact[i] *= fact[i - 1];
fact[i] %= mod;
}
}
Coeff(int n, int m, long mod) {
// n > m
cycleWay = true;
this.mod = mod;
C = new long[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= Math.min(i, m); j++) {
if (j == 0 || j == i) {
C[i][j] = 1;
} else {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
C[i][j] %= mod;
}
}
}
}
public long C(int n, int m) {
if (cycleWay) {
return C[n][m];
}
return fC(n, m);
}
private long fC(int n, int m) {
return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod;
}
private long inv(long r) {
if (r == 1)
return 1;
return ((mod - mod / r) * inv(mod % r)) % mod;
}
}
class Pair {
int first;
int second;
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
}
class MultisetTree<T> {
int size = 0;
TreeMap<T, Integer> mp = new TreeMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
T greatest() {
return mp.lastKey();
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
class Multiset<T> {
int size = 0;
Map<T, Integer> mp = new HashMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
static class Range {
int l, r;
int id;
public int getL() {
return l;
}
public int getR() {
return r;
}
public Range(int l, int r, int id) {
this.l = l;
this.r = r;
this.id = id;
}
}
static class Array {
static Range[] readRanges(int n, FastScanner in) {
Range[] result = new Range[n];
for (int i = 0; i < n; i++) {
result[i] = new Range(in.nextInt(), in.nextInt(), i);
}
return result;
}
static public Integer[] read(int n, FastScanner in) {
Integer[] out = new Integer[n];
for (int i = 0; i < out.length; i++) {
out[i] = in.nextInt();
}
return out;
}
static public int[] readint(int n, FastScanner in) {
int[] out = new int[n];
for (int i = 0; i < out.length; i++) {
out[i] = in.nextInt();
}
return out;
}
}
class Graph {
List<List<Integer>> create(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
return graph;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream io) {
br = new BufferedReader(new InputStreamReader(io));
}
public String line() {
String result = "";
try {
result = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
void run_t_tests() {
int t = in.nextInt();
int i = 0;
while (t-- > 0) {
solve(in, out, i++);
}
}
void run_one() {
solve(in, out, -1);
}
@Override
public void run() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
if (use_n_tests) {
run_t_tests();
} else {
run_one();
}
out.close();
}
static FastScanner in;
static PrintWriter out;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(null, new Main(), "", stack_size);
thread.start();
thread.join();
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | c343a446c880055767e8e2e92a6825cf | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
// code
Scanner scn = new Scanner(System.in);
int t = 1;
while (t > 0) {
t--;
int n = scn.nextInt();
int m = scn.nextInt();
int x = Math.min(n, m);
int y = Math.max(n, m);
System.out.println(x + 1);
int i;
int j;
if (n == x) {
i = 0;
j = m;
while (i <= x) {
System.out.println(i + " " + j);
i++;
j--;
}
} else {
i = 0;
j = m;
while (j >= 0) {
System.out.println(i + " " + j);
i++;
j--;
}
}
}
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 2831c8a29e9218fc6ab30eed7b878a8f | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
// code
Scanner scn = new Scanner(System.in);
int t = 1;
while (t > 0) {
t--;
int n = scn.nextInt();
int m = scn.nextInt();
int x = Math.min(n, m);
int y = Math.max(n, m);
System.out.println(x + 1);
int i;
int j;
if (n == x) {
i = 0;
j = m;
while (i <= x) {
System.out.println(i + " " + j);
i++;
j--;
}
} else {
i = 0;
j = m;
while (j >= 0) {
System.out.println(i + " " + j);
i++;
j--;
}
}
}
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | a347842cb991373f626d483100d3ddf2 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
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);
CBeautifulSetsOfPoints solver = new CBeautifulSetsOfPoints();
solver.solve(1, in, out);
out.close();
}
static class CBeautifulSetsOfPoints {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int ntc = 1;
// ntc = in.nextInt();
while ((ntc--) > 0) {
int n = in.nextInt();
int m = in.nextInt();
out.println(_F.min(n, m) + 1);
for (int i = 0; i < _F.min(n, m) + 1; i++) {
out.println(i, _F.min(n, m) - i);
}
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int 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 boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class _F {
public static <T extends Comparable<T>> T min(T... list) {
T candidate = list[0];
for (T i : list) {
if (candidate.compareTo(i) > 0) {
candidate = i;
}
}
return candidate;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; ++i) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void println(long i) {
writer.println(i);
}
}
}
| Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 964d2d9e94dc13af7131b816e37e3399 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.util.*;
public class BeautifulSet{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int m=s.nextInt();
int x=Math.min(n,m);
System.out.println(x+1);
for(int i=0;i<=x;i++)
{
System.out.println(i+" "+(x-i));
}
}
}
| Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 761764e7f356b8d30a710894755549f0 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.util.*;
public class cf268c {
public static void main(String args[])
{Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
System.out.println((int)Math.min(m,n)+1);
int i=0;int j=n;
while(i<=m && j>=0)
{System.out.println(i+" "+j);
i++;j--;}
sc.close();
}} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 73577d30fefcf8d1ac85b887d512af00 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.util.*;
import java.io.*;
public class Beautiful_Set_Of_Points {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader t = new FastReader();
PrintWriter o = new PrintWriter(System.out);
int x = t.nextInt();
int y = t.nextInt();
int min = Math.min(x, y) + 1;
int m = min - 1, n = 0;
o.println(min);
for (int i = 1; i <= min; ++i) {
o.println(m-- + " " + n++);
}
o.flush();
o.close();
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 2997d6a1ba2cf681c6a6584d805a413a | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class test {
// static boolean sf[];
public static void main(String[] args) {
// int test = fs.nextInt();
int test = 1;
for (int cases = 0; cases < test; cases++) {
int n = fs.nextInt();
int m = fs.nextInt();
int c = Integer.min(n, m) + 1;
op.print(c + "\n");
Pair ar[] = new Pair[c];
int x = Integer.min(n, m);
if (n > m) {
for (int i = 0; i < c; i++) {
ar[i] = new Pair(x - i, i);
}
} else {
for (int i = 0; i < c; i++) {
ar[i] = new Pair(i, x - i);
}
}
for (Pair pair : ar) {
op.print(pair.first + " " + pair.second + " \n");
}
op.flush();
}
}
// static ArrayList<Integer> gf(int x) {
// ArrayList<Integer> al = new ArrayList<Integer>();
// while (x != 1) {
// al.add(sf[x]);
// x = x / sf[x];
// }
// return al;
// }
static long expmodulo(long a, long b, long c) {
long x = 1;
long y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y) % c;
}
y = (y * y) % c; // squaring the base
b /= 2;
}
return (long) x % c;
}
static int modInverse(int a, int m) {
int m0 = m;
int y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
int q = a / m;
int t = m;
// m is remainder now, process
// same as Euclid's algo
m = a % m;
a = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
static int countBits(int number) {
return (int) (Math.log(number) / Math.log(2) + 1);
}
static int countDifferentBits(int a, int b) {
int count = 0;
for (int i = 0; i < 32; i++) {
if (((a >> i) & 1) != ((b >> i) & 1)) {
++count;
}
}
return count;
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sortMyMapusingValues(HashMap<String, Integer> hm) {
List<Map.Entry<String, Integer>> capitalList = new LinkedList<>(hm.entrySet());
Collections.sort(capitalList, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<String, Integer> result = new HashMap<>();
for (Map.Entry<String, Integer> entry : capitalList) {
result.put(entry.getKey(), entry.getValue());
}
}
static boolean ispowerof2(long num) {
if ((num & (num - 1)) == 0)
return true;
return false;
}
static void primeFactors(int n) {
while (n % 2 == 0) {
System.out.print(2 + " ");
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
System.out.print(i + " ");
n /= i;
}
}
if (n > 2)
System.out.print(n);
}
static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
long sq = (long) Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static class Graph {
HashMap<Integer, LinkedList<Integer>> hm = new HashMap<Integer, LinkedList<Integer>>();
private void addVertex(int vertex) {
hm.put(vertex, new LinkedList<>());
}
private void addEdge(int source, int dest, boolean bi) {
if (!hm.containsKey(source))
addVertex(source);
if (!hm.containsKey(dest))
addVertex(dest);
hm.get(source).add(dest);
if (bi) {
hm.get(dest).add(source);
}
}
private boolean uniCycle(int i, HashSet<Integer> visited, int parent) {
visited.add(i);
LinkedList<Integer> list = hm.get(i);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
Integer integer = (Integer) it.next();
if (!visited.contains(integer)) {
if (uniCycle(integer, visited, i))
return true;
} else if (integer != parent) {
return true;
}
}
return false;
}
private boolean uniCyclic() {
HashSet<Integer> visited = new HashSet<Integer>();
Set<Integer> set = hm.keySet();
for (Integer integer : set) {
if (!visited.contains(integer)) {
if (uniCycle(integer, visited, -1)) {
return true;
}
}
}
return false;
}
private boolean isbiCycle(int i, HashSet<Integer> visited, HashSet<Integer> countered) {
if (countered.contains(i))
return true;
if (visited.contains(i))
return false;
visited.add(i);
countered.add(i);
LinkedList<Integer> list = hm.get(i);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
Integer integer = (Integer) it.next();
if (isbiCycle(integer, visited, countered)) {
return true;
}
}
countered.remove(i);
return false;
}
private boolean isbiCyclic() {
HashSet<Integer> visited = new HashSet<Integer>();
HashSet<Integer> countered = new HashSet<Integer>();
Set<Integer> set = hm.keySet();
for (Integer integer : set) {
if (isbiCycle(integer, visited, countered)) {
return true;
}
}
return false;
}
}
static class Node {
Node left, right;
int data;
public Node(int data) {
this.data = data;
}
public void insert(int val) {
if (val <= data) {
if (left == null) {
left = new Node(val);
} else {
left.insert(val);
}
} else {
if (right == null) {
right = new Node(val);
} else {
right.insert(val);
}
}
}
public boolean contains(int val) {
if (data == val) {
return true;
} else if (val < data) {
if (left == null) {
return false;
} else {
return left.contains(val);
}
} else {
if (right == null) {
return false;
} else {
return right.contains(val);
}
}
}
public void inorder() {
if (left != null) {
left.inorder();
}
System.out.print(data + " ");
if (right != null) {
right.inorder();
}
}
public int maxDepth() {
if (left == null)
return 0;
if (right == null)
return 0;
else {
int ll = left.maxDepth();
int rr = right.maxDepth();
if (ll > rr)
return (ll + 1);
else
return (rr + 1);
}
}
public int countNodes() {
if (left == null)
return 1;
if (right == null)
return 1;
else {
return left.countNodes() + right.countNodes() + 1;
}
}
public void preorder() {
System.out.print(data + " ");
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
}
public void postorder() {
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
System.out.print(data + " ");
}
public void levelorder(Node node) {
LinkedList<Node> ll = new LinkedList<Node>();
ll.add(node);
getorder(ll);
}
public void getorder(LinkedList<Node> ll) {
while (!ll.isEmpty()) {
Node node = ll.poll();
System.out.print(node.data + " ");
if (node.left != null)
ll.add(node.left);
if (node.right != null)
ll.add(node.right);
}
}
}
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 Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
private static final Scanner sc = new Scanner(System.in);
private static final FastReader fs = new FastReader();
private static final OutputWriter op = new OutputWriter(System.out);
static int[] getintarray(int n) {
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextInt();
}
return ar;
}
static long[] getlongarray(int n) {
long ar[] = new long[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextLong();
}
return ar;
}
static int[][] get2darray(int n, int m) {
int ar[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ar[i][j] = fs.nextInt();
}
}
return ar;
}
static Pair[] getpairarray(int n) {
Pair ar[] = new Pair[n];
for (int i = 0; i < n; i++) {
ar[i] = new Pair(fs.nextInt(), fs.nextInt());
}
return ar;
}
static void printarray(int ar[]) {
for (int i : ar) {
op.print(i + " ");
}
op.flush();
}
static int fact(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
//
// function to find largest prime factor
}
| Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 00581d47cbf386712c05bf13cbcea11d | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.util.Scanner;
public class BeautifulSetsOfPoints {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(), y = sc.nextInt();
int min = Integer.min(x, y);
int sets = min + 1;
System.out.println(sets);
for(int i = 0; i < sets; i++)
{
System.out.println(i + " " + (sets - i - 1));
}
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | b6e47b99a4fd5731f9d86243a8ec71a2 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.util.*;
public class BeautifulSet{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int m=s.nextInt();
int x=Math.min(n,m);
System.out.println(x+1);
for(int i=0;i<=x;i++)
{
System.out.println(i+" "+(x-i));
}
}
}
| Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 85e477382d96203f0a1df0ae67198250 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class ladders268c {
static BufferedReader __in;
static PrintWriter __out;
static StringTokenizer input;
public static void main(String[] args) throws IOException {
__in = new BufferedReader(new InputStreamReader(System.in));
__out = new PrintWriter(new OutputStreamWriter(System.out));
r();
int n = ni(), m = ni(), k;
prln(k = min(n, m) + 1);
for(int i = 0; i < k; ++i) {
prln(k - i - 1, i);
}
close();
}
// references
// IBIG = 1e9 + 7
// IRAND ~= 3e8
// IMAX ~= 2e10
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IRAND = 327859546;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {assert x.length >= 2; return x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
static int minstarting(int offset, int... x) {assert x.length >= 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {assert x.length >= 2; return x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
static long minstarting(int offset, long... x) {assert x.length >= 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
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 floori(double d) {return (int)d;}
static int ceili(double d) {return (int)ceil(d);}
static long floorl(double d) {return (long)d;}
static long ceill(double d) {return (long)ceil(d);}
// input
static void r() throws IOException {input = new StringTokenizer(__in.readLine());}
static int ri() throws IOException {return Integer.parseInt(__in.readLine());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;}
static char[] rcha() throws IOException {return __in.readLine().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static int ni() {return Integer.parseInt(input.nextToken());}
static long nl() {return Long.parseLong(input.nextToken());}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {__out.println("yes");}
static void pry() {__out.println("Yes");}
static void prY() {__out.println("YES");}
static void prno() {__out.println("no");}
static void prn() {__out.println("No");}
static void prN() {__out.println("NO");}
static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), i++); __out.println(a[a.length - 1]);}
static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), i++); __out.println(a[a.length - 1]);}
static void flush() {__out.flush();}
static void close() {__out.close();}
// debug
static void debug(String desc, int... a) {System.out.print(desc); System.out.print(": "); for(int i = 0, len = a.length - 1; i < len; System.out.print(a[i]), System.out.print(", "), i++); System.out.println(a[a.length - 1]);}
static void debug(String desc, long... a) {System.out.print(desc); System.out.print(": "); for(int i = 0, len = a.length - 1; i < len; System.out.print(a[i]), System.out.print(", "), i++); System.out.println(a[a.length - 1]);}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | fc7c7648dd98779fa4088afceca76f7a | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.util.*;
import java.io.*;
public class afterALongTime1 {
public static void main(String[] args) throws Exception{
Scanner scn=new Scanner(System.in);
int a=scn.nextInt();
int b=scn.nextInt();
System.out.println(Math.min(a,b)+1);
for(int i=0;i<=Math.min(a,b);i++){
System.out.println(i+" "+(Math.min(a,b)-i));
}
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | bcea660e5a3b5f89616abbb8631e3c7a | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class P_268C {
static final FS sc = new FS();
static final PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int n = sc.nextInt();
int m = sc.nextInt();
int min = Math.min(n,m);
System.out.println(min+1);
for(int i=0; i<=min; i++){
System.out.println(i+" "+(min-i));
}
}
static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception ignored) {
}
}
return st.nextToken();
}
int[] nextArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | bdc23679387e45ff57377996392dd02e | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | //package com.company;
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
public static class Task {
public void solve(Scanner sc, PrintWriter pw) throws IOException {
int n = sc.nextInt();
int m = sc.nextInt();
int lo = Math.min(n, m);
pw.println(lo + 1);
for (int i = 0; i <= lo; i++) {
pw.println((lo - i) + " " + i);
}
}
}
static long TIME_START, TIME_END;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
// PrintWriter pw = new PrintWriter(new FileOutputStream("ans.txt"));
Runtime runtime = Runtime.getRuntime();
long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
TIME_START = System.currentTimeMillis();
Task t = new Task();
t.solve(sc, pw);
TIME_END = System.currentTimeMillis();
long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
pw.close();
System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000);
System.err.println("Time used: " + (TIME_END - TIME_START) + ".");
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader s) throws FileNotFoundException {
br = new BufferedReader(s);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | b3c44239decc3d531236c2dae01f250a | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes |
import java.util.*;
import java.io.*;
public class BeautifulSetsofPoints {
// https://codeforces.com/contest/268/problem/C
public static void main(String[] args) throws IOException, FileNotFoundException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader in = new BufferedReader(new FileReader("BeautifulSetsofPoints"));
StringTokenizer st = new StringTokenizer(in.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
System.out.println(Math.min(n+1, m+1));
if (m<n) {
int temp = m;
m = n;
n = temp;
}
// smaller is n
for (int i=0; i<=n; i++) {
System.out.println((n-i) + " " + i);
}
}
}
| Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 8211d3057b2bd4d2f725cae52042c8e5 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastReader f = new FastReader();
int n = f.nextInt();
int m = f.nextInt();
int min = Math.min(n,m);
System.out.println(min+1);
for(int i=0;i<=min;i++) {
System.out.println(i+" "+(min-i));
}
}
//fast input reader
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 | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | e42548c32c66529fd0bf2c26d6c357dd | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.io.*;
import java.util.*;
public class Beautifulset
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int m = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
int x = (int)Math.min(m,n);
System.out.println(x+1);
for(int i=x;i>=0;i--)
{
System.out.println(i+" "+(x-i));
}
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | 879e3182ee2cdae0b56402041ccd9a25 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class C {
static MyScanner in = new MyScanner();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static long H,W;
static BitSet bs;
static long mod = 1000000007;
// All possible moves of a knight
static int X[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
static int Y[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
static int [] dr = {-1,0,0,1};
static int [] dc = {0,1,-1,0};
public static void main(String args[]) throws IOException {
/**
1.What is the unknown:
2.What are the data: an array of tlen ght the value is 10^9
3.What is the condition:
4. understand the problem:
5. What are the cases edges in the problem:
6.What what max:
7. Are you using a data stcuture. And which one:
8.Is there reursion what is the base case. For example COINS:
*/
int n = in.nextInt();
int m= in.nextInt();
out.println(Math.min(n, m)+1);
for(int i=0;i<=Math.min(n, m);i++){
out.println(i+" "+(Math.min(n, m)-i));
}
out.flush();
}
static long lcm(int a, int b){
return (a*b)/gcd(a, b);
}
static boolean isVowel(char c) {
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='y'){
return true;
}
return false ;
}
static class IP implements Comparable<IP>{
public int first,second;
IP(int first, int second){
this.first = first;
this.second = second;
}
public int compareTo(IP ip){
if(first==ip.first)
return second-ip.second;
return first-ip.first;
}
@Override
public String toString() {
return first+" "+second;
}
}
static long gcd(long a, long b){
return b!=0?gcd(b, a%b):a;
}
static boolean isEven(long a) {
return (a&1)==0;
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | e31ea8459c23288835f1c0d972503222 | train_003.jsonl | 1359387000 | Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points. | 256 megabytes |
import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n , m;
n = sc.nextInt();
m = sc.nextInt();
int x = Math.min(n,m);
System.out.println(x+1);
for(int i = 0 ; i <= x ; i++ ) System.out.println( i + " " + (x - i));
}
} | Java | ["2 2", "4 3"] | 1 second | ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"] | NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | 0e99f4a49b408cc8874a6d5ec4167acb | The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). | 1,500 | In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. | standard output | |
PASSED | f3e4857d06f2fa1c16302bbb0c7ba8e8 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.*;
import java.util.*;
public class C
{
static int n = 1;
public static void main(String [] args) throws IOException
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
if(!(n%4 == 0 || n%4 == 1))
{
System.out.println(-1);
return;
}
int [] arr = new int[n];
int lb = 2;
int ub = n;
for(int i = 0 ; i < n/2 ; i+=2)
{
arr[i] = lb;
arr[i+1] = ub;
arr[n-i-1] = ub-1;
arr[n-i-2] = lb-1;
lb += 2;
ub -= 2;
}
if(n % 2 != 0)
arr[n/2] = n/2+1;
for(int i = 0 ; i < n-1 ; i++)
writer.print(arr[i]+" ");
writer.println(arr[n-1]);
writer.flush();
writer.close();
}
} | Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 1d4d72bd05814a5f46bcf8354e9dd24c | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes |
import java.io.PrintWriter;
import java.util.Scanner;
public class C {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
if(n%4==1 || n%4==0){
int a [] = new int[n+1];
for(int i = 1 ; i <= n/2 ; i+=2 ){
a[i]=i+1;
a[n-i+1] = n-i;
}
for(int i = 2 ; i <= n/2 ; i+=2){
a[i]=n-i+2;
a[n-i+1] = i - 1;
}
if(n%2 ==1){
a[n/2+1]= n/2+1;
}
out.print(a[1]);
for(int i = 2 ; i <= n ; i++){
out.print(" "+a[i]);
}
}
else{
out.print(-1);
}
out.close();
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 3d51a4468520b40c4b4609c622814b69 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.*;
import java.util.*;
public class z3 {
public static void wiw(long a,long b)
{ if (a<=b) {if (a==b) System.out.print(""+a+" "); else
{
System.out.print(""+(a+1)+" "+b+" ");
wiw(a+2,b-2);
System.out.print(""+(a)+" "+(b-1)+" ");
}}
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
long n=in.nextLong();
if (((n%4)==2)||((n%4)==3)) System.out.println("-1"); else
{
wiw(1,n);
}
}
} | Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | a75e629a04abd7399738fe9d0b7e2e5b | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C {
static StringTokenizer st;
static BufferedReader in;
static PrintWriter pw;
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = nextInt();
if (n==1) {
System.out.println(1);
return;
}
if (n % 4==2 || n % 4==3)
System.out.println(-1);
else {
int min = 1, max = n;
int[]ans = new int[n+1];
int left = 1, right = n;
for (int i = 1; i <= n/4; i++) {
ans[left] = min+1;
ans[left+1] = max;
ans[right] = max-1;
ans[right-1] = min;
min += 2;
max -= 2;
left += 2;
right -= 2;
}
if (n % 2==1)
ans[n/2+1] = n/2+1;
for (int i = 1; i <= n; i++) {
pw.print(ans[i]+" ");
}
pw.println();
}
// int[]p = new int[n+1];
// for (int i = 1; i <= n; i++) {
// p[i] = i;
// }
// while (true) {
// boolean f = true;
// for (int i = 1; i <= n; i++) {
// if (p[p[i]] != n-i+1) {
// f = false;
// break;
// }
// }
// if (f) {
// for (int i = 1; i <= n; i++) {
// System.out.print(p[i]+" ");
// }
// return;
// }
// int ind1 = n;
// while (ind1 > 1 && p[ind1] < p[ind1-1])
// ind1--;
// if (ind1==1)
// break;
// int ind2 = n;
// while (p[ind2] < p[ind1-1])
// ind2--;
// int t = p[ind1-1];
// p[ind1-1] = p[ind2];
// p[ind2] = t;
// for (int i = 0; i+ind1 < n-i; i++) {
// t = p[ind1+i];
// p[ind1+i] = p[n-i];
// p[n-i] = t;
// }
// }
pw.close();
}
private static int nextInt() throws IOException{
return Integer.parseInt(next());
}
private static long nextLong() throws IOException{
return Long.parseLong(next());
}
private static double nextDouble() throws IOException{
return Double.parseDouble(next());
}
private static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
}
/*
8
2 8 4 6 3 5 1 7
9
2 9 4 7 5 3 6 1 8
*/ | Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 3135689f1c111fd4aee4582107e542c9 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int N = r.nextInt();
if(N % 4 == 0 || (N % 4 == 1)){
int[] a = new int[N];
int rem = N;
if(N % 2 == 1)a[N / 2] = N / 2 + 1;
for(int i = 0; i < N; i += 2){
if(rem < 4)break;
a[i] = (i + 1) + 1;
a[i + 1] = N - i - 1 + 1;
a[m(i, N) - 1] = i + 1;
a[m(i, N)] = m(i, N) - 1 + 1;
rem -= 4;
}
for(int i = 0; i < N; i++)
out.println(a[i]);
}else{
out.println(-1);
}
out.close();
}
private static int m(int i, int N) {
return N - i - 1;
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String nextLine(){
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 406e5fd7c1b7366372486ba620955a08 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main{
static PrintWriter out=new PrintWriter(System.out);
static void output(int l,int r){
if(l<r){
out.print(1+l+" "+r+" ");
output(2+l,r-2);
out.print(l+" "+(r-1)+" ");
}else if(l==r) out.print(l+" ");
}
public static void main(String[]args){
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
if(2>(3&n)) output(1,n);
else out.print(-1);
out.close();
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 2f678c93f438d3b7999074760f45d69e | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
public class B {
static int N;
static PrintWriter out;
public static void main(String[] args) {
MScanner sc = new MScanner();
out = new PrintWriter(System.out);
int N = sc.nextInt();
if(N%4>1){
out.println(-1);
out.close();
return;
}
LinkedList<Integer> start = new LinkedList<Integer>();
if(N%4==1)start.add(1);
else{
start.add(2);
start.add(4);
start.add(1);
start.add(3);
}
while(start.size()!=N){
int s = start.size();
start.addFirst(s+4);
start.addFirst(2);
start.addLast(1);
start.addLast(s+3);
}
LinkedList<Integer> outputL = new LinkedList<Integer>();
LinkedList<Integer> outputR = new LinkedList<Integer>();
int val = 0;
while(!start.isEmpty()){
if(start.size()<4){
while(!start.isEmpty()){
outputL.add(start.poll()+val);
}
}
else{
outputL.addLast(start.poll()+val);
outputL.addLast(start.poll()+val);
outputR.addFirst(start.pollLast()+val);
outputR.addFirst(start.pollLast()+val);
val+=2;
}
}
for(Integer x : outputL)out.print(x+" ");
for(Integer x : outputR)out.print(x+" ");
out.close();
}
static class MScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public MScanner() {
stream = System.in;
// stream = new FileInputStream(new File("dec.in"));
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextInt(int N) {
int[] ret = new int[N];
for (int a = 0; a < N; a++)
ret[a] = nextInt();
return ret;
}
int[][] nextInt(int N, int M) {
int[][] ret = new int[N][M];
for (int a = 0; a < N; a++)
ret[a] = nextInt(M);
return ret;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLong(int N) {
long[] ret = new long[N];
for (int a = 0; a < N; a++)
ret[a] = nextLong();
return ret;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDouble(int N) {
double[] ret = new double[N];
for (int a = 0; a < N; a++)
ret[a] = nextDouble();
return ret;
}
String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
String[] next(int N) {
String[] ret = new String[N];
for (int a = 0; a < N; a++)
ret[a] = next();
return ret;
}
String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
String[] nextLine(int N) {
String[] ret = new String[N];
for (int a = 0; a < N; a++)
ret[a] = nextLine();
return ret;
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 3cbadc64d24bab63801ba4e36e1a6a77 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes |
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class LuckyPermutation {
/*
index , val
i, f(i)
f(i),n-i+1
n-i+1,n-f(i)+1
n-f(i)+1,i
if n-i+1 = i -> i=(n+1)/2, cycle length = 1
if n-i+1 != i -> f(i)!=n-f(i)+1 to be a permutation too, cycle length = 4, must
total n mod 4 = 1 or 0, then
choose f(i) = i+1 because it's nice to see how the array will be filled up.
Maybe it's possible to choose a different f(i) too?
*/
public static void main(String[] args){
FastScanner sc = new FastScanner();
int n = sc.nextInt();
if(n%4 <= 1){
int[] a = new int[n+1];
for(int i=1;i<=n/2;i+=2){
a[i] = i+1;
a[i+1] = n-i+1;
a[n-i+1] = n-i;
a[n-i] = i;
}
if(n%4 == 1){
a[(n+1)/2] = (n+1)/2;
}
for(int i=1;i<=n;i++){
System.out.print(a[i] + " ");
}
}else{
System.out.println(-1);
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | e8c6faf526237d67f1210d61e7ed167f | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.util.*;
public class C {
Scanner sc = new Scanner(System.in);
void doIt()
{
int n = sc.nextInt();
if(n % 4 >= 2) System.out.println(-1);
else {
int [] ans = new int[n];
if(n % 4 == 1) ans[n / 2] = (n + 1) / 2;
for(int i = 0; i < n / 2; i += 2 ){
ans[i] = i+2;
ans[i+1] = n-i;
ans[n-i-2] = i+1;
ans[n-i-1] = n-i-1;
//System.out.println(Arrays.toString(ans));
}
StringBuilder sb = new StringBuilder("");
for(int i = 0; i < n; i++) sb.append(ans[i] + " ");
System.out.println(sb);
}
}
/**
* @param args
*/
public static void main(String[] args) {
new C().doIt();
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | fe0303ac516fb222054a96ec2124063a | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class C {
static StreamTokenizer st;
static PrintWriter pw;
private static int nextInt() throws IOException {
st.nextToken();
return (int) st.nval;
}
public static void main(String[] args) throws IOException {
st = new StreamTokenizer(new BufferedReader(new InputStreamReader(
System.in)));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
System.out)));
// -----------begin-------------------------
int n = nextInt();
if ((n - 1) % 4 == 1 || (n - 1) % 4 == 2) {
pw.println("-1");
pw.close();
return;
}
if (n % 2 == 0) {
for (int i = 0; i < n/2; i += 2) {
pw.print(i + 2+ " ");
pw.print(n-i+" ");
}
int a[][] = new int [n/2][2];
int cnt = 0;
for (int i = 0; i < n/2; i += 2) {
a[cnt][0] = i+1;
a[cnt][1] = n-i-1;
cnt++;
}
for (int i = cnt-1; i >= 0; i--) {
pw.print(a[i][0]+" ");
pw.print(a[i][1]+" ");
}
} else {
for (int i = 0; i < n/2; i += 2) {
pw.print(i + 2+ " ");
pw.print(n-i+" ");
}
int a[][] = new int [n/2][2];
int cnt = 0;
for (int i = 0; i < n/2; i += 2) {
a[cnt][0] = i+1;
a[cnt][1] = n-i-1;
cnt++;
}
pw.print(n/2+1+" ");
for (int i = cnt-1; i >= 0; i--) {
pw.print(a[i][0]+" ");
pw.print(a[i][1]+" ");
}
}
// pw.print(n);
pw.close();
// ------------end--------------------------
}
} | Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 933d68a517bd88b5fb7b7ec3b2826798 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class C176 {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedInputStream(System.in));
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = in.nextInt();
if (n % 4 == 2 || n % 4 == 3) out.println(-1);
else {
int[] p = new int[n];
for (int i = 0; i < n / 4; i++) {
p[2 * i] = 2 * i + 2;
p[2 * i + 1] = n + 2 - p[2 * i];
p[n - 1 - (2 * i + 1)] = 2 * i + 1;
p[n - 1 - 2 * i] = n - p[n - 1 - (2 * i + 1)];
}
if (n % 4 == 1) p[n / 2] = n / 2 + 1;
for (int i = 0; i < n; i++) {
out.print(p[i] + " ");
}
out.println();
}
out.close();
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 263eec9f1d5890a65bcc160c0257cb5f | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.PrintWriter;
import java.util.Scanner;
public class Problem3 implements Runnable {
public int n;
public int [] p;
public boolean [] used;
public boolean isGood() {
for (int i = 0; i < n; i++) {
if (p[p[i] - 1] != n - (i + 1) + 1) {
return false;
}
}
return true;
}
/*public void rec(int pos) {
if (pos == n) {
boolean flag = true;
for (int i = 0; i < n; i++) {
if (p[p[i] - 1] != n - (i + 1) + 1) {
flag = false;
}
}
if (flag) {
for (int i = 0; i < n; i++) {
System.out.print(p[i] + " ");
}
System.out.println();
}
return;
}
for (int i = 1; i <= n; i++) {
if (!used[i]) {
used[i] = true;
p[pos] = i;
rec(pos + 1);
used[i] = false;
}
}
} */
public void run() {
Scanner scanner = new Scanner(System.in);
PrintWriter writer = new PrintWriter(System.out);
n = scanner.nextInt();
//used = new boolean[n + 1];
p = new int[n];
//rec(0);
if (n == 2 || n ==3) {
writer.println(-1);
writer.close();
return;
}
if (n % 2 == 0) {
if (n % 4 != 0) {
writer.println(-1);
writer.close();
return;
}
for (int i = 0; i < n / 4; i++) {
p[i * 2] = (i + 1) * 2;
p[i * 2 + 1] = (n - i * 2);
}
for (int i = 0; i < n / 4; i++) {
p[n - (i + 1) * 2] = (i * 2 + 1);
p[n - (i + 1) * 2 + 1] = n - (i * 2 + 1);
}
if (isGood()) {
for (int i = 0; i < n; i++) {
writer.print(p[i] + " ");
}
} else {
writer.println(-1);
}
writer.close();
return;
} else {
if ((n - 1) % 4 != 0) {
writer.println(-1);
writer.close();
return;
}
for (int i = 0; i < n / 4; i++) {
p[i * 2] = (i + 1) * 2;
p[n - 1 - (i * 2)] = (n - 1) - i * 2;
}
for (int i = 0; i < n / 4; i++) {
p[i * 2 + 1] = n - i * 2;
p[n - 2 - (i * 2)] = i * 2 + 1;
}
p[n / 2] = n / 2 + 1;
if (isGood()) {
for (int i = 0; i < n; i++) {
writer.print(p[i] + " ");
}
} else {
writer.println(-1);
}
writer.close();
}
writer.close();
}
public static void main(String[] args) {
new Problem3().run();
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 5bb6aa6b20850411cf742f4317154910 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.util.*; //Scanner;
import java.io.PrintWriter; //PrintWriter
public class R176_Div2_C //Name: Lucky Permutation
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
solve(in, out);
out.close();
in.close();
}
public static void solve(Scanner in, PrintWriter out)
{
int n = in.nextInt();
if (n % 4 == 2 || n % 4 == 3)
out.println(-1);
else if (n == 1)
out.println(1);
else
{
int[] p = new int[n + 1];
p[1] = 2;
p[2] = n;
p[n - 1] = 1;
p[n] = n - 1;
int it = n / 4 - 1;
for (int i = 1; i <= it; i++)
{
p[1 + 2*i] = p[1 + 2*(i-1)] + 2;
p[2 + 2*i] = p[2 + 2*(i-1)] - 2;
p[n-1 - 2*i] = p[n-1 - 2*(i-1)] + 2;
p[n - 2*i] = p[n - 2*(i-1)] - 2;
}
if (n % 4 == 1)
p[n / 2 + 1] = n / 2 + 1;
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++)
sb.append(p[i] + " ");
out.println(sb);
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 3c8e6d9944162f57edf1f84f1e8d0fba | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author vadimmm
*/
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);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
if ((n & 3) > 1)
out.println(-1);
else {
int[] permutation = new int[n + 1];
int up = n >> 1;
if ((n & 1) == 1)
permutation[up + 1] = up + 1;
for (int i = 1; i <= up; i += 2) {
permutation[i] = i + 1;
permutation[n - i + 1] = n - i;
permutation[i + 1] = n - i + 1;
permutation[n - i] = i;
}
out.print(permutation[1]);
for (int i = 2; i <= n; ++i) {
out.print(" ");
out.print(permutation[i]);
}
}
}
}
class InputReader {
private static BufferedReader bufferedReader;
private static StringTokenizer stringTokenizer;
public InputReader(InputStream inputStream) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
stringTokenizer = null;
}
public String next() {
while(stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return stringTokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 34a5cbba189cc34298d620782da34be7 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C {
private static StringTokenizer tokenizer;
private static BufferedReader bf;
private static PrintWriter out;
private static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private static String nextToken() throws IOException {
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(bf.readLine());
}
return tokenizer.nextToken();
}
public static void main(String[] args) throws IOException {
bf = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = nextInt();
int k = n%4;
int[] a = new int[n+1];
if(k == 0) {
for(int i = 1; i <= n/2; i += 2) {
a[i] = i+1; a[i+1] = n+1-i;
a[n+1-i] = n-i; a[n-i] = i;
}
for(int i = 1; i <= n; i++)
out.print(a[i] + " ");
}
else if(k == 1) {
for(int i = 1; i <= n/2; i += 2) {
a[i] = i+1; a[i+1] = n+1-i;
a[n+1-i] = n-i; a[n-i] = i;
}
a[n/2+1] = n/2+1;
for(int i = 1; i <= n; i++)
out.print(a[i] + " ");
}
else out.println(-1);
out.close();
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 5fe9d7c21b726d5629a9b7d0911ba572 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.util.Scanner;
public class CodeforcesRound176C {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner kde= new Scanner(System.in);
int n=kde.nextInt();
if((n%4==2)||(n%4==3))
{
System.out.println(-1);
return;
}
int[] m= new int[n+1];
if (n % 4 == 0)
{
int t = n/4;
for (int i = 1; i <= t; i++)
{
int odd = 2*i - 1;
int even = 2*i;
m[odd] = even;
m[even] = n+1 - odd;
m[n+1 - odd] = n+1 - even;
m[n+1 - even] = odd;
}
}
if (n % 4 == 1)
{
int t = (n - 1)/4;
for (int i = 1; i <= t; i++)
{
int odd = 2*i-1;
int even = 2*i;
m[odd] = even;
m[even] = n+1 - odd;
m[n+1 - odd] = n+1 - even;
m[n+1 - even] = odd;
}
m[(n+1)/2] = (n+1)/2;
}
for (int i = 1; i <= n; i++)
{
System.out.print(m[i]+" ");
}
System.out.println();
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 51d64a0a8c8ff2543ccdf498522e911c | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class C implements Runnable {
// leave empty to read from stdin/stdout
private static final String TASK_NAME_FOR_IO = "";
// file names
private static final String FILE_IN = TASK_NAME_FOR_IO + ".in";
private static final String FILE_OUT = TASK_NAME_FOR_IO + ".out";
BufferedReader in;
PrintWriter out;
StringTokenizer tokenizer = new StringTokenizer("");
public static void main(String[] args) {
new Thread(new C()).start();
}
boolean good(int n, int[] a) {
for (int i = 0; i < n; i++)
if (a[a[i] - 1] != n - i) {
return false;
}
return true;
}
private void gen(int pos, int n, int[] used, int[] a, List<int[]> all) {
if (pos == n) {
if (good(n, a)) {
all.add(a.clone());
}
return;
}
for (int i = 0; i < n; i++)
if (used[i] == 0) {
used[i] = 1;
a[pos] = i + 1;
gen(pos + 1, n, used, a, all);
used[i] = 0;
}
}
private void solveNaive(int n) {
List<int[]> all = new ArrayList<int[]>();
int[] used = new int[n];
int[] p = new int[n];
gen(0, n, used, p, all);
System.out.println("N = " + n);
for (int[] a : all) {
System.out.println(Arrays.toString(a));
}
}
private void solve() throws IOException {
/*
for (int n = 5; n <= 12; n++) {
solveNaive(n);
solveGood(n);
}
*/
int n = nextInt();
solveGood2(n);
}
private void solveGood2(int n) {
int[] a = new int[n];
int mod4 = n % 4;
if (n == 1) {
out.print("1");
return;
}
if (mod4 > 1) {
out.print("-1");
return;
}
int l = 0;
int r = n - 1;
int lo = 1;
for (;;) {
a[l] = n - 1;
a[l + 1] = lo;
a[r] = lo + 1;
a[r - 1] = n;
n -= 2;
lo += 2;
l += 2;
r -= 2;
if (l > r) {
break;
}
if (l == r) {
a[l] = n;
break;
}
}
for (int v : a) {
out.print(v);
out.print(' ');
}
}
private void solveGood(int n) {
System.out.println("N (GOOD) = " + n);
int[] a = new int[n];
int mod4 = n % 4;
if (n == 1) {
System.out.println(Arrays.toString(new int[] {1}));
return;
}
if (mod4 > 1) {
System.out.println(-1);
return;
}
int l = 0;
int r = n - 1;
int lo = 1;
for (;;) {
a[l] = n - 1;
a[l + 1] = lo;
a[r] = lo + 1;
a[r - 1] = n;
n -= 2;
lo += 2;
l += 2;
r -= 2;
if (l > r) {
break;
}
if (l == r) {
a[l] = n;
break;
}
}
System.out.println(Arrays.toString(a));
}
public void run() {
long timeStart = System.currentTimeMillis();
boolean fileIO = TASK_NAME_FOR_IO.length() > 0;
try {
if (fileIO) {
in = new BufferedReader(new FileReader(FILE_IN));
out = new PrintWriter(new FileWriter(FILE_OUT));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
}
solve();
in.close();
out.close();
} catch (IOException e) {
throw new IllegalStateException(e);
}
long timeEnd = System.currentTimeMillis();
if (fileIO) {
System.out.println("Time spent: " + (timeEnd - timeStart) + " ms");
}
}
private String nextToken() throws IOException {
while (!tokenizer.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer.nextToken();
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private BigInteger nextBigInt() throws IOException {
return new BigInteger(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 4eb41395615cbf997c7c6e5fa100a282 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.*;
import java.util.*;
public class LuckyPermutation {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(f.readLine());
if (n % 4 == 2 || n % 4 == 3)
{
System.out.println(-1);
return;
}
int[] a = new int[n+1];
for (int i = 1; i <= n/2-1; i += 2)
{
a[i] = i+1;
a[i+1] = n-i+1;
a[n-i+1] = n-i;
a[n-i] = i;
}
if (n % 4 == 1)
a[(n+1)/2] = (n+1)/2;
for (int i = 1; i <= n; i++)
System.out.print(a[i]+" ");
System.out.println();
}
} | Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 20b41c1e77bf8a7db99b603d2a75650a | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes |
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if (n % 4 > 1)
System.out.println(-1);
else {
int[] P = new int[n];
int start = 0;
int end = n - 1;
int min = 1;
int max = n;
while (n > 1) {
P[end - 1] = min++;
P[start] = min++;
P[start + 1] = max--;
P[end] = max--;
start += 2;
end -= 2;
n -= 4;
}
if (n == 1)
P[start] = min;
for (int i = 0; i < P.length; i++)
System.out.print(P[i] + " ");
System.out.println();
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 971fc9a9cfc3add3467a0fae48d9c9da | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Deque;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
private void solve() throws IOException {
int n = ni();
if (n == 1) {
prln(1);
return;
}
if (n < 4) {
prln(-1);
return;
}
if (n % 4 == 0 || n % 4 == 1) {
Deque<Integer> deque = new LinkedList<>();
if (n % 4 == 1)
deque.add(n / 2 + 1);
int t = n / 4;
int l = n / 2;
int r = n / 2 + 1;
if (n % 4 == 1)
++r;
for (int i = 0; i < t; ++i) {
deque.addFirst(r + 1);
deque.addFirst(l);
deque.addLast(l - 1);
deque.addLast(r);
l -= 2;
r += 2;
}
for (int i = 0; i < n; ++i) {
pr(deque.pollFirst() + " ");
}
} else
prln(-1);
}
public static void main(String[] args) {
new Main().run();
}
public void run() {
try {
if (isFileIO) {
pw = new PrintWriter(new File("output.out"));
br = new BufferedReader(new FileReader("input.in"));
} else {
pw = new PrintWriter(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
}
solve();
pw.close();
br.close();
} catch (IOException e) {
System.err.println("IO Error");
}
}
private int[] nia(int n) throws IOException {
int arr[] = new int[n];
for (int i = 0; i < n; ++i)
arr[i] = Integer.parseInt(nextToken());
return arr;
}
private int[] niam1(int n) throws IOException {
int arr[] = new int[n];
for (int i = 0; i < n; ++i)
arr[i] = Integer.parseInt(nextToken()) - 1;
return arr;
}
private long[] nla(int n) throws IOException {
long arr[] = new long[n];
for (int i = 0; i < n; ++i)
arr[i] = Long.parseLong(nextToken());
return arr;
}
private void pr(Object o) {
pw.print(o);
}
private void prln(Object o) {
pw.println(o);
}
private void prln() {
pw.println();
}
int ni() throws IOException {
return Integer.parseInt(nextToken());
}
long nl() throws IOException {
return Long.parseLong(nextToken());
}
double nd() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(br.readLine());
}
return tokenizer.nextToken();
}
// private void qsort(int a[]) {
// Random rand = new Random(271828182l);
// qsort(a, 0, a.length, rand);
// }
//
// private void qsort(int a[], int l, int r, Random rand) {
// if (r - l <= 1)
// return;
//
// if (r - l == 2) {
// if (a[r - 1] < a[l]) {
// int t = a[r - 1];
// a[r - 1] = a[l];
// a[l] = t;
// }
//
// return;
// }
//
// int x = a[rand.nextInt(r - l) + l];
// int i = l, j = r - 1;
// while (i < j) {
// while (a[i] < x)
// ++i;
// while (a[j] > x)
// --j;
// if (i < j) {
// int t = a[i];
// a[i] = a[j];
// a[j] = t;
// ++i;
// --j;
// }
// }
//
// qsort(a, l, j + 1, rand);
// qsort(a, i, r, rand);
// }
private BufferedReader br;
private StringTokenizer tokenizer;
private PrintWriter pw;
private final boolean isFileIO = false;
} | Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 2b7ab43055ae9dc33105d96ae8d07f91 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.*;
import java.util.*;
public class My {
public static void main(String[] args) {
new My().go();
}
void go() {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if (n % 4 == 0 || n % 4 == 1) {
int rn = n / 4;
int[] val = new int[n];
for (int i = 0; i < rn; i++) {
val[2*i] = n - 2*i - 2;
val[2*i + 1] = 2*i;
val[n - 2*i - 1] = 2*i + 1;
val[n - 2*i - 2] = n - 2*i - 1;
}
if (n % 4 == 1) {
val[n/2] = n/2;
}
for (int i = 0; i < n; i++) {
if (i < n - 1) {
p(++val[i] + " ");
} else {
pl(++val[i] + "");
}
}
} else {
pl("-1");
}
}
void p(String s) {
System.out.print(s);
}
void pl(String s) {
System.out.println(s);
}
} | Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 32e69acdd10ea5f49d1061a9941b498b | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (((n / 2) * 2) % 4 != 0) {
System.out.println(-1);
} else {
int[] perm = new int[n];
for (int i = 0; i < n; i++) {
perm[i] = n - 1 - i;
}
int[] res = new int[n];
Arrays.fill(res, -1);
for (int i = 0; i < n/2;) {
res[i] = i + 1;
int tmp=i;
while (res[res[i]] == -1) {
res[res[i]] = perm[i];
i = res[i];
}
i =tmp+2;
}
if (n % 2 == 1) {
res[n / 2] = n / 2;
}
PrintWriter out = new PrintWriter(System.out);
for (int i = 0; i < n; i++) {
out.print((res[i] + 1) + " ");
}
out.println();
out.flush();
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | f885937af4d038616da375f2bed7931d | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.IOException;
import java.util.InputMismatchException;
public class LuckyPermutation {
public static void main(String[] args) {
FasterScanner sc = new FasterScanner();
int N = sc.nextInt();
if (N % 4 == 2 || N % 4 == 3) {
System.out.println(-1);
return;
}
int[] arr = new int[N + 1];
int L = 1;
int R = N;
for (int i = 0; i < N / 4; i++) {
arr[L] = L + 1;
arr[L + 1] = R;
arr[R] = R - 1;
arr[R - 1] = L;
L += 2;
R -= 2;
}
if (N % 2 == 1) {
arr[N / 2 + 1] = N / 2 + 1;
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= N; i++) {
sb.append(arr[i] + " ");
}
System.out.println(sb.toString());
}
public static class FasterScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
} | Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 8752a76019181748d1ff126db44ded5a | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.*;
import java.util.*;
public class C176
{
public static void main(String[] args)
{
new C176();
}
public C176()
{
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
if(n % 4 != 1 && n % 4 != 0)
System.out.println("-1");
else
{
int[] arr = new int[n];
if(n % 2 == 1)
arr[n / 2] = n / 2 + 1;
for(int x = 0; x < n / 2; x++)
{
if(x % 2 == 0)
arr[x] = x + 2;
else
arr[x] = n + 2 - arr[x - 1];
//System.out.println(x);
}
for(int x = 0; x < n / 2; x++)
{
arr[n - 1 - x] = n + 1 - arr[x];
//System.out.println(x);
}
for(int x = 0; x < n; x++)
System.out.print(arr[x] + " ");
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | a8a5f939c8b35eb2f0ad986b1a50b228 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.util.Scanner;
// http://codeforces.com/contest/287/problem/C
public class LuckyPermutation {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
s.close();
if (n % 4 == 0 || (n - 1) % 4 == 0) {
int p = n / 4;
int[] a = new int[n + 1];
if ((n - 1) % 4 == 0) {
int mid = (n + 1) / 2;
a[mid] = mid;
}
for (int i = 1; i <= p; i++) {
a[i] = p + p - i + 1;
}
for (int i = p + 1; i <= p + p; i++) {
a[i] = n + i - p - p;
}
for (int i = n - p - p + 1; i <= n - p; i++) {
a[i] = i - n + p + p;
}
for (int i = n - p + 1; i <= n; i++) {
a[i] = n + n - i - p - p + 1;
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) {
sb.append(a[i]);
sb.append(' ');
}
System.out.println(sb);
} else {
System.out.println(-1);
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 2a052a005081b977c86e38daaafe438f | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.util.Scanner;
public class c_176 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if ((n - 1) % 4 == 1 || (n - 1) % 4 == 2) {
System.out.println(-1);
return;
}
for (int i = 2; i <= n / 2; i += 2) {
System.out.print(i + " " + ((n + 2) - i) + " ");
}
if (n % 2 == 1) {
System.out.print(n / 2 + 1 + " ");
}
for (int i = n / 2 - 1; i >= 1; i -= 2) {
System.out.print(i + " " + (n - i) + " ");
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 96b7915e6e4a5b05942121e378749110 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.util.Scanner;
public class c_176 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if ((n-1) % 4 == 1 || (n-1) % 4 == 2) {
System.out.println(-1);
return;
}
if (n % 2 == 0) {
for (int i = 2; i <= n / 2; i += 2) {
System.out.print(i + " " + ((n + 2) - i) + " ");
}
for (int i = n / 2 - 1; i >= 1; i -= 2) {
System.out.print(i + " " + (n - i) + " ");
}
} else {
for (int i = 2; i <= n / 2; i += 2) {
System.out.print(i + " " + ((n + 2) - i) + " ");
}
System.out.print(n/2+1+" ");
for (int i = n / 2 - 1; i >= 1; i -= 2) {
System.out.print(i + " " + (n - i) + " ");
}
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 995d505152658894e284f23cf2f86919 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class C {
static PrintWriter out;
static StreamTokenizer st;
static BufferedReader bf;
public static void main(String[] args) throws IOException {
bf = new BufferedReader(new InputStreamReader(System.in));
st = new StreamTokenizer(bf);
out = new PrintWriter(new OutputStreamWriter(System.out));
int n = nextInt();
if (n == 1){
System.out.println(1);
return;
}
if (n%4 == 0 || ((n-1)%4) == 0){
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int []ans = new int [n+1];
int c = n;
int cnt = 0;
if (n == 4){
System.out.println("2 4 1 3");
return;
}
while (c > cnt+1){
cnt++;
ans[cnt] = cnt+1;
ans[cnt+1] = c;
ans[c] = --c;
ans[c] = cnt;
c--;
cnt++;
}
if (n%2 != 0) ans[c] = c;
for (int i = 1; i < ans.length; i++) {
out.print(ans[i]+" ");
}
out.close();
}else{
System.out.println(-1);
return;
}
}
private static String nextLine() throws IOException {
return bf.readLine().trim();
}
private static double nextDouble() throws IOException{
st.nextToken();
return st.nval;
}
private static long nextLong() throws IOException{
st.nextToken();
return (long) st.nval;
}
private static int nextInt() throws IOException {
st.nextToken();
return (int) st.nval;
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 6bf27cdfd343815b1ecc166db30846e8 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
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);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
if (n % 4 == 3 || n % 4 == 2) {
out.print(-1);
return;
}
int[] result = new int[n];
for (int i = 0; i < n / 2; i += 2) {
result[n - i - 1 - 1] = i + 1;
result[i] = i + 2;
result[i + 1] = n - i;
result[n - i - 1] = n - 1 - i;
}
if (n % 4 == 1) {
result[n / 2] = n / 2 + 1;
}
for (int i = 0; i < n; i++) {
out.print(result[i] + " ");
}
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | aa77763ec9543ede99f593a12cf27186 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author George Marcus
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
int[] A;
int N;
public void solve(int testNumber, InputReader in, PrintWriter out) {
N = in.nextInt();
if(N % 4 > 1) {
out.println(-1);
return;
}
LinkedList<Integer> left = new LinkedList<Integer>();
for(int i = 1; i <= N; i++)
left.addLast(i);
A = new int[N + 1];
if(N % 2 == 1)
A[N / 2 + 1] = N / 2 + 1;
boolean odd = N % 2 == 1;
for(int i = 1; i <= N; i++) {
if(odd && i == N / 2 + 1)
continue;
if(A[i] == 0) {
int other = N - i + 1;
int k;
for(int j = 0; ; j++) {
k = left.get(j);
if(A[k] != 0) {
left.remove(j);
j--;
continue;
}
if(k != i && k != other) {
left.remove(j);
break;
}
}
A[i] = k;
A[k] = other;
go(k);
}
}
for(int i = 1; i <= N; i++)
out.print(A[i] + " ");
}
private void go(int k) {
if(A[A[k]] == 0) {
A[A[k]] = N - k + 1;
go(A[k]);
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
return Integer.parseInt(nextString());
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 662e355f07cc78531555b479ad8f0a0d | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.util.*;
public class Main {
public static long calc(long k, long n) {
long l = 2, r = k + 1, mid, tmp;
while ( l < r ) {
mid = (l + r) / 2;
tmp = (k - mid + 1) * (k + mid) / 2 - (k - mid);
if ( tmp <= n ) r = mid;
else l = mid + 1;
}
return l;
}
public static void main(String[] strings) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.close();
if ( ((n % 4) > 1) ) {
System.out.println("-1");
return;
}
int i, n2 = n / 2, gap;
int[] p = new int[100005];
for (gap = n-1, i = 1; i <= n2; i += 2, gap -= 4) {
p[i] = gap - 1 + i;
p[i+1] = i;
p[gap+i-1] = gap + i;
p[gap+i] = 1 + i;
}
if ( (n % 4) == 1 ) p[n2+1] = n2 + 1;
for ( i = 1; i <= n; ++i ) {
System.out.print(p[i] + " ");
}
System.out.print("\r\n");
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 9ed8fc8cae9b91986053b00e787d7581 | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.util.Locale;
import java.util.Scanner;
public class HappyPermutationSolver {
private int n;
public static void main(String[] args) {
HappyPermutationSolver solver = new HappyPermutationSolver();
solver.readData();
int[] solution = solver.solve();
solver.print(solution);
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
private int lcm(int a, int b) {
return a * b / gcd(a, b);
}
private void print(int[] values) {
if (values.length > 0) {
StringBuilder builder = new StringBuilder();
for (int value : values) {
builder.append(value);
builder.append(" ");
}
print(builder);
} else {
print(-1);
}
}
private void print(Object value) {
System.out.println(value);
}
private void print(boolean value) {
System.out.println(value ? "YES" : "NO");
}
private void print(int value) {
System.out.println(value);
}
private void print(long value) {
System.out.println(value);
}
private void print(double value) {
System.out.printf(Locale.ENGLISH, "%.10f", value);
}
private int[] getDigits(int number) {
int[] digits = new int[10];
int index = digits.length - 1;
int digitsCount = 0;
while (number > 0) {
digits[index] = number % 10;
number /= 10;
index--;
digitsCount++;
}
int[] result = new int[digitsCount];
System.arraycopy(digits, digits.length - digitsCount, result, 0, digitsCount);
return result;
}
private int[] readArray(Scanner scanner, int size) {
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = scanner.nextInt();
}
return result;
}
private void readData() {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
}
private int[] solve() {
int[] result;
int x = n & 3;
if (x == 0 || x == 1) {
result = new int[n];
if ((n & 1) == 1) {
result[n >> 1] = (n >> 1) + 1;
}
for (int i = 1; i < (n >> 1); i += 2) {
result[i - 1] = i + 1;
result[i] = n - i + 1;
result[n - i] = n - i;
result[n - i - 1] = i;
}
}
else {
result = new int[0];
}
return result;
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | 8e8b1dabe02dd99f4ac3cdcd7b41b7aa | train_003.jsonl | 1364025600 | A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class LuckyPermutation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
int[] numeros = new int[n + 1];
Arrays.fill(numeros, 0);
if (n%4 > 1){
System.out.println(-1);
return;
}
int actual = 1;
int cantidad = 0;
if (n % 2 == 1) {
numeros[n / 2 + 1] = n / 2 + 1;
cantidad++;
}else if(n == 1){
System.out.println(1);
return;
}
while (cantidad < n) {
while (numeros[actual] != 0) {
actual++;
}
int c = actual;
actual++;
while (numeros[actual] != 0) {
actual++;
}
int r = actual;
numeros[c] = r;
cantidad++;
while (numeros[r] == 0) {
numeros[r] = n - c + 1;
c = r;
r = numeros[r];
cantidad++;
}
}
for (int i = 1; i < n + 1; i++) {
System.out.print(numeros[i]);
if (i != n)
System.out.print(" ");
}
System.out.println();
}
}
| Java | ["1", "2", "4", "5"] | 2 seconds | ["1", "-1", "2 4 1 3", "2 5 3 1 4"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"greedy",
"math"
] | dfca19b36c1d682ee83224a317e495e9 | The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size. | 1,400 | Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them. | standard output | |
PASSED | fdc4c2464fc2537577a68e9695bad160 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.util.Scanner;
import java.util.Arrays;
public class Main{
static long ll(String x){
return Long.parseLong(x);
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
long k = ll(s.nextLine());
String x = s.nextLine();
String n[] = x.split("");
long suma=0;
for(int i=0;i<n.length;i++) suma+=ll(n[i]);
if (suma>k)
System.out.println(0);
else{
long dif = k-suma;
int id=0;
Arrays.sort(n);
//System.out.println(Arrays.toString(n));
for (int i =0;i<n.length;i++) {
long d = 9-ll(n[i]);
//System.out.println("dif = " + dif);
//System.out.println("d = " + d);
if (dif<=0){
break;
}
else{
if (dif>=d) {
dif-=d;
n[i]=Long.toString(ll(n[i])+d);
id++;
}
else{
d-=dif-1;
dif-=d;
n[i]=Long.toString(ll(n[i])+d);
id++;
break;
}
}
}
System.out.println(id);
}
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | db4d7ad88ae177a06ccdef83c9ac8f27 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.util.*;
public class NumBoard{
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
long num1 = in.nextLong();
String num2 = String.valueOf(in.next());
int array[] = new int [num2.length()];
boolean verify = false;
long suma = 0;
for (int i = 0; i<num2.length() ;i++){
int actual = Integer.parseInt(String.valueOf(num2.charAt(i)));
array[i] = actual;
suma+= actual;
if (suma>=num1){
System.out.println("0");
verify = true;
break;
}
}
if(!verify){
Arrays.sort(array);
int counter = 0;
for (int i = 0; i<num2.length(); i++){
int actual = 9-array[i];
suma = suma+actual;
counter++;
if (suma>=num1){
System.out.println(counter);
break;
}
}
}
}
} | Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 47153a5cff8617a204f29f357fc2010a | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.*;
public class BCF427B {
public static void main(String[]args)throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int k=Integer.parseInt(br.readLine());
char cc[]=br.readLine().toCharArray();
int a=0;
long arr[]=new long[10];
Arrays.fill(arr, 0);
for(int i=0;i<cc.length;i++)
{
int no=cc[i]-'0';
a+=no;
arr[no]++;
}
if(a>=k)
System.out.println(0);
else
{
Arrays.sort(cc);
int i=0;
int res=0;
while(a<k)
{
int no=9-(cc[i]-'0');
a+=no;
res++;
i++;
}
System.out.println(res);
}
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 57d5b10c7c49cbd087819006fca2dc63 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Board {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
char[] p = in.readLine().toCharArray();
int c = 0;
int[] pp = new int[10];
for(int i = 0;i<p.length;i++) {
int y = (int)(p[i] - '0');
c += y;
pp[y]++;
}
if(c >= n) {
System.out.println(0);
} else {
int u = n - c;
int cc = 0;
for(int i = 0;i<9&&u>0;i++) {
int k = 9 - i;
int j = (int) Math.ceil(u / (double) k);
if(j <= pp[i]) {
u -= k*j;
cc += j;
} else {
u -= k*pp[i];
cc += pp[i];
}
}
System.out.println(cc);
}
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | f26592c04c7fbdc6026ccc3120ff553f | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Board {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
char[] p = in.readLine().toCharArray();
int c = 0;
int[] pp = new int[p.length];
for(int i = 0;i<p.length;i++) {
int y = (int)(p[i] - '0');
c += y;
pp[i] = y;
}
Arrays.sort(pp);
if(c >= n) {
System.out.println(0);
} else {
int cc = 0;
int l = 0;
while(c < n) {
int u = (9 - pp[l++]);
if(u > 0) {
c += u;
cc++;
}
}
System.out.println(cc);
}
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 9f7527bbf3a992b5fcf8c40913cf7d21 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import static java.util.Collections.sort;
/**
* Created by nikitos on 11.08.17.
*/
public class B {
public static int K;
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader ( new InputStreamReader(System.in));
int k = Integer.parseInt(buf.readLine());
K = k;
String num = buf.readLine();
if (sum(num) >= k) {
System.out.println(0);
} else {
System.out.println(ans(num));
}
}
public static int sum(String num) {
int summa = 0;
for (int i = 0; i < num.length(); i++) {
int ch = Integer.parseInt(num.charAt(i)+"");
summa += ch;
}
return summa;
}
public static int ans(String num) {
int summa = 0;
int i;
int k = 0;
boolean f = false;
ArrayList<Integer> aList = new ArrayList<Integer>();
for (i = 0; i < num.length(); i++) {
int ch = Integer.parseInt(num.charAt(i)+"");
aList.add(ch);
summa += ch;
}
sort(aList);
for (i = 0; i < aList.size(); i++) {
if (!f) {
if (9 + summa - aList.get(i) >= K) {
f = true;
}
summa = 9 + summa - aList.get(i);
k++;
}
}
return k;
}
} | Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 87bd88e431e50a300742e0421e09491a | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
import java.util.Arrays;
/**
*
* @author diegomorenoacevedo
*/
public class NumberBoard {
public static void main(String[]args){
long k;
String n;
int cont=0;
Scanner lector = new Scanner (System.in);
k=lector.nextLong();
n=lector.next();
int t=0;
int [] array = new int[n.length()];
boolean flag=false;
for (int i = 0; i < n.length(); i++) {
int a = Integer.parseInt(String.valueOf(n.charAt(i)));
array[i]=a;
t+=a;
if(t>=k){
System.out.println(0);
flag=true;
break;
}
}
if(flag==false){
Arrays.sort(array);
for (int i = 0; i < n.length(); i++) {
t=t+9-array[i];
cont++;
if(t>=k){
System.out.print(cont);
break;
}
}
}
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 309c738beccc8e1a2da79ac18a343f0a | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class B {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static FastReader scn = new FastReader();
public static void main(String[] args) {
int k = scn.nextInt();
// int n = scn.nextInt();
String n = scn.next();
int sum = 0;
int min = Integer.MAX_VALUE;
PriorityQueue<Integer> heap = new PriorityQueue<>();
int i =0;
while(i<n.length()){
int a = n.charAt(i)-48;
heap.add(a);
sum += a;
if(a<min){
min = a;
}
if(sum>=k){
System.out.println(0);
return;
}
i++;
}
int count = 0;
while(sum<k){
sum = sum+(9-heap.poll());
count++;
}
System.out.println(count);
}
} | Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | e4774181dfb884f0c377886c47b316a5 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.util.Scanner;
import java.util.Arrays;
public class Codeforces {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int k = Integer.parseInt(sc.nextLine());
String n = sc.nextLine();
char cc[] = n.toCharArray();
Arrays.sort(cc);
int sum = 0;
for(char c: cc)
sum += Character.getNumericValue(c);
if(sum >= k) {
System.out.println(0);
return;
}
int razn = (k - sum);
int res = 0;
for(int i = 0; i < n.length(); i++) {
int real = 9 - Character.getNumericValue(cc[i]);
if(razn < real) {
res++;
break;
}
res++;
razn -= real;
if(razn == 0) break;
}
System.out.println(res);
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 3bc7de65dea80113740f8dcb5ab73a56 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.util.*;
public class NumberOnTheBoard{
public static void main(String[] args) {
long total = 0;
Scanner sc = new Scanner(System.in);
long ob = sc.nextLong();
boolean is0 = false;
String number = String.valueOf(sc.next());
int numArray[] = new int [number.length()];
for (int i = 0; i< number.length();i++){
int current = Integer.parseInt(String.valueOf(number.charAt(i)));
numArray[i] = current;
total+= current;
if (total>=ob){
System.out.println("0");
is0 = true;
break;
}
}
if(!is0){
Arrays.sort(numArray);
int counter = 0;
for (int i = 0; i< number.length();i++){
int current = 9-numArray[i];
total +=current;
counter++;
if (total>=ob){
System.out.println(counter);
break;
}
}
}
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 240a7a4335c1d7fb01bba9d4eca5360d | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | /* package whatever; // don't place package name! */
import java.util.Scanner;
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
String n=sc.next();
sc.close();
int sum=0;
Vector<Integer> v=new Vector<Integer>();
for (int i=0;i<n.length();i++)
{
sum=sum+Integer.parseInt(n.substring(i,i+1));
v.add(Integer.parseInt(n.substring(i,i+1)));
}
if (sum>=s)
{
System.out.println(0);
}
else
{
Collections.sort(v);
int p=0;
Iterator <Integer> it = v. iterator();
while (sum<s && it.hasNext())
{
sum=sum-it.next()+9;
p++;
}
System.out.println(p);
}
// your code goes here
}
} | Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 6c991eb19f9fdb35abc4401d3ea3ec50 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | //package semanai;
import java.util.Arrays;
import java.util.Scanner;
public class BoardNumber {
public static void bn(){
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
String s = sc.next();
long suma = 0;
int[] n = new int[s.length()];
for(int i=0; i<n.length; i++){
n[i] = Integer.parseInt(s.charAt(i)+"");
suma += Integer.parseInt(s.charAt(i)+"");
}
if(suma >= k) {
System.out.println(0);
}
else{
Arrays.sort(n);
for(int i=0; i<n.length; i++){
//System.out.print(n[i]+" ");
suma -= n[i];
suma += 9;
if(suma >= k) {
System.out.println(i+1);
return;
}
}
}
}
public static void main(String[] args) {
bn();
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 2c92a1c897d713e79c0215f3c6badd46 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int p = s.nextInt();
String n = s.next();
long x = 0;
int l = n.length(), i, cnt = 0;
int[] freq = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int j = 0; j < l; j ++) {
int v = Character.getNumericValue(n.charAt(j));
x += v;
freq[v] ++;
}
while(x < p){
i = -1;
while (freq[++ i] == 0) ;
x += 9 - i;
freq[i] --;
cnt ++;
}
System.out.println(cnt);
}
} | Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 0d28d623d697605999e7c49a1b953251 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.util.*;
import java.io.*;
public class Sample implements Runnable{
// static ArrayList<Integer>[] al;
static class Pair implements Comparable<Pair>{
long a;
long b;
Pair(){
}
Pair(long a,long b){
this.a=a;
this.b=b;
}
public int compareTo(Pair x)
{
return Long.compare(x.a,this.a);
}
}
static class Beauty implements Comparator<Beauty>{
int beauty;
int cost;
Beauty(){
}
Beauty(int beauty,int cost){
this.beauty=beauty;
this.cost=cost;
}
public int compare(Beauty b1,Beauty b2 ){
if(b2.beauty-b1.beauty==0){
return b1.cost-b2.cost;
}else{
return b2.beauty-b1.beauty;
}
}
}
static InputReader sc = new InputReader();
static int gcd(int a,int b){
if(a==0){
return b;
}
return gcd(b%a,a);
}
static int d;
static int[] color;
static boolean[] v;
/* static void dfs(int src,int x,int y){
v[src]=true;
int i=1;
color[src]=x;
for(int a :al[src]){
if(v[a]) continue;
if(i==x|| i==y){
i++;
if(i==x|| i==y){
i++;
}
}
color[a]=i;
i++;
}
for(int a:al[src]){
if(!v[a]){
dfs(a,color[a],x);
}
}
}*/
static long[] a;
static long[] b;
public static boolean possible(double mid,int n){
double l=a[1]-b[1]*mid;
double r=a[1]+b[1]*mid;
for(int i=2;i<=n;i++){
l=Math.max(l,a[i]-b[i]*mid);
r=Math.min(r,a[i]+b[i]*mid);
}
if(l>r)return false;
else return true;
}
public static boolean isvowel(char ch){
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){
return true;
}else{
return false;
}
}
static long[] fac;
static long[] inv;
static long mod=(long)1e9+7;
static int[] prime;
static Integer[] ans;
static ArrayList<Integer>[] al1;
static ArrayList<Integer>[] al2;
static int[] lol1;
static int[] lol2;
static boolean[] v1;
static boolean[] v2;
public static void main(String[] args) throws IOException
{
new Thread(null, new Sample(), "whatever", 1<<26).start();
}
boolean findSum(int set[], int n, long sum)
{
// Base Cases
if (sum == 0)
return true;
if (n == 0 && sum != 0)
return false;
// If last element is greater than sum, then ignore it
if (set[n-1] > sum)
return findSum(set, n-1, sum);
/* else, check if sum can be obtained by any of the following
(a) including the last element
(b) excluding the last element */
return findSum(set, n-1, sum) ||
findSum(set, n-1, sum-set[n-1]);
}
public class IntegerComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
}
static int[] pro_a;
static ArrayList pro_b[];
static ArrayList<Integer> seg[];
public static void build(int low,int high,int pos){
if(low>high){ return ;}
if(low==high){
seg[pos]=pro_b[low];
return;
}
int mid=(low+high)>>1;
build(low,mid,2*pos+1);
build(mid+1,high,2*pos+2);
seg[pos]=merge(seg[2*pos+1],seg[2*pos+2]);
}
public static ArrayList merge(ArrayList<Integer> a1,ArrayList<Integer> a2){
ArrayList<Integer> result=new ArrayList<Integer>();
int i=0;
int j=0;
while(i<a1.size() && j<a2.size()){
if((a1.get(i))<(a2.get(j))){
result.add(a1.get(i));
i++;
}
else if(a1.get(i)>a2.get(j)){
result.add(a2.get(j));
j++;
}
else{
result.add(a1.get(i));
result.add(a2.get(j));
i++;j++;
}
}
while(i<a1.size()){
result.add(a1.get(i));
i++;
}
while(j<a2.size()){
result.add(a2.get(j));
j++;
}
return result;
}
public int find(int low,int high,int pos,int l,int r,int x,int y){
if(l<=low && high<=r){
int find_x=lower_bound(seg[pos],x);
int find_y=upper_bound(seg[pos],y);
// System.out.println(find_x+" "+find_y);
return (find_y-find_x);
}
if(l>high || r<low){
return 0;
}
int mid=(low+high)>>1;
return find(low,mid,2*pos+1,l,r,x,y)+find(mid+1,high,2*pos+2,l,r,x,y);
}
int lower_bound(ArrayList<Integer> a, int key) {
int l = 0;
int h = a.size(); // Not n - 1
while (l < h) {
int mid = (l + h) / 2;
if (key <= a.get(mid)) {
h = mid;
} else {
l = mid + 1;
}
}
return l;
}
int upper_bound(ArrayList<Integer> a, int key) {
int l = 0;
int h = a.size(); // Not n - 1
if(key>=a.get(h-1))
return (h);
while (l < h) {
int mid = (l + h) / 2;
if (key >= a.get(mid)) {
l = mid + 1;
} else {
h = mid;
}
}
return l;
}
static ArrayList[] dfs_array;
static boolean[] visited;
static int count=0;
static double ans22=0;
public void run(){
long n=l();
String k=s();
long sum=0;
long[] a=new long[(""+k).length()];
int i=0;
while(i<a.length){
a[i]=Integer.parseInt(""+k.charAt(i));
sum+=a[i];
i++;
}
Arrays.sort(a);
if(sum>=n){System.out.println("0");}
else{
long start=n-sum;
int count=0;
for(int t=0;t<a.length;t++){
start-=(9-a[t]);
count++;
if(start<=0)break;
}
System.out.println(count);
}
}
public static int lower_bound_array(int[] arr, long key) {
int len = arr.length;
int lo = 0;
int hi = len-1;
int mid = (lo + hi)/2;
while (true) {
int cmp = Long.compare(arr[mid],(key));
if (cmp < 0) {
lo = mid+1;
if (hi < lo)
return mid<len-1?mid+1:-1;
} else {
hi = mid-1;
if (hi < lo)
return mid;
}
mid = (lo + hi)/2;
}
}
/////////////////////////////////////////////////////////// shortcuts //////////////////////////////
public static long modPow(long base, long exp, long mod) {
base = base % mod;
long result = 1;
while (exp > 0) {
if (exp % 2 == 1) {
result = (result * base) % mod;
exp--;
} else {
base = (base * base) % mod;
exp = exp >> 1;
}
}
return result;
}
public static void cal() {
fac = new long[1000005];
inv = new long[1000005];
fac[0] = 1;
inv[0] = 1;
for (int i = 1; i <= 1000000; i++) {
fac[i] = (fac[i - 1] * i) % mod;
inv[i] = (inv[i - 1] * modPow(i, mod - 2, mod)) % mod;
}
}
/* public static long ncr(int n, int r) {
return (((fac[n] * inv[r]) % mod) * inv[n - r]) % mod;
}
*/
static int i() {
return sc.nextInt();
}
static long l(){
return sc.nextLong();
}
static double d() {
return sc.nextDouble();
}
static String s(){
return sc.next();
}
static String sL() throws IOException{
return sc.nextLine();
}
static void loop(int[] a) throws IOException{
for(int j=0;j<a.length;j++){
a[j]=i();
}
}
static void eloop(int[] a) throws IOException{
for(int i=1;i<=a.length;i++){
a[i]=i();
}
}
static void ploop(int[] a)throws IOException{
for(int j=0;j<a.length;j++){
System.out.println(a[j]);
}
}
static void eploop(int[] a)throws IOException{
for(int j=1;j<=a.length;j++){
System.out.println(a[j]);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
throw new RuntimeException();
}
}
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) {
throw new RuntimeException();
}
return str;
}
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | f6db68db64767b5f7e1618d8ff23be7f | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Solution {
public static void main (String[] args) throws Exception {
String k, n;
BigInteger count = new BigInteger("0");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
k = br.readLine();
n = br.readLine();
int sum = 0;
char[] char_array = new char[n.length()];
char_array = n.toCharArray();
Arrays.sort(char_array);
int[] arr = new int[10];
Arrays.fill(arr, 0);
for (int i = 0; i < n.length(); i++) {
sum += (char_array[i] - '0');
arr[char_array[i] - '0']++;
}
//System.out.println("Sum = " + sum);
int diff = Integer.parseInt(k) - sum;
// System.out.println("diff = " + diff);
if (diff <= 0) {
// System.out.println("Inside if");
System.out.println("0");
System.exit(0);
}
int current = 0;
while (diff > 0) {
int number = 9 - current;
if (arr[current] * number > diff) {
if (diff % number == 0) {
count = count.add(BigInteger.valueOf(diff / number));
diff -= arr[current] * number;
}
else {
count = count.add(BigInteger.valueOf(diff / number + 1));
diff -= arr[current] * number;
}
}
else {
count = count.add(BigInteger.valueOf(arr[current]));
diff -= (arr[current] * number);
}
current++;
}
System.out.println(count);
}
} | Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 7bed9309b95996239a2da62b5171ed1e | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes |
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main{
InputStream is;
PrintWriter out;
String INPUT = "./data/judge/201707/C427B.txt";
void solve() {
long k = nl();
String n = ns();
long sum = 0;
for (char c : n.toCharArray()){
sum += (c - '0');
}
if (sum >= k) out.println(0);
else{
int cnt = 0;
char[] nn = n.toCharArray();
int[] nums = new int[nn.length];
for (int i = 0; i < nn.length; ++i){
nums[i] = 9 - (nn[i] - '0');
}
Arrays.sort(nums);
long rem = k - sum;
for (int i = nn.length - 1; i >= 0; --i){
long min = Math.min(rem, nums[i]);
cnt ++;
rem -= min;
if (rem == 0) break;
}
out.println(cnt);
}
}
void run() throws Exception {
is = oj ? System.in : new FileInputStream(new File(INPUT));
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (!oj)
System.out.println(Arrays.deepToString(o));
}
} | Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 2419355002f026a5dd895a747d0df1a4 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.util.Scanner;
public class ThenumberonTheboard1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
long k = input.nextInt();
String s = input.next();
int[] arr = new int[10];
long total=0;
for(int i=0; i<s.length();i++){
total+=(s.charAt(i)-'0');
arr[(int)(s.charAt(i)-'0')]++;
}
long z = 0;
//System.out.println(total + " " + k);
//for(int i=0; i<10;i++)System.out.println(arr[i]);
if(k<=total)System.out.println(0);
else{
total = k-total;
if(arr[0]*9>=total){
if(total%9==0)System.out.println(total/9);
else {
System.out.println(total/9+1);
}
return;
}
total-=arr[0]*9;
z+=arr[0];
// System.out.println(total + " " + z);
if(arr[1]*8>=total){
if(total%8==0)System.out.println(z+total/8);
else{
System.out.println(z+total/8+1);
}
return;
}
//System.out.println(total + " " + k);
total-=arr[1]*8;
z+=arr[1];
if(arr[2]*7>=total){
if(total%7==0)System.out.println(z+total/7);
else{
System.out.println(total/7+1+z);
}
return;
}
total-=arr[2]*7;
z+=arr[2];
//System.out.println(total + " " + k);
if(arr[3]*6 >=total){
if(total%6==0)System.out.println(z+total/6);
else
{
System.out.println(total/6+1+z);
}
return;
}
z+=arr[3];
total-=arr[3]*6;
//System.out.println(total + " " + k);
if(arr[4]*5 >=total){
if(total%5==0)System.out.println(z+total/5);
else{
System.out.println(z+total/5+1);
}
return;
}
total-=arr[4]*5;
z+=arr[4];
//System.out.println(total + " " + k);
if(arr[5]*4 >=total){
if(total%4==0)System.out.println(z+total/4);
else{
System.out.println(total/4+1+z);
}
return;
}
total-=arr[5]*4;
z+=arr[5];
//System.out.println(total + " " + k);
if(arr[6]*3 >=total){
if(total%3==0)System.out.println(total/3+z);
else{
System.out.println(total/3+1+z);
}
return;
}
total-=arr[6]*3;
z+=arr[6];
//System.out.println(total + " " + k);
if(arr[7]*2 >=total){
if(total%2==0)System.out.println(total/2+z);
else{
System.out.println(total/2+1+z);
}
return;
}
total-=arr[7]*2;
z+=arr[7];
// System.out.println(total + " " + k);
if(arr[8]*1 >=total){
System.out.println(total+z);
return;
}
else{
System.out.println("Humse na ho paega");
}
}
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 0792e818f4c908b0c0b1c0efcc7f376f | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int k=in.nextInt();
char[] arr=in.readLine().toCharArray();
int [] current=new int[10];
int sum=0;
for (char digit : arr) {
sum += digit - '0';
}
for (char digit : arr) {
current[digit - '0']++;
}
int answer=0;
for(int i=0;i<9;++i)
{
while(sum<k && current[i]>0)
{
sum+=9-i;
--current[i];
++answer;
}
}
out.print(answer);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public 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;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void print(int i) {
writer.print(i);
}
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | e57743dceee68226b8d93a289b20576d | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Josè Manuel Garcìa
*/
public class TheNumberOnTheBoard {
/**
* @param args the command line arguments
*/
public static long suma(char []arr)
{
long s=0;
for (int i = 0; i < arr.length; i++) {
s+=(long)arr[i]-48;
}
return s;
}
public static boolean add(char[] digitos, int c){
if(digitos[c]!=57){
digitos[c]=57;
return true;
}
return false;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner leer= new Scanner(System.in);
long k= leer.nextLong();
String n=leer.next();
int c=0,h=0,i;
char[] digitos= new char[n.length()];
digitos=n.toCharArray();
Arrays.sort(digitos);
/*while(suma(digitos)<=k && c<digitos.length){
if(add(digitos, c))h++;
c++;
} */
long suma=suma(digitos);
for ( i = 0; i < digitos.length; i++) {
if(suma>=k)
{
break;
}
suma+=57-digitos[i];
}
System.out.println(i);
}
}
| Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 6d70dafef9bdfaa9159fb6be9db884be | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.util.*;
public class number {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
long k = Long.parseLong(sc.nextLine());
String n = sc.nextLine();
int[] after = new int[n.length()];
for(int i = 0; i < after.length; i++){
after[i] = Integer.parseInt(n.charAt(i)+"");
}
Arrays.sort(after);
long sum = 0;
int answer = 0;
for(int i = 0; i < after.length; i++){
sum += after[i];
}
for(int i = 0; i < after.length; i++){
if(sum < k){
sum += (9-after[i]);
answer++;
}
}
System.out.println(answer);
}
} | Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | cd99c1986827424034d9108e43ae6ed7 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
public class Main {
/**
*
* Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive.
* Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours milk from one bucket to another until the second bucket is filled or the first bucket is empty.
* Once begun, a pour must be completed, of course. Being thrifty, no milk may be tossed out.
Write a program to help FJ determine what amounts of milk he can leave in bucket C when he begins with three buckets as above, pours milk among the buckets for a while,
and then notes that bucket A is empty.
PROGRAM NAME: milk3
INPUT FORMAT
A single line with the three integers A, B, and C.
SAMPLE INPUT (file milk3.in)
8 9 10
OUTPUT FORMAT
A single line with a sorted list of all the possible amounts of milk that can be in bucket C when bucket A is empty.
SAMPLE OUTPUT (file milk3.out)
1 2 8 9 10
SAMPLE INPUT (file milk3.in)
2 5 10
SAMPLE OUTPUT (file milk3.out)
5 6 7 8 9 10
*/
/**
*
* Some natural number was written on the board. Its sum of digits was not less than k.
* But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.
You have to find the minimum number of digits in which these two numbers can differ.
Input
The first line contains integer k (1 ≤ k ≤ 109).
The second line contains integer n (1 ≤ n < 10100000).
There are no leading zeros in n. It's guaranteed that this situation is possible.
Output
Print the minimum number of digits in which the initial number and n can differ.
Examples
input
3
11
output
1
input
3
99
output
0
Note
In the first example, the initial number could be 12.
In the second example the sum of the digits of n is not less than k. The initial number could be equal to n.
*/
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static int[] readIntArray(int size) throws IOException {
int[] result = new int[(int) size];
for (int i = 0; i < size; i++) {
result[(int) i] = (int)nextInt();
}
return result;
}
static void solve() throws Exception {
int k = nextInt();
String n = nextLine();
char c[] = n.toCharArray();
int digits = 0;
long total = 0;
for (int i = 0; i < n.length(); i++) {
total += Character.getNumericValue(n.charAt(i));
}
int result = 0;
int index = 0;
Arrays.sort(c);
while(total < k) {
total = total - (long)(c[index] - '0');
total +=9;
result++;
index++;
}
System.out.println(result);
}
public static boolean isPalindrome(int[] num){
for(int i = 0 ; i < num.length/2 ; i++) {
if(num[i]!=num[num.length-(i+1)]) return false;
}
return true;
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String args[]) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
static int[] nextIntArray(int len, int start) throws IOException {
int[] a = new int[len];
for (int i = start; i < len; i++)
a[i] = nextInt();
return a;
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static long[] nextLongArray(int len, int start) throws IOException {
long[] a = new long[len];
for (int i = start; i < len; i++)
a[i] = nextLong();
return a;
}
static double[] nextDoubleArray(int len, int start) throws IOException {
double[] a = new double[len];
for (int i = start; i < len; i++)
a[i] = nextDouble();
return a;
}
static BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
static String next() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static String nextLine() throws IOException {
tok = new StringTokenizer("");
return in.readLine();
}
static void shuffleArray(long[] array) {
Random random = new Random();
for (int i = array.length - 1; i > 0; i--) {
int index = random.nextInt(i + 1);
long temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}
static boolean hasNext() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
String s = in.readLine();
if (s == null) {
return false;
}
tok = new StringTokenizer(s);
}
return true;
}
} | Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 91aecd09e1426761c510b274073dc9f4 | train_003.jsonl | 1501511700 | Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ. | 256 megabytes | import java.util.*;
import java.io.*;
public class test
{
public static void main(String[] args)
{
int x,y;
FastReader scan=new FastReader();
int k=scan.nextInt();
String str=scan.next();
int len=str.length();
int[] arr=new int[10];
boolean fina;
int sum=0;
while(len>0)
{
arr[Character.getNumericValue(str.charAt(len-1))]++;
sum+=Character.getNumericValue(str.charAt(len-1));
len--;
}
x=0;
int count=0;
if(sum>=k)
{
System.out.println("0");
System.exit(0);
}
while(true)
{
if(arr[x]!=0)
{
sum=sum+arr[x]*(9-x);
count+=arr[x];
// System.out.println(x+" change to 9");
}
x++;
if(k==sum)
{
fina=true;
break;
}
else if(sum>k)
{
fina=false;
break;
}
}
// System.out.println(count+"count");
if(!fina)
{
x=x-1;
sum=sum-arr[x]*(9-x);
count=count-arr[x];
// System.out.println(count);
// System.out.println(x+"value of x");
// System.out.println(k+"k");
int p=0;
while(p<arr[x])
{
sum=sum+(9-x);
p++;
//System.out.println(x+"change to 9 n");
count++;
if(sum>=k)
break;
}
}
System.out.println(count);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n11", "3\n99"] | 2 seconds | ["1", "0"] | NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n. | Java 8 | standard input | [
"greedy"
] | d7e6e5042b8860bb2470d5a493b0adf6 | The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible. | 1,100 | Print the minimum number of digits in which the initial number and n can differ. | standard output | |
PASSED | 81500958f6aff12ee73fc38a5f1ffdb1 | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
int test=input.scanInt();
StringBuilder ans=new StringBuilder("");
for(int t=1;t<=test;t++) {
int n=input.scanInt();
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=input.scanInt();
}
solve(n,arr);
}
System.out.println(ans);
}
public static void solve(int n,int arr[]) {
int cnt=0,prev=-1;
while(true) {
int max=0,max_indx=-1;
for(int i=0;i<n;i++) {
if(i==prev) {
continue;
}
if(arr[i]>max) {
max=arr[i];
max_indx=i;
}
}
if(max_indx==-1) {
break;
}
arr[max_indx]--;
prev=max_indx;
cnt++;
}
if(cnt%2==0) {
System.out.println("HL");
}
else {
System.out.println("T");
}
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 2cceb38536002aa56298d578c7eff09d | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static long sx = 0, sy = 0, mod = (long) (1e9 + 7);
// static HashSet<Integer>[] a;
static Double[][][] dp;
static long[] fa;
static long[] farr;
public static PrintWriter out = new PrintWriter(System.out);
static ArrayList<pair> pa = new ArrayList<>();
static long[] fact = new long[(int) 1e6];
static StringBuilder sb = new StringBuilder();
static boolean cycle = false;
static long m = 998244353;
static long[] no, col;
static String s;
static long k = 0, n = 0, one = 0;
static int cnt;
// static long[] dp;
static long[] p;
static long[] ans;
static int[][] a = new int[2][2];
static int[][] b = new int[2][2];
static Double[] x;
static Double[] y;
public static void main(String[] args) throws IOException {
// Scanner scn = new Scanner(new BufferedReader(new
// InputStreamReader(System.in)));
Reader scn = new Reader();
int t = scn.nextInt();
z: while (t-- != 0) {
int n = scn.nextInt();
PriorityQueue<pair> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++)
pq.add(new pair(i, scn.nextInt()));
boolean b = true;
int prev = -1;
while (true) {
if (b) {
if (pq.size() == 0 || (pq.size() == 1 && pq.peek().i == prev)) {
System.out.println("HL");
break;
}
pair rp1 = pq.remove();
if (rp1.i == prev) {
pair rp2 = pq.remove();
pq.add(rp1);
rp1 = rp2;
}
if (rp1.val > 1)
pq.add(new pair(rp1.i, rp1.val - 1));
prev = rp1.i;
}
else {
if (pq.size() == 0 || (pq.size() == 1 && pq.peek().i == prev)) {
System.out.println("T");
break;
}
pair rp1 = pq.remove();
if (rp1.i == prev) {
pair rp2 = pq.remove();
pq.add(rp1);
rp1 = rp2;
}
if (rp1.val > 1)
pq.add(new pair(rp1.i, rp1.val - 1));
prev = rp1.i;
}
b = !b;
}
}
}
// _________________________TEMPLATE_____________________________________________________________
// public static long lcm(long x, long y) {
//
// return (x * y) / gcd(x, y);
// }
//
// private static long gcd(long x, long y) {
// if (x == 0)
// return y;
//
// return gcd(y % x, x);
// }
//
// static class comp implements Comparator<Integer> {
//
// @Override
// public int compare(Integer p1, Integer p2) {
//
// return p2 - p1;
//
// }
// }
//
// }
//
// public static long pow(long a, long b) {
//
// if (b < 0)
// return 0;
// if (b == 0 || b == 1)
// return (long) Math.pow(a, b);
//
// if (b % 2 == 0) {
//
// long ret = pow(a, b / 2);
// ret = (ret % mod * ret % mod) % mod;
// return ret;
// }
//
// else {
// return ((pow(a, b - 1) % mod) * a % mod) % mod;
// }
// }
private static class pair implements Comparable<pair> {
int i, val;
pair(int a, int b) {
i = a;
val = b;
}
@Override
public int compareTo(pair o) {
return o.val - this.val;
}
// @Override
//
// public int hashCode() {
// return i;
// }
//
// @Override
//
// public boolean equals(Object o) {
//
// pair p = (pair) o;
// return this.i == p.i;
// }
}
private static String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
public static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[1000000 + 1]; // 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();
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public int[][] nextInt2DArray(int m, int n) throws IOException {
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
public long[][] nextInt2DArrayL(int m, int n) throws IOException {
long[][] arr = new long[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
// kickstart - Solution
// atcoder - Main
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 402ef83045f92e77adadce2c028562ce | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes |
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
public class Round659 {
static int rec = 0;
static int X[] = { -1, 0, 0, 1 };
static int Y[] = { 0, -1, 1, 0 };
static long mod = 1000000007;
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static long[] initArray(int n, Reader scan) throws IOException {
long arr[] = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = scan.nextLong();
}
return arr;
}
public static long sum(long arr[]) {
long sum = 0;
for (long i : arr) {
sum += (long) i;
}
return sum;
}
public static long max(long arr[]) {
long max = Long.MIN_VALUE;
for (long i : arr) {
max = Math.max(i, max);
}
return max;
}
public static long min(long arr[]) {
long min = Long.MAX_VALUE;
for (long i : arr) {
min = Math.min(i, min);
}
return min;
}
public static List<Integer>[] initAdjacency(int n, int e, Reader scan, boolean type) throws IOException {
List<Integer> adj[] = new ArrayList[n + 1];
for (int i = 0; i < e; i++) {
int u = scan.nextInt();
int v = scan.nextInt();
if (adj[u] == null)
adj[u] = new ArrayList<>();
if (type && adj[v] == null)
adj[v] = new ArrayList<>();
adj[u].add(v);
if (type)
adj[v].add(u);
}
return adj;
}
public static void main(String[] args) throws IOException {
Reader scan = new Reader();
// Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while (t-- > 0) {
int n = scan.nextInt();
long arr[]=initArray(n, scan);
D(n, arr);
}
}
public static void D(int n, long arr[]) {
PriorityQueue<Long> pq=new PriorityQueue<>(Collections.reverseOrder());
long last=0;
for(long val: arr) {
pq.add(val);
}
int cur=0;
while(!pq.isEmpty()) {
long val= pq.remove();
if(val==0) break;
val--;
pq.add(last);
last=val;
cur=1-cur;
}
if(cur==0) {
System.out.println("HL");
}else {
System.out.println("T");
}
}
public static void C(int n, long arr[]) {
if(n==1) {
System.out.println("1 1");
System.out.println(-arr[0]);
System.out.println("1 1");
System.out.println(0);
System.out.println("1 1");
System.out.println(0);
return;
}
System.out.println(1+" "+(n-1));
for(int i=0;i<n-1;i++) {
System.out.print((arr[i]*(n-1))+" ");
}
System.out.println();
System.out.println(n+" "+n);
System.out.println(-arr[n-1]);
System.out.println(1+" "+(n));
for(int i=0;i<n;i++) {
if(i==n-1) {
System.out.print(0+" ");
}else {
System.out.print(-(arr[i]*n)+" ");
}
}
System.out.println();
}
public static void B(int n, long arr[]) {
Arrays.sort(arr);
double upperVal= Math.pow(10, 12/(n-1));
long ans=Long.MAX_VALUE;
for(long i=1;i<=(long)Math.ceil(upperVal);i++) {
long curAns=0;
for(int j=0;j<n;j++) {
curAns+= (long)Math.abs(Math.pow(i, j)- arr[j]);
}
ans=Math.min(ans, curAns);
}
System.out.println(ans);
return;
//
// if(n<=32) {
//
//
//
//
//
//
//
// }else {
//
//
// long ans=0;
//
// for(long val: arr) {
//
// ans+= Math.abs(val-1);
// }
//
// System.out.println(ans);
//
// }
}
public static void A(int n,String arr[]) {
int count[]=new int[26];
for(String s: arr) {
for(int i=0;i<s.length();i++) {
count[s.charAt(i)-'a']++;
}
}
for(Integer x: count) {
if(x%n!=0) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
}
class MyPair {
int value;
int weight;
public MyPair(int value, int w) {
this.value = value;
weight = w;
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 09cffba17ae2cbfe1bdf778625a158bb | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Collections;
import java.util.StringTokenizer;
public class Codeforces1397D {
public static void main(String[] args) {
FastScanner sc=new FastScanner();
int T=sc.nextInt();
for (int tt=0; tt<T; tt++) {
int n = sc.nextInt();
int a[] = sc.readArray(n);
Arrays.sort(a);
int sum = 0 ;
for(int item : a) {
sum+= item;
}
if(a[n-1]*2 > sum) {
System.out.println("T");
}else {
if(sum % 2 == 0) {
System.out.println("HL");
}else System.out.println("T");
}
}
}
public void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
}
}
| Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 3737ef47d85cffbbd9c9b0d38e8a24d4 | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class D {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for(int i = 0; i < t; i++){
int n = scanner.nextInt();
int[] arr = new int[n];
int sum = 0;
for(int j = 0; j < n; j++){
arr[j] = scanner.nextInt();
sum += arr[j];
}
Arrays.sort(arr);
if(n==1){
System.out.println("T");
}else if(sum/2 < arr[n-1]){
System.out.println("T");
} else {
if (sum % 2 == 0) {
System.out.println("HL");
} else {
System.out.println("T");
}
}
}
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | e0e68cfafe840bb133642e569032fa34 | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static long sx = 0, sy = 0, mod = (long) (1e9 + 7);
// static HashSet<Integer>[] a;
static Double[][][] dp;
static long[] fa;
static long[] farr;
public static PrintWriter out = new PrintWriter(System.out);
static ArrayList<pair> pa = new ArrayList<>();
static long[] fact = new long[(int) 1e6];
static StringBuilder sb = new StringBuilder();
static boolean cycle = false;
static long m = 998244353;
static long[] no, col;
static String s;
static long k = 0, n = 0, one = 0;
static int cnt;
// static long[] dp;
static long[] p;
static long[] ans;
static int[][] a = new int[2][2];
static int[][] b = new int[2][2];
static Double[] x;
static Double[] y;
public static void main(String[] args) throws IOException {
// Scanner scn = new Scanner(new BufferedReader(new
// InputStreamReader(System.in)));
Reader scn = new Reader();
int t = scn.nextInt();
z: while (t-- != 0) {
int n = scn.nextInt();
PriorityQueue<pair> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++)
pq.add(new pair(i, scn.nextInt()));
boolean b = true;
int prev = -1;
while (true) {
if (b) {
if (pq.size() == 0 || (pq.size() == 1 && pq.peek().i == prev)) {
System.out.println("HL");
break;
}
pair rp1 = pq.remove();
if (rp1.i == prev) {
pair rp2 = pq.remove();
pq.add(rp1);
rp1 = rp2;
}
if (rp1.val > 1)
pq.add(new pair(rp1.i, rp1.val - 1));
prev = rp1.i;
}
else {
if (pq.size() == 0 || (pq.size() == 1 && pq.peek().i == prev)) {
System.out.println("T");
break;
}
pair rp1 = pq.remove();
if (rp1.i == prev) {
pair rp2 = pq.remove();
pq.add(rp1);
rp1 = rp2;
}
if (rp1.val > 1)
pq.add(new pair(rp1.i, rp1.val - 1));
prev = rp1.i;
}
b = !b;
}
}
}
// _________________________TEMPLATE_____________________________________________________________
// public static long lcm(long x, long y) {
//
// return (x * y) / gcd(x, y);
// }
//
// private static long gcd(long x, long y) {
// if (x == 0)
// return y;
//
// return gcd(y % x, x);
// }
//
// static class comp implements Comparator<Integer> {
//
// @Override
// public int compare(Integer p1, Integer p2) {
//
// return p2 - p1;
//
// }
// }
//
// }
//
// public static long pow(long a, long b) {
//
// if (b < 0)
// return 0;
// if (b == 0 || b == 1)
// return (long) Math.pow(a, b);
//
// if (b % 2 == 0) {
//
// long ret = pow(a, b / 2);
// ret = (ret % mod * ret % mod) % mod;
// return ret;
// }
//
// else {
// return ((pow(a, b - 1) % mod) * a % mod) % mod;
// }
// }
private static class pair implements Comparable<pair> {
int i, val;
pair(int a, int b) {
i = a;
val = b;
}
@Override
public int compareTo(pair o) {
return o.val - this.val;
}
// @Override
//
// public int hashCode() {
// return i;
// }
//
// @Override
//
// public boolean equals(Object o) {
//
// pair p = (pair) o;
// return this.i == p.i;
// }
}
private static String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
public static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[1000000 + 1]; // 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();
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public int[][] nextInt2DArray(int m, int n) throws IOException {
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
public long[][] nextInt2DArrayL(int m, int n) throws IOException {
long[][] arr = new long[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
// kickstart - Solution
// atcoder - Main
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 7673ee4f47a257534f73e5dc1bafc7ec | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | import java.io.*;
import java.util.Iterator;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.TreeSet;
public class Main {
static class Piles{
int idx, capacity;
int is_available;
public Piles(int idx, int capacity, int is_available) {
this.idx = idx;
this.capacity = capacity;
this.is_available = is_available;
}
public Piles() {
idx = 0;
capacity = 0;
is_available = 0;
}
}
static class comp implements Comparator<Piles>{
public int compare(Piles x, Piles y) {
if(x.is_available == y.is_available) {
if(y.capacity == x.capacity)
return y.idx - x.idx;
else return y.capacity - x.capacity;
}
else return (y.is_available - x.is_available);
}
}
public static void main(String[] args) throws Exception {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int t = in.readInt();
while(t-- > 0) {
int n = in.readInt();
int arr[] = new int[n];
TreeSet<Piles> My_set = new TreeSet<Piles>(new comp());
for(int i = 0; i < n; ++i) {
arr[i] = in.readInt();
Piles p = new Piles(i, arr[i], 1);
My_set.add(p);
}
boolean p1 = true, p2 = false;
boolean done = false;
while(My_set.size() > 0) {
boolean del = (My_set.size() > 1);
Iterator<Piles> itr = My_set.descendingIterator();
Piles last = new Piles();
boolean to_do = false;
if(del) {
last = itr.next();
to_do = (last.is_available == 0);
}
Piles first = My_set.first();
if(first.is_available == 0) {
if(p1) {
out.printLine("HL");
done = true;
break;
}
else if(p2) {
out.printLine("T");
done = true;
break;
}
}
My_set.remove(first);
first.is_available = 0;
first.capacity--;
if(first.capacity > 0) My_set.add(first);
if(p1) {
p2 = true;
p1 = false;
}
else {
p2 = false;
p1 = true;
}
if(to_do) {
My_set.remove(last);
last.is_available = 1;
My_set.add(last);
}
}
if(done) continue;
if(p1) out.printLine("HL");
else out.printLine("T");
}
out.close();
}
private static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double readDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
private static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.flush();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
writer.flush();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 6b69df4a588a5d11905e699b7acddb91 | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | import java.util.*;
import java.io.*;
public class D {
public static void main(String[] args) throws Exception {
int t = i();
for (int tt = 0; tt < t; tt++) {
int n = i();
PriorityQueue<P> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
pq.add(new P(i(), i));
}
int lastI = -1;
String ans = "?";
for (int turn = 0; turn < 100000; turn++) {
if (pq.isEmpty()) {
ans = turn % 2 == 0 ? "HL" : "T";
break;
}
P p = pq.remove();
if (p.s == lastI) {
if (pq.isEmpty()) {
ans = turn % 2 == 0 ? "HL" : "T";
break;
}
P p2 = pq.remove();
p2.f--;
lastI = p2.s;
if (p2.f != 0) {
pq.add(p2);
}
pq.add(p);
} else {
p.f--;
lastI = p.s;
if (p.f != 0) pq.add(p);
}
}
out.println(ans);
}
out.close();
}
static class P implements Comparable<P> {
public int f, s;
public P(int i, int j) {
f = i; s = j;
}
public int compareTo(P other) {
return f == other.f ? s - other.s : -(f - other.f);
}
public String toString() {
return "(" + f + ", " + s + ")";
}
}
static BufferedReader in;
static StringTokenizer st = new StringTokenizer("");
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static {
try {
in = new BufferedReader(new FileReader("test.in"));
} catch (Exception e) {
in = new BufferedReader(new InputStreamReader(System.in));
}
}
static void check() throws Exception {
while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine());
}
static int i() throws Exception {check(); return Integer.parseInt(st.nextToken());}
static String s() throws Exception {check(); return st.nextToken();}
static double d() throws Exception {check(); return Double.parseDouble(st.nextToken());}
static long l() throws Exception {check(); return Long.parseLong(st.nextToken());}
}
| Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 511e36efba7f6719b6d2600ffa3b5541 | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
public class D {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
outer: while (t-- > 0) {
int n = sc.nextInt();
int a[] = new int[n];
PriorityQueue<Integer> piles=new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
piles.add(a[i]);
}
String turn ="T";
int hl=1;
while(!piles.isEmpty())
{
int t1=piles.remove();
if(hl!=1)
piles.add(hl-1);
if(piles.isEmpty())
{
System.out.println("T");
continue outer;
}
hl=piles.remove();
turn="HL";
if(t1!=1)
piles.add(t1-1);
if(piles.isEmpty())
{
System.out.println("HL");
continue outer;
}
}
System.out.println(turn);
}
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 4adfb285a2a38c508a27e1f31a2ef579 | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main implements Runnable {
static boolean use_n_tests = true;
static int stack_size = 1 << 27;
static long mod = 1_000_000_007;
void solve(FastScanner in, PrintWriter out, int testNumber) {
int n = in.nextInt();
int[] a = new int[n];
int sum = 0;
int mx = 0;
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
sum += a[i];
mx = Math.max(a[i], mx);
}
if (mx > sum - mx || sum % 2 == 1) {
out.println("T");
} else {
out.println("HL");
}
}
// ****************************** template code ***********
static class Range {
int l, r;
int id;
public int getL() {
return l;
}
public int getR() {
return r;
}
public Range(int l, int r, int id) {
this.l = l;
this.r = r;
this.id = id;
}
}
static class Array {
static Range[] readRanges(int n, FastScanner in) {
Range[] result = new Range[n];
for (int i = 0; i < n; i++) {
result[i] = new Range(in.nextInt(), in.nextInt(), i);
}
return result;
}
static public Integer[] read(int n, FastScanner in) {
Integer[] out = new Integer[n];
for (int i = 0; i < out.length; i++) {
out[i] = in.nextInt();
}
return out;
}
static public int[] readint(int n, FastScanner in) {
int[] out = new int[n];
for (int i = 0; i < out.length; i++) {
out[i] = in.nextInt();
}
return out;
}
}
class Graph {
List<List<Integer>> create(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
return graph;
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream io) {
br = new BufferedReader(new InputStreamReader(io));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
void run_t_tests() {
int t = in.nextInt();
int i = 0;
while (t-- > 0) {
solve(in, out, i++);
}
}
void run_one() {
solve(in, out, -1);
}
@Override
public void run() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
if (use_n_tests) {
run_t_tests();
} else {
run_one();
}
out.close();
}
static FastScanner in;
static PrintWriter out;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(null, new Main(), "", stack_size);
thread.start();
thread.join();
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 3df65cca43042dce79cfaf95c5ad068b | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
ArrayList<int[]> a = new ArrayList<>();
for(int i=0; i<t; i++) {
int n = sc.nextInt();
int[] arr = new int[n];
for(int j=0; j<n; j++)
arr[j] = sc.nextInt();
a.add(arr);
}
for(int i=0; i<t; i++) {
boolean result = getWinner(a.get(i));
if(result == true)
System.out.println("T");
else
System.out.println("HL");
}
sc.close();
}
private static boolean getWinner(int[] a) {
PriorityQueue<Integer> q = new PriorityQueue<>((n1, n2) -> n2-n1);
for(int num: a)
q.add(num);
boolean winner = true;
int temp = q.poll();
while(!q.isEmpty()) {
if(q.size() == 0)
return winner;
int num = temp-1;
temp = q.poll();
winner = !winner;
if(num != 0)
q.add(num);
}
return winner;
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 555a30ee4bcb3786a99ebc47063d4ef8 | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | //package com.cf.r666;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;
import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class D {
FastScanner in;
PrintWriter out;
public static void main(String[] args) {
new D().solve();
}
private void solve() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
int t = in.nextInt();
for (int caso = 0; caso < t; caso++) {
int n = in.nextInt();
SortedMap<Integer, Integer> a = new TreeMap<>();
for (int j = 0; j < n; j++) {
int cur = in.nextInt();
a.put(cur, a.getOrDefault(cur, 0) + 1);
}
boolean playerZero = true;
int forbidden = -1;
for (int i = 0; i < 1e9; i++) {
int best = 0;
if (!a.isEmpty() &&
(forbidden != a.lastKey() ||
a.get(a.lastKey()) > 1)) {
best = a.lastKey();
} else if (a.size() > 1) {
best = a.headMap(a.lastKey()).lastKey();
}
if (best == 0) break;
forbidden = best - 1;
a.put(best, a.get(best) - 1);
a.put(best - 1, a.getOrDefault(best - 1, 0) + 1);
if (a.get(best) == 0) a.remove(best);
playerZero = !playerZero;
}
if (!playerZero) {
out.println("T");
} else {
out.println("HL");
}
}
out.close();
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 03e996b8b41a82caa3e5a447bf64d51c | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | /**
* Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools
*/
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
int[] a = new int[n];
String[] line = br.readLine().split(" ");
for (int i = 0; i < n; ++i) {
a[i] = Integer.parseInt(line[i]);
}
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
}
boolean ez = false;
for (int i = 0; i < n; ++i) {
if (a[i] * 2 > sum) {
ez = true;
break;
}
}
if (ez || (sum & 1) == 1) {
bw.write('T');
} else {
bw.write("HL");
}
bw.newLine();
}
br.close();
bw.close();
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | b534d7e957c66f217e3d6f3c19ee1a41 | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.util.Collections.*;
import static java.lang.Math.*;
import java.util.stream.*;
@SuppressWarnings("unchecked")
public class D_Stoned_Game {
public static PrintWriter out;
public static InputReader in;
public static long MOD = (long)1e9+7;
public static void main(String[] args)throws IOException {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
int cases = in.nextInt();
for(int t = 0; t < cases; t++){
int n = in.nextInt();
int[] arr = in.nextArrI(n);
int pf = -1, ps = -1;
int turn = 0;
while(true){
if(turn==1){
//sec
int mx = -1;
for(int i=0;i<n;i++){
if(i!=pf && (mx==-1 || arr[i]>arr[mx])) mx=i;
}
if(mx==-1 || arr[mx]==0){
out.println("T"); break;
}
ps = mx; arr[ps]--;
}
else{
int mx = -1;
for(int i=0;i<n;i++){
if(i!=ps && (mx==-1 || arr[i]>arr[mx])) mx=i;
}
if(mx==-1 || arr[mx]==0){
out.println("HL"); break;
}
pf = mx; arr[pf]--;
}
turn^=1;
}
}
out.close();
}
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 int[] nextArrI(int n) {
int[] a=new int[n];
for(int i=0; i<n; i++) a[i]=nextInt();
return a;
}
public long[] nextArrL(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output | |
PASSED | 0dd3c1b5984005592f1b877acee4b341 | train_003.jsonl | 1598798100 | T is playing a game with his friend, HL.There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
InputStream is;
PrintWriter out;
String INPUT = "";
long MAX = (long) 1e5, MOD = (long)1e18;
void solve(int TC) {
int n = ni(),even=0,odd=0;
int sum = 0, mx = 1;
int[] a = new int[n];
for (int i=0;i<n;i++) {
a[i] = ni();
sum += a[i];
mx = Math.max(a[i], mx);
if(a[i]%2==0) ++even;
else ++odd;
}
if (mx - (sum-mx) > 0 || n==1) {
pn("T");
} else {
if (odd%2 == 0) {
pn("HL");
} else {
pn("T");
}
}
}
boolean TestCases = true;
public static void main(String[] args) throws Exception { new Main().run(); }
long pow(long a, long b) {
if(b==0 || a==1) return 1;
long o = 1;
for(long p = b; p > 0; p>>=1) {
if((p&1)==1) o = (o*a) % MOD;
a = (a*a) % MOD;
} return o;
}
long power(long a, long b) {
if(b==0 || a==1) return 1;
long o = 1;
for(long p = b; p > 0; p>>=1) {
if((p&1) == 1) o = o*a;
a = a*a;
} return o;
}
long inv(long x) {
long o = 1;
for(long p = MOD-2; p > 0; p>>=1) {
if((p&1)==1)o = (o*x)%MOD;
x = (x*x)%MOD;
} return o;
}
static final Random random = new Random();
static void shuffleSort(int[] a) {
int n = a.length; // shuffle, then sort
for (int i = 0; i<n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
long gcd(long a, long b) { return (b==0) ? a : gcd(b,a%b); }
int gcd(int a, int b) { return (b==0) ? a : gcd(b,a%b); }
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
int T = TestCases ? ni() : 1;
for(int t=1;t<=T;t++) solve(t);
out.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}
void p(Object o) { out.print(o); }
void pn(Object o) { out.println(o); }
void pni(Object o) { out.println(o);out.flush(); }
double PI = 3.141592653589793238462643383279502884197169399;
int ni() {
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-') {
minus = true;
b = readByte();
}
while(true) {
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
long nl() {
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-') {
minus = true;
b = readByte();
}
while(true) {
if(b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
double nd() { return Double.parseDouble(ns()); }
char nc() { return (char)skip(); }
int BUF_SIZE = 1024 * 8; // 1 MB is 1024*1024
byte[] inbuf = new byte[BUF_SIZE];
int lenbuf = 0, ptrbuf = 0;
int readByte() {
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
} return inbuf[ptrbuf++];
}
boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))) { // when nextLine, (b != '\n')
sb.appendCodePoint(b); b = readByte();
} return sb.toString();
}
char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
} return n == p ? buf : Arrays.copyOf(buf, p);
}
void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
} | Java | ["2\n1\n2\n2\n1 1"] | 1 second | ["T\nHL"] | NoteIn the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner. | Java 11 | standard input | [
"implementation",
"greedy",
"games",
"brute force"
] | 5bfce6c17af24959b4af3c9408536d05 | The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines: The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$. | 1,800 | For each game, print on a single line the name of the winner, "T" or "HL" (without quotes) | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.