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 | 8b6f0b8325726cec8feef16c0fc8a703 | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.*;
import static java.lang.Math.*;
public class ProblemA {
void run() {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
String w = sc.next();
int[] counts = new int[256];
for (char c : w.toCharArray()) {
++counts[c];
}
boolean good = true;
for (char c = 'a'; c <= 'z'; ++c) {
good = good && (counts[c] % k == 0);
}
if (good) {
char[] out = new char[w.length()];
int index = 0;
for (int i = 0; i < k; ++i) {
for (char c = 'a'; c <= 'z'; ++c) {
for (int j = 0; j < counts[c]/k; ++j) {
out[index] = c;
++index;
}
}
}
p("%s\n", new String(out));
}
else {
p("-1\n");
}
}
boolean debug = false;
void p(String f, Object...params) {
System.out.printf(f, params);
}
void d(Object...params) {
if (debug) {
p("DEBUG: %s\n", Arrays.deepToString(params));
}
}
void die() {
throw new RuntimeException();
}
public ProblemA(String[] args) {
if (args.length > 0 && args[0].equals("debug")) {
debug = true;
}
}
public static void main(String[] args) {
new ProblemA(args).run();
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | cad48326d800c710e9b29373b62a59ba | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.Scanner;
public class CodeForces {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int k = scan.nextInt();
String s = scan.next();
char[] v = new char[26];
boolean can = true;
for (char c : s.toCharArray()) {
v[c - 'a']++;
}
for(int i: v)if (i> 0&&i % k != 0) can = false;
if (can) {
String ans = "";
for(int w=0; w < k; w++)
for (int i = 0; i < v.length; i++) {
if (v[i] > 0) {
char c = (char) ('a' + i);
for(int j=1; j<=v[i]/k;j++)
ans += c;
}
}
StringBuilder b = new StringBuilder(ans);
b.reverse();
System.out.println(b.toString());
} else
System.out.println("-1");
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 584a954d427cabd009deca5cfd510dd2 | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
public class A {
public static void main(String[] Args) throws IOException {
new A().solve();
}
int o = 0;
void solve() throws IOException {
int k = si();
String s = ss();
if (k == 1)
System.out.println(s);
else {
int[] z = new int[26];
for (char c : s.toCharArray())
z[c - 'a']++;
for (int i = 0; i < z.length; i++) {
if (z[i] % k != 0) {
System.out.println("-1");
return;
}
}
for (int i = 1; i <= k; i++) {
for (char c = 'a'; c <= 'z'; c++) {
int m = z[c - 'a'];
if (m > 0) {
m /= k;
while (m-- > 0) {
System.out.print(c);
}
}
}
}
}
}
// ----------------------- Library ------------------------
Scanner in = new Scanner(System.in);
String ss() {
return in.next();
}
String sline() {
return in.nextLine();
}
int si() {
return in.nextInt();
}
int[] sai(int n) {
int[] a = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
return a;
}
int[] sai_(int n) {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
}
return a;
}
BufferedReader br;
StringTokenizer tokenizer;
{
br = new BufferedReader(new InputStreamReader(System.in));
}
void tok() throws IOException {
tokenizer = new StringTokenizer(br.readLine());
}
int toki() throws IOException {
return Integer.parseInt(tokenizer.nextToken());
}
int[] rint(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
a[i] = Integer.parseInt(tokenizer.nextToken());
return a;
}
int[] rint_(int n) throws IOException {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = Integer.parseInt(tokenizer.nextToken());
return a;
}
String[] rstrlines(int n) throws IOException {
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = br.readLine();
}
return a;
}
long tokl() {
return Long.parseLong(tokenizer.nextToken());
}
double tokd() {
return Double.parseDouble(tokenizer.nextToken());
}
String toks() {
return tokenizer.nextToken();
}
String rline() throws IOException {
return br.readLine();
}
List<Integer> toList(int[] a) {
List<Integer> v = new ArrayList<Integer>();
for (int i : a)
v.add(i);
return v;
}
static void pai(int[] a) {
System.out.println(Arrays.toString(a));
}
static int toi(Object s) {
return Integer.parseInt(s.toString());
}
static int[] dx_ = { 0, 0, 1, -1 };
static int[] dy_ = { 1, -1, 0, 0 };
static int[] dx3 = { 1, -1, 0, 0, 0, 0 };
static int[] dy3 = { 0, 0, 1, -1, 0, 0 };
static int[] dz3 = { 0, 0, 0, 0, 1, -1 };
static int[] dx = { 1, 0, -1, 1, -1, 1, 0, -1 }, dy = { 1, 1, 1, 0, 0, -1,
-1, -1 };
static int INF = 2147483647; // -8
static long LINF = 922337203854775807L; // -8
static short SINF = 32767; // -32768
// finds GCD of a and b using Euclidian algorithm
public int GCD(int a, int b) {
if (b == 0)
return a;
return GCD(b, a % b);
}
static List<String> toList(String[] a) {
return Arrays.asList(a);
}
static String[] toArray(List<String> a) {
String[] o = new String[a.size()];
a.toArray(o);
return o;
}
static int[] pair(int... a) {
return a;
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | eac4663f6d7ffbdf7877f3545481fb8a | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.Scanner;
public class CodeForces {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int k = scan.nextInt();
String s = scan.next();
char[] v = new char[26];
boolean can = true;
for (char c : s.toCharArray()) {
v[c - 'a']++;
}
for(int i: v)if (i> 0&&i % k != 0) can = false;
if (can) {
String ans = "";
int tk=k;
while (tk-- > 0)
for (int i = 0; i < v.length; i++) {
if (v[i] > 0) {
char c = (char) ('a' + i);
for(int j=1; j<=v[i]/k;j++)
ans += c;
}
}
System.out.println(ans);
} else
System.out.println("-1");
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 91f3f59fcb9d808a29a6c05807a0b34d | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
sc.nextLine();
char[] a = sc.nextLine().toCharArray();
int n = a.length;
if (n % k != 0) {
System.out.println(-1);
System.exit(0);
}
int t = n / k;
qSort(a, 0, n - 1);
char[] b = new char[n];
int p = 0;
for (int i = 0; i < n; i += k) {
if (a[i] == a[i + k-1]) {
for (int j = p; j < n; j += t)
b[j] = a[i];
p++;
} else {
System.out.println(-1);
System.exit(0);
}
}
for (int i = 0; i < n; i++)
System.out.print(b[i]);
}
public static void qSort(char[] A, int low, int high) {
int i = low;
int j = high;
int x = A[(low + high) / 2];
do {
while (A[i] < x)
++i;
while (A[j] > x)
--j;
if (i <= j) {
char temp = A[i];
A[i] = A[j];
A[j] = temp;
i++;
j--;
}
} while (i <= j);
if (low < j)
qSort(A, low, j);
if (i < high)
qSort(A, i, high);
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 9aa5ad52fff7bb6ebf6c56c5513ca092 | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
char[] s = sc.next().toCharArray();
int[] a = new int[255];
for(int i = 0; i < s.length; i++){
a[s[i]]++;
}
for(int i = 'a'; i <= 'z'; i++){
if(a[i] % k != 0){
System.out.print(-1);
return;
}
}
String ans = "";
for(int i = 0; i < a.length; i++){
if(a[i] > 0){
int n = a[i] / k;
for(int j = 0; j < n; j++){
ans += (char)i;
}
}
}
for(int i = 0; i < k; i++){
System.out.print(ans);
}
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 44ab2afe4d92edf962aaf8e50101bdf4 | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Locale;
public class A219
{
public static void main(String[] args) throws IOException
{
setStandardIO();
//setFileIO("a219");
int k = nextInt();
String s = nextString();
if (k == 1) {
println(s);
}
else if (s.length() % k != 0) {
println("-1");
}
else {
int tab[] = new int['z' - 'a' + 1];
for (int i = 0; i < s.length(); i++) {
tab[s.charAt(i) - 'a']++;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < tab.length; i++) {
if (tab[i] % k != 0) {
println("-1");
flush();
return;
}
for (int j = 0; j < tab[i] / k; j++) {
sb.append((char)('a' + i));
}
}
for (int i = 0; i < k; i++) {
print(sb.toString());
}
}
flush();
}
// ~ -----------------------------------------------------------------------
// ~ - Auxiliary code ------------------------------------------------------
// ~ -----------------------------------------------------------------------
static InputStream in;
static PrintWriter out;
/**
* Simple buffered reader class. Reads if buffer is empty. Try to fill up
* whole buffer.
*/
public static class SimpleBufferedInputStream extends InputStream
{
// ~ Static fields / Initializers --------------------------------------
private static int defaultCharBufferSize = 16384;
private InputStream in;
byte[] buffer;
int pos = 0;
int len = 0;
// ~ Constructors ------------------------------------------------------
public SimpleBufferedInputStream(InputStream in) {
this(in, defaultCharBufferSize);
}
public SimpleBufferedInputStream(InputStream in, int size) {
this.in = in;
this.buffer = new byte[size];
}
// ~ Methods -----------------------------------------------------------
@Override
public int read() throws IOException {
if (pos < len)
return buffer[pos++];
len = in.read(buffer);
if (len == -1)
return len;
pos = 0;
if (len <= 0)
return -1;
return buffer[pos++];
}
}
public static void setStandardIO() {
in = new SimpleBufferedInputStream(System.in);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
public static void setFileIO(String fileName) throws IOException {
in = new SimpleBufferedInputStream(new FileInputStream(fileName + ".in"));
out = new PrintWriter(new BufferedWriter(new FileWriter(fileName + ".out")));
}
public static int skipWhitespaces() throws IOException {
int c;
for (;;) {
c = in.read();
if (c == -1) throw new IOException();
if (!isWhitespace(c)) break;
}
return c;
}
public static int skipNewLines() throws IOException {
int c;
for (;;) {
c = in.read();
if (c == -1) throw new IOException();
if (!isNewLine(c)) break;
}
return c;
}
public static boolean isNewLine(int c) {
return c == '\n' || c == '\r' ? true : false;
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' ? true : false;
}
public static int nextNoWhitespace() throws IOException {
int c = in.read();
if (c == -1 || isWhitespace(c)) return -1;
return c;
}
public static int nextNoNewLine() throws IOException {
int c = in.read();
if (c == -1 || isNewLine(c)) return -1;
return c;
}
public static long nextLong() throws IOException {
int c;
for (;;) {
c = in.read();
if (!isWhitespace(c)) break;
if (c == -1) throw new RuntimeException();
}
boolean neg = false;
long val = 0;
if (c == '-')
neg = true;
else
val = c - '0';
for (;;) {
c = in.read();
if (isWhitespace(c) || c == -1)
break;
val = val * 10 + (c - '0');
}
if (neg)
return -val;
return val;
}
public static int nextInt() throws IOException {
int c;
for (;;) {
c = in.read();
if (!isWhitespace(c)) break;
if (c == -1) throw new RuntimeException();
}
boolean neg = false;
int val = 0;
if (c == '-')
neg = true;
else
val = c - '0';
for (;;) {
c = in.read();
if (isWhitespace(c) || c == -1)
break;
val = val * 10 + (c - '0');
}
if (neg)
return -val;
return val;
}
public static byte nextByte() throws IOException {
return (byte) nextInt();
}
public static String nextLine() throws IOException {
int c = (char) skipNewLines();
StringBuilder sb = new StringBuilder();
sb.append((char) c);
for (;;) {
c = nextNoNewLine();
if (c == -1)
break;
sb.append((char) c);
}
return sb.toString();
}
public static String nextString() throws IOException {
int c = (char) skipWhitespaces();
StringBuilder sb = new StringBuilder();
sb.append((char) c);
for (;;) {
c = nextNoWhitespace();
if (c == -1)
break;
sb.append((char) c);
}
return sb.toString();
}
public static double nextDouble() throws IOException {
return Double.parseDouble(nextString());
}
public static BigDecimal nextBigDecimal() throws IOException {
return new BigDecimal(nextString());
}
public static void flush() {
out.flush();
}
public static void println(Object object) {
out.println(object);
}
public static void print(Object object) {
out.print(object);
}
public static void println(double d, int precision) {
String s = "%1$." + precision + "f";
out.format(Locale.ENGLISH, s, d);
out.println();
}
public static void println(BigDecimal d, int precision) {
String s = "%1$." + precision + "f";
out.format(Locale.ENGLISH, s, d);
out.println();
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | cce22ddeca938d5e92fb03de28154e21 | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
//=========================================================================
public static final String INPUT = "";
public static final String OUTPUT = "";
//=========================================================================
BufferedReader br;
BufferedWriter bw;
Scanner in;
PrintWriter out;
StreamTokenizer st;
int nextInt() throws IOException { st.nextToken(); return (int)(st.nval); }
long nextLong() throws IOException { st.nextToken(); return (long)(st.nval); }
double nextDouble() throws IOException { st.nextToken(); return st.nval; }
String nextLine() throws IOException { return br.readLine(); }
//=========================================================================
public Main() throws IOException {
br = new BufferedReader(INPUT.isEmpty() ? new InputStreamReader(System.in) : new FileReader(INPUT));
bw = new BufferedWriter(OUTPUT.isEmpty() ? new OutputStreamWriter(System.out) : new FileWriter(OUTPUT));
in = new Scanner(br);
out = new PrintWriter(bw);
st = new StreamTokenizer(br);
}
public static void main(String[] args) throws IOException {
new Main().run();
}
//=========================================================================
int[] a;
void f(int x) {
if (x == 1) return;
int t = a[x - 1];
a[x - 1] = a[x - 2];
a[x - 2] = t;
f(x - 1);
}
void run() throws IOException {
int n = in.nextInt();
char[] str = in.next().toCharArray();
int[] count = new int[256];
for (char c : str) {
++count[c];
}
int p = 0;
for (int i = 0; i < 256; ++i) {
if (count[i] % n != 0) {
out.println(-1);
out.flush();
return;
}
while (count[i] != 0) {
for (int j = 0; j < n; ++j) {
str[str.length / n * j + p] = (char)(i);
}
++p;
count[i] -= n;
}
}
out.println(String.copyValueOf(str));
out.flush();
}
} | Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | fd959d2c207bec066f88db5208a6095d | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Problem implements Runnable {
public static void main(String[] args) {
new Problem().run();
}
BufferedReader br;
StringTokenizer in;
PrintWriter out;
String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens())
in = new StringTokenizer(br.readLine());
return in.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
final int ALPHABET = 256;
void solve() throws IOException {
int k = nextInt();
String s = nextToken();
int[] c = new int[ALPHABET];
for (int i = 0; i < s.length(); i++)
++c[s.charAt(i)];
StringBuilder ans = new StringBuilder();
for (int i = 0; i < k; i++)
for (char j = 'a'; j <= 'z'; j++) {
if (c[j] % k != 0) {
System.out.print("-1");
return;
} else {
int p = c[j] / k;
for (int z = 0; z < p; z++)
ans = ans.append(j);
}
}
System.out.print(ans);
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
//out = new PrintWriter("output.txt");
solve();
//out.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
} | Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | b48be73886c05b71e03abf604c7fb98b | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
public class main {
public static void main(String[]args)throws IOException
{
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
PrintWriter out = new PrintWriter (System.out,true);
int n = Integer.parseInt(in.readLine());
char[] s = in.readLine().toCharArray();
if(s.length % n != 0)
{
out.println(-1);
}else{
int[] chars = new int[26];
for(int i = 0; i<s.length ; i++)
{
int c = s[i] - 97;
chars[c]++;
}
boolean check = true;
for(int i = 0; i<26 ; i++)
{
if(chars[i] % n != 0)check = false;
}
if(!check)
{
out.println(-1);
}else{
Arrays.sort(s);
String sub = "";
for(int i = 0 ; i<s.length ; i+=n)
{
sub += s[i];
}
String result = "";
for(int i = 0 ; i < n ; i++)
{
result += sub;
}
out.println(result);
}
}
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 67696665244a3c1391531d0b2361498f | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.*;
public class KString
{
public static void main(String[] args)
{
// Set up scanner
Scanner sc = new Scanner(System.in);
// System.out.println("Enter k");
int k = sc.nextInt();
// System.out.println("Enter string");
String st = sc.next();
int len = st.length();
if (len%k != 0)
{
System.out.println(-1);
return;
}
Map<String, Integer> m = new HashMap<String, Integer>();
for (int i=0; i<len; i++)
{
String ch = st.substring(i, i+1);
if (!m.containsKey(ch))
{
m.put(ch, 1);
}
else
{
int num = m.get(ch);
num++;
m.put(ch, num);
}
}
String piece = "";
for (String ch: m.keySet())
{
int howmany = m.get(ch);
if (howmany%k != 0)
{
System.out.println(-1);
return;
}
for (int i=0; i<howmany/k; i++)
{
piece += ch;
}
}
String answer = "";
for (int i=0; i<k; i++)
{
answer = answer + piece;
}
System.out.println(answer);
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | ddb617c0b09fa1bf571b1a99043f312e | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.*;
/*
3
abcabcabcabc
*/
/*
4
aabaabaabaab
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char array [];
int k = sc.nextInt(),n;
sc.nextLine();
array = sc.nextLine().toCharArray();
if(array.length%k==0)
{
n=array.length/k;
Arrays.sort(array);
char c = array[0];
String res = ""+c,cad="";
boolean sw = true;
for(int i = 1 ; i <= n ; i++)
{
if(c!=array[i*k-1])
{
sw = false;
break;
}
if(i*k<array.length)
{
c = array[i*k];
res+=c;
}
}
for(int i = 0 ; i < k ; i++)
cad+=res;
System.out.println(sw?cad:-1);
}
else
{
System.out.println(-1);
}
}
} | Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 4ccc5499c138232fc13ff808327e0c9d | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.*;
/**
*
* @author N-AssassiN
*/
public class Main {
private static BufferedReader reader;
private static BufferedWriter out;
private static StringTokenizer tokenizer;
//private final static String filename = "filename";
private static void init(InputStream input, OutputStream output) {
reader = new BufferedReader(new InputStreamReader(input));
out = new BufferedWriter(new OutputStreamWriter(output));
//reader = new BufferedReader(new FileReader(filename + ".in"));
//out = new BufferedWriter(new FileWriter(filename + ".out"));
tokenizer = new StringTokenizer("");
}
private static String nextLine() throws IOException {
return reader.readLine();
}
private static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(nextLine());
}
return tokenizer.nextToken();
}
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());
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
init(System.in, System.out);
int[] count = new int[26];
int k = nextInt();
//long startTime = System.currentTimeMillis();
String s = nextLine();
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;
}
boolean able = true;
for (int i = 0; i < 26; i++) {
if (count[i] % k != 0) {
able = false;
break;
}
}
if (!able) {
out.write("-1\n");
} else {
for (int i = 0; i < k; i++) {
for (int j = 0; j < 26; j++) {
for (int l = 0; l < count[j] / k; l++) {
out.write((char) ('a' + j));
}
}
}
out.write("\n");
}
//long runTime = System.currentTimeMillis() - startTime;
//out.write(runTime + "\n");
out.flush();
}
} | Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 19d134c00d3723c9dd3d0674c0bc2c63 | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int k=scan.nextInt();
int[] alphabet= new int [40];
char[] s=scan.next().toCharArray();
for(char c:s){
alphabet[(int)(c)-97]+=1;
}
boolean notRes=false;
char res[] = new char[1000];
int index=0;
for(int i=0;i<26;i++){
int delit=alphabet[i]%k;
if(delit==0){
int kratn=alphabet[i]/k;
for(int j=0;j<kratn;j++){
res[index++]=(char)(i+97);
}
}
else{
notRes=true;
break;
}
}
if(notRes){
System.out.println("-1");
}
else{
String repeat=String.copyValueOf(res, 0, index);
for(int i=0;i<k;i++){
System.out.print(repeat);
}
}
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 489c74da0cddfc13705759c9f846ac81 | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Stack;
/**
*
* @author DELL
*/
public class CodeForce219AKString {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
PrintWriter p = new PrintWriter(System.out);
String s1 = s.readLine();
int j = 0, n = Integer.parseInt(s1);
s1 = s.readLine();
Hashtable hh = new Hashtable();
char[] xx = s1.toCharArray();
char[] yy = new char[xx.length / n];
for (int i = 0; i < xx.length; i++) {
if (hh.containsKey(xx[i])) {
hh.put(xx[i], ((Integer) hh.get(xx[i]) + 1));
} else {
if (j < xx.length / n) {
yy[j] = xx[i];
j++;
hh.put(xx[i], 1);
} else {
p.println("-1");
p.flush();
System.exit(0);
}
}
}
String zz = "";
int temp, k = 0,l;
while (k < j) {
l = (Integer) hh.get(yy[k])%n;
temp = (Integer) hh.get(yy[k]) / n;
if ( l!=0 || temp == 0) {
p.println("-1");
p.flush();
System.exit(0);
}
for (int i = 0; i < temp; i++) {
zz = zz + yy[k];
}
k++;
}
for (int i = 0; i < n; i++) {
p.print(zz);
}
p.flush();
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 4cc50d62ee0ee7903fbe9a9c38cc2586 | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.Scanner;
public class Kstring {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
String cad = sc.next();
int[] ar = new int[26];
boolean correcto = true;
String cadFin = "";
for(int i=0; i<cad.length(); i++) {
ar[(int)cad.charAt(i) - 97]++;
}
for(int i=0; i<ar.length; i++) {
if(ar[i] > 0 && ar[i] % k != 0) {
correcto = false;
}
}
if(correcto) {
for(int i=0; i<k; i++) {
for(int j=0; j<ar.length; j++) {
if(ar[j] != 0) {
for(int f=0; f<ar[j] / k; f++) {
cadFin += (char)(j + 97);
}
}
}
}
System.out.println(cadFin);
}
else {
System.out.println(-1);
}
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 807630380414d35c938f096208c6331a | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int k = in.nextInt();
String s = in.next();
String ans = solve(s, k);
if(ans != null)
System.out.println(ans);
else
System.out.println(-1);
}
private static String solve(String s, int k) {
if(s.length()%k != 0)
return null;
int[] freq = new int[26];
for(int i = 0; i < s.length(); i++)
freq[s.charAt(i)-'a']++;
for(int i = 0; i < freq.length; i++)
if(freq[i]%k != 0)
return null;
StringBuilder ans = new StringBuilder();
for(int i = 0; i < freq.length; i++)
if(freq[i] != 0)
for(int j =0; j < freq[i]/k; j++)
ans.append((char)(i+'a'));
StringBuilder total = new StringBuilder();
for(int i = 0; i < k; i++)
total.append(ans);
return total.toString();
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | e877cda8ae4c545cb91598ff9217e29b | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.*;
public class k_string
{
static int n;
static String str;
static int[] az = new int[26];
public static void main( String[] args )
{
Scanner in = new Scanner( System.in );
n = in.nextInt();
str = in.next();
for ( int i = 0; i < str.length(); i++ )
az[str.charAt( i ) - 97]++;
for ( int i = 0; i < 26; i++ )
{
if ( az[i] % n != 0 )
{
System.out.println( -1 );
System.exit( 0 );
}
az[i] /= n;
}
str = "";
for ( int i = 0; i < 26; i++ )
for ( int j = 0; j < az[i]; j++ )
{
str += (char)( i + 97 );
}
for ( int i = 0; i < n; i++ )
System.out.print( str );
System.out.println();
in.close();
System.exit( 0 );
}
} | Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | f77bca8c13998d15940f21096637a626 | train_001.jsonl | 1346081400 | A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string. | 256 megabytes | import java.util.Scanner;
public class KString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
String s = sc.next();
System.out.println(valid(s,k));
}
public static int split(String s, int n){
char[] c = s.toCharArray();
return 0;
}
public static String valid(String s, int k){
int[] c = new int[26];
s = s.toLowerCase();
for(int i=0; i<s.length();i++){
c[s.charAt(i)-'a']++;
}
for(int i=0;i<26;i++){
if((c[i] != 0)&&(c[i]%k !=0)) return "-1";
}
String ret = "";
for(int i=0; i<26;i++){
if(c[i] !=0){
c[i]=c[i]/k;
while(c[i] !=0){
ret +=""+(char)(i+'a');
c[i]--;
}
}
}
String seed = ret;
ret = "";
for(int i=0; i<k;i++){
ret+=seed;
}
return ret;
}
}
| Java | ["2\naazz", "3\nabcabcabz"] | 2 seconds | ["azaz", "-1"] | null | Java 6 | standard input | [
"implementation",
"strings"
] | f5451b19cf835b1cb154253fbe4ea6df | The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s. | 1,000 | Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). | standard output | |
PASSED | 8ffd5391e52df83a210e3af8dad23fed | train_001.jsonl | 1553006100 | There are $$$n$$$ left boots and $$$n$$$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $$$l$$$ and $$$r$$$, both of length $$$n$$$. The character $$$l_i$$$ stands for the color of the $$$i$$$-th left boot and the character $$$r_i$$$ stands for the color of the $$$i$$$-th right boot.A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair. | 256 megabytes | import java.util.*;import java.io.*;import java.math.*;
public class Main
{
public static void process()throws IOException
{
int n=ni();
char left[]=(" "+nln()).toCharArray(),right[]=(" "+nln()).toCharArray();
Queue<Integer> l[]=new Queue[30],r[]=new Queue[30],uk_l=new LinkedList<>(),uk_r=new LinkedList<>();
for(int i=0;i<30;i++){
l[i]=new LinkedList<>();
r[i]=new LinkedList<>();
}
for(int i=1;i<=n;i++){
int idx=(int)(left[i]-'a');
if(left[i]=='?')
uk_l.add(i);
else
l[idx].add(i);
idx=(int)(right[i]-'a');
if(right[i]=='?')
uk_r.add(i);
else
r[idx].add(i);
}
int cnt=0;
StringBuilder res = new StringBuilder();
for(int i=0;i<26;i++){
int take=Math.min(l[i].size(), r[i].size());
for(int j=0;j<take;j++){
Integer a=l[i].poll(),b=r[i].poll();
res.append(a+" "+b+"\n");
cnt++;
}
while(l[i].size()>0 && uk_r.size()>0){
Integer a=l[i].poll(),b=uk_r.poll();
res.append(a+" "+b+"\n");
cnt++;
}
while(uk_l.size()>0 && r[i].size()>0){
Integer a=uk_l.poll(),b=r[i].poll();
res.append(a+" "+b+"\n");
cnt++;
}
}
while(uk_l.size()>0 && uk_r.size()>0){
Integer a=uk_l.poll(),b=uk_r.poll();
res.append(a+" "+b+"\n");
cnt++;
}
p(cnt+"\n"+res);
}
static FastReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
out = new PrintWriter(System.out);
sc=new FastReader();
long s = System.currentTimeMillis();
int t=1;
//t=ni();
while(t-->0)
process();
out.flush();
System.err.println(System.currentTimeMillis()-s+"ms");
System.out.close();
}
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static void pni(Object o){out.println(o);System.out.flush();}
static int ni()throws IOException{return Integer.parseInt(sc.next());}
static long nl()throws IOException{return Long.parseLong(sc.next());}
static double nd()throws IOException{return Double.parseDouble(sc.next());}
static String nln()throws IOException{return sc.nextLine();}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
static boolean multipleTC=false;
static long mod=(long)1e9+7l;
static<T> void r_sort(T arr[],int n){
Random r = new Random();
for (int i = n-1; i > 0; i--){
int j = r.nextInt(i+1);
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Arrays.sort(arr);
}
static long mpow(long x, long n) {
if(n == 0)
return 1;
if(n % 2 == 0) {
long root = mpow(x, n / 2);
return root * root % mod;
}else {
return x * mpow(x, n - 1) % mod;
}
}
static long mcomb(long a, long b) {
if(b > a - b)
return mcomb(a, a - b);
long m = 1;
long d = 1;
long i;
for(i = 0; i < b; i++) {
m *= (a - i);
m %= mod;
d *= (i + 1);
d %= mod;
}
long ans = m * mpow(d, mod - 2) % mod;
return ans;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
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();
}
String nextLine() {
String str = "";
try{
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
} | Java | ["10\ncodeforces\ndodivthree", "7\nabaca?b\nzabbbcc", "9\nbambarbia\nhellocode", "10\ncode??????\n??????test"] | 2 seconds | ["5\n7 8\n4 9\n2 2\n9 10\n3 1", "5\n6 5\n2 3\n4 6\n7 4\n1 2", "0", "10\n6 2\n1 6\n7 3\n3 5\n4 8\n9 7\n5 1\n2 4\n10 9\n8 10"] | null | Java 11 | standard input | [
"implementation",
"greedy"
] | 6bf3e5a542ebce81c1e6ce7260644a3c | The first line contains $$$n$$$ ($$$1 \le n \le 150000$$$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $$$l$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th left boot. The third line contains the string $$$r$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th right boot. | 1,500 | Print $$$k$$$ — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $$$k$$$ lines should contain pairs $$$a_j, b_j$$$ ($$$1 \le a_j, b_j \le n$$$). The $$$j$$$-th of these lines should contain the index $$$a_j$$$ of the left boot in the $$$j$$$-th pair and index $$$b_j$$$ of the right boot in the $$$j$$$-th pair. All the numbers $$$a_j$$$ should be distinct (unique), all the numbers $$$b_j$$$ should be distinct (unique). If there are many optimal answers, print any of them. | standard output | |
PASSED | 59e21356044088f5a9834e1199077086 | train_001.jsonl | 1553006100 | There are $$$n$$$ left boots and $$$n$$$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $$$l$$$ and $$$r$$$, both of length $$$n$$$. The character $$$l_i$$$ stands for the color of the $$$i$$$-th left boot and the character $$$r_i$$$ stands for the color of the $$$i$$$-th right boot.A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair. | 256 megabytes | import java.io.*;
import java.util.*;
public class swapSort {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader((System.in)));
int n =Integer.parseInt(br.readLine());
String s1 = br.readLine();
String s2 = br.readLine();
HashMap<Character , Stack<Integer>> ri = new HashMap<>();
Stack<Integer> q2=new Stack<>();
Stack<Integer> q1=new Stack<>();
boolean[] li = new boolean [n];
for (int i=0;i<n;i++)
{
char ch = s2.charAt(i);
if (ch=='?') {
q2.push(i+1);
continue;
}
if (!ri.containsKey(ch)) ri.put(ch , new Stack<Integer>());
ri.get(ch).push((i+1));
}
StringBuilder res = new StringBuilder();
long cnt = 0;
for(int i=0;i<n;i++)
{
char ch = s1.charAt(i);
if (ch=='?') {
q1.push(i+1);
li[i]=true;
continue;
}
if (ri.containsKey(ch) && !ri.get(ch).isEmpty())
{
li[i] = true;
res.append((i+1)+" "+ri.get(ch).pop()+"\n");
cnt++;
}
}
for(char ch='a';!q1.isEmpty()&&ch<='z';)
{
if (!ri.containsKey(ch) || ri.get(ch).isEmpty()) ch++;
else
{
res.append(q1.pop()+" "+ri.get(ch).pop()+"\n");
cnt++;
}
}
for (int i=0;!q2.isEmpty()&&i<n;i++)
{
if (!li[i])
{
res.append((i+1)+" "+q2.pop()+"\n");
cnt++;
}
}
while (!q1.isEmpty() && !q2.isEmpty())
{
res.append(q1.pop()+" "+q2.pop()+"\n");
cnt++;
}
System.out.println(cnt);
System.out.println(res);
}
}
| Java | ["10\ncodeforces\ndodivthree", "7\nabaca?b\nzabbbcc", "9\nbambarbia\nhellocode", "10\ncode??????\n??????test"] | 2 seconds | ["5\n7 8\n4 9\n2 2\n9 10\n3 1", "5\n6 5\n2 3\n4 6\n7 4\n1 2", "0", "10\n6 2\n1 6\n7 3\n3 5\n4 8\n9 7\n5 1\n2 4\n10 9\n8 10"] | null | Java 11 | standard input | [
"implementation",
"greedy"
] | 6bf3e5a542ebce81c1e6ce7260644a3c | The first line contains $$$n$$$ ($$$1 \le n \le 150000$$$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $$$l$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th left boot. The third line contains the string $$$r$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th right boot. | 1,500 | Print $$$k$$$ — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $$$k$$$ lines should contain pairs $$$a_j, b_j$$$ ($$$1 \le a_j, b_j \le n$$$). The $$$j$$$-th of these lines should contain the index $$$a_j$$$ of the left boot in the $$$j$$$-th pair and index $$$b_j$$$ of the right boot in the $$$j$$$-th pair. All the numbers $$$a_j$$$ should be distinct (unique), all the numbers $$$b_j$$$ should be distinct (unique). If there are many optimal answers, print any of them. | standard output | |
PASSED | 86e725b482040cc92d6434eb24afcbdb | train_001.jsonl | 1553006100 | There are $$$n$$$ left boots and $$$n$$$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $$$l$$$ and $$$r$$$, both of length $$$n$$$. The character $$$l_i$$$ stands for the color of the $$$i$$$-th left boot and the character $$$r_i$$$ stands for the color of the $$$i$$$-th right boot.A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair. | 256 megabytes | import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
String s=in.next(),t=in.next();
int l[]=new int[26];
int r[]=new int[26];
int le=0,re=0;
Stack<Integer> left[]=new Stack[27];
Stack<Integer> right[]=new Stack[27];
Arrays.setAll(left,e-> new Stack<Integer>());
Arrays.setAll(right,e-> new Stack<Integer>());
ArrayList<Integer> a=new ArrayList<>();
ArrayList<Integer> b=new ArrayList<>();
for(int i=0;i<n;i++){
if(s.charAt(i)=='?') { le++; left[26].push(i+1); }
else { l[s.charAt(i)-'a']++; left[s.charAt(i)-'a'].push(i+1); }
if(t.charAt(i)=='?'){ re++; right[26].push(i+1); }
else { r[t.charAt(i)-'a']++; right[t.charAt(i)-'a'].push(i+1); }
}
int count=0;
for(int i=0;i<26;i++){
int min=Math.min(l[i],r[i]); count+=min;
l[i]-=min; r[i]-=min;
for(int c=0;c<min;c++) {
a.add(left[i].pop()); b.add(right[i].pop());
}
if(l[i]!=0) { int x=Math.min(l[i],re); re-=x; l[i]-=x; count+=x;
for(int c=0;c<x;c++) {
a.add(left[i].pop()); b.add(right[26].pop());
}
}
if(r[i]!=0) { int x=Math.min(r[i],le); le-=x; r[i]-=x; count+=x;
for(int c=0;c<x;c++) {
a.add(left[26].pop()); b.add(right[i].pop());
}
}
}
int ss=Math.min(re,le);
System.out.println(count+ss);
for(int i=0;i<ss;i++){
a.add(left[26].pop()); b.add(right[26].pop());
}
for(int i=0;i<a.size();i++){
System.out.println(a.get(i)+" "+b.get(i));
}
}
} | Java | ["10\ncodeforces\ndodivthree", "7\nabaca?b\nzabbbcc", "9\nbambarbia\nhellocode", "10\ncode??????\n??????test"] | 2 seconds | ["5\n7 8\n4 9\n2 2\n9 10\n3 1", "5\n6 5\n2 3\n4 6\n7 4\n1 2", "0", "10\n6 2\n1 6\n7 3\n3 5\n4 8\n9 7\n5 1\n2 4\n10 9\n8 10"] | null | Java 11 | standard input | [
"implementation",
"greedy"
] | 6bf3e5a542ebce81c1e6ce7260644a3c | The first line contains $$$n$$$ ($$$1 \le n \le 150000$$$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $$$l$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th left boot. The third line contains the string $$$r$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th right boot. | 1,500 | Print $$$k$$$ — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $$$k$$$ lines should contain pairs $$$a_j, b_j$$$ ($$$1 \le a_j, b_j \le n$$$). The $$$j$$$-th of these lines should contain the index $$$a_j$$$ of the left boot in the $$$j$$$-th pair and index $$$b_j$$$ of the right boot in the $$$j$$$-th pair. All the numbers $$$a_j$$$ should be distinct (unique), all the numbers $$$b_j$$$ should be distinct (unique). If there are many optimal answers, print any of them. | standard output | |
PASSED | a541f2b41422b23793add594347fe804 | train_001.jsonl | 1553006100 | There are $$$n$$$ left boots and $$$n$$$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $$$l$$$ and $$$r$$$, both of length $$$n$$$. The character $$$l_i$$$ stands for the color of the $$$i$$$-th left boot and the character $$$r_i$$$ stands for the color of the $$$i$$$-th right boot.A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair. | 256 megabytes | //package codeforces_464_div2;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class D {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String l = sc.nextLine();
Stack<Integer>[] left = new Stack[26];
Stack<Integer> wl = new Stack<>();
for(int i=0; i<26; i++) left[i] = new Stack<>();
for(int i=0; i<l.length(); i++) {
if(l.charAt(i) == '?')
wl.push(i+1);
else
left[l.charAt(i) - 'a'].push(i+1);
}
String r = sc.nextLine();
Stack<Integer>[] right = new Stack[26];
Stack<Integer> wr = new Stack<>();
for(int i=0; i<26; i++) right[i] = new Stack<>();
for(int i=0; i<r.length(); i++) {
if(r.charAt(i) == '?')
wr.push(i+1);
else
right[r.charAt(i) - 'a'].push(i+1);
}
List<P> ans = new ArrayList<>();
for(int i=0; i<26; i++) {
while(!left[i].isEmpty() && !right[i].isEmpty()) {
P temp = new P(left[i].pop(), right[i].pop());
ans.add(temp);
}
}
for(int i=0; i<26; i++) {
while(!right[i].isEmpty() && !wl.isEmpty()) {
P temp = new P(wl.pop(), right[i].pop());
ans.add(temp);
}
}
for(int i=0; i<26; i++) {
while (!left[i].isEmpty() && !wr.isEmpty()) {
P temp = new P(left[i].pop(), wr.pop());
ans.add(temp);
}
}
while (!wl.isEmpty() && !wr.isEmpty()) {
P temp = new P(wl.pop(), wr.pop());
ans.add(temp);
}
System.out.println(ans.size());
for(P it : ans) {
System.out.println(it.a + " " + it.b);
}
}
}
class P {
int a;
int b;
public P(int a, int b) {
this.a = a;
this.b = b;
}
} | Java | ["10\ncodeforces\ndodivthree", "7\nabaca?b\nzabbbcc", "9\nbambarbia\nhellocode", "10\ncode??????\n??????test"] | 2 seconds | ["5\n7 8\n4 9\n2 2\n9 10\n3 1", "5\n6 5\n2 3\n4 6\n7 4\n1 2", "0", "10\n6 2\n1 6\n7 3\n3 5\n4 8\n9 7\n5 1\n2 4\n10 9\n8 10"] | null | Java 11 | standard input | [
"implementation",
"greedy"
] | 6bf3e5a542ebce81c1e6ce7260644a3c | The first line contains $$$n$$$ ($$$1 \le n \le 150000$$$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $$$l$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th left boot. The third line contains the string $$$r$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th right boot. | 1,500 | Print $$$k$$$ — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $$$k$$$ lines should contain pairs $$$a_j, b_j$$$ ($$$1 \le a_j, b_j \le n$$$). The $$$j$$$-th of these lines should contain the index $$$a_j$$$ of the left boot in the $$$j$$$-th pair and index $$$b_j$$$ of the right boot in the $$$j$$$-th pair. All the numbers $$$a_j$$$ should be distinct (unique), all the numbers $$$b_j$$$ should be distinct (unique). If there are many optimal answers, print any of them. | standard output | |
PASSED | 772158bf1c1bdcceb5e72950bfd11fac | train_001.jsonl | 1553006100 | There are $$$n$$$ left boots and $$$n$$$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $$$l$$$ and $$$r$$$, both of length $$$n$$$. The character $$$l_i$$$ stands for the color of the $$$i$$$-th left boot and the character $$$r_i$$$ stands for the color of the $$$i$$$-th right boot.A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair. | 256 megabytes | /*
Author: Anthony Ngene
Created: 29/09/2020 - 05:36
*/
import java.io.*;
import java.util.*;
public class D {
// checks: 1. edge cases 2. overflow 3. possible errors (e.g 1/0, arr[out]) 4. time/space complexity
void solver() throws IOException {
int n = in.intNext();
HashMap<Character, Stack<Integer>> lefts = new HashMap<>();
String left = in.next();
for (int i = 1; i < n + 1; i++) {
char ch = left.charAt(i - 1);
if (!lefts.containsKey(ch)) lefts.put(ch, new Stack<>());
lefts.get(ch).add(i);
}
// out.println(lefts);
String right = in.next();
int count = 0;
Stack<Integer> unpairedRight = new Stack<>();
Stack<Integer> rightQuestions = new Stack<>();
StringBuilder pairs = new StringBuilder();
for (int i = 1; i < n + 1; i++) {
char ch = right.charAt(i - 1);
if (ch == '?') {
rightQuestions.add(i);
continue;
}
if (lefts.containsKey(ch) && !lefts.get(ch).isEmpty()) {
count++;
pairs.append(lefts.get(ch).pop()).append(" ").append(i).append("\n");
} else
unpairedRight.add(i);
}
Stack<Integer> unpairedLeft = new Stack<>();
for (char k: lefts.keySet()) if (k != '?') unpairedLeft.addAll(lefts.get(k));
// out.println(unpairedLeft);
// out.println(unpairedRight);
while (!unpairedLeft.isEmpty() && !rightQuestions.isEmpty()) {
count++;
pairs.append(unpairedLeft.pop()).append(" ").append(rightQuestions.pop()).append("\n");
}
while (!unpairedRight.isEmpty() && lefts.containsKey('?') && !lefts.get('?').isEmpty()) {
count++;
pairs.append(lefts.get('?').pop()).append(" ").append(unpairedRight.pop()).append("\n");
}
while (!rightQuestions.isEmpty() && lefts.containsKey('?') && !lefts.get('?').isEmpty()) {
count++;
pairs.append(lefts.get('?').pop()).append(" ").append(rightQuestions.pop()).append("\n");
}
out.println(count);
out.println(pairs);
}
/*
10
code??????
??????test
*/
// Generated Code Below:
private static final FastWriter out = new FastWriter();
private static FastScanner in;
static ArrayList<Integer>[] adj;
private static long e97 = (long)1e9 + 7;
public static void main(String[] args) throws IOException {
in = new FastScanner();
new D().solver();
out.close();
}
static class FastWriter {
private static final int IO_BUFFERS = 128 * 1024;
private final StringBuilder out;
public FastWriter() { out = new StringBuilder(IO_BUFFERS); }
public FastWriter p(Object object) { out.append(object); return this; }
public FastWriter p(String format, Object... args) { out.append(String.format(format, args)); return this; }
public FastWriter pp(Object... args) { for (Object ob : args) { out.append(ob).append(" "); } out.append("\n"); return this; }
public FastWriter pp(int[] args) { for (int ob : args) { out.append(ob).append(" "); } out.append("\n"); return this; }
public FastWriter pp(long[] args) { for (long ob : args) { out.append(ob).append(" "); } out.append("\n"); return this; }
public FastWriter pp(char[] args) { for (char ob : args) { out.append(ob).append(" "); } out.append("\n"); return this; }
public void println(long[] arr) { for(long e: arr) out.append(e).append(" "); out.append("\n"); }
public void println(int[] arr) { for(int e: arr) out.append(e).append(" "); out.append("\n"); }
public void println(char[] arr) { for(char e: arr) out.append(e).append(" "); out.append("\n"); }
public void println(double[] arr) { for(double e: arr) out.append(e).append(" "); out.append("\n"); }
public void println(boolean[] arr) { for(boolean e: arr) out.append(e).append(" "); out.append("\n"); }
public <T>void println(T[] arr) { for(T e: arr) out.append(e).append(" "); out.append("\n"); }
public void println(long[][] arr) { for (long[] row: arr) out.append(Arrays.toString(row)).append("\n"); }
public void println(int[][] arr) { for (int[] row: arr) out.append(Arrays.toString(row)).append("\n"); }
public void println(char[][] arr) { for (char[] row: arr) out.append(Arrays.toString(row)).append("\n"); }
public void println(double[][] arr) { for (double[] row: arr) out.append(Arrays.toString(row)).append("\n"); }
public <T>void println(T[][] arr) { for (T[] row: arr) out.append(Arrays.toString(row)).append("\n"); }
public FastWriter println(Object object) { out.append(object).append("\n"); return this; }
public void toFile(String fileName) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(out.toString());
writer.close();
}
public void close() throws IOException { System.out.print(out); }
}
static class FastScanner {
private InputStream sin = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
public FastScanner(){}
public FastScanner(String filename) throws FileNotFoundException {
File file = new File(filename);
sin = new FileInputStream(file);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = sin.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long longNext() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b) || b == ':'){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int intNext() {
long nl = longNext();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double doubleNext() { return Double.parseDouble(next());}
public long[] nextLongArray(final int n){
final long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = longNext();
return a;
}
public int[] nextIntArray(final int n){
final int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = intNext();
return a;
}
public double[] nextDoubleArray(final int n){
final double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = doubleNext();
return a;
}
public ArrayList<Integer>[] getAdj(int n) {
ArrayList<Integer>[] adj = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) adj[i] = new ArrayList<>();
return adj;
}
public ArrayList<Integer>[] adjacencyList(int nodes, int edges) throws IOException {
return adjacencyList(nodes, edges, false);
}
public ArrayList<Integer>[] adjacencyList(int nodes, int edges, boolean isDirected) throws IOException {
adj = getAdj(nodes);
for (int i = 0; i < edges; i++) {
int a = intNext(), b = intNext();
adj[a].add(b);
if (!isDirected) adj[b].add(a);
}
return adj;
}
}
static class u {
public static int upperBound(long[] array, long obj) {
int l = 0, r = array.length - 1;
while (r - l >= 0) {
int c = (l + r) / 2;
if (obj < array[c]) {
r = c - 1;
} else {
l = c + 1;
}
}
return l;
}
public static int upperBound(ArrayList<Long> array, long obj) {
int l = 0, r = array.size() - 1;
while (r - l >= 0) {
int c = (l + r) / 2;
if (obj < array.get(c)) {
r = c - 1;
} else {
l = c + 1;
}
}
return l;
}
public static int lowerBound(long[] array, long obj) {
int l = 0, r = array.length - 1;
while (r - l >= 0) {
int c = (l + r) / 2;
if (obj <= array[c]) {
r = c - 1;
} else {
l = c + 1;
}
}
return l;
}
public static int lowerBound(ArrayList<Long> array, long obj) {
int l = 0, r = array.size() - 1;
while (r - l >= 0) {
int c = (l + r) / 2;
if (obj <= array.get(c)) {
r = c - 1;
} else {
l = c + 1;
}
}
return l;
}
static <T> T[][] deepCopy(T[][] matrix) { return Arrays.stream(matrix).map(el -> el.clone()).toArray($ -> matrix.clone()); }
static int[][] deepCopy(int[][] matrix) { return Arrays.stream(matrix).map(int[]::clone).toArray($ -> matrix.clone()); }
static long[][] deepCopy(long[][] matrix) { return Arrays.stream(matrix).map(long[]::clone).toArray($ -> matrix.clone()); }
private static void sort(int[][] arr){ Arrays.sort(arr, Comparator.comparingDouble(o -> o[0])); }
private static void sort(long[][] arr){ Arrays.sort(arr, Comparator.comparingDouble(o -> o[0])); }
private static <T>void rSort(T[] arr) { Arrays.sort(arr, Collections.reverseOrder()); }
private static void customSort(int[][] arr) {
Arrays.sort(arr, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if (a[0] == b[0]) return Integer.compare(a[1], b[1]);
return Integer.compare(a[0], b[0]);
}
});
}
public static int[] swap(int[] arr, int left, int right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
return arr;
}
public static char[] swap(char[] arr, int left, int right) {
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
return arr;
}
public static int[] reverse(int[] arr, int left, int right) {
while (left < right) {
int temp = arr[left];
arr[left++] = arr[right];
arr[right--] = temp;
}
return arr;
}
public static boolean findNextPermutation(int[] data) {
if (data.length <= 1) return false;
int last = data.length - 2;
while (last >= 0) {
if (data[last] < data[last + 1]) break;
last--;
}
if (last < 0) return false;
int nextGreater = data.length - 1;
for (int i = data.length - 1; i > last; i--) {
if (data[i] > data[last]) {
nextGreater = i;
break;
}
}
data = swap(data, nextGreater, last);
data = reverse(data, last + 1, data.length - 1);
return true;
}
public static int biSearch(int[] dt, int target){
int left=0, right=dt.length-1;
int mid=-1;
while(left<=right){
mid = (right+left)/2;
if(dt[mid] == target) return mid;
if(dt[mid] < target) left=mid+1;
else right=mid-1;
}
return -1;
}
public static int biSearchMax(long[] dt, long target){
int left=-1, right=dt.length;
int mid=-1;
while((right-left)>1){
mid = left + (right-left)/2;
if(dt[mid] <= target) left=mid;
else right=mid;
}
return left;
}
public static int biSearchMaxAL(ArrayList<Integer> dt, long target){
int left=-1, right=dt.size();
int mid=-1;
while((right-left)>1){
mid = left + (right-left)/2;
if(dt.get(mid) <= target) left=mid;
else right=mid;
}
return left;
}
private static <T>void fill(T[][] ob, T res){for(int i=0;i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(boolean[][] ob,boolean res){for(int i=0;i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(int[][] ob, int res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(long[][] ob, long res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(char[][] ob, char res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(double[][] ob, double res){for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(int[][][] ob,int res){for(int i=0;i<ob.length;i++){for(int j=0;j<ob[0].length;j++){for(int k=0;k<ob[0][0].length;k++){ob[i][j][k]=res;}}}}
private static void fill(long[][][] ob,long res){for(int i=0;i<ob.length;i++){for(int j=0;j<ob[0].length;j++){for(int k=0;k<ob[0][0].length;k++){ob[i][j][k]=res;}}}}
private static <T>void fill(T[][][] ob,T res){for(int i=0;i<ob.length;i++){for(int j=0;j<ob[0].length;j++){for(int k=0;k<ob[0][0].length;k++){ob[i][j][k]=res;}}}}
private static void fill_parent(int[] ob){ for(int i=0; i<ob.length; i++) ob[i]=i; }
private static boolean same3(long a, long b, long c){
if(a!=b) return false;
if(b!=c) return false;
if(c!=a) return false;
return true;
}
private static boolean dif3(long a, long b, long c){
if(a==b) return false;
if(b==c) return false;
if(c==a) return false;
return true;
}
private static double hypotenuse(double a, double b){
return Math.sqrt(a*a+b*b);
}
private static long factorial(int n) {
long ans=1;
for(long i=n; i>0; i--){ ans*=i; }
return ans;
}
private static long facMod(int n, long mod) {
long ans=1;
for(long i=n; i>0; i--) ans = (ans * i) % mod;
return ans;
}
private static long lcm(long m, long n){
long ans = m/gcd(m,n);
ans *= n;
return ans;
}
private static long gcd(long m, long n) {
if(m < n) return gcd(n, m);
if(n == 0) return m;
return gcd(n, m % n);
}
private static boolean isPrime(long a){
if(a==1) return false;
for(int i=2; i<=Math.sqrt(a); i++){ if(a%i == 0) return false; }
return true;
}
static long modInverse(long a, long mod) {
/* Fermat's little theorem: a^(MOD-1) => 1
Therefore (divide both sides by a): a^(MOD-2) => a^(-1) */
return binpowMod(a, mod - 2, mod);
}
static long binpowMod(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if (b % 2 == 1) res = (res * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return res;
}
private static int getDigit2(long num){
long cf = 1; int d=0;
while(num >= cf){ d++; cf = 1<<d; }
return d;
}
private static int getDigit10(long num){
long cf = 1; int d=0;
while(num >= cf){ d++; cf*=10; }
return d;
}
private static boolean isInArea(int y, int x, int h, int w){
if(y<0) return false;
if(x<0) return false;
if(y>=h) return false;
if(x>=w) return false;
return true;
}
private static ArrayList<Integer> generatePrimes(int n) {
int[] lp = new int[n + 1];
ArrayList<Integer> pr = new ArrayList<>();
for (int i = 2; i <= n; ++i) {
if (lp[i] == 0) {
lp[i] = i;
pr.add(i);
}
for (int j = 0; j < pr.size() && pr.get(j) <= lp[i] && i * pr.get(j) <= n; ++j) {
lp[i * pr.get(j)] = pr.get(j);
}
}
return pr;
}
static long nPrMod(int n, int r, long MOD) {
long res = 1;
for (int i = (n - r + 1); i <= n; i++) {
res = (res * i) % MOD;
}
return res;
}
static long nCr(int n, int r) {
if (r > (n - r))
r = n - r;
long ans = 1;
for (int i = 1; i <= r; i++) {
ans *= n;
ans /= i;
n--;
}
return ans;
}
static long nCrMod(int n, int r, long MOD) {
long rFactorial = nPrMod(r, r, MOD);
long first = nPrMod(n, r, MOD);
long second = binpowMod(rFactorial, MOD-2, MOD);
return (first * second) % MOD;
}
static void printBitRepr(int n) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < 32; i++) {
int mask = (1 << i);
res.append((mask & n) == 0 ? "0" : "1");
}
out.println(res);
}
static String bitString(int n) {return Integer.toBinaryString(n);}
static int setKthBitToOne(int n, int k) { return (n | (1 << k)); } // zero indexed
static int setKthBitToZero(int n, int k) { return (n & ~(1 << k)); }
static int invertKthBit(int n, int k) { return (n ^ (1 << k)); }
static boolean isPowerOfTwo(int n) { return (n & (n - 1)) == 0; }
static HashMap<Character, Integer> counts(String word) {
HashMap<Character, Integer> counts = new HashMap<>();
for (int i = 0; i < word.length(); i++) counts.merge(word.charAt(i), 1, Integer::sum);
return counts;
}
static HashMap<Integer, Integer> counts(int[] arr) {
HashMap<Integer, Integer> counts = new HashMap<>();
for (int value : arr) counts.merge(value, 1, Integer::sum);
return counts;
}
static HashMap<Long, Integer> counts(long[] arr) {
HashMap<Long, Integer> counts = new HashMap<>();
for (long l : arr) counts.merge(l, 1, Integer::sum);
return counts;
}
static HashMap<Character, Integer> counts(char[] arr) {
HashMap<Character, Integer> counts = new HashMap<>();
for (char c : arr) counts.merge(c, 1, Integer::sum);
return counts;
}
static long hash(int x, int y) {
return x* 1_000_000_000L +y;
}
static final Random random = new Random();
static void sort(int[] a) {
int n = a.length;// shuffle, then sort
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void sort(long[] arr) {
shuffleArray(arr);
Arrays.sort(arr);
}
static void shuffleArray(long[] arr) {
int n = arr.length;
for(int i=0; i<n; ++i){
long tmp = arr[i];
int randomPos = i + random.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
}
static class Tuple implements Comparable<Tuple> {
int a;
int b;
int c;
public Tuple(int a, int b) {
this.a = a;
this.b = b;
this.c = 0;
}
public Tuple(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public int getA() { return a; }
public int getB() { return b; }
public int getC() { return c; }
public int compareTo(Tuple other) {
if (this.a == other.a) {
if (this.b == other.b) return Long.compare(this.c, other.c);
return Long.compare(this.b, other.b);
}
return Long.compare(this.a, other.a);
}
@Override
public int hashCode() { return Arrays.deepHashCode(new Integer[]{a, b, c}); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Tuple)) return false;
Tuple pairo = (Tuple) o;
return (this.a == pairo.a && this.b == pairo.b && this.c == pairo.c);
}
@Override
public String toString() { return String.format("(%d %d %d) ", this.a, this.b, this.c); }
}
private static int abs(int a){ return (a>=0) ? a: -a; }
private static int min(int... ins){ int min = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] < min) min = ins[i]; } return min; }
private static int max(int... ins){ int max = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] > max) max = ins[i]; } return max; }
private static int sum(int... ins){ int total = 0; for (int v : ins) { total += v; } return total; }
private static long abs(long a){ return (a>=0) ? a: -a; }
private static long min(long... ins){ long min = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] < min) min = ins[i]; } return min; }
private static long max(long... ins){ long max = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] > max) max = ins[i]; } return max; }
private static long sum(long... ins){ long total = 0; for (long v : ins) { total += v; } return total; }
private static double abs(double a){ return (a>=0) ? a: -a; }
private static double min(double... ins){ double min = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] < min) min = ins[i]; } return min; }
private static double max(double... ins){ double max = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] > max) max = ins[i]; } return max; }
private static double sum(double... ins){ double total = 0; for (double v : ins) { total += v; } return total; }
}
| Java | ["10\ncodeforces\ndodivthree", "7\nabaca?b\nzabbbcc", "9\nbambarbia\nhellocode", "10\ncode??????\n??????test"] | 2 seconds | ["5\n7 8\n4 9\n2 2\n9 10\n3 1", "5\n6 5\n2 3\n4 6\n7 4\n1 2", "0", "10\n6 2\n1 6\n7 3\n3 5\n4 8\n9 7\n5 1\n2 4\n10 9\n8 10"] | null | Java 11 | standard input | [
"implementation",
"greedy"
] | 6bf3e5a542ebce81c1e6ce7260644a3c | The first line contains $$$n$$$ ($$$1 \le n \le 150000$$$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $$$l$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th left boot. The third line contains the string $$$r$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th right boot. | 1,500 | Print $$$k$$$ — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $$$k$$$ lines should contain pairs $$$a_j, b_j$$$ ($$$1 \le a_j, b_j \le n$$$). The $$$j$$$-th of these lines should contain the index $$$a_j$$$ of the left boot in the $$$j$$$-th pair and index $$$b_j$$$ of the right boot in the $$$j$$$-th pair. All the numbers $$$a_j$$$ should be distinct (unique), all the numbers $$$b_j$$$ should be distinct (unique). If there are many optimal answers, print any of them. | standard output | |
PASSED | 75db6606ff91fb679533dc4a0bf12d51 | train_001.jsonl | 1553006100 | There are $$$n$$$ left boots and $$$n$$$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $$$l$$$ and $$$r$$$, both of length $$$n$$$. The character $$$l_i$$$ stands for the color of the $$$i$$$-th left boot and the character $$$r_i$$$ stands for the color of the $$$i$$$-th right boot.A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair. | 256 megabytes | import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class Application1
{
public static String Reduce(String s)
{
int k;
int[]temp=new int[256];
List<Character>arr=new ArrayList<Character>();
for(k=97;k<=122;k++)
{
temp[k]=0;
}
for(k=0;k<s.length();k++)
{
if(temp[s.charAt(k)]==0&&s.charAt(k)!='?')
{
temp[s.charAt(k)]++;
arr.add(s.charAt(k));
}
}
StringBuilder str=new StringBuilder();
for(Character i: arr)
{
str.append(i);
}
String r=str.toString();
return r;
}
public static int BinarySearch(String target,char a)
{
if(target.length()==0)
{
return -1;
}
int low=0,mid,high=target.length()-1;
while(low<high)
{
mid=(low+high+2)/2;
if(target.charAt(mid-1)==a)
{
return mid-1;
}
else if(target.charAt(mid-1)<a)
{
low=mid;
}
else
{
high=mid-2;
}
}
if(target.charAt(low)==a)
{
return low;
}
else
{
return -1;
}
}
public static String Common(String target,String sorted,List<Character>a)
{
int k=0;
if(sorted.length()==0)
{
return "";
}
List<Character>b=new ArrayList<Character>();
while(k<sorted.length())
{
if(BinarySearch(target,sorted.charAt(k))!=-1)
{
a.add(sorted.charAt(k));
k++;
}
else if(BinarySearch(target,sorted.charAt(k))==-1)
{
b.add(sorted.charAt(k));
k++;
}
}
StringBuilder u=new StringBuilder();
for(Character i:b)
{
u.append(i);
}
return u.toString();
}
public static void Index(String str,char a,List<Integer>arr)
{
int k=0;
while(k<str.length())
{
if(str.charAt(k)==a)
{
arr.add(k+1);
k++;
}
else
{
k++;
}
}
return;
}
public static void total_index(String str,String u,List<Integer>arr)
{
int k=0,index=0;
while(k<u.length())
{
while(index<str.length())
{
if(str.charAt(index)==u.charAt(k))
{
arr.add(index+1);
index++;
}
else
{
index++;
}
}
k++;
index=0;
}
}
public static void main(String[] args)
{
String str1,str2,r1,r2,s1,s2,common,u1,u2;
List<Integer>left=new ArrayList<Integer>();
List<Integer>right=new ArrayList<Integer>();
List<Integer>in1=new ArrayList<Integer>();
List<Integer>in2=new ArrayList<Integer>();
List<Integer>up=new ArrayList<Integer>();
List<Integer>down=new ArrayList<Integer>();
int k,m,n,length;
Scanner input=new Scanner(System.in);
length=input.nextInt();
str1=input.next();
str2=input.next();
Index(str1,'?',in1);
Index(str2,'?',in2);
r1=Reduce(str1);
r2=Reduce(str2);
char[]arr=r1.toCharArray();
Arrays.sort(arr);
s1=new String(arr);
arr=r2.toCharArray();
Arrays.sort(arr);
s2=new String(arr);
List<Character>cm=new ArrayList<Character>();
u1=Common(s1,s2,cm);
StringBuilder s=new StringBuilder();
for(Character i:cm)
{
s.append(i);
}
common=s.toString();
u2=Common(s2,s1,cm);
total_index(str1,u2,up);
total_index(str2,u1,down);
k=0;
m=0;
n=0;
while(k<common.length())
{
List<Integer>l=new ArrayList<Integer>();
List<Integer>r=new ArrayList<Integer>();
Index(str1,common.charAt(k),l);
Index(str2,common.charAt(k),r);
while(m<l.size()&&n<r.size())
{
left.add(l.get(m));
right.add(r.get(n));
m++;
n++;
}
if(m<l.size())
{
while(m<l.size())
{
up.add(l.get(m));
m++;
}
}
else if(n<r.size())
{
while(n<r.size())
{
down.add(r.get(n));
n++;
}
}
k++;
m=0;
n=0;
}
m=0;
n=0;
while(m<up.size()&&n<in2.size())
{
left.add(up.get(m));
right.add(in2.get(n));
m++;
n++;
}
int p=0,q=0;
while(p<in1.size()&&q<down.size())
{
left.add(in1.get(p));
right.add(down.get(q));
p++;
q++;
}
if(p<in1.size()&&n<in2.size())
{
while(p<in1.size()&&n<in2.size())
{
left.add(in1.get(p));
right.add(in2.get(n));
p++;
n++;
}
}
System.out.println(left.size());
for(k=0;k<left.size();k++)
{
System.out.println(left.get(k)+" "+right.get(k));
}
input.close();
}
}
| Java | ["10\ncodeforces\ndodivthree", "7\nabaca?b\nzabbbcc", "9\nbambarbia\nhellocode", "10\ncode??????\n??????test"] | 2 seconds | ["5\n7 8\n4 9\n2 2\n9 10\n3 1", "5\n6 5\n2 3\n4 6\n7 4\n1 2", "0", "10\n6 2\n1 6\n7 3\n3 5\n4 8\n9 7\n5 1\n2 4\n10 9\n8 10"] | null | Java 11 | standard input | [
"implementation",
"greedy"
] | 6bf3e5a542ebce81c1e6ce7260644a3c | The first line contains $$$n$$$ ($$$1 \le n \le 150000$$$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $$$l$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th left boot. The third line contains the string $$$r$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th right boot. | 1,500 | Print $$$k$$$ — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $$$k$$$ lines should contain pairs $$$a_j, b_j$$$ ($$$1 \le a_j, b_j \le n$$$). The $$$j$$$-th of these lines should contain the index $$$a_j$$$ of the left boot in the $$$j$$$-th pair and index $$$b_j$$$ of the right boot in the $$$j$$$-th pair. All the numbers $$$a_j$$$ should be distinct (unique), all the numbers $$$b_j$$$ should be distinct (unique). If there are many optimal answers, print any of them. | standard output | |
PASSED | b966adc8632723bbfcf2c62885d78cf0 | train_001.jsonl | 1553006100 | There are $$$n$$$ left boots and $$$n$$$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $$$l$$$ and $$$r$$$, both of length $$$n$$$. The character $$$l_i$$$ stands for the color of the $$$i$$$-th left boot and the character $$$r_i$$$ stands for the color of the $$$i$$$-th right boot.A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair. | 256 megabytes | /*
Author: Anthony Ngene
Created: 29/09/2020 - 05:36
*/
import java.io.*;
import java.util.*;
public class D {
// checks: 1. edge cases 2. overflow 3. possible errors (e.g 1/0, arr[out]) 4. time/space complexity
void solver() throws IOException {
int n = in.intNext();
HashMap<Character, Stack<Integer>> lefts = new HashMap<>();
String left = in.next();
for (int i = 1; i < n + 1; i++) {
char ch = left.charAt(i - 1);
if (!lefts.containsKey(ch)) lefts.put(ch, new Stack<>());
lefts.get(ch).add(i);
}
// out.println(lefts);
String right = in.next();
int count = 0;
Stack<Integer> unpairedRight = new Stack<>();
Stack<Integer> rightQuestions = new Stack<>();
StringBuilder pairs = new StringBuilder();
for (int i = 1; i < n + 1; i++) {
char ch = right.charAt(i - 1);
if (ch == '?') {
rightQuestions.add(i);
continue;
}
if (lefts.containsKey(ch) && !lefts.get(ch).isEmpty()) {
count++;
pairs.append(lefts.get(ch).pop()).append(" ").append(i).append("\n");
} else
unpairedRight.add(i);
}
Stack<Integer> unpairedLeft = new Stack<>();
for (char k: lefts.keySet()) if (k != '?') unpairedLeft.addAll(lefts.get(k));
// out.println(unpairedLeft);
// out.println(unpairedRight);
while (!unpairedLeft.isEmpty() && !rightQuestions.isEmpty()) {
count++;
pairs.append(unpairedLeft.pop()).append(" ").append(rightQuestions.pop()).append("\n");
}
while (!unpairedRight.isEmpty() && lefts.containsKey('?') && !lefts.get('?').isEmpty()) {
count++;
pairs.append(lefts.get('?').pop()).append(" ").append(unpairedRight.pop()).append("\n");
}
while (!rightQuestions.isEmpty() && lefts.containsKey('?') && !lefts.get('?').isEmpty()) {
count++;
pairs.append(lefts.get('?').pop()).append(" ").append(rightQuestions.pop()).append("\n");
}
out.println(count);
out.println(pairs);
}
// Generated Code Below:
private static final FastWriter out = new FastWriter();
private static FastScanner in;
static ArrayList<Integer>[] adj;
private static long e97 = (long)1e9 + 7;
public static void main(String[] args) throws IOException {
in = new FastScanner();
new D().solver();
out.close();
}
static class FastWriter {
private static final int IO_BUFFERS = 128 * 1024;
private final StringBuilder out;
public FastWriter() { out = new StringBuilder(IO_BUFFERS); }
public FastWriter p(Object object) { out.append(object); return this; }
public FastWriter p(String format, Object... args) { out.append(String.format(format, args)); return this; }
public FastWriter pp(Object... args) { for (Object ob : args) { out.append(ob).append(" "); } out.append("\n"); return this; }
public FastWriter pp(int[] args) { for (int ob : args) { out.append(ob).append(" "); } out.append("\n"); return this; }
public FastWriter pp(long[] args) { for (long ob : args) { out.append(ob).append(" "); } out.append("\n"); return this; }
public FastWriter pp(char[] args) { for (char ob : args) { out.append(ob).append(" "); } out.append("\n"); return this; }
public void println(long[] arr) { for(long e: arr) out.append(e).append(" "); out.append("\n"); }
public void println(int[] arr) { for(int e: arr) out.append(e).append(" "); out.append("\n"); }
public void println(char[] arr) { for(char e: arr) out.append(e).append(" "); out.append("\n"); }
public void println(double[] arr) { for(double e: arr) out.append(e).append(" "); out.append("\n"); }
public void println(boolean[] arr) { for(boolean e: arr) out.append(e).append(" "); out.append("\n"); }
public <T>void println(T[] arr) { for(T e: arr) out.append(e).append(" "); out.append("\n"); }
public void println(long[][] arr) { for (long[] row: arr) out.append(Arrays.toString(row)).append("\n"); }
public void println(int[][] arr) { for (int[] row: arr) out.append(Arrays.toString(row)).append("\n"); }
public void println(char[][] arr) { for (char[] row: arr) out.append(Arrays.toString(row)).append("\n"); }
public void println(double[][] arr) { for (double[] row: arr) out.append(Arrays.toString(row)).append("\n"); }
public <T>void println(T[][] arr) { for (T[] row: arr) out.append(Arrays.toString(row)).append("\n"); }
public FastWriter println(Object object) { out.append(object).append("\n"); return this; }
public void toFile(String fileName) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(out.toString());
writer.close();
}
public void close() throws IOException { System.out.print(out); }
}
static class FastScanner {
private InputStream sin = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
public FastScanner(){}
public FastScanner(String filename) throws FileNotFoundException {
File file = new File(filename);
sin = new FileInputStream(file);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = sin.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long longNext() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b) || b == ':'){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int intNext() {
long nl = longNext();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double doubleNext() { return Double.parseDouble(next());}
public long[] nextLongArray(final int n){
final long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = longNext();
return a;
}
public int[] nextIntArray(final int n){
final int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = intNext();
return a;
}
public double[] nextDoubleArray(final int n){
final double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = doubleNext();
return a;
}
public ArrayList<Integer>[] getAdj(int n) {
ArrayList<Integer>[] adj = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) adj[i] = new ArrayList<>();
return adj;
}
public ArrayList<Integer>[] adjacencyList(int nodes, int edges) throws IOException {
return adjacencyList(nodes, edges, false);
}
public ArrayList<Integer>[] adjacencyList(int nodes, int edges, boolean isDirected) throws IOException {
adj = getAdj(nodes);
for (int i = 0; i < edges; i++) {
int a = intNext(), b = intNext();
adj[a].add(b);
if (!isDirected) adj[b].add(a);
}
return adj;
}
}
static class u {
public static int upperBound(long[] array, long obj) {
int l = 0, r = array.length - 1;
while (r - l >= 0) {
int c = (l + r) / 2;
if (obj < array[c]) {
r = c - 1;
} else {
l = c + 1;
}
}
return l;
}
public static int upperBound(ArrayList<Long> array, long obj) {
int l = 0, r = array.size() - 1;
while (r - l >= 0) {
int c = (l + r) / 2;
if (obj < array.get(c)) {
r = c - 1;
} else {
l = c + 1;
}
}
return l;
}
public static int lowerBound(long[] array, long obj) {
int l = 0, r = array.length - 1;
while (r - l >= 0) {
int c = (l + r) / 2;
if (obj <= array[c]) {
r = c - 1;
} else {
l = c + 1;
}
}
return l;
}
public static int lowerBound(ArrayList<Long> array, long obj) {
int l = 0, r = array.size() - 1;
while (r - l >= 0) {
int c = (l + r) / 2;
if (obj <= array.get(c)) {
r = c - 1;
} else {
l = c + 1;
}
}
return l;
}
static <T> T[][] deepCopy(T[][] matrix) { return Arrays.stream(matrix).map(el -> el.clone()).toArray($ -> matrix.clone()); }
static int[][] deepCopy(int[][] matrix) { return Arrays.stream(matrix).map(int[]::clone).toArray($ -> matrix.clone()); }
static long[][] deepCopy(long[][] matrix) { return Arrays.stream(matrix).map(long[]::clone).toArray($ -> matrix.clone()); }
private static void sort(int[][] arr){ Arrays.sort(arr, Comparator.comparingDouble(o -> o[0])); }
private static void sort(long[][] arr){ Arrays.sort(arr, Comparator.comparingDouble(o -> o[0])); }
private static <T>void rSort(T[] arr) { Arrays.sort(arr, Collections.reverseOrder()); }
private static void customSort(int[][] arr) {
Arrays.sort(arr, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if (a[0] == b[0]) return Integer.compare(a[1], b[1]);
return Integer.compare(a[0], b[0]);
}
});
}
public static int[] swap(int[] arr, int left, int right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
return arr;
}
public static char[] swap(char[] arr, int left, int right) {
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
return arr;
}
public static int[] reverse(int[] arr, int left, int right) {
while (left < right) {
int temp = arr[left];
arr[left++] = arr[right];
arr[right--] = temp;
}
return arr;
}
public static boolean findNextPermutation(int[] data) {
if (data.length <= 1) return false;
int last = data.length - 2;
while (last >= 0) {
if (data[last] < data[last + 1]) break;
last--;
}
if (last < 0) return false;
int nextGreater = data.length - 1;
for (int i = data.length - 1; i > last; i--) {
if (data[i] > data[last]) {
nextGreater = i;
break;
}
}
data = swap(data, nextGreater, last);
data = reverse(data, last + 1, data.length - 1);
return true;
}
public static int biSearch(int[] dt, int target){
int left=0, right=dt.length-1;
int mid=-1;
while(left<=right){
mid = (right+left)/2;
if(dt[mid] == target) return mid;
if(dt[mid] < target) left=mid+1;
else right=mid-1;
}
return -1;
}
public static int biSearchMax(long[] dt, long target){
int left=-1, right=dt.length;
int mid=-1;
while((right-left)>1){
mid = left + (right-left)/2;
if(dt[mid] <= target) left=mid;
else right=mid;
}
return left;
}
public static int biSearchMaxAL(ArrayList<Integer> dt, long target){
int left=-1, right=dt.size();
int mid=-1;
while((right-left)>1){
mid = left + (right-left)/2;
if(dt.get(mid) <= target) left=mid;
else right=mid;
}
return left;
}
private static <T>void fill(T[][] ob, T res){for(int i=0;i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(boolean[][] ob,boolean res){for(int i=0;i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(int[][] ob, int res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(long[][] ob, long res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(char[][] ob, char res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(double[][] ob, double res){for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; }}}
private static void fill(int[][][] ob,int res){for(int i=0;i<ob.length;i++){for(int j=0;j<ob[0].length;j++){for(int k=0;k<ob[0][0].length;k++){ob[i][j][k]=res;}}}}
private static void fill(long[][][] ob,long res){for(int i=0;i<ob.length;i++){for(int j=0;j<ob[0].length;j++){for(int k=0;k<ob[0][0].length;k++){ob[i][j][k]=res;}}}}
private static <T>void fill(T[][][] ob,T res){for(int i=0;i<ob.length;i++){for(int j=0;j<ob[0].length;j++){for(int k=0;k<ob[0][0].length;k++){ob[i][j][k]=res;}}}}
private static void fill_parent(int[] ob){ for(int i=0; i<ob.length; i++) ob[i]=i; }
private static boolean same3(long a, long b, long c){
if(a!=b) return false;
if(b!=c) return false;
if(c!=a) return false;
return true;
}
private static boolean dif3(long a, long b, long c){
if(a==b) return false;
if(b==c) return false;
if(c==a) return false;
return true;
}
private static double hypotenuse(double a, double b){
return Math.sqrt(a*a+b*b);
}
private static long factorial(int n) {
long ans=1;
for(long i=n; i>0; i--){ ans*=i; }
return ans;
}
private static long facMod(int n, long mod) {
long ans=1;
for(long i=n; i>0; i--) ans = (ans * i) % mod;
return ans;
}
private static long lcm(long m, long n){
long ans = m/gcd(m,n);
ans *= n;
return ans;
}
private static long gcd(long m, long n) {
if(m < n) return gcd(n, m);
if(n == 0) return m;
return gcd(n, m % n);
}
private static boolean isPrime(long a){
if(a==1) return false;
for(int i=2; i<=Math.sqrt(a); i++){ if(a%i == 0) return false; }
return true;
}
static long modInverse(long a, long mod) {
/* Fermat's little theorem: a^(MOD-1) => 1
Therefore (divide both sides by a): a^(MOD-2) => a^(-1) */
return binpowMod(a, mod - 2, mod);
}
static long binpowMod(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if (b % 2 == 1) res = (res * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return res;
}
private static int getDigit2(long num){
long cf = 1; int d=0;
while(num >= cf){ d++; cf = 1<<d; }
return d;
}
private static int getDigit10(long num){
long cf = 1; int d=0;
while(num >= cf){ d++; cf*=10; }
return d;
}
private static boolean isInArea(int y, int x, int h, int w){
if(y<0) return false;
if(x<0) return false;
if(y>=h) return false;
if(x>=w) return false;
return true;
}
private static ArrayList<Integer> generatePrimes(int n) {
int[] lp = new int[n + 1];
ArrayList<Integer> pr = new ArrayList<>();
for (int i = 2; i <= n; ++i) {
if (lp[i] == 0) {
lp[i] = i;
pr.add(i);
}
for (int j = 0; j < pr.size() && pr.get(j) <= lp[i] && i * pr.get(j) <= n; ++j) {
lp[i * pr.get(j)] = pr.get(j);
}
}
return pr;
}
static long nPrMod(int n, int r, long MOD) {
long res = 1;
for (int i = (n - r + 1); i <= n; i++) {
res = (res * i) % MOD;
}
return res;
}
static long nCr(int n, int r) {
if (r > (n - r))
r = n - r;
long ans = 1;
for (int i = 1; i <= r; i++) {
ans *= n;
ans /= i;
n--;
}
return ans;
}
static long nCrMod(int n, int r, long MOD) {
long rFactorial = nPrMod(r, r, MOD);
long first = nPrMod(n, r, MOD);
long second = binpowMod(rFactorial, MOD-2, MOD);
return (first * second) % MOD;
}
static void printBitRepr(int n) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < 32; i++) {
int mask = (1 << i);
res.append((mask & n) == 0 ? "0" : "1");
}
out.println(res);
}
static String bitString(int n) {return Integer.toBinaryString(n);}
static int setKthBitToOne(int n, int k) { return (n | (1 << k)); } // zero indexed
static int setKthBitToZero(int n, int k) { return (n & ~(1 << k)); }
static int invertKthBit(int n, int k) { return (n ^ (1 << k)); }
static boolean isPowerOfTwo(int n) { return (n & (n - 1)) == 0; }
static HashMap<Character, Integer> counts(String word) {
HashMap<Character, Integer> counts = new HashMap<>();
for (int i = 0; i < word.length(); i++) counts.merge(word.charAt(i), 1, Integer::sum);
return counts;
}
static HashMap<Integer, Integer> counts(int[] arr) {
HashMap<Integer, Integer> counts = new HashMap<>();
for (int value : arr) counts.merge(value, 1, Integer::sum);
return counts;
}
static HashMap<Long, Integer> counts(long[] arr) {
HashMap<Long, Integer> counts = new HashMap<>();
for (long l : arr) counts.merge(l, 1, Integer::sum);
return counts;
}
static HashMap<Character, Integer> counts(char[] arr) {
HashMap<Character, Integer> counts = new HashMap<>();
for (char c : arr) counts.merge(c, 1, Integer::sum);
return counts;
}
static long hash(int x, int y) {
return x* 1_000_000_000L +y;
}
static final Random random = new Random();
static void sort(int[] a) {
int n = a.length;// shuffle, then sort
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void sort(long[] arr) {
shuffleArray(arr);
Arrays.sort(arr);
}
static void shuffleArray(long[] arr) {
int n = arr.length;
for(int i=0; i<n; ++i){
long tmp = arr[i];
int randomPos = i + random.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
}
static class Tuple implements Comparable<Tuple> {
int a;
int b;
int c;
public Tuple(int a, int b) {
this.a = a;
this.b = b;
this.c = 0;
}
public Tuple(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public int getA() { return a; }
public int getB() { return b; }
public int getC() { return c; }
public int compareTo(Tuple other) {
if (this.a == other.a) {
if (this.b == other.b) return Long.compare(this.c, other.c);
return Long.compare(this.b, other.b);
}
return Long.compare(this.a, other.a);
}
@Override
public int hashCode() { return Arrays.deepHashCode(new Integer[]{a, b, c}); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Tuple)) return false;
Tuple pairo = (Tuple) o;
return (this.a == pairo.a && this.b == pairo.b && this.c == pairo.c);
}
@Override
public String toString() { return String.format("(%d %d %d) ", this.a, this.b, this.c); }
}
private static int abs(int a){ return (a>=0) ? a: -a; }
private static int min(int... ins){ int min = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] < min) min = ins[i]; } return min; }
private static int max(int... ins){ int max = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] > max) max = ins[i]; } return max; }
private static int sum(int... ins){ int total = 0; for (int v : ins) { total += v; } return total; }
private static long abs(long a){ return (a>=0) ? a: -a; }
private static long min(long... ins){ long min = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] < min) min = ins[i]; } return min; }
private static long max(long... ins){ long max = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] > max) max = ins[i]; } return max; }
private static long sum(long... ins){ long total = 0; for (long v : ins) { total += v; } return total; }
private static double abs(double a){ return (a>=0) ? a: -a; }
private static double min(double... ins){ double min = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] < min) min = ins[i]; } return min; }
private static double max(double... ins){ double max = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] > max) max = ins[i]; } return max; }
private static double sum(double... ins){ double total = 0; for (double v : ins) { total += v; } return total; }
}
| Java | ["10\ncodeforces\ndodivthree", "7\nabaca?b\nzabbbcc", "9\nbambarbia\nhellocode", "10\ncode??????\n??????test"] | 2 seconds | ["5\n7 8\n4 9\n2 2\n9 10\n3 1", "5\n6 5\n2 3\n4 6\n7 4\n1 2", "0", "10\n6 2\n1 6\n7 3\n3 5\n4 8\n9 7\n5 1\n2 4\n10 9\n8 10"] | null | Java 11 | standard input | [
"implementation",
"greedy"
] | 6bf3e5a542ebce81c1e6ce7260644a3c | The first line contains $$$n$$$ ($$$1 \le n \le 150000$$$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $$$l$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th left boot. The third line contains the string $$$r$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th right boot. | 1,500 | Print $$$k$$$ — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $$$k$$$ lines should contain pairs $$$a_j, b_j$$$ ($$$1 \le a_j, b_j \le n$$$). The $$$j$$$-th of these lines should contain the index $$$a_j$$$ of the left boot in the $$$j$$$-th pair and index $$$b_j$$$ of the right boot in the $$$j$$$-th pair. All the numbers $$$a_j$$$ should be distinct (unique), all the numbers $$$b_j$$$ should be distinct (unique). If there are many optimal answers, print any of them. | standard output | |
PASSED | 653853d6f9badfb143d676dc0420ecce | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes |
import java.util.Scanner;
public class TanyaAndCandies {
public static void main(String []args) {
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int []arr=new int[n];
int []left_odd=new int[n];
int []left_even=new int[n];
int []right_odd=new int[n];
int []right_even=new int[n];
for(int i=0;i<n;i++) {
arr[i]=scn.nextInt();
if(i==0) {
left_even[i]=arr[i];
}else {
if(i%2==0) {
left_even[i]=left_even[i-1]+arr[i];
left_odd[i]=left_odd[i-1];
}
else {
left_odd[i]=left_odd[i-1]+arr[i];
left_even[i]=left_even[i-1];
}
}
}
if(n>1) {
for(int i=n-1;i>=0;i--) {
if(i==n-1) {
if((n-1)%2==0) {
right_even[i]=arr[i];
}else {
right_odd[i]=arr[i];
}
}else {
if(i%2==0) {
right_even[i]=right_even[i+1]+arr[i];
right_odd[i]=right_odd[i+1];
}
else {
right_odd[i]=right_odd[i+1]+arr[i];
right_even[i]=right_even[i+1];
}
}
}
int count=0;
for(int i=0;i<n;i++) {
if(i==0) {
if(right_odd[i+1]==right_even[i+1]) {
count++;
}
}else if(i==n-1) {
if(left_odd[i-1]==left_even[i-1]) {
count++;
}
}
else {
int even=right_odd[i+1]+left_even[i-1];
int odd=right_even[i+1]+left_odd[i-1];
if(even==odd) {
count++;
}
}
}
System.out.println(count);
}else {
System.out.println(1);
}
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 24bfc293d6be594759b23f1188cbcaf6 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution
{
public static void main(String []args)throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
int n=Integer.parseInt(br.readLine());
if(n==1)
{
bw.write("1");
}
else
{
int A[]=Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
long oddcount=0,evencount=0;
long lefteven[]=new long[n];
long leftodd[]=new long[n];
long righteven[]=new long[n];
long rightodd[]=new long[n];
lefteven[0]=A[0];
leftodd[1]=A[1];
for(int i=2;i<A.length;i++)
{
if(i%2==0)
lefteven[i]=lefteven[i-2]+A[i];
else
leftodd[i]=leftodd[i-2]+A[i];
}
if(n%2==0)
{
rightodd[n-1]=A[n-1];
righteven[n-2]=A[n-2];
}
else
{
righteven[n-1]=A[n-1];
rightodd[n-2]=A[n-2];
}
for(int i=A.length-3;i>=0;i--)
{
if(i%2==0)
righteven[i]=righteven[i+2]+A[i];
else
rightodd[i]=rightodd[i+2]+A[i];
}
long c=0;
for(int i=1;i<A.length-1;i++)
{
if(i%2==1)
{
if((leftodd[i]-A[i]+righteven[i+1])==(lefteven[i-1]-A[i]+rightodd[i]))
c++;
}
else
{
if((lefteven[i]-A[i]+rightodd[i+1])==(leftodd[i-1]-A[i]+righteven[i]))
c++;
}
}
if(rightodd[1]==(righteven[0]-A[0]))
c++;
if(n%2==0)
{
if(lefteven[n-2]==(leftodd[n-1]-A[n-1]))
c++;
}
else
{
if(leftodd[n-2]==(lefteven[n-1]-A[n-1]))
c++;
}
bw.write(Long.toString(c));
}
br.close();
bw.close();
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | c59cbe90cd01056607fce21a09dd3864 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes |
/**
* Date: 19 Feb, 2019
* Link:
*
* @author Prasad-Chaudhari
* @linkedIn: https://www.linkedin.com/in/prasad-chaudhari-841655a6/
* @git: https://github.com/Prasad-Chaudhari
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
public class newProgram6 {
public static void main(String[] args) throws IOException {
// TODO code application logic here
FastIO in = new FastIO();
int n = in.ni();
int a[] = in.gia(n);
int pre_sum_even[] = new int[n];
int pre_sum_odd[] = new int[n];
int suf_sum_even[] = new int[n];
int suf_sum_odd[] = new int[n];
if (n == 1) {
System.out.println(1);
return;
}
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
suf_sum_even[i] = a[i];
pre_sum_even[i] = a[i];
} else {
suf_sum_odd[i] = a[i];
pre_sum_odd[i] = a[i];
}
}
for (int i = 1; i < n; i++) {
pre_sum_even[i] += pre_sum_even[i - 1];
pre_sum_odd[i] += pre_sum_odd[i - 1];
}
for (int i = n - 2; i >= 0; i--) {
suf_sum_even[i] += suf_sum_even[i + 1];
suf_sum_odd[i] += suf_sum_odd[i + 1];
}
int count = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
if (suf_sum_even[i + 1] == suf_sum_odd[i + 1]) {
count++;
}
} else if (i == n - 1) {
if (pre_sum_even[i - 1] == pre_sum_odd[i - 1]) {
count++;
}
} else {
if (pre_sum_even[i - 1] + suf_sum_odd[i + 1] == pre_sum_odd[i - 1] + suf_sum_even[i + 1]) {
count++;
}
}
}
System.out.println(count);
}
static class FastIO {
private final BufferedReader br;
private final BufferedWriter bw;
private String s[];
private int index;
private StringBuilder sb;
public FastIO() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"));
s = br.readLine().split(" ");
sb = new StringBuilder();
index = 0;
}
public int ni() throws IOException {
return Integer.parseInt(nextToken());
}
public double nd() throws IOException {
return Double.parseDouble(nextToken());
}
public long nl() throws IOException {
return Long.parseLong(nextToken());
}
public String next() throws IOException {
return nextToken();
}
public String nli() throws IOException {
try {
return br.readLine();
} catch (IOException ex) {
}
return null;
}
public int[] gia(int n) throws IOException {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public int[] gia(int n, int start, int end) throws IOException {
validate(n, start, end);
int a[] = new int[n];
for (int i = start; i < end; i++) {
a[i] = ni();
}
return a;
}
public double[] gda(int n) throws IOException {
double a[] = new double[n];
for (int i = 0; i < n; i++) {
a[i] = nd();
}
return a;
}
public double[] gda(int n, int start, int end) throws IOException {
validate(n, start, end);
double a[] = new double[n];
for (int i = start; i < end; i++) {
a[i] = nd();
}
return a;
}
public long[] gla(int n) throws IOException {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nl();
}
return a;
}
public long[] gla(int n, int start, int end) throws IOException {
validate(n, start, end);
long a[] = new long[n];
for (int i = start; i < end; i++) {
a[i] = nl();
}
return a;
}
public int[][][] gwtree(int n) throws IOException {
int m = n - 1;
int adja[][] = new int[n + 1][];
int weight[][] = new int[n + 1][];
int from[] = new int[m];
int to[] = new int[m];
int count[] = new int[n + 1];
int cost[] = new int[n + 1];
for (int i = 0; i < m; i++) {
from[i] = i + 1;
to[i] = ni();
cost[i] = ni();
count[from[i]]++;
count[to[i]]++;
}
for (int i = 0; i <= n; i++) {
adja[i] = new int[count[i]];
weight[i] = new int[count[i]];
}
for (int i = 0; i < m; i++) {
adja[from[i]][count[from[i]] - 1] = to[i];
adja[to[i]][count[to[i]] - 1] = from[i];
weight[from[i]][count[from[i]] - 1] = cost[i];
weight[to[i]][count[to[i]] - 1] = cost[i];
count[from[i]]--;
count[to[i]]--;
}
return new int[][][]{adja, weight};
}
public int[][][] gwg(int n, int m) throws IOException {
int adja[][] = new int[n + 1][];
int weight[][] = new int[n + 1][];
int from[] = new int[m];
int to[] = new int[m];
int count[] = new int[n + 1];
int cost[] = new int[n + 1];
for (int i = 0; i < m; i++) {
from[i] = ni();
to[i] = ni();
cost[i] = ni();
count[from[i]]++;
count[to[i]]++;
}
for (int i = 0; i <= n; i++) {
adja[i] = new int[count[i]];
weight[i] = new int[count[i]];
}
for (int i = 0; i < m; i++) {
adja[from[i]][count[from[i]] - 1] = to[i];
adja[to[i]][count[to[i]] - 1] = from[i];
weight[from[i]][count[from[i]] - 1] = cost[i];
weight[to[i]][count[to[i]] - 1] = cost[i];
count[from[i]]--;
count[to[i]]--;
}
return new int[][][]{adja, weight};
}
public int[][] gtree(int n) throws IOException {
int adja[][] = new int[n + 1][];
int from[] = new int[n - 1];
int to[] = new int[n - 1];
int count[] = new int[n + 1];
for (int i = 0; i < n - 1; i++) {
from[i] = i + 1;
to[i] = ni();
count[from[i]]++;
count[to[i]]++;
}
for (int i = 0; i <= n; i++) {
adja[i] = new int[count[i]];
}
for (int i = 0; i < n - 1; i++) {
adja[from[i]][--count[from[i]]] = to[i];
adja[to[i]][--count[to[i]]] = from[i];
}
return adja;
}
public int[][] gg(int n, int m) throws IOException {
int adja[][] = new int[n + 1][];
int from[] = new int[m];
int to[] = new int[m];
int count[] = new int[n + 1];
for (int i = 0; i < m; i++) {
from[i] = ni();
to[i] = ni();
count[from[i]]++;
count[to[i]]++;
}
for (int i = 0; i <= n; i++) {
adja[i] = new int[count[i]];
}
for (int i = 0; i < m; i++) {
adja[from[i]][--count[from[i]]] = to[i];
adja[to[i]][--count[to[i]]] = from[i];
}
return adja;
}
public void print(String s) throws IOException {
bw.write(s);
}
public void println(String s) throws IOException {
bw.write(s);
bw.newLine();
}
private String nextToken() throws IndexOutOfBoundsException, IOException {
if (index == s.length) {
s = br.readLine().split(" ");
index = 0;
}
return s[index++];
}
private void validate(int n, int start, int end) {
if (start < 0 || end >= n) {
throw new IllegalArgumentException();
}
}
}
static class Data implements Comparable<Data> {
int a, b;
public Data(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Data o) {
if (a == o.a) {
return Integer.compare(b, o.b);
}
return Integer.compare(a, o.a);
}
public static void sort(int a[]) {
Data d[] = new Data[a.length];
for (int i = 0; i < a.length; i++) {
d[i] = new Data(a[i], 0);
}
Arrays.sort(d);
for (int i = 0; i < a.length; i++) {
a[i] = d[i].a;
}
}
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 202b788770dc60d01a4d4c4b8ff175ea | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes |
import java.util.ArrayList;
import java.util.Scanner;
/**
* @Created by sbhowmik on 19/02/19
*/
public class TanyaAndCandies {
static public class TanyaAndCandiesObject {
int x;
int y;
public TanyaAndCandiesObject(int x, int y) {
this.x = x;
this.y = y;
}
}
static public int numberTotalCandy(int arr[]) {
ArrayList<TanyaAndCandiesObject> forwlist = new ArrayList<>();
ArrayList<TanyaAndCandiesObject> backlist = new ArrayList<>();
TanyaAndCandiesObject forw[] = new TanyaAndCandiesObject[arr.length];
TanyaAndCandiesObject back[] = new TanyaAndCandiesObject[arr.length];
if(arr.length == 1) {
return 1;
}
int evenSum = 0;
int oddSum = 0;
int count = 0;
for(int i = 0; i < arr.length; i++) {
if(i%2 == 0) {
oddSum = oddSum + arr[i];
TanyaAndCandiesObject object = new TanyaAndCandiesObject(oddSum, evenSum);
forw[i] = object;
} else {
evenSum = evenSum + arr[i];
TanyaAndCandiesObject object = new TanyaAndCandiesObject(oddSum, evenSum);
forw[i] = object;
}
}
oddSum = 0;
evenSum = 0;
for(int i = arr.length - 1; i>=0; i--) {
if(i%2 == 0) {
oddSum = oddSum + arr[i];
TanyaAndCandiesObject object = new TanyaAndCandiesObject(oddSum, evenSum);
back[i] = object;
} else {
evenSum = evenSum + arr[i];
TanyaAndCandiesObject object = new TanyaAndCandiesObject(oddSum, evenSum);
back[i] = object;
}
}
if(back[1].x == back[1].y) {
count++;
}
if(forw[arr.length - 2].x == forw[arr.length - 2].y) {
count++;
}
for(int i = 1; i < arr.length -1;i++) {
int diffX = forw[i-1].x + back[i+1].y;
int diffY = forw[i-1].y + back[i+1].x;
if(diffX == diffY) {
count++;
}
}
return count;
}
public static void main(String []args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int i = 0;
int arr[] = new int[n];
while ( i < n) {
arr[i++] = scanner.nextInt();
}
System.out.println(numberTotalCandy(arr));
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | abc57360ad6a3f697bbcead9efdbcd72 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* Created by NubbY
*/
public class A {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tk;
int n = Integer.parseInt(reader.readLine());
int[] a = new int[n];
tk = new StringTokenizer(reader.readLine());
int prefOdd = 0, prefEven = 0, sufOdd = 0, sufEven = 0;
for (int i = 0; i < n; i++){
a[i] = Integer.parseInt(tk.nextToken());
sufEven += (i % 2 == 0 ? a[i] : 0);
sufOdd += (i % 2 != 0 ? a[i] : 0);
}
if (n == 1) {
System.out.println(1);
return;
}
int sumGood = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
sufEven -= a[i];
} else {
sufOdd -= a[i];
}
if (prefOdd + sufEven == prefEven + sufOdd) sumGood ++;
if (i % 2 == 0) {
prefEven += a[i];
} else {
prefOdd += a[i];
}
}
System.out.println(sumGood);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | a4c882ec3fe54c7773623b98bea81b97 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Code
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner s= new Scanner(System.in);
int n= s.nextInt();
int[] a = new int[n];
long sume=0;
long sumo=0;
for(int i=0;i<n;i++){
a[i]=s.nextInt();
if((i+1)%2==0)
sume=sume+a[i];
else
sumo=sumo+a[i];
}
int count=0;
int temp=0;
for(int j=n-1;j>=0;j--){
if((j+1)%2==0){
sume=sume-a[j]+temp;
}
else{
sumo=sumo-a[j]+temp;
}
temp=a[j];
if(sume==sumo)
count++;
}
System.out.println(count);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 917d961ec76a434fd4743437ffb2c8b7 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int i,t=0,s=0,s1=0,s2=0;
int n=sc.nextInt();
int a[]=new int [n];
int b[]=new int [n];
for(i=0;i<n;i++){
a[i]=sc.nextInt();
s+=a[i];
if(i>1)
b[i]=b[i-2]+a[i];
else
b[i]=a[i];
}
int l;
for(i=0;i<n;i++){
s1=s-a[i];
s2=0;
if(s1%2==0){
if(i>1)
l=b[i-2]-b[i-1];
else if(i==1)
l=-b[0];
else
l=0;
if(i%2==0){
if(n%2==0&&n>=1)
s2=l+b[n-1];
else if(n>1)
s2=l+b[n-2];
}
else{
if(n%2==0&&n>=2)
s2=l+b[n-2];
else if(n>=1)
s2=l+b[n-1];
}
if(s1/2==s2)
{t++;}
}
}
System.out.println(t);
sc.close();
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 6358c303d0c9e1534153854a6247e076 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class tr2 {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt();
int []a=new int[n];
long odd=0;
long even=0;
long []e=new long[n];
long []o=new long[n];
long []pre=new long[n];
long []suf=new long[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
if(i%2==0) {
even+=a[i];
e[i]=even;
pre[i]=even;
}
else {
odd+=a[i];
o[i]=odd;
pre[i]=odd;
}
}
for(int i=n-1;i>=0;i--) {
if(i==n-1 || i==n-2)
suf[i]=a[i];
else if(i%2==0) {
suf[i]=a[i]+suf[i+2];
}
else {
suf[i]=a[i]+suf[i+2];
}
}
int ans=0;
if(n==1) {
System.out.println(1);
return;
}
for(int i=0;i<n;i++) {
long ev=0;
long od=0;
if(i==0) {
od=suf[i+1];
ev=suf[i]-a[i];
// System.out.println();
if(od==ev)
ans++;
}
else if(i==n-1) {
if((n-1)%2==0) {
ev=pre[i]-a[i];
od+=pre[i-1];
}
else {
od=pre[i]-a[i];
ev+=pre[i-1];
}
if(od==ev)
ans++;
}
else {
if((i)%2==0) {
ev=pre[i]-a[i];
od=pre[i-1];
ev+=suf[i+1];
if(i+2<n)
od+=suf[i+2];
}
else {
od=pre[i]-a[i];
ev=pre[i-1];
od+=suf[i+1];
if(i+2<n)
ev+=suf[i+2];
}
if(od==ev)
ans++;
}
// System.out.println(ans+" "+i);
}
out.println(ans);
out.flush();
}
static int gcd(int a,int b) {
if(b==0) {
return a;
}
return gcd(b,a%b);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 6299ad55873eb3da1f56794ff2a21eb3 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class b {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int n = stdin.nextInt();
int[] in = new int[n];
int[] evenf = new int[n];
int[] oddf = new int[n];
int[] evenb = new int[n];
int[] oddb = new int[n];
int e = 0, o = 0;
for (int i = 0; i < n; i ++) {
in[i] = stdin.nextInt();
if (i%2 == 0) e += in[i];
else o += in[i];
evenf[i] = e;
oddf[i] = o;
}
e = 0;
o = 0;
for (int i = n-1; i >= 0; i --) {
if (i%2 == 0) e += in[i];
else o += in[i];
evenb[i] = e;
oddb[i] = o;
}
// System.out.println(Arrays.toString(evenf));
// System.out.println(Arrays.toString(oddf));
// System.out.println(Arrays.toString(evenb));
// System.out.println(Arrays.toString(oddb));
int count = 0;
if (n < 2) {
System.out.println("1");
return;
}
for (int i = 1; i < n-1; i ++) {
if (evenf[i-1] + oddb[i+1] == oddf[i-1] + evenb[i+1]) {
// System.out.println(evenf[i-1] + oddb[i+1] + " " + (oddf[i-1] + evenb[i+1]));
count ++;
}
}
if (evenb[1] == oddb[1]) count ++;
if (evenf[n-2] == oddf[n-2]) count ++;
System.out.println(count);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | d8e879ae0e650f86eeddf78997065423 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes |
import static java.lang.System.exit;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.*;
/**
*
* @author abdelmagied
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/*
* @author abdelmagied,
*/
public class JavaApplication1 {
/**
* @param args tthe command line arguments
*/
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt() , ans = 0;
ArrayList<Integer> arr = new ArrayList<>();
int evenSuff = 0 , oddSuff = 0 , evenPref = 0 , oddPref = 0;
for(int i = 0 ; i < n ; i++){
arr.add(sc.nextInt());
if(i%2 == 0)evenSuff += arr.get(i);
else
oddSuff += arr.get(i);
}
for(int i = 0 ; i < n ; i++){
if(i%2 == 0){
evenSuff -= arr.get(i);
}else
{
oddSuff -= arr.get(i);
}
if(oddPref + evenSuff == evenPref + oddSuff){
ans++;
}
if(i%2 == 0)
evenPref+= arr.get(i);
else
oddPref += arr.get(i);
}
System.out.println(ans);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | ba1195e9ee796bc9c33cdc2cbe87463e | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Candies
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int q;
q=Integer.parseInt(in.nextLine().trim());
List<Integer> list = new ArrayList<Integer>();
int sumevenb=0;
int sumoddb=0;
for(int i = 0 ; i < q; i++)
{
list.add(in.nextInt());
if (i%2 == 0)
{
sumevenb+=list.get(i);
}
else
{
sumoddb+=list.get(i) ;
}
}
cases(q ,list,sumevenb,sumoddb);
}
public static void cases(int q,List<Integer> weight,int sumevenb, int sumoddb)
{
/*if(weight.size()==1)
{
System.out.println(0);
}
else*/
int sumodd=0,sumeven=0;
int count = 0;
for(int i = 0 ; i<q ; i++)
{
if (i%2 == 0)
{
sumevenb-=weight.get(i);
}
else
{
sumoddb-=weight.get(i) ;
}
if(sumodd+sumevenb==sumeven+sumoddb)
{
count++;
}
if (i%2 == 0)
{
sumeven+=weight.get(i);
}
else
{
sumodd+=weight.get(i) ;
}
}
System.out.println(count);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | f275a6271713907388b23ebfbf610848 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class can
{
public static void main(String args[]) throws Exception
{
FastReader sc=new FastReader();
int n=sc.nextInt();
int c=0;
int sum=0,sum0=0,sum1=0;
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
sum+=a[i];
if(i%2==1)
sum1+=a[i];
}
for(int i=0;i<n;i++)
{
if(i%2==0)
{
sum0=sum-sum1-a[i];
}
else
{
sum1=sum-sum0-a[i];
}
if(sum1==sum0)
{
c++;
}
}
System.out.print(c);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 056837940fa301e720323306a5462abb | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int ePref = 0, oPref = 0, eSuf = 0, oSuf = 0;
for (int i = 0; i < n; ++i) {
a[i]=sc.nextInt();
if ((i & 1)==1) eSuf += a[i];
else oSuf += a[i];
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if ((i & 1)==1) eSuf -= a[i];
else oSuf -= a[i];
if (ePref + oSuf == oPref + eSuf) {
++ans;
}
if ((i & 1)==1) ePref += a[i];
else oPref += a[i];
}
System.out.println(ans);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 359206ff10e663a0928033f7125cfebd | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int ePref=0,oPref=0,oSuf=0,eSuf=0;
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
if(i%2==0){
eSuf+=arr[i];
}else
{
oSuf+=arr[i];
}
}
int ans=0;
for(int i=0;i<n;i++){
if(i%2==0){
eSuf-=arr[i];
}else{
oSuf-=arr[i];
}
if(eSuf+ePref==oSuf+oPref){
ans++;
}
if(i%2==0){
oPref+=arr[i];
}else{
ePref+=arr[i];
}
}
System.out.println(ans);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 070ba9047055f86f18a75c1f8cd10e61 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.*;
public class Tanya_and_Candies {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n =sc.nextInt(),evenf=0,oddf=0,evenb=0,oddb=0,count=0;
int arr[] = new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
if(i%2==0)
oddf+=arr[i];
else
evenf+=arr[i];
}
for(int i=0;i<n;i++)
{
if(i%2==0)
oddf-=arr[i];
else
evenf-=arr[i];
if(evenf+oddb==oddf+evenb)
count++;
if(i%2==0)
oddb+=arr[i];
else
evenb+=arr[i];
}
System.out.print(count);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 90e2314c8f93741703f9b9b44381ca39 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class TanyaCandy {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
int[] C = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
int odd = 0, even = 0;
for (int n = 0; n < N; n++) {
C[n] = Integer.parseInt(st.nextToken());
if (n % 2 == 0) {
even += C[n];
} else {
odd += C[n];
}
}
int ret = 0;
for (int n = 0; n < N; n++) {
if (n % 2 == 0) {
even -= C[n];
if (even == odd) {
ret++;
}
odd += C[n];
} else {
odd -= C[n];
if (even == odd) {
ret++;
}
even += C[n];
}
}
bw.write(ret + "\n");
bw.close();
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | cab4647a33eec40ded29d85144969083 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.*;
import java.lang.Math;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class mainClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
ArrayList<Integer> inputList = new ArrayList<>();
int left = 0;
int right = 0;
int count = 0;
for (int i=0; i<r; i++) {
int m = sc.nextInt();
inputList.add(m);
if (i>0) {
if (i % 2 == 1) {
left+=m;
} else {
right += m;
}
}
}
if (left == right) {
count++;
}
for (int i=1; i<r; i++) {
if (i % 2 == 1) {
left+=inputList.get(i-1);
left-=inputList.get(i);
} else {
right+=inputList.get(i-1);
right-=inputList.get(i);
}
if (left == right) {
count++;
}
}
System.out.println(count);
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | a3ab066ccd13f051730655ac68f0f568 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | //package com.company;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int A[]=new int[n];
int e=0;
int o=0;
for(int i=0;i<n;i++)
{
A[i]=in.nextInt();
}
int sumeven=0;
int sumodd=0;
int evensum[]=new int[n];
int oddsum[]=new int[n];
for(int i=0;i<n;i++)
{
if((i+1)%2==0)
{
evensum[i]=A[i]+sumeven;
sumeven=evensum[i];
}
else
{
evensum[i]=sumeven;
}
}
for(int i=0;i<n;i++)
{
if((i+1)%2!=0)
{
oddsum[i]=A[i]+sumodd;
sumodd=oddsum[i];
}
else
{
oddsum[i]=sumodd;
}
}
int ans=0;
if(evensum[n-1]-evensum[0]==oddsum[n-1]-oddsum[0])
{
ans++;
}
for(int i=1;i<n;i++)
{
if(evensum[i-1]+oddsum[n-1]-oddsum[i]==oddsum[i-1]+evensum[n-1]-evensum[i])
ans++;
}
System.out.println(ans);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 7937db4ef258a3bedb04cfcd26067758 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.Scanner;
public class A_20_1 {
public static void main(String []args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] a = new long[n+1];
long[] oddSum = new long[n+1];
long[] evenSum = new long[n+1];
long[] sum = new long[n+1];
for(int i=1;i<=n;i++){
a[i] = in.nextLong();
sum[i] = sum[i-1]+a[i];
if(i%2==0){
evenSum[i] = evenSum[i-2]+a[i];
oddSum[i] = oddSum[i-1];
}else{
if(i!=1) {
oddSum[i] = oddSum[i - 2] + a[i];
}else{
oddSum[i] = a[i];
}
evenSum[i] = evenSum[i-1];
}
}
long mine;
int count =0;
for(int i=1;i<=n;i++){
if(i==4){
int debug=-1;
}
if(i%2==0){
mine = oddSum[i]+evenSum[n]-evenSum[i];
}else{
mine = oddSum[i-1] + evenSum[n] - evenSum[i];
}
if(mine==sum[n]-a[i]-mine){
count++;
}
}
System.out.println(count);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 108807f5715f0a56e097938e3b392ff5 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
int sum1=0;
int sum2=0;
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
if(i%2==0)sum2+=arr[i];
else sum1+=arr[i];
}
int ans=0;
int tmp1=0;int tmp2=0;
for(int i=0;i<n;i++) {
if(i%2==0) {
if((tmp1+sum2-arr[i]-tmp2)==(tmp2+sum1-tmp1))
ans++;
tmp2+=arr[i];
}else {
if((tmp2+sum1-arr[i]-tmp1)==(tmp1+sum2-tmp2))
ans++;
tmp1+=arr[i];
}
}
System.out.println(ans);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 34c029383a590b39b11a6bc34c1b0603 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.math.BigDecimal;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class IUPC
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
// your code goes here
int n=sc.nextInt();
int a[]=new int[n];
int evenP[]=new int[n];
int oddP[]=new int[n];
int sum=0;
int odd=0;
int even=0;
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
if(i%2==0) {
even+=a[i];
evenP[i]+=even;}
else {
odd+=a[i];
oddP[i]+=odd;}
sum+=a[i];
}
int count=0;
for(int i=0;i<n-1;i++) {
int even1=even;
int odd1=odd;
int sum1=sum;
if(i%2==0) {
even1=evenP[i]-a[i];
even1+=odd-oddP[i+1]+a[i+1];
sum1-=a[i];
odd1=sum1-even1;
if(odd1==even1) {
count++;}
}
else {
odd1=oddP[i]-a[i];
odd1+=even-evenP[i+1]+a[i+1];
sum1-=a[i];
even1=sum1-odd1;
if(odd1==even1) {count++;
}
}
}
if(n%2==0) {
if(odd-a[n-1]==even)count++;
}
else {
if(even-a[n-1]==odd)count++;
}
System.out.println(count);
}}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 109f1f040aa2b0a7a48dc321e6cb9dd3 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
private static final boolean FROM_FILE = false;
private static final boolean TO_FILE = false;
public static void main(String[] args) throws IOException {
BufferedReader br;
if (FROM_FILE) {
br = new BufferedReader(new FileReader("C:/home/programming/input.txt"));
} else {
br = new BufferedReader(new InputStreamReader(System.in));
}
PrintWriter pw;
if (TO_FILE) {
pw = new PrintWriter("C:/home/programming/output.txt");
} else {
pw = new PrintWriter(System.out);
}
// Algorithm:
int n = Integer.parseInt(br.readLine());
String[] line = br.readLine().split(" ");
int[] a = new int[n];
long total = 0;
long[] sums = new long[n + 1];
long sumsOdd[] = new long[n + 2];
long sumsEven[] = new long[n + 2];
sums[0] = 0;
boolean lastEven = true;
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(line[i]);
total += a[i];
sums[i + 1] = total;
if (i % 2 == 0) {
sumsEven[i + 1] = sumsEven[i] + a[i];
sumsEven[i + 2] = sumsEven[i + 1];
lastEven = true;
} else {
sumsOdd[i + 1] = sumsOdd[i] + a[i];
sumsOdd[i + 2] = sumsOdd[i + 1];
lastEven = false;
}
}
if (lastEven) {
sumsOdd[n + 1] = sumsOdd[n];
} else {
sumsEven[n + 1] = sumsEven[n];
}
int result = 0;
for (int i = 0; i < n; i++) {
if ((total - a[i]) % 2 == 0) {
long sumEven = sumsEven[i];
long sumOdd = sumsOdd[i];
sumEven += sumsOdd[n + 1] - sumsOdd[i + 1];
sumOdd += sumsEven[n + 1] - sumsEven[i + 1];
if (sumEven == sumOdd) {
result++;
}
}
}
pw.println(result);
pw.flush();
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 012df407e36088740f7be573d5216ec8 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.Scanner;
public class B{
static Scanner fr = new Scanner(System.in);
public static void main(String args[]){
int n = fr.nextInt();
int arr[] = nextArray(n);
int odd = 0, even = 0;
int curr_odd = 0, curr_even = 0;
for(int i=0;i<n;i++){
if(i % 2 == 0){
odd += arr[i];
}else{
even += arr[i];
}
}
int idx = 0;
for(int i=0;i<n;i++){
int o = curr_odd + even - curr_even;
if(i % 2 == 0){
curr_odd += arr[i];
}else{
curr_even += arr[i];
o -= arr[i];
}
int e = odd + even - o - arr[i];
if(o == e){
idx ++;
}
}
System.out.println(idx);
}
static int[] nextArray(int n){
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = fr.nextInt();
}
return arr;
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 6b828efca96eb2666651bfd39a2948b9 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class B {
public static void main(String[] args) throws Exception {
//BufferedReader bufferedReader = new BufferedReader(new FileReader("input.txt"));
//Scanner scanner = new Scanner(bufferedReader);
//Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
FastScanner scanner = new FastScanner();
int n = scanner.nextInt();
int[] candy = scanner.nextIntArray();
int count = 0;
int prefixEvenSum, prefixOddSum, suffixEvenSum, suffixOddSum;
prefixEvenSum = prefixOddSum = suffixEvenSum = suffixOddSum = 0;
// remove first candy
for (int i = 0; i < candy.length; i++) {
if (i % 2 == 0)
suffixEvenSum += candy[i];
else
suffixOddSum += candy[i];
}
// remove inner candies
for (int i = 0; i < candy.length; i++) {
if (i % 2 == 0) {
suffixEvenSum -= candy[i];
} else {
suffixOddSum -= candy[i];
}
if (prefixOddSum + suffixEvenSum == prefixEvenSum + suffixOddSum)
count++;
if (i % 2 == 0) {
prefixEvenSum += candy[i];
} else {
prefixOddSum += candy[i];
}
}
System.out.println(count);
}
private static class FastScanner {
private BufferedReader br;
public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); }
public int[] nextIntArray() throws IOException {
String line = br.readLine();
String[] strings = line.trim().split("\\s+");
int[] array = new int[strings.length];
for (int i = 0; i < array.length; i++)
array[i] = Integer.parseInt(strings[i]);
return array;
}
public int nextInt() throws IOException { return Integer.parseInt(br.readLine()); }
public long[] nextLongArray() throws IOException {
String line = br.readLine();
String[] strings = line.trim().split("\\s+");
long[] array = new long[strings.length];
for (int i = 0; i < array.length; i++)
array[i] = Long.parseLong(strings[i]);
return array;
}
public long nextLong() throws IOException { return Long.parseLong(br.readLine()); }
public String nextLine() throws IOException { return br.readLine(); }
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 8268597e4ec09358e53aaa2b4243f24a | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.Scanner;
public class ProblemB {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if (n == 1) {
System.out.println(1);
in.close();
return;
}
int[] sum = new int[n+2];
int[] sum1 = new int[n+2];
int[] a = new int[n];
int count = 0;
int num;
for (int i = 1; i <= n; i++) {
num = in.nextInt();
a[i-1] = num;
if (i % 2 == 0) {
sum[i] = sum[i-1] + num;
}
else {
sum[i] = sum[i-1] - num;
}
}
for (int i = n; i>= 1; i--) {
if (i % 2 == 0) {
sum1[i] = sum1[i+1] - a[i-1];
}
else {
sum1[i] = sum1[i+1] + a[i-1];
}
}
in.close();
for (int i = 2; i < n; i++) {
if (sum[i - 1] + sum1[i+1] == 0)
count++;
}
if (sum1[2] == 0)
count++;
if (sum[n-1] == 0)
count++;
System.out.println(count);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | ebf1d11f5fe77cbbfeff41de4bfa1673 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | //package com.freemaker.first;
import java.util.Scanner;
public class TanyasCandies {
public static void main(String[] s){
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int[] candies = new int[size];
for(int i=0; i < size; i++ ){
candies[i] = sc.nextInt();
}
System.out.println(numberOfGoodCandies(size, candies));
}
static int numberOfGoodCandies(int size, int[] candies){
int count = 0;
long oddSum = 0;
long evenSum = 0;
long carryOddSum = 0;
long carryEvenSum = 0;
for(int i = 0 ; i < size; i++){
if((i+1)%2 == 0)
evenSum+=candies[i];
else
oddSum+=candies[i];
}
for(int i =0; i < size; i++){
if((i+1)%2 == 0){
evenSum -= candies[i];
if(evenSum + carryEvenSum == oddSum + carryOddSum)
count++;
carryOddSum += candies[i];
}
else{
oddSum -= candies[i];
if(evenSum + carryEvenSum == oddSum + carryOddSum)
count++;
carryEvenSum += candies[i];
}
}
return count;
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 9a915a633f6ecdb9a25cf2d022f0f9f6 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.*;
// The number is simply too large to even consider running a loop through all the candies and then
// compare the odd and the even.
// So why not count the odd and the even first time, and then find the difference.
// Loop from the beginning an
public class TanyaAndCandles {
public int countGoodCandies(int[] candies) {
int even = 0;
int odd = 0;
int len = candies.length;
if (len == 1) {
return 1;
}
if (len == 2) {
return 0;
}
for (int it = 0; it < len; it++) {
if (it % 2==0) {
even += candies[it];
}
else {
odd += candies[it];
} } int[] evenLeft = new int[len];
int[] evenRight = new int[len];
int[] oddLeft = new int[len];
int[] oddRight = new int[len];
evenLeft[0] = candies[0];
evenRight[0] = even - candies[0];
if (len >= 1) {
oddLeft[1] = candies[1];
oddRight[1] = odd - candies[1];
}
for (int it = 2; it < len;it++) {
if (it % 2 == 0) {
evenLeft[it] = evenLeft[it-2] + candies[it];
evenRight[it] += even - evenLeft[it];
}
else {
oddLeft[it] = oddLeft[it-2] + candies[it];
oddRight[it] = odd - oddLeft[it];
}
}
// Calculate the first case
int goodCandies = 0;
if (odd - even + candies[0] == 0) {
goodCandies++;
}
int diff = 0;
int sumOfEven = 0;
int sumOfOdd = 0;
for (int it = 1; it < len; it++) {
if (it % 2 == 0) {
sumOfEven = evenLeft[it] - candies[it] + oddRight[it-1];
sumOfOdd = oddLeft[it-1] + evenRight[it];
if (sumOfEven == sumOfOdd) {
goodCandies++;
}
}
else {
sumOfOdd = oddLeft[it] - candies[it] + evenRight[it-1];
sumOfEven = evenLeft[it-1] + oddRight[it];
if (sumOfOdd == sumOfEven) {
goodCandies++;
}
}
}
// Meaning even > odd, then just find all difference from all the even indices
return goodCandies;
}
public void printDiff(int diff, int sumOfOdd, int sumOfEven) {
System.out.println("Sum Of Odd " + sumOfOdd);
System.out.println("Sum Of Even" + sumOfEven);
System.out.println("Here is the difference: " + diff);
}
public int[] createRandomTest(int n) {
int[] m = new int[n];
for (int it = 0; it < n; it++) {
m[it] = 1;
}
return m;
}
public static void main(String[] args) {
TanyaAndCandles t = new TanyaAndCandles();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] candies = new int[n];
for (int it = 0; it < n; it++) {
candies[it] = sc.nextInt();
}
System.out.println(t.countGoodCandies(candies));
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | b1b168b43b97beef4d9e6ccca29c86e8 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
*
Tanya has 𝑛 candies numbered from 1 to 𝑛. The 𝑖-th candy has the weight 𝑎𝑖.
She plans to eat exactly 𝑛−1 candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.
Your task is to find the number of such candies 𝑖 (let's call these candies good) that if dad gets the 𝑖-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.
For example, 𝑛=4 and weights are [1,4,3,3]. Consider all possible cases to give a candy to dad:
Tanya gives the 1-st candy to dad (𝑎1=1), the remaining candies are [4,3,3]. She will eat 𝑎2=4 in the first day, 𝑎3=3 in the second day, 𝑎4=3 in the third day. So in odd days she will eat 4+3=7 and in even days she will eat 3. Since 7≠3 this case shouldn't be counted to the answer (this candy isn't good).
Tanya gives the 2-nd candy to dad (𝑎2=4), the remaining candies are [1,3,3]. She will eat 𝑎1=1 in the first day, 𝑎3=3 in the second day, 𝑎4=3 in the third day. So in odd days she will eat 1+3=4 and in even days she will eat 3. Since 4≠3 this case shouldn't be counted to the answer (this candy isn't good).
Tanya gives the 3-rd candy to dad (𝑎3=3), the remaining candies are [1,4,3]. She will eat 𝑎1=1 in the first day, 𝑎2=4 in the second day, 𝑎4=3 in the third day. So in odd days she will eat 1+3=4 and in even days she will eat 4. Since 4=4 this case should be counted to the answer (this candy is good).
Tanya gives the 4-th candy to dad (𝑎4=3), the remaining candies are [1,4,3]. She will eat 𝑎1=1 in the first day, 𝑎2=4 in the second day, 𝑎3=3 in the third day. So in odd days she will eat 1+3=4 and in even days she will eat 4. Since 4=4 this case should be counted to the answer (this candy is good).
In total there 2 cases which should counted (these candies are good), so the answer is 2.
Input
The first line of the input contains one integer 𝑛 (1≤𝑛≤2⋅105) — the number of candies.
The second line of the input contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤104), where 𝑎𝑖 is the weight of the 𝑖-th candy.
Output
Print one integer — the number of such candies 𝑖 (good candies) that if dad gets the 𝑖-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days.
* @author fbokovikov
*/
public class Cf_2019_19_02_02 {
public static void main(String[] args) {
InputReader reader = new InputReader(System.in);
int n = reader.nextInt();
int[] weights = new int[n];
for (int i = 0; i < weights.length; i++) {
weights[i] = reader.nextInt();
}
int goodCandiesCount = computeGoodCandiesCount(n, weights);
System.out.println(goodCandiesCount);
}
private static int computeGoodCandiesCount(int n, int[] weights) {
int[] oddSum = new int[n];
int[] evenSum = new int[n];
IntStream.range(0, n).forEach(i -> {
if (i % 2 == 0) {
oddSum[i] = i == 0 ? weights[0] : oddSum[i - 1] + weights[i];
evenSum[i] = i == 0 ? 0 : evenSum[i - 1];
} else {
evenSum[i] = evenSum[i - 1] + weights[i];
oddSum[i] = oddSum[i - 1];
}
});
int goodCandiesDays = 0;
for (int i = 0; i < n; i++) {
int curOddSum = i == 0 ? 0 : oddSum[i - 1];
int curEvenSum = i == 0 ? 0 : evenSum[i - 1];
curOddSum += evenSum[n - 1] - evenSum[i];
curEvenSum += oddSum[n - 1] - oddSum[i];
if (curOddSum == curEvenSum) {
goodCandiesDays++;
}
}
return goodCandiesDays;
}
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());
}
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 35d7157c4ed872e731440bd1bc2a7fa9 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
long[] arr=new long[n];
long odd=0;
long even=0;
for(int i=0;i<n;i++){
arr[i]=in.nextInt();
if(i%2==0)even+=arr[i];
else odd+=arr[i];
}
long temp=even;
even=odd;
odd=temp;
int count=0;
//System.out.println(even+" "+odd);
if(even==odd-arr[0]){count++;}
if(arr.length<2){
System.out.println(1);
return;
}
long te=even-arr[1]+arr[0];
long to=odd-arr[0];
if(te==to){count++;}
long cur_odd=arr[0];
long cur_even=arr[1];
int ptr=2;
while(ptr<n){
te=cur_even+odd-cur_odd-arr[ptr];
to=cur_odd+even-cur_even;
if(te==to){count++;}
cur_odd+=arr[ptr];
ptr++;
if(ptr<n){
te=cur_even+odd-cur_odd;
to=cur_odd+even-cur_even-arr[ptr];
if(to==te){count++;}
cur_even+=arr[ptr];
ptr++;
}
}
System.out.println(count);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 57067d4724bb0dcc96a29648da580793 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author Andrei Chugunov
*/
public class Main {
private static class Solution {
private void solve(FastScanner in, PrintWriter out) {
int n = in.nextInt();
int[] a = new int[n+1];
for (int i = 1; i < n+1; i++) {
a[i] = in.nextInt();
}
long oddPref = 0;
long evenPref = 0;
long oddSuf = 0;
long evenSuff = 0;
for (int i = 1; i < n+1; i++) {
int temp = a[i];
if (i%2 == 0) {
evenSuff+=temp;
} else {
oddSuf+=temp;
}
}
int count = 0;
for (int i = 1; i < n + 1; i++) {
int temp = a[i];
if (i%2 == 0) {
evenSuff-=temp;
} else {
oddSuf-=temp;
}
if (oddPref + evenSuff == evenPref + oddSuf) {
count++;
}
if (i%2 == 0) {
evenPref+=temp;
} else {
oddPref+=temp;
}
}
out.println(count);
}
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solution solver = new Solution();
solver.solve(in, out);
out.flush();
//out.close();
}
private static class FastScanner {
private BufferedReader br;
private StringTokenizer st;
FastScanner(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream), 32768);
st = null;
}
private String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
private int nextInt() {
return Integer.parseInt(next());
}
private long nextLong() {
return Long.parseLong(next());
}
private int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | ad9bc2958ebb24fa5c2dae41faa9035a | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class CodeForces540B {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
int[] sum = new int[2];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
sum[i % 2] += a[i];
}
int count = 0;
for (int i = 0; i < n; i++) {
sum[i % 2] -= a[i];
if (sum[0] == sum[1]) {
count++;
}
sum[1 - i % 2] += a[i];
}
System.out.println(count);
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | d76f66d405a5091494070d3761a380ab | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class test {
static BufferedReader br;
static StringTokenizer st;
static int[] vals;
static int n;
public static void main(String[] args) throws NumberFormatException, IOException {
br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
vals = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i<n; i++) {
vals[i] = Integer.parseInt(st.nextToken());
}
solve();
}
public static void solve() {
if (n==1) {
System.out.println(1);
return;
}
long[] oddprefsum = new long[n];
long[] evenprefsum = new long[n];
long[] oddsufsum = new long[n];
long[] evensufsum = new long[n];
oddprefsum[1] = vals[1];
evenprefsum[0] = evenprefsum[1] = vals[0];
for (int i = 2; i<n; i++) {
if (i%2 == 1) {
oddprefsum[i] = oddprefsum[i-2]+vals[i];
evenprefsum[i] = evenprefsum[i-1];
}
else {
evenprefsum[i] = evenprefsum[i-2]+vals[i];
oddprefsum[i] = oddprefsum[i-1];
}
}
if (n%2==0) {
oddsufsum[n-1] = oddsufsum[n-2] = vals[n-1];
evensufsum[n-2] = vals[n-2];
}
else {
oddsufsum[n-2] = vals[n-2];
evensufsum[n-1] = evensufsum[n-2] = vals[n-1];
}
for (int i = n-3; i>=0; i--) {
if (i%2 == 1) {
oddsufsum[i] = oddsufsum[i+2]+vals[i];
evensufsum[i] = evensufsum[i+1];
}
else {
evensufsum[i] = evensufsum[i+2]+vals[i];
oddsufsum[i] = oddsufsum[i+1];
}
}
int result = 0;
long evenBefore, evenAfter, oddBefore, oddAfter;
for (int i = 0; i<n; i++) {
if (i==0) {
evenBefore = oddBefore = 0;
evenAfter = oddsufsum[i+1];
oddAfter = evensufsum[i+1];
}
else if (i==n-1) {
evenAfter = oddAfter = 0;
evenBefore = evenprefsum[i-1];
oddBefore = oddprefsum[i-1];
}
else {
evenBefore = evenprefsum[i-1];
oddBefore = oddprefsum[i-1];
evenAfter = oddsufsum[i+1];
oddAfter = evensufsum[i+1];
}
if (evenBefore+evenAfter == oddBefore+oddAfter) {
result++;
}
}
System.out.println(result);
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 0e41d701e2f82dc566a7b4de10710a15 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int length=s.nextInt();
int[] input=new int[length];
int[] oddSum=new int[length];
int[] evenSum=new int[length];
int sum,result;
sum=result=0;
for(int a=0;a<length;a++)
{
input[a]=s.nextInt();
sum+=input[a];
if((a+1)%2!=0)
{
if(a-1>=0)
{
oddSum[a]=oddSum[a-1]+input[a];
evenSum[a]=evenSum[a-1];
}
else
{
oddSum[a]=input[a];
evenSum[a]=0;
}
}
else
{
oddSum[a]=oddSum[a-1];
evenSum[a]=evenSum[a-1]+input[a];
}
}
for(int a=0;a<length;a++)
{
if((sum-input[a])%2!=0)
continue;
else
{
int temp=0;
if(a-1>=0)
temp+=oddSum[a-1];
temp+=evenSum[length-1]-evenSum[a];
if(temp==(sum-input[a])/2)
result++;
}
}
System.out.println(result);
s.close();
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 13e1412d65167df8fdc1cb18a47aab01 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.*;
public class Sol {
public static void main(String[] args) {
Scanner ip=new Scanner(System.in);
int n=ip.nextInt();
int arr[]=new int[n];
int evenP=0;
int oddP=0;
int evenS=0;
int oddS=0;
for(int i=0;i<n;i++) {
arr[i]=ip.nextInt();
if(i%2==0) {
evenS+=arr[i];
}else {
oddS+=arr[i];
}
}
int c=0;
for(int i=0;i<n;i++) {
if(i%2==0) {
evenS-=arr[i];
if(evenS+oddP==oddS+evenP) {
c++;
}
evenP+=arr[i];
}else {
oddS-=arr[i];
if(evenS+oddP==oddS+evenP) {
c++;
}
oddP+=arr[i];
}
}
System.out.println(c);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 8db3054349532dbeaa8d2a49aa34bf69 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
long even[]=new long[200005];
long odd[]=new long[200005];
int N=Integer.parseInt(br.readLine());
int arr[]=new int[N+1];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int i=1; i<=N; i++)
{
arr[i]=Integer.parseInt(st.nextToken());
if(i%2==0)
{
even[i]=even[i-1]+arr[i];
odd[i]=odd[i-1];
}
else
{
odd[i]=odd[i-1]+arr[i];
even[i]=even[i-1];
}
}
int ans=0;
for(int i=1; i<=N; i++)
{
if(odd[N]-odd[i]+even[i-1]==even[N]-even[i]+odd[i-1])
ans++;
}
System.out.println(ans);
}
}
| Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | cc077ac93b78d688a4a232796cbedbb3 | train_001.jsonl | 1550586900 | Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int answer = 0, arraySum = 0, oddSum = 0, evenSum = 0, n = input.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
arraySum += a[i];
if (i % 2 == 0 && i != 0)
evenSum += a[i];
else if (i % 2 == 1)
oddSum += a[i];
}
for (int i = 0; i < n; i++) {
if (evenSum == oddSum)
answer++;
if (i != n - 1) {
if (isEvenDay(i)) {
evenSum += a[i];
evenSum -= a[i + 1];
} else {
oddSum += a[i];
oddSum -= a[i + 1];
}
}
}
System.out.println(answer);
}
private static boolean isEvenDay(int candyIndex) {
if (candyIndex % 2 == 0)
return false;
else
return true;
}
} | Java | ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"] | 1 second | ["2", "2", "3"] | NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$. | Java 8 | standard input | [
"implementation"
] | dcc380c544225c8cadf0df8d8b6ffb4d | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. | 1,200 | Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. | standard output | |
PASSED | 693302bccc920641e19e5c2a95f6c5c3 | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.io.*;
import java.util.*;
public class LimitTokenizer
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,n,m,a,b,hcf,t;
hcf=0;
StringTokenizer d1=new StringTokenizer(br.readLine());
n=Integer.parseInt(d1.nextToken());
m=Integer.parseInt(d1.nextToken());
StringTokenizer d2=new StringTokenizer(br.readLine());
StringTokenizer d3=new StringTokenizer(br.readLine());
a =new Integer(d2.nextToken()).intValue();
b =new Integer(d3.nextToken()).intValue();
if(n>m)
{
if((a>0&&b>0)||(a<0&&b<0))
System.out.print("Infinity");
else
System.out.print("-Infinity");
}
else if(n<m)
{
System.out.print("0/1");
}
else if(n==m)
{
if(Math.abs(b)<Math.abs(a))
t=(int)Math.abs(b);
else
t=(int)Math.abs(a);
for(i=1;i<=t;i++)
{
if((a%i==0)&&(b%i==0))
hcf=i;
}
if((a*b)<0)
{
System.out.print("-"+(Math.abs(a)/hcf)+"/"+(Math.abs(b)/hcf));
}
else if((a*b)>0)
{
System.out.print((Math.abs(a)/hcf)+"/"+(Math.abs(b)/hcf));
}
}
}
}
| Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 7bf8df3b3d4d988cbb00e9f322f6d129 | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: piyushd
* Date: 3/26/11
* Time: 10:53 PM
* To change this template use File | Settings | File Templates.
*/
public class TaskB {
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void run() {
int n = nextInt(), m = nextInt();
int [] a = new int[n + 1];
int [] b = new int[m + 1];
for (int i = 0; i < n + 1; i++) a[i] = nextInt();
for (int i = 0; i < m + 1; i++) b[i] = nextInt();
int GCD = gcd(Math.abs(a[0]), Math.abs(b[0]));
if (b[0] < 0) {a[0] *= -1; b[0] *= -1;}
a[0] /= GCD; b[0] /= GCD;
if (n == m) {
System.out.println(a[0] + "/" + b[0]);
return;
}
if (n < m) {
System.out.println("0/1");
return;
}
if (a[0] > 0) {
System.out.println("Infinity");
return;
}
System.out.println("-Infinity");
}
int nextInt(){
try{
int c = System.in.read();
if(c == -1) return c;
while(c != '-' && (c < '0' || '9' < c)){
c = System.in.read();
if(c == -1) return c;
}
if(c == '-') return -nextInt();
int res = 0;
do{
res *= 10;
res += c - '0';
c = System.in.read();
}while('0' <= c && c <= '9');
return res;
}catch(Exception e){
return -1;
}
}
long nextLong(){
try{
int c = System.in.read();
if(c == -1) return -1;
while(c != '-' && (c < '0' || '9' < c)){
c = System.in.read();
if(c == -1) return -1;
}
if(c == '-') return -nextLong();
long res = 0;
do{
res *= 10;
res += c-'0';
c = System.in.read();
}while('0' <= c && c <= '9');
return res;
}catch(Exception e){
return -1;
}
}
double nextDouble(){
return Double.parseDouble(next());
}
String next(){
try{
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while(Character.isWhitespace(c))
c = System.in.read();
do{
res.append((char)c);
}while(!Character.isWhitespace(c=System.in.read()));
return res.toString();
}catch(Exception e){
return null;
}
}
String nextLine(){
try{
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while(c == '\r' || c == '\n')
c = System.in.read();
do{
res.append((char)c);
c = System.in.read();
}while(c != '\r' && c != '\n');
return res.toString();
}catch(Exception e){
return null;
}
}
public static void main(String[] args){
new TaskB().run();
}
}
| Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 2ad17cfb7d2c3492785f845a898c6fc5 | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] Args) {
int n = si(), m = si();
int[] p = sai(n + 1);
int[] q = sai(m + 1);
if (n > m)
System.out.println(p[0]*q[0] > 0 ? "Infinity" : "-Infinity");
else if (n < m)
System.out.println("0/1");
else {
int a = p[0], b = q[0];
if (b < 0) {
b *= -1;
a *= -1;
}
int gcd = BigInteger.valueOf(a).gcd(BigInteger.valueOf(b))
.intValue();
a /= gcd;
b /= gcd;
System.out.println(a + "/" + b);
}
}
// ----------------------- Library ------------------------
static Scanner scan = new Scanner(System.in);
static int INF = 2147483647;
// finds GCD of a and b using Euclidian algorithm
public int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
static List<String> toList(String[] a) {
return Arrays.asList(a);
}
static String[] toArray(List<String> a) {
String[] o = new String[a.size()];
a.toArray(o);
return o;
}
static int[] pair(int... a) {
return a;
}
static int si() {
return scan.nextInt();
}
static String ss() {
return scan.nextLine();
}
static int[] sai(int n) {
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
a[i] = si();
return a;
}
static String[] sas(int n) {
String[] a = new String[n];
for (int i = 0; i < a.length; i++)
a[i] = ss();
return a;
}
static Object[][] _sm1(int r, int c) {
Object[][] a = new Object[r][c];
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
a[i][j] = scan.next();
return a;
}
static Object[][] _sm2(int r) {
Object[][] a = new Object[r][3];
for (int i = 0; i < r; i++)
a[i] = new Object[] { ss(), ss(), ss() };
return a;
}
}
| Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 363f3dfd500d1a48e982c5ddb3e4ec0c | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt() + 1;
int m = sc.nextInt() + 1;
int[] p = new int[n];
int[] q = new int[m];
for (int i = 0; i < n; i++)
p[i] = sc.nextInt();
for (int i = 0; i < m; i++)
q[i] = sc.nextInt();
if (n < m)
System.out.println("0/1");
else if (n > m && p[0]*q[0]>0)
System.out.println("Infinity");
else if (n > m && p[0]*q[0] < 0)
System.out.println("-Infinity");
else // n==m {
{
if (p[0]<0&&q[0]<0) {
p[0]*=-1;
q[0]*=-1;
}
else if (p[0]>0&&q[0]<0){
p[0]*=-1;
q[0]*=-1;
}
int t = NOD(p[0], q[0]);
System.out.println(p[0] / t + "/" + q[0] / t);
}
}
public static int NOD(int b, int a) {
if (b != 0)
return Math.abs(NOD(a % b, b));
else
return Math.abs(a);
}
}
| Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 59730ca3f339e58ef81e775202e0e6ae | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws Exception {
new B().solve();
}
void solve() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] sp = in.readLine().split(" ");
int n = Integer.parseInt(sp[0]);
int m = Integer.parseInt(sp[1]);
int a[] = new int[n + 1];
int b[] = new int[m + 1];
sp = in.readLine().split(" ");
for (int i = 0; i <= n; i++) {
a[i] = Integer.parseInt(sp[i]);
}
sp = in.readLine().split(" ");
for (int i = 0; i <= m; i++) {
b[i] = Integer.parseInt(sp[i]);
}
if (n > m) {
System.out.println((a[0] * b[0] > 0) ? "Infinity" : "-Infinity");
} else if (n < m) {
System.out.println("0/1");
} else {
// n==m
int g = gcd(Math.abs(a[0]), Math.abs(b[0]));
a[0] /= g;
b[0] /= g;
String str = "";
if (a[0] * b[0] < 0) str = "-";
str += Math.abs(a[0]) + "/" + Math.abs(b[0]);
System.out.println(str);
}
}
int gcd(int a, int b) {
if (a % b == 0) {
return b;
} else {
return gcd(b, a % b);
}
}
}
| Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | fba40d087940806fe6c09ec649160b6e | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.util.Scanner;
public class Main {
private static Scanner s = new Scanner(System.in);
public static void main(String[] args)
{
int n=s.nextInt();
int m=s.nextInt();
int nl=0;
int ml=0;
boolean b=false;
for(int i=0;i<=n;i++)
{
if(i==0)
{
nl=s.nextInt();
}
else
{
s.nextInt();
}
}
for(int i=0;i<=m;i++)
{
if(i==0)
{
ml=s.nextInt();
}
else
{
s.nextInt();
}
}
if(ml*nl<0)
{
b=true;
}
if(n<m)
{
System.out.println("0/1");
return;
}
if(n>m)
{
if(!b)
{
System.out.println("Infinity");
return;
}
else
{
System.out.println("-Infinity");
return;
}
}
ml=ml<0?-ml:ml;
nl=nl<0?-nl:nl;
if(n==m)
{
int max=nl<=ml?ml:nl;
for(int i=2;i<=max;i++)
{
while(nl%i==0&&ml%i==0)
{
nl/=i;
ml/=i;
}
}
if(!b)
{
System.out.println(nl+"/"+ml);
}
else
{
System.out.println("-"+nl+"/"+ml);
}
}
}
} | Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 91e287070055d2b4fc660570196fd78a | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class wadup {
public static void debug(Object... obs)
{
System.out.println(Arrays.deepToString(obs));
}
static int gcd(int a, int b)
{
return (b==0)? a : gcd(b, a%b);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int a0 = sc.nextInt(), tmp;
for(int i=0;i<n;i++)
tmp = sc.nextInt();
int b0 = sc.nextInt();
for(int i=0;i<m;i++)
tmp = sc.nextInt();
int sgn = a0 * b0;
String s = (sgn < 0)? "-" : "";
if(n > m)
{
System.out.println(s + "Infinity");
return;
}
else if(m > n)
{
System.out.println("0/1");
return;
}
else
{
int c = gcd(Math.abs(a0), Math.abs(b0));
int ag = Math.abs(a0)/c;
int bg = Math.abs(b0)/c;
System.out.println(s + ag + "/" + bg);
}
}
} | Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 9dd01b935880cbc85566ac99dab8fa76 | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B {
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter writer;
static void init() {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
}
static String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens())
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return tokenizer.nextToken();
}
static byte nextByte() {
return Byte.parseByte(nextToken());
}
static short nextShort() {
return Short.parseShort(nextToken());
}
static int nextInt() {
return Integer.parseInt(nextToken());
}
static long nextLong() {
return Long.parseLong(nextToken());
}
static float nextFloat() {
return Float.parseFloat(nextToken());
}
static double nextDouble() {
return Double.parseDouble(nextToken());
}
static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static void solve() {
int n = nextInt(), m = nextInt();
int[] a = new int[n + 1], b = new int[m + 1];
for (int i = 0; i <= n; ++i) a[i] = nextInt();
for (int i = 0; i <= m; ++i) b[i] = nextInt();
if (n > m) {
if (a[0] * b[0] < 0) writer.print("-");
writer.println("Infinity");
} else if (n < m) {
writer.println("0/1");
} else {
if (a[0] * b[0] < 0) writer.print("-");
a[0] = Math.abs(a[0]);
b[0] = Math.abs(b[0]);
int gcd = gcd(a[0], b[0]);
writer.print(a[0] / gcd);
writer.print("/");
writer.println(b[0] / gcd);
}
}
static void close() {
try {
reader.close();
writer.flush();
writer.close();
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
}
public static void main(String[] args) {
init();
solve();
close();
}
}
| Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 319473ff2013b02fabbb1ec1ee9fd921 | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.io.*;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader r;
r = new BufferedReader(new InputStreamReader(System.in));
//r = new BufferedReader(new FileReader(new File("in.txt")));
String[] line = r.readLine().split(" ");
int n = Integer.parseInt(line[0]);
int m = Integer.parseInt(line[1]);
line = r.readLine().split(" ");
int[] a = new int[n + 1];
int[] b = new int[m + 1];
for (int i = 0; i <= n; i++)
a[i] = Integer.parseInt(line[i]);
line = r.readLine().split(" ");
for (int i = 0; i <= m; i++)
b[i] = Integer.parseInt(line[i]);
if (n == m) {
int p = a[0] * ((b[0] < 0) ? -1 : 1);
int q = Math.abs(b[0]);
int k = Math.abs(gcd(p, q));
System.out.print((p / k) + "/" + (q / k));
} else if (n > m)
System.out.print((a[0] * b[0] < 0 ? "-" : "") + "Infinity");
else
System.out.print("0/1");
}
static int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
} | Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 8dd0fbcba9f7c55b5b1c3442f152ad97 | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 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 Sergey Parkhomenko
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n=in.nextInt(),m=in.nextInt(),a[]=new int[n+1],b[]=new int[m+1];
for (int i=0;i<n+1;i++)
a[i]=in.nextInt();
for (int i=0;i<m+1;i++)
b[i]=in.nextInt();
int a1=a[0],b1=b[0];
if (m>n)
{
out.print("0/1");
return ;
}
if (n>m)
{
if ((a1>0 && b1>0) || (a1<0 && b1<0))
out.print("Infinity"); else
out.print("-Infinity");
return;
}
int k=gcd(a1,b1);
if (a1/k<0 || b1/k<0)
out.print("-"+Math.abs(a1/k)+"/"+Math.abs(b1/k)); else
out.print(a1/k+"/"+b1/k);
}
public int gcd(int a,int b)
{
if (b==0)
return a; else
return gcd(b,a%b);
}
}
class InputReader {
private BufferedReader reader;
private 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 String nextLine() {
if (tokenizer != null && tokenizer.hasMoreTokens()) {
throw new RuntimeException();
}
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public char nextChar() {
char chr='\n';
try {
int i=reader.read();
if (i>=0)
chr=(char)i;
} catch (IOException e) {
throw new RuntimeException(e);
}
return chr;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
| Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 4cf760a9d51d6454804818e6b0c95ccf | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Main {
public static void main(String[] args) throws Exception {
// char[] c = inB.readLine().toCharArray();
// int n = c.length;
//
// int curind = 0;
//
// for(char symb = 'z'; symb>='a'; symb--) {
// int last = curind;
// for(int i = curind; i<n; i++) {
// if(c[i] == symb) {
// out.print(symb);
// last = i;
// }
// }
// curind = last;
// }
//
// out.println();
// out.flush();
int n = nextInt(), m = nextInt();
int[] nn = new int[n+1];
int[] mm = new int[m+1];
for(int i = 0; i<n+1; i++) {
nn[i] = nextInt();
}
for(int i = 0; i<m+1; i++) {
mm[i] = nextInt();
}
if(n > m && nn[0]*mm[0] > 0)
exit("Infinity");
if(n > m && nn[0]*mm[0] < 0)
exit("-Infinity");
if(n < m)
exit("0/1");
int a = Math.abs(nn[0]), b = Math.abs(mm[0]);
out.println((nn[0]*mm[0] < 0 ? "-" : "") + (a/gcd(a,b)) + "/" + (b/gcd(a,b)));
out.flush();
}
private static int gcd(int a, int b) {
if(b == 0)return a;
return gcd(b, a%b);
}
/////////////////////////////////////////////////////////////////
// IO
/////////////////////////////////////////////////////////////////
private static StreamTokenizer in;
private static PrintWriter out;
private static BufferedReader inB;
private static boolean FILE=false;
private static int nextInt() throws Exception{
in.nextToken();
return (int)in.nval;
}
private static String nextString() throws Exception{
in.nextToken();
return in.sval;
}
static{
try {
out = new PrintWriter(FILE ? (new FileOutputStream("output.txt")) : System.out);
inB = new BufferedReader(new InputStreamReader(FILE ? new FileInputStream("input.txt") : System.in));
} catch(Exception e) {e.printStackTrace();}
in = new StreamTokenizer(inB);
}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
// pre - written
/////////////////////////////////////////////////////////////////
private static void println(Object o) throws Exception {
out.println(o);
out.flush();
}
private static void exit(Object o) throws Exception {
println(o);
exit();
}
private static void exit() {
System.exit(0);
}
private static final int INF = Integer.MAX_VALUE;
private static final int MINF = Integer.MIN_VALUE;
//////////////////////////////////////////////////////////////////
}
| Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 5e1995ad8d2d3ff5db072fa5afc91dba | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
int gcd( int a, int b ) {
return b == 0 ? a : gcd( b, a % b );
}
public void solve( ) throws Throwable {
int n = in.nextInt( ), m = in.nextInt( );
int a[ ] = new int[ n + 1 ], b[ ] = new int[ m + 1 ];
for ( int i = 0; i <= n; ++i ) {
a[ i ] = in.nextInt( );
}
for ( int i = 0; i <= m; ++i ) {
b[ i ] = in.nextInt( );
}
if ( n > m ) {
if ( a[ 0 ] < 0 && b[ 0 ] > 0 || b[ 0 ] < 0 && a[ 0 ] > 0 ) {
out.println( "-Infinity" );
} else {
out.println( "Infinity" );
}
return;
}
if ( m > n ) {
out.println( "0/1" );
return;
}
int s1 = a[ 0 ] < 0 ? -1 : 1, s2 = b[ 0 ] < 0 ? -1 : 1;
int p = a[ 0 ] * s1, q = b[ 0 ] * s2;
int g = gcd( p, q );
p /= g;
q /= g;
p *= s1 * s2;
out.printf( "%d/%d%n", p, q );
}
public void run( ) {
in = new FastScanner( System.in );
out = new PrintWriter( new PrintStream( System.out ), true );
try {
solve( );
out.close( );
System.exit( 0 );
} catch( Throwable e ) {
e.printStackTrace( );
System.exit( -1 );
}
}
public void debug( Object...os ) {
System.err.println( Arrays.deepToString( os ) );
}
public static void main( String[ ] args ) {
( new Main( ) ).run( );
}
private FastScanner in;
private PrintWriter out;
private static class FastScanner {
private int charsRead;
private int currentRead;
private byte buffer[ ] = new byte[ 0x1000 ];
private InputStream reader;
public FastScanner( InputStream in ) {
reader = in;
}
public int read( ) {
if ( charsRead == -1 ) {
throw new InputMismatchException( );
}
if ( currentRead >= charsRead ) {
currentRead = 0;
try {
charsRead = reader.read( buffer );
} catch( IOException e ) {
throw new InputMismatchException( );
}
if ( charsRead <= 0 ) {
return -1;
}
}
return buffer[ currentRead++ ];
}
public int nextInt( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( sign == -1 ) {
c = read( );
}
if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int ans = 0;
while ( !isWhitespace( c ) && c != -1 ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10 + num;
c = read( );
}
return ans * sign;
}
public long nextLong( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( sign == -1 ) {
c = read( );
}
if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
long ans = 0;
while ( !isWhitespace( c ) && c != -1 ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10 + num;
c = read( );
}
return ans * sign;
}
public boolean isWhitespace( int c ) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == -1;
}
public String next( ) {
int c = read( );
StringBuffer ans = new StringBuffer( );
while ( isWhitespace( c ) && c != -1 ) {
c = read( );
}
if ( c == -1 ) {
return null;
}
while ( !isWhitespace( c ) && c != -1 ) {
ans.appendCodePoint( c );
c = read( );
}
return ans.toString( );
}
public String nextLine( ) {
String ans = nextLine0( );
while ( ans.trim( ).length( ) == 0 ) {
ans = nextLine0( );
}
return ans;
}
private String nextLine0( ) {
int c = read( );
if ( c == -1 ) {
return null;
}
StringBuffer ans = new StringBuffer( );
while ( c != '\n' && c != '\r' && c != -1 ) {
ans.appendCodePoint( c );
c = read( );
}
return ans.toString( );
}
public double nextDouble( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '.' && c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( c == '-' ) {
c = read( );
}
double ans = 0;
while ( c != -1 && c != '.' && !isWhitespace( c ) ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10.0 + num;
c = read( );
}
if ( !isWhitespace( c ) && c != -1 && c != '.' ) {
throw new InputMismatchException( );
}
double pow10 = 1.0;
if ( c == '.' ) {
c = read( );
}
while ( !isWhitespace( c ) && c != -1 ) {
pow10 *= 10.0;
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10.0 + num;
c = read( );
}
return ans * sign / pow10;
}
}
}
| Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 0ed2283dbf870b162cd29396e203afbe | train_001.jsonl | 1339506000 | You are given two polynomials: P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm. Calculate limit . | 256 megabytes | import java.util.Scanner;
public class C_124B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int a[] = new int [n+1];
for (int i = 0; i < n+1; i++) {
a[i] = sc.nextInt();
}
int b[] = new int [m+1];
for (int i = 0; i < m+1; i++) {
b[i] = sc.nextInt();
}
if(m>n){
System.out.println(0+"/"+1);
return;
}
if(n>m){
if(a[0]*b[0]>0){
System.out.println("Infinity");
}else{
System.out.println("-Infinity");
}
return;
}
int g = Math.abs(gcd(Math.abs(a[0]), Math.abs(b[0])));
a[0] /=g; b[0]/=g;
if(a[0]*b[0]<0) System.out.print("-");
a[0]=Math.abs(a[0]); b[0] = Math.abs(b[0]);
System.out.println(a[0]+"/"+b[0]);
}
public static int gcd(int a, int b){
if(b == 0){
return a;
}else{
return gcd(b, a % b);
}
}
}
| Java | ["2 1\n1 1 1\n2 5", "1 0\n-1 3\n2", "0 1\n1\n1 0", "2 2\n2 1 6\n4 5 -7", "1 1\n9 0\n-5 2"] | 2 seconds | ["Infinity", "-Infinity", "0/1", "1/2", "-9/5"] | NoteLet's consider all samples: You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function | Java 6 | standard input | [
"math"
] | 37cf6edce77238db53d9658bc92b2cab | The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly. The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0). The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0). | 1,400 | If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes). If the value of the limit equals zero, print "0/1" (without the quotes). Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction. | standard output | |
PASSED | 0e4d8391697684101adfe2ddbd95b755 | train_001.jsonl | 1603623900 | Tenten runs a weapon shop for ninjas. Today she is willing to sell $$$n$$$ shurikens which cost $$$1$$$, $$$2$$$, ..., $$$n$$$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.Tenten keeps a record for all events, and she ends up with a list of the following types of records: + means that she placed another shuriken on the showcase; - x means that the shuriken of price $$$x$$$ was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.TreeSet;
import java.util.Vector;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Tarek
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DShurikens solver = new DShurikens();
solver.solve(1, in, out);
out.close();
}
static class DShurikens {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] ans = new int[n];
String[] q = new String[2 * n];
int[] value = new int[2 * n];
Arrays.fill(value, 0);
int cnt = 0;
int id = 0;
Stack<Integer> s = new Stack<>();
for (int i = 0; i < 2 * n; i++) {
q[i] = in.next();
if (q[i].charAt(0) == '+') {
cnt++;
s.add(id);
id++;
} else {
int k = in.nextInt();
value[i] = k;
//out.println(k);
if (cnt == 0) {
out.println("NO");
return;
}
ans[s.pop()] = k;
cnt--;
}
}
id = 0;
TreeSet<Integer> ts = new TreeSet<>();
for (int i = 0; i < 2 * n; i++) {
if (q[i].charAt(0) == '+') {
ts.add(ans[id]);
id++;
} else {
int k = value[i];
if (k != ts.pollFirst()) {
System.out.println("NO");
return;
}
}
}
out.println("YES");
for (int i = 0; i < n; i++) {
out.print(ans[i] + " ");
}
out.println();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4", "1\n- 1\n+", "3\n+\n+\n+\n- 2\n- 1\n- 3"] | 1 second | ["YES\n4 2 3 1", "NO", "NO"] | NoteIn the first example Tenten first placed shurikens with prices $$$4$$$ and $$$2$$$. After this a customer came in and bought the cheapest shuriken which costed $$$2$$$. Next, Tenten added a shuriken with price $$$3$$$ on the showcase to the already placed $$$4$$$-ryo. Then a new customer bought this $$$3$$$-ryo shuriken. After this she added a $$$1$$$-ryo shuriken. Finally, the last two customers bought shurikens $$$1$$$ and $$$4$$$, respectively. Note that the order $$$[2, 4, 3, 1]$$$ is also valid.In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $$$2$$$. This is impossible since the shuriken was not the cheapest, we know that the $$$1$$$-ryo shuriken was also there. | Java 8 | standard input | [
"data structures",
"implementation",
"greedy"
] | 5fa2af185c4e3c8a1ce3df0983824bad | The first line contains the only integer $$$n$$$ ($$$1\leq n\leq 10^5$$$) standing for the number of shurikens. The following $$$2n$$$ lines describe the events in the format described above. It's guaranteed that there are exactly $$$n$$$ events of the first type, and each price from $$$1$$$ to $$$n$$$ occurs exactly once in the events of the second type. | 1,700 | If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain $$$n$$$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. | standard output | |
PASSED | f26375fd16a11489bc66f101c959b47b | train_001.jsonl | 1603623900 | Tenten runs a weapon shop for ninjas. Today she is willing to sell $$$n$$$ shurikens which cost $$$1$$$, $$$2$$$, ..., $$$n$$$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.Tenten keeps a record for all events, and she ends up with a list of the following types of records: + means that she placed another shuriken on the showcase; - x means that the shuriken of price $$$x$$$ was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! | 256 megabytes |
import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.DecimalFormat;
import java.lang.reflect.Array;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.security.cert.TrustAnchor;
public class B {
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static long MOD = (long) (1e9 + 7);
//static int MOD = 998244353;
static long MOD2 = MOD * MOD;
static FastReader sc = new FastReader();
static int pInf = Integer.MAX_VALUE;
static int nInf = Integer.MIN_VALUE;
public static void main(String[] args) {
int test = 1;
//test = sc.nextInt();
while (test-- > 0) solve();
out.flush();
out.close();
}
public static void solve() {
int n= sc.nextInt();
int[] a = new int[n+1];
TreeSet<Integer> A = new TreeSet<>();
ArrayList<Pair> Q = new ArrayList<>();
for(int i = 0; i < 2*n; i++){
String s = sc.next();
if(s.equals("+")){
Q.add(new Pair(1,-1));
A.add(i);
}
else{
int r = sc.nextInt();
Q.add(new Pair(0, r));
a[r] = i;
}
}
TreeMap<Integer,Integer> ans = new TreeMap<>();
for(int i = 1; i < n+1; i++){
Integer x = A.lower(a[i]);
if(x==null){
out.println("NO");
return;
}
A.remove(x);
ans.put(x,i);
}
int[] p = new int[n];
int k = 0;
for(int g:ans.values()){
p[k++] = g;
}
// out.println(A);
// out.println(Arrays.toString(p));
// out.println(Arrays.toString(a));
// out.println(ans);
PriorityQueue<Integer> V = new PriorityQueue<>();
int h = 0;
for(int i = 0; i < 2*n; i++){
if(Q.get(i).x==1){
V.add(p[h++]);
}
else{
if(V.size()==0){
out.println("NO");
return;
}
int x = V.poll();
if(Q.get(i).y!=x){
out.println("NO");
return;
}
}
}
out.println("YES");
for(int i = 0; i < n; i++){
out.print(p[i]+" ");
}
}
static class Node {
ArrayList<Node> connect = new ArrayList<>();
int comp = -1;
public int gsize() {
return connect.size();
}
}
static long nC2(long n) {
return add((n * (n + 1)) / 2, 0);
}
public static int maxRight(int x, int[] a) {
int l = -1; //a[l]<=x
int r = a.length;//a[r]>x
while (r - l > 1) {
int m = l + (r - l) / 2;
if (a[m] <= x) {
l = m;
} else {
r = m;
}
}
return l + 1;
}
public static int minLeft(int x, int[] a) {
int l = -1; //a[l]<x
int r = a.length;//a[r]>=x
while (r - l > 1) {
int m = l + (r - l) / 2;
if (a[m] < x) {
l = m;
} else {
r = m;
}
}
return r + 1;
}
public static int lowerBound(int key, int[] a) {
int s = 0;
int e = a.length - 1;
if (e == -1) {
return 0;
}
int ans = -1;
while (s <= e) {
int m = s + (e - s) / 2;
if (a[m] >= key) {
ans = m;
e = m - 1;
} else {
s = m + 1;
}
}
return ans == -1 ? s : ans;
}
public static int upperBound(int key, int[] a) {
int s = 0;
int e = a.length - 1;
if (e == -1) {
return 0;
}
int ans = -1;
while (s <= e) {
int m = s + (e - s) / 2;
if (a[m] > key) {
ans = m;
e = m - 1;
} else {
s = m + 1;
}
}
return ans == -1 ? s : ans;
}
public static long c2(long n) {
if ((n & 1) == 0) {
return mul(n / 2, n - 1);
} else {
return mul(n, (n - 1) / 2);
}
}
public static long mul(long a, long b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
public static long add(long a, long b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
public static long sub(long a, long b) {
return ((a % MOD) - (b % MOD)) % MOD;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Integer.lowestOneBit(i) Equals k where k is the position of the first one in the binary
//Integer.highestOneBit(i) Equals k where k is the position of the last one in the binary
//Integer.bitCount(i) returns the number of one-bits
//Collections.sort(A,(p1,p2)->(int)(p2.x-p1.x)) To sort ArrayList in descending order wrt values of x.
// Arrays.parallelSort(a,new Comparator<TPair>() {
// public int compare(TPair a,TPair b) {
// if(a.y==b.y) return a.x-b.x;
// return b.y-a.y;
// }
// });
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//PrimeFactors
public static ArrayList<Long> primeFactors(long n) {
ArrayList<Long> arr = new ArrayList<>();
if (n % 2 == 0)
arr.add((long) 2);
while (n % 2 == 0)
n /= 2;
for (long i = 3; i <= Math.sqrt(n); i += 2) {
int flag = 0;
while (n % i == 0) {
n /= i;
flag = 1;
}
if (flag == 1)
arr.add(i);
}
if (n > 2)
arr.add(n);
return arr;
}
//Pair Class
static class Pair implements Comparable<Pair> {
int x;
int y;
int len;
public Pair(int x, int y) {
this.x = x;
this.y = y;
this.len = y - x + 1;
}
@Override
public int compareTo(Pair o) {
if (this.x == o.x) {
return (this.y - o.y);
}
return -1 * (this.x - o.x);
}
}
static class TPair implements Comparable<TPair> {
int l;
int r;
long index;
public TPair(int l, int r, long index) {
this.l = l;
this.r = r;
this.index = index;
}
@Override
public int compareTo(TPair o) {
if (this.l == o.l) {
return (this.r - o.r);
}
return -1 * (this.l - o.l);
}
}
//nCr
static long[] facts, factInvs;
static long exp(long base, long e) {
if (e == 0) return 1;
long half = exp(base, e / 2);
if (e % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long modInv(long x) {
return exp(x, MOD - 2);
}
static void precomp() {
facts = new long[1_000_000];
factInvs = new long[1_000_000];
factInvs[0] = facts[0] = 1;
for (int i = 1; i < facts.length; i++)
facts[i] = mul(facts[i - 1], i);
factInvs[facts.length - 1] = modInv(facts[facts.length - 1]);
for (int i = facts.length - 2; i >= 0; i--)
factInvs[i] = mul(factInvs[i + 1], i + 1);
}
static long nCk(int n, int k) {
return mul(facts[n], mul(factInvs[k], factInvs[n - k]));
}
//Kadane's Algorithm
static long maxSubArraySum(ArrayList<Long> a) {
if (a.size() == 0) {
return 0;
}
long max_so_far = a.get(0);
long curr_max = a.get(0);
for (int i = 1; i < a.size(); i++) {
curr_max = Math.max(a.get(i), curr_max + a.get(i));
max_so_far = Math.max(max_so_far, curr_max);
}
return max_so_far;
}
//Shuffle Sort
static final Random random = new Random();
static void ruffleSort(int[] a) {
int n = a.length;//shuffle, then sort
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
//Merge Sort
static void merge(long arr[], int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
/*Copy data to temp arrays*/
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(long arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
//Brian Kernighans Algorithm
static long countSetBits(long n) {
if (n == 0) return 0;
return 1 + countSetBits(n & (n - 1));
}
//Factorial
static long factorial(long n) {
if (n == 1) return 1;
if (n == 2) return 2;
if (n == 3) return 6;
return n * factorial(n - 1);
}
//Euclidean Algorithm
static long gcd(long A, long B) {
if (B == 0) return A;
return gcd(B, A % B);
}
//Modular Exponentiation
static long fastExpo(long x, long n) {
if (n == 0) return 1;
if ((n & 1) == 0) return fastExpo((x * x) % MOD, n / 2) % MOD;
return ((x % MOD) * fastExpo((x * x) % MOD, (n - 1) / 2)) % MOD;
}
//AKS Algorithm
static boolean isPrime(long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i <= Math.sqrt(n); i += 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
//Reverse an array
static <T> void reverse(T arr[], int l, int r) {
Collections.reverse(Arrays.asList(arr).subList(l, r));
}
//Print array
static void print1d(long arr[]) {
out.println(Arrays.toString(arr));
}
static void print2d(int arr[][]) {
for (int a[] : arr) out.println(Arrays.toString(a));
}
//Sieve of eratosthenes
static int[] findPrimes(int n) {
boolean isPrime[] = new boolean[n + 1];
ArrayList<Integer> a = new ArrayList<>();
int result[];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= n; ++i) {
if (isPrime[i] == true) {
for (int j = i * i; j <= n; j += i) isPrime[j] = false;
}
}
for (int i = 0; i <= n; i++) if (isPrime[i] == true) a.add(i);
result = new int[a.size()];
for (int i = 0; i < a.size(); i++) result[i] = a.get(i);
return result;
}
//Indivisual factors of all nos till n
static ArrayList<Integer>[] indiFactors(int n) {
ArrayList<Integer>[] A = new ArrayList[n + 1];
for (int i = 0; i <= n; i++) {
A[i] = new ArrayList<>();
}
int[] sieve = new int[n + 1];
for (int i = 2; i <= n; i++) {
if (sieve[i] == 0) {
for (int j = i; j <= n; j += i)
if (sieve[j] == 0) {
//sieve[j]=i;
A[j].add(i);
}
}
}
return A;
}
//Segmented Sieve
static boolean[] segmentedSieve(long l, long r) {
boolean[] segSieve = new boolean[(int) (r - l + 1)];
Arrays.fill(segSieve, true);
int[] prePrimes = findPrimes((int) Math.sqrt(r));
for (int p : prePrimes) {
long low = (l / p) * p;
if (low < l) {
low += p;
}
if (low == p) {
low += p;
}
for (long j = low; j <= r; j += p) {
segSieve[(int) (j - l)] = false;
}
}
if (l == 1) {
segSieve[0] = false;
}
return segSieve;
}
//Euler Totent function
static long countCoprimes(long n) {
ArrayList<Long> prime_factors = new ArrayList<>();
long x = n, flag = 0;
while (x % 2 == 0) {
if (flag == 0) prime_factors.add(2L);
flag = 1;
x /= 2;
}
for (long i = 3; i * i <= x; i += 2) {
flag = 0;
while (x % i == 0) {
if (flag == 0) prime_factors.add(i);
flag = 1;
x /= i;
}
}
if (x > 2) prime_factors.add(x);
double ans = (double) n;
for (Long p : prime_factors) {
ans *= (1.0 - (Double) 1.0 / p);
}
return (long) ans;
}
static long modulo = (long) 1e9 + 7;
public static long modinv(long x) {
return modpow(x, modulo - 2);
}
public static long modpow(long a, long b) {
if (b == 0) {
return 1;
}
long x = modpow(a, b / 2);
x = (x * x) % modulo;
if (b % 2 == 1) {
return (x * a) % modulo;
}
return x;
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
//it reads the data about the specified point and divide the data about it ,it is quite fast
//than using direct
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception r) {
r.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());//converts string to integer
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception r) {
r.printStackTrace();
}
return str;
}
}
} | Java | ["4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4", "1\n- 1\n+", "3\n+\n+\n+\n- 2\n- 1\n- 3"] | 1 second | ["YES\n4 2 3 1", "NO", "NO"] | NoteIn the first example Tenten first placed shurikens with prices $$$4$$$ and $$$2$$$. After this a customer came in and bought the cheapest shuriken which costed $$$2$$$. Next, Tenten added a shuriken with price $$$3$$$ on the showcase to the already placed $$$4$$$-ryo. Then a new customer bought this $$$3$$$-ryo shuriken. After this she added a $$$1$$$-ryo shuriken. Finally, the last two customers bought shurikens $$$1$$$ and $$$4$$$, respectively. Note that the order $$$[2, 4, 3, 1]$$$ is also valid.In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $$$2$$$. This is impossible since the shuriken was not the cheapest, we know that the $$$1$$$-ryo shuriken was also there. | Java 8 | standard input | [
"data structures",
"implementation",
"greedy"
] | 5fa2af185c4e3c8a1ce3df0983824bad | The first line contains the only integer $$$n$$$ ($$$1\leq n\leq 10^5$$$) standing for the number of shurikens. The following $$$2n$$$ lines describe the events in the format described above. It's guaranteed that there are exactly $$$n$$$ events of the first type, and each price from $$$1$$$ to $$$n$$$ occurs exactly once in the events of the second type. | 1,700 | If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain $$$n$$$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. | standard output | |
PASSED | dcb5e9539770f6bf2e4ef12d7b7dde88 | train_001.jsonl | 1603623900 | Tenten runs a weapon shop for ninjas. Today she is willing to sell $$$n$$$ shurikens which cost $$$1$$$, $$$2$$$, ..., $$$n$$$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.Tenten keeps a record for all events, and she ends up with a list of the following types of records: + means that she placed another shuriken on the showcase; - x means that the shuriken of price $$$x$$$ was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! | 256 megabytes | // Main Code at the Bottom
import java.util.*;
import java.io.*;
public class Main{
//Fast IO class
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
boolean env=System.getProperty("ONLINE_JUDGE") != null;
if(!env) {
try {
br=new BufferedReader(new FileReader("src\\input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else 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 long MOD=1000000000+7;
//debug
static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
// Pair
static class pair{
long x,y;
pair(long a,long b){
this.x=a;
this.y=b;
}
public boolean equals(Object obj) {
if(obj == null || obj.getClass()!= this.getClass()) return false;
pair p = (pair) obj;
return (this.x==p.x && this.y==p.y);
}
public int hashCode() {
return Objects.hash(x,y);
}
}
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
//Global variables and functions
//Main function(The main code starts from here)
public static void main (String[] args) throws java.lang.Exception {
int test=1;
//test=sc.nextInt();
while(test-->0) {
int n=sc.nextInt();
int cur=0,ok=1;
Stack<Integer> stack=new Stack<>();
Stack<Integer> x=new Stack<>();
int ans[]=new int[n];
for(int i=0;i<2*n;i++) {
if(sc.next().charAt(0)=='+') {
stack.push(cur++);
x.push(0);
}
else {
int val=sc.nextInt();
if(stack.isEmpty() || val<x.peek()) {
ok=0;
break;
}
ans[stack.pop()]=val;
int v=Math.max(val, x.pop());
if(x.isEmpty()) continue;
v=Math.max(v, x.pop());
x.push(v);
}
}
if(ok==0) out.println("NO");
else {
out.println("YES");
for(int val: ans) out.print(val+" ");
}
}
out.flush();
out.close();
}
} | Java | ["4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4", "1\n- 1\n+", "3\n+\n+\n+\n- 2\n- 1\n- 3"] | 1 second | ["YES\n4 2 3 1", "NO", "NO"] | NoteIn the first example Tenten first placed shurikens with prices $$$4$$$ and $$$2$$$. After this a customer came in and bought the cheapest shuriken which costed $$$2$$$. Next, Tenten added a shuriken with price $$$3$$$ on the showcase to the already placed $$$4$$$-ryo. Then a new customer bought this $$$3$$$-ryo shuriken. After this she added a $$$1$$$-ryo shuriken. Finally, the last two customers bought shurikens $$$1$$$ and $$$4$$$, respectively. Note that the order $$$[2, 4, 3, 1]$$$ is also valid.In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $$$2$$$. This is impossible since the shuriken was not the cheapest, we know that the $$$1$$$-ryo shuriken was also there. | Java 8 | standard input | [
"data structures",
"implementation",
"greedy"
] | 5fa2af185c4e3c8a1ce3df0983824bad | The first line contains the only integer $$$n$$$ ($$$1\leq n\leq 10^5$$$) standing for the number of shurikens. The following $$$2n$$$ lines describe the events in the format described above. It's guaranteed that there are exactly $$$n$$$ events of the first type, and each price from $$$1$$$ to $$$n$$$ occurs exactly once in the events of the second type. | 1,700 | If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain $$$n$$$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. | standard output | |
PASSED | 0ce84aa3a6b4f1b294a8695bc4ee7bac | train_001.jsonl | 1603623900 | Tenten runs a weapon shop for ninjas. Today she is willing to sell $$$n$$$ shurikens which cost $$$1$$$, $$$2$$$, ..., $$$n$$$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.Tenten keeps a record for all events, and she ends up with a list of the following types of records: + means that she placed another shuriken on the showcase; - x means that the shuriken of price $$$x$$$ was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
import static java.util.Comparator.comparingInt;
public class Miston {
FastScanner in;
PrintWriter out;
private void DP_solveC() throws IOException {
int n = in.nextInt();
int Ans = (int) -1e5, AnsL = -1, AnsR = -1;
int ans = -1, ansL = -1;
for (int i = 0; i < n; i++) {
int a = in.nextInt();
if (ans > 0) {
ans += a;
} else {
ans = a;
ansL = i;
}
if (ans > Ans) {
Ans = ans;
AnsL = ansL;
AnsR = i;
}
}
out.println(AnsL + 1 + " " + (AnsR + 1) + " " + Ans);
}
private void DP_solveD() throws IOException {
int n = in.nextInt(), M = in.nextInt();
int[] m = new int[n], c = new int[n];
for (int i = 0; i < n; i++)
m[i] = in.nextInt();
for (int i = 0; i < n; i++)
c[i] = in.nextInt();
int[][] dp = new int[n][M + 1];
boolean[][] color = new boolean[n][M + 1];
if (m[0] <= M) {
fill(dp[0], m[0], M + 1, c[0]);
fill(color[0], m[0], M + 1, true);
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < M + 1; j++) {
if (j < m[i] || c[i] + dp[i - 1][j - m[i]] < dp[i - 1][j]) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = c[i] + dp[i - 1][j - m[i]];
color[i][j] = true;
}
}
}
int[] st = new int[n];
int s = 0;
for (int i = n - 1, j = M; i >= 0; i--) {
if (color[i][j]) {
st[s++] = i;
j -= m[i];
}
}
out.println(s);
while (s > 0)
out.print(st[--s] + 1 + " ");
}
private void LCA_solveI() throws IOException {
int n = in.nextInt(), q = in.nextInt();
long[] h = new long[n];
for (int i = 0; i < n; i++)
h[i] = in.nextLong();
while (q-- > 0) {
int a = in.nextInt() - 1, b = in.nextInt() - 1;
}
}
private void TC_solveA() throws IOException {
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = in.nextInt();
for (int i = 0; i < n; i += 2)
out.print(-a[i + 1] + " " + a[i] + " ");
out.println();
}
private void TC_solveB() throws IOException {
int n = in.nextInt(), m = in.nextInt();
int[][] pos = new int[2][n * m + 1];
fill(pos[0], -1);
fill(pos[1], -1);
int[][] row = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
row[i][j] = in.nextInt();
pos[0][row[i][0]] = i;
}
int lu = -1;
int[][] col = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
col[i][j] = in.nextInt();
pos[1][col[i][0]] = i;
if (pos[0][col[i][0]] != -1) {
lu = col[i][0];
}
}
for (int i = 0; i < n; i++) {
int first = col[pos[1][lu]][i];
int rowId = pos[0][first];
for (int j = 0; j < m; j++)
out.print(row[rowId][j] + " ");
out.println();
}
}
class PairС {
int l, n;
PairС(int l, int n) {
this.l = l;
this.n = n;
}
}
private void TC_solveC() throws IOException {
int[] a = new int[6];
for (int i = 0; i < 6; i++)
a[i] = in.nextInt();
sort(a);
int n = in.nextInt();
int[][] l = new int[6][n];
for (int i = 0; i < n; i++)
for (int b = in.nextInt(), j = 0; j < 6; j++)
l[5 - j][i] = b - a[j];
PairС[] p = new PairС[n * 6];
for (int i = 0; i < n; i++)
for (int j = 0; j < 6; j++)
p[i * 6 + j] = new PairС(l[j][i], i);
sort(p, comparingInt(o -> o.l));
int[] cnt = new int[n];
int max = 0;
for (int i = 0; i < n; i++)
max = max(max, l[0][i]);
int ans = (int) 2e9;
for (int i = 0; i < n * 6; i++) {
ans = min(ans, max - p[i].l);
if (++cnt[p[i].n] == 6)
break;
max = max(max, l[cnt[p[i].n]][p[i].n]);
}
out.println(ans);
}
private void TC_solveD() throws IOException {
int n = in.nextInt();
int[] qq = new int[n * 2];
for (int i = 0; i < n * 2; i++)
if (in.next().equals("-"))
qq[i] = in.nextInt();
int[] st = new int[n], ans = new int[n];
int s = 0, as = 0;
for (int i = qq.length - 1; i >= 0; i--) {
if (qq[i] == 0) {
if (s == 0) {
out.println("NO");
return;
}
ans[as++] = st[--s];
} else
st[s++] = qq[i];
}
for (int l = 0, r = n - 1; l < r; l++, r--) {
int swap = ans[l];
ans[l] = ans[r];
ans[r] = swap;
}
PriorityQueue<Integer> pq = new PriorityQueue<>();
as = 0;
for (int i = 0; i < n * 2; i++) {
if (qq[i] == 0)
pq.add(ans[as++]);
else if (pq.remove() != qq[i]) {
out.println("NO");
return;
}
}
out.println("YES");
for (int i = 0; i < n; i++)
out.print(ans[i] + " ");
}
private void solve() throws IOException {
// TC_solveA();
// TC_solveB();
// TC_solveC();
TC_solveD();
}
class FastScanner {
StringTokenizer st;
BufferedReader br;
FastScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
boolean hasNext() throws IOException {
return br.ready() || (st != null && st.hasMoreTokens());
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
String nextLine() throws IOException {
return br.readLine();
}
boolean hasNextLine() throws IOException {
return br.ready();
}
}
private void run() throws IOException {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
// for (int t = in.nextInt(); t > 0; t--)
solve();
out.flush();
out.close();
}
public static void main(String[] args) throws IOException {
new Miston().run();
}
} | Java | ["4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4", "1\n- 1\n+", "3\n+\n+\n+\n- 2\n- 1\n- 3"] | 1 second | ["YES\n4 2 3 1", "NO", "NO"] | NoteIn the first example Tenten first placed shurikens with prices $$$4$$$ and $$$2$$$. After this a customer came in and bought the cheapest shuriken which costed $$$2$$$. Next, Tenten added a shuriken with price $$$3$$$ on the showcase to the already placed $$$4$$$-ryo. Then a new customer bought this $$$3$$$-ryo shuriken. After this she added a $$$1$$$-ryo shuriken. Finally, the last two customers bought shurikens $$$1$$$ and $$$4$$$, respectively. Note that the order $$$[2, 4, 3, 1]$$$ is also valid.In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $$$2$$$. This is impossible since the shuriken was not the cheapest, we know that the $$$1$$$-ryo shuriken was also there. | Java 8 | standard input | [
"data structures",
"implementation",
"greedy"
] | 5fa2af185c4e3c8a1ce3df0983824bad | The first line contains the only integer $$$n$$$ ($$$1\leq n\leq 10^5$$$) standing for the number of shurikens. The following $$$2n$$$ lines describe the events in the format described above. It's guaranteed that there are exactly $$$n$$$ events of the first type, and each price from $$$1$$$ to $$$n$$$ occurs exactly once in the events of the second type. | 1,700 | If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain $$$n$$$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. | standard output | |
PASSED | ac649a8f0a87e8c7224106963cfcf473 | train_001.jsonl | 1603623900 | Tenten runs a weapon shop for ninjas. Today she is willing to sell $$$n$$$ shurikens which cost $$$1$$$, $$$2$$$, ..., $$$n$$$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.Tenten keeps a record for all events, and she ends up with a list of the following types of records: + means that she placed another shuriken on the showcase; - x means that the shuriken of price $$$x$$$ was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! | 256 megabytes | //cyan piece of shit
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class x1413D
{
public static void main(String hi[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
Stack<Integer> stack = new Stack<Integer>();
int[] res = new int[N];
int dex = 0;
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int qw=0; qw < 2*N; qw++)
{
st = new StringTokenizer(infile.readLine());
if(st.nextToken().charAt(0) == '+')
{
stack.push(dex++);
ls.add(0);
}
else
{
if(stack.size() == 0)
shaft("NO");
int val = stack.pop();
res[val] = Integer.parseInt(st.nextToken());
ls.add(res[val]);
}
}
PriorityQueue<Integer> active = new PriorityQueue<Integer>();
dex = 0;
for(int q: ls)
{
if(q == 0)
active.add(res[dex++]);
else
{
if(active.peek() != q)
shaft("NO");
active.poll();
}
}
StringBuilder sb = new StringBuilder("YES\n");
for(int x: res)
sb.append(x+" ");
System.out.println(sb);
}
public static void shaft(String str)
{
System.out.println(str);
System.exit(0);
}
} | Java | ["4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4", "1\n- 1\n+", "3\n+\n+\n+\n- 2\n- 1\n- 3"] | 1 second | ["YES\n4 2 3 1", "NO", "NO"] | NoteIn the first example Tenten first placed shurikens with prices $$$4$$$ and $$$2$$$. After this a customer came in and bought the cheapest shuriken which costed $$$2$$$. Next, Tenten added a shuriken with price $$$3$$$ on the showcase to the already placed $$$4$$$-ryo. Then a new customer bought this $$$3$$$-ryo shuriken. After this she added a $$$1$$$-ryo shuriken. Finally, the last two customers bought shurikens $$$1$$$ and $$$4$$$, respectively. Note that the order $$$[2, 4, 3, 1]$$$ is also valid.In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $$$2$$$. This is impossible since the shuriken was not the cheapest, we know that the $$$1$$$-ryo shuriken was also there. | Java 8 | standard input | [
"data structures",
"implementation",
"greedy"
] | 5fa2af185c4e3c8a1ce3df0983824bad | The first line contains the only integer $$$n$$$ ($$$1\leq n\leq 10^5$$$) standing for the number of shurikens. The following $$$2n$$$ lines describe the events in the format described above. It's guaranteed that there are exactly $$$n$$$ events of the first type, and each price from $$$1$$$ to $$$n$$$ occurs exactly once in the events of the second type. | 1,700 | If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain $$$n$$$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. | standard output | |
PASSED | d5684f0a0b76eb710d3b9394b78ad9d2 | train_001.jsonl | 1603623900 | Tenten runs a weapon shop for ninjas. Today she is willing to sell $$$n$$$ shurikens which cost $$$1$$$, $$$2$$$, ..., $$$n$$$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.Tenten keeps a record for all events, and she ends up with a list of the following types of records: + means that she placed another shuriken on the showcase; - x means that the shuriken of price $$$x$$$ was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! | 256 megabytes | import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.StringTokenizer;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class D_Techno implements Runnable{
// SOLUTION AT THE TOP OF CODE!!!
// HACK ME PLEASE IF YOU CAN!!!
// PLEASE!!!
// PLEASE!!!
// PLEASE!!!
@SuppressWarnings("unused")
private final static Random rnd = new Random();
private final static String fileName = "";
private final static long MODULO = 1000 * 1000 * 1000 + 7;
// THERE SOLUTION STARTS!!!
private void solve() {
int n = readInt();
Point[] queries = new Point[n + n];
for (int i = 0; i < queries.length; ++i) {
char type = readString().charAt(0);
int value = (remove == type ? readInt() - 1 : -1);
queries[i] = new Point(type, value);
}
int[] answer = getAnswer(n, queries);
if (yesNo(0 != answer.length)) {
for (int value : answer) {
out.print(value + 1);
out.print(" ");
}
out.println();
}
}
final char add = '+', remove = '-';
int[] getAnswer(int n, Point[] queries) {
final int[] noAnswer = new int[0];
int[] answer = new int[n];
Queue<Integer> freePositions = new PriorityQueue<>(
Comparator.reverseOrder()
);
int[] answerPositions = new int[queries.length];
SegmentTree removedPositions = new SegmentTree(n);
for (int i = 0, pos = 0; i < queries.length; ++i) {
if (add == queries[i].x) {
freePositions.add(i);
answerPositions[i] = pos++;
} else {
if (freePositions.isEmpty()) return noAnswer;
int value = queries[i].y;
int nearestRemovedHigherPosition = removedPositions.getSuffixMax(value);
int lastFreePosition = freePositions.poll();
if (nearestRemovedHigherPosition > lastFreePosition) {
return noAnswer;
}
answer[answerPositions[lastFreePosition]] = value;
removedPositions.updateIndex(value, i);
}
}
return answer;
}
static class SegmentTree {
int size;
int[] tree;
SegmentTree(int size) {
this.size = size;
this.tree = new int[size << 2];
Arrays.fill(tree, -1);
}
int start, end;
int result;
int getSuffixMax(int startInclusive) {
this.start = startInclusive;
this.end = size;
this.result = -1;
getTreeSegment(1, 0, size);
return result;
}
void getTreeSegment(int v, int left, int right) {
if (start <= left && right <= end) {
result = max(result, tree[v]);
} else {
int mid = (left + right) / 2;
int vLeft = (v << 1), vRight = (vLeft + 1);
if (start < mid) getTreeSegment(vLeft, left, mid);
if (mid < end) getTreeSegment(vRight, mid, right);
}
}
int index;
int value;
void updateIndex(int index, int value) {
this.index = index;
this.value = value;
updateTreeIndex(1, 0, size);
}
void updateTreeIndex(int v, int left, int right) {
if (left + 1 == right) {
tree[v] = value;
} else {
int mid = (left + right) / 2;
int vLeft = (v << 1), vRight = (vLeft + 1);
if (index < mid) updateTreeIndex(vLeft, left, mid);
else updateTreeIndex(vRight, mid, right);
updateVertex(v, vLeft, vRight);
}
}
void updateVertex(int v, int vLeft, int vRight) {
tree[v] = max(tree[vLeft], tree[vRight]);
}
}
/////////////////////////////////////////////////////////////////////
private final static boolean MULTIPLE_TESTS = true;
private final boolean ONLINE_JUDGE = !new File("input.txt").exists();
// private final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
private final static int MAX_STACK_SIZE = 128;
private final static boolean OPTIMIZE_READ_NUMBERS = false;
/////////////////////////////////////////////////////////////////////
@SuppressWarnings("unused")
private static long inverse(long x) {
return binpow(x, MODULO - 2);
}
private static long binpow(long base, long power) {
if (power == 0) return 1;
if ((power & 1) == 0) {
long half = binpow(base, power >> 1);
return mult(half, half);
} else {
long prev = binpow(base, power - 1);
return mult(prev, base);
}
}
private static long add(long a, long b) { return (a + b) % MODULO; }
@SuppressWarnings("unused")
private static long subtract(long a, long b) { return add(a, MODULO - b % MODULO); }
private static long mult(long a, long b) { return (a * b) % MODULO; }
/////////////////////////////////////////////////////////////////////
@SuppressWarnings("unused")
boolean yesNo(boolean yes) {
out.println(yes ? "YES" : "NO");
return yes;
}
/////////////////////////////////////////////////////////////////////
public void run(){
try{
timeInit();
Locale.setDefault(Locale.US);
init();
if (ONLINE_JUDGE) {
solve();
} else {
do {
try {
timeInit();
solve();
time();
out.println();
out.flush();
} catch (NumberFormatException | EOFException e) {
break;
}
} while (MULTIPLE_TESTS);
}
out.close();
time();
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
/////////////////////////////////////////////////////////////////////
private BufferedReader in;
private OutputWriter out;
private StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args){
new Thread(null, new D_Techno(), "", MAX_STACK_SIZE * (1L << 20)).start();
}
/////////////////////////////////////////////////////////////////////
private void init() throws FileNotFoundException{
Locale.setDefault(Locale.US);
if (ONLINE_JUDGE){
if (fileName.isEmpty()) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new OutputWriter(System.out);
} else {
in = new BufferedReader(new FileReader(fileName + ".in"));
out = new OutputWriter(fileName + ".out");
}
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new OutputWriter("output.txt");
}
}
////////////////////////////////////////////////////////////////
private long timeBegin;
private void timeInit() {
this.timeBegin = System.currentTimeMillis();
}
private void time(){
long timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
@SuppressWarnings("unused")
private void debug(Object... objects){
if (ONLINE_JUDGE){
for (Object o: objects){
System.err.println(o.toString());
}
}
}
/////////////////////////////////////////////////////////////////////
private String delim = " ";
private String readNullableLine() {
try {
return in.readLine();
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
private String readLine() {
String line = readNullableLine();
if (null == line) throw new EOFException();
return line;
}
private String readString() {
while(!tok.hasMoreTokens()){
tok = new StringTokenizer(readLine(), delim);
}
return tok.nextToken(delim);
}
/////////////////////////////////////////////////////////////////
private final char NOT_A_SYMBOL = '\0';
@SuppressWarnings("unused")
private char readChar() {
try {
int intValue = in.read();
if (intValue == -1){
return NOT_A_SYMBOL;
}
return (char) intValue;
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
private char[] readCharArray() {
return readLine().toCharArray();
}
@SuppressWarnings("unused")
private char[][] readCharField(int rowsCount) {
char[][] field = new char[rowsCount][];
for (int row = 0; row < rowsCount; ++row) {
field[row] = readCharArray();
}
return field;
}
/////////////////////////////////////////////////////////////////
private long optimizedReadLong() {
int sign = 1;
long result = 0;
boolean started = false;
while (true) {
try {
int j = in.read();
if (-1 == j) {
if (started) return sign * result;
throw new NumberFormatException();
}
if (j == '-') {
if (started) throw new NumberFormatException();
sign = -sign;
}
if ('0' <= j && j <= '9') {
result = result * 10 + j - '0';
started = true;
} else if (started) {
return sign * result;
}
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
}
private int readInt() {
if (!OPTIMIZE_READ_NUMBERS) {
return Integer.parseInt(readString());
} else {
return (int) optimizedReadLong();
}
}
private int[] readIntArray(int size) {
int[] array = new int[size];
for (int index = 0; index < size; ++index){
array[index] = readInt();
}
return array;
}
@SuppressWarnings("unused")
private int[] readSortedIntArray(int size) {
Integer[] array = new Integer[size];
for (int index = 0; index < size; ++index) {
array[index] = readInt();
}
Arrays.sort(array);
int[] sortedArray = new int[size];
for (int index = 0; index < size; ++index) {
sortedArray[index] = array[index];
}
return sortedArray;
}
private int[] readIntArrayWithDecrease(int size) {
int[] array = readIntArray(size);
for (int i = 0; i < size; ++i) {
array[i]--;
}
return array;
}
///////////////////////////////////////////////////////////////////
@SuppressWarnings("unused")
private int[][] readIntMatrix(int rowsCount, int columnsCount) {
int[][] matrix = new int[rowsCount][];
for (int rowIndex = 0; rowIndex < rowsCount; ++rowIndex) {
matrix[rowIndex] = readIntArray(columnsCount);
}
return matrix;
}
@SuppressWarnings("unused")
private int[][] readIntMatrixWithDecrease(int rowsCount, int columnsCount) {
int[][] matrix = new int[rowsCount][];
for (int rowIndex = 0; rowIndex < rowsCount; ++rowIndex) {
matrix[rowIndex] = readIntArrayWithDecrease(columnsCount);
}
return matrix;
}
///////////////////////////////////////////////////////////////////
private long readLong() {
if (!OPTIMIZE_READ_NUMBERS) {
return Long.parseLong(readString());
} else {
return optimizedReadLong();
}
}
@SuppressWarnings("unused")
private long[] readLongArray(int size) {
long[] array = new long[size];
for (int index = 0; index < size; ++index){
array[index] = readLong();
}
return array;
}
////////////////////////////////////////////////////////////////////
private double readDouble() {
return Double.parseDouble(readString());
}
@SuppressWarnings("unused")
private double[] readDoubleArray(int size) {
double[] array = new double[size];
for (int index = 0; index < size; ++index){
array[index] = readDouble();
}
return array;
}
////////////////////////////////////////////////////////////////////
@SuppressWarnings("unused")
private BigInteger readBigInteger() {
return new BigInteger(readString());
}
@SuppressWarnings("unused")
private BigDecimal readBigDecimal() {
return new BigDecimal(readString());
}
/////////////////////////////////////////////////////////////////////
private Point readPoint() {
int x = readInt();
int y = readInt();
return new Point(x, y);
}
@SuppressWarnings("unused")
private Point[] readPointArray(int size) {
Point[] array = new Point[size];
for (int index = 0; index < size; ++index){
array[index] = readPoint();
}
return array;
}
/////////////////////////////////////////////////////////////////////
@Deprecated
@SuppressWarnings("unused")
private List<Integer>[] readGraph(int vertexNumber, int edgeNumber) {
@SuppressWarnings("unchecked")
List<Integer>[] graph = new List[vertexNumber];
for (int index = 0; index < vertexNumber; ++index){
graph[index] = new ArrayList<>();
}
while (edgeNumber-- > 0){
int from = readInt() - 1;
int to = readInt() - 1;
graph[from].add(to);
graph[to].add(from);
}
return graph;
}
private static class GraphBuilder {
final int size;
final List<Integer>[] edges;
static GraphBuilder createInstance(int size) {
@SuppressWarnings("unchecked")
List<Integer>[] edges = new List[size];
for (int v = 0; v < size; ++v) {
edges[v] = new ArrayList<>();
}
return new GraphBuilder(edges);
}
private GraphBuilder(List<Integer>[] edges) {
this.size = edges.length;
this.edges = edges;
}
public void addEdge(int from, int to) {
addDirectedEdge(from, to);
addDirectedEdge(to, from);
}
public void addDirectedEdge(int from, int to) {
edges[from].add(to);
}
public int[][] build() {
int[][] graph = new int[size][];
for (int v = 0; v < size; ++v) {
List<Integer> vEdges = edges[v];
graph[v] = castInt(vEdges);
}
return graph;
}
}
@SuppressWarnings("unused")
private final static int ZERO_INDEXATION = 0, ONE_INDEXATION = 1;
@SuppressWarnings("unused")
private int[][] readUnweightedGraph(int vertexNumber, int edgesNumber) {
return readUnweightedGraph(vertexNumber, edgesNumber, ONE_INDEXATION, false);
}
private int[][] readUnweightedGraph(int vertexNumber, int edgesNumber,
int indexation, boolean directed
) {
GraphBuilder graphBuilder = GraphBuilder.createInstance(vertexNumber);
for (int i = 0; i < edgesNumber; ++i) {
int from = readInt() - indexation;
int to = readInt() - indexation;
if (directed) graphBuilder.addDirectedEdge(from, to);
else graphBuilder.addEdge(from, to);
}
return graphBuilder.build();
}
private static class Edge {
int to;
int w;
Edge(int to, int w) {
this.to = to;
this.w = w;
}
}
@SuppressWarnings("unused")
private Edge[][] readWeightedGraph(int vertexNumber, int edgesNumber) {
return readWeightedGraph(vertexNumber, edgesNumber, ONE_INDEXATION, false);
}
private Edge[][] readWeightedGraph(int vertexNumber, int edgesNumber,
int indexation, boolean directed) {
@SuppressWarnings("unchecked")
List<Edge>[] graph = new List[vertexNumber];
for (int v = 0; v < vertexNumber; ++v) {
graph[v] = new ArrayList<>();
}
while (edgesNumber --> 0) {
int from = readInt() - indexation;
int to = readInt() - indexation;
int w = readInt();
graph[from].add(new Edge(to, w));
if (!directed) graph[to].add(new Edge(from, w));
}
Edge[][] graphArrays = new Edge[vertexNumber][];
for (int v = 0; v < vertexNumber; ++v) {
graphArrays[v] = graph[v].toArray(new Edge[0]);
}
return graphArrays;
}
/////////////////////////////////////////////////////////////////////
private static class IntIndexPair {
@SuppressWarnings("unused")
static Comparator<IntIndexPair> increaseComparator = (indexPair1, indexPair2) -> {
int value1 = indexPair1.value;
int value2 = indexPair2.value;
if (value1 != value2) {
return value1 - value2;
}
int index1 = indexPair1.index;
int index2 = indexPair2.index;
return index1 - index2;
};
@SuppressWarnings("unused")
static Comparator<IntIndexPair> decreaseComparator = (indexPair1, indexPair2) -> {
int value1 = indexPair1.value;
int value2 = indexPair2.value;
if (value1 != value2) {
return -(value1 - value2);
}
int index1 = indexPair1.index;
int index2 = indexPair2.index;
return index1 - index2;
};
@SuppressWarnings("unused")
static IntIndexPair[] from(int[] array) {
IntIndexPair[] iip = new IntIndexPair[array.length];
for (int i = 0; i < array.length; ++i) {
iip[i] = new IntIndexPair(array[i], i);
}
return iip;
}
int value, index;
IntIndexPair(int value, int index) {
super();
this.value = value;
this.index = index;
}
@SuppressWarnings("unused")
int getRealIndex() {
return index + 1;
}
}
@SuppressWarnings("unused")
private IntIndexPair[] readIntIndexArray(int size) {
IntIndexPair[] array = new IntIndexPair[size];
for (int index = 0; index < size; ++index) {
array[index] = new IntIndexPair(readInt(), index);
}
return array;
}
/////////////////////////////////////////////////////////////////////
private static class OutputWriter extends PrintWriter {
final int DEFAULT_PRECISION = 12;
private int precision;
private String format, formatWithSpace;
{
precision = DEFAULT_PRECISION;
format = createFormat(precision);
formatWithSpace = format + " ";
}
OutputWriter(OutputStream out) {
super(out);
}
OutputWriter(String fileName) throws FileNotFoundException {
super(fileName);
}
int getPrecision() {
return precision;
}
void setPrecision(int precision) {
precision = max(0, precision);
this.precision = precision;
format = createFormat(precision);
formatWithSpace = format + " ";
}
String createFormat(int precision){
return "%." + precision + "f";
}
@Override
public void print(double d){
printf(format, d);
}
@Override
public void println(double d){
print(d);
println();
}
void printWithSpace(double d){
printf(formatWithSpace, d);
}
void printAll(double...d){
for (int i = 0; i < d.length - 1; ++i){
printWithSpace(d[i]);
}
print(d[d.length - 1]);
}
@SuppressWarnings("unused")
void printlnAll(double... d){
printAll(d);
println();
}
void printAll(int... array) {
for (int value : array) {
print(value + " ");
}
}
@SuppressWarnings("unused")
void printlnAll(int... array) {
printAll(array);
println();
}
void printAll(long... array) {
for (long value : array) {
print(value + " ");
}
}
@SuppressWarnings("unused")
void printlnAll(long... array) {
printAll(array);
println();
}
}
/////////////////////////////////////////////////////////////////////
private static class EOFException extends RuntimeException {
EOFException() {
super();
}
}
private static class RuntimeIOException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = -6463830523020118289L;
RuntimeIOException(Throwable cause) {
super(cause);
}
}
/////////////////////////////////////////////////////////////////////
//////////////// Some useful constants andTo functions ////////////////
/////////////////////////////////////////////////////////////////////
private static void swap(int[] array, int i, int j) {
if (i != j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
private static <T> void swap(T[] array, int i, int j) {
if (i != j) {
T tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
private static final int[][] steps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
private static final int[][] steps8 = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1},
{-1, -1}, {1, 1}, {1, -1}, {-1, 1}
};
@SuppressWarnings("unused")
private static boolean checkCell(int row, int rowsCount, int column, int columnsCount) {
return checkIndex(row, rowsCount) && checkIndex(column, columnsCount);
}
private static boolean checkIndex(int index, int lim){
return (0 <= index && index < lim);
}
/////////////////////////////////////////////////////////////////////
private static int getBit(long mask, int bit) { return (int)((mask >> bit) & 1); }
@SuppressWarnings("unused")
private static boolean checkBit(long mask, int bit){
return getBit(mask, bit) != 0;
}
/////////////////////////////////////////////////////////////////////
@SuppressWarnings("unused")
private static long getSum(int[] array) {
long sum = 0;
for (int value: array) {
sum += value;
}
return sum;
}
@SuppressWarnings("unused")
private static Point getMinMax(int[] array) {
int min = array[0];
int max = array[0];
for (int index = 0, size = array.length; index < size; ++index, ++index) {
int value = array[index];
if (index == size - 1) {
min = min(min, value);
max = max(max, value);
} else {
int otherValue = array[index + 1];
if (value <= otherValue) {
min = min(min, value);
max = max(max, otherValue);
} else {
min = min(min, otherValue);
max = max(max, value);
}
}
}
return new Point(min, max);
}
/////////////////////////////////////////////////////////////////////
@SuppressWarnings("unused")
private static boolean isPrime(int x) {
if (x < 2) return false;
for (int d = 2; d * d <= x; ++d) {
if (x % d == 0) return false;
}
return true;
}
@SuppressWarnings("unused")
private static int[] getPrimes(int n) {
boolean[] used = new boolean[n];
used[0] = used[1] = true;
int size = 0;
for (int i = 2; i < n; ++i) {
if (!used[i]) {
++size;
for (int j = 2 * i; j < n; j += i) {
used[j] = true;
}
}
}
int[] primes = new int[size];
for (int i = 0, cur = 0; i < n; ++i) {
if (!used[i]) {
primes[cur++] = i;
}
}
return primes;
}
/////////////////////////////////////////////////////////////////////
@SuppressWarnings("unused")
int[] getDivisors(int value) {
List<Integer> divisors = new ArrayList<>();
for (int divisor = 1; divisor * divisor <= value; ++divisor) {
if (value % divisor == 0) {
divisors.add(divisor);
if (divisor * divisor != value) {
divisors.add(value / divisor);
}
}
}
return castInt(divisors);
}
@SuppressWarnings("unused")
long[] getDivisors(long value) {
List<Long> divisors = new ArrayList<>();
for (long divisor = 1; divisor * divisor <= value; ++divisor) {
if (value % divisor == 0) {
divisors.add(divisor);
if (divisor * divisor != value) {
divisors.add(value / divisor);
}
}
}
return castLong(divisors);
}
/////////////////////////////////////////////////////////////////////
@SuppressWarnings("unused")
private static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
private static long gcd(long a, long b) {
return (a == 0 ? b : gcd(b % a, a));
}
/////////////////////////////////////////////////////////////////////
private static int[] castInt(List<Integer> list) {
int[] array = new int[list.size()];
for (int i = 0; i < array.length; ++i) {
array[i] = list.get(i);
}
return array;
}
@SuppressWarnings("unused")
private static long[] castLong(List<Long> list) {
long[] array = new long[list.size()];
for (int i = 0; i < array.length; ++i) {
array[i] = list.get(i);
}
return array;
}
/////////////////////////////////////////////////////////////////////
/**
* Generates list with keys 0..<bowlsCount
* @param n - exclusive limit of sequence
*/
@SuppressWarnings("unused")
private static List<Integer> order(int n) {
List<Integer> sequence = new ArrayList<>();
for (int i = 0; i < n; ++i) {
sequence.add(i);
}
return sequence;
}
@SuppressWarnings("unused")
private static List<Integer> shuffled(List<Integer> list) {
Collections.shuffle(list, rnd);
return list;
}
} | Java | ["4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4", "1\n- 1\n+", "3\n+\n+\n+\n- 2\n- 1\n- 3"] | 1 second | ["YES\n4 2 3 1", "NO", "NO"] | NoteIn the first example Tenten first placed shurikens with prices $$$4$$$ and $$$2$$$. After this a customer came in and bought the cheapest shuriken which costed $$$2$$$. Next, Tenten added a shuriken with price $$$3$$$ on the showcase to the already placed $$$4$$$-ryo. Then a new customer bought this $$$3$$$-ryo shuriken. After this she added a $$$1$$$-ryo shuriken. Finally, the last two customers bought shurikens $$$1$$$ and $$$4$$$, respectively. Note that the order $$$[2, 4, 3, 1]$$$ is also valid.In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $$$2$$$. This is impossible since the shuriken was not the cheapest, we know that the $$$1$$$-ryo shuriken was also there. | Java 8 | standard input | [
"data structures",
"implementation",
"greedy"
] | 5fa2af185c4e3c8a1ce3df0983824bad | The first line contains the only integer $$$n$$$ ($$$1\leq n\leq 10^5$$$) standing for the number of shurikens. The following $$$2n$$$ lines describe the events in the format described above. It's guaranteed that there are exactly $$$n$$$ events of the first type, and each price from $$$1$$$ to $$$n$$$ occurs exactly once in the events of the second type. | 1,700 | If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain $$$n$$$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. | standard output | |
PASSED | 2b21e89217a75e684ba47e847046997b | train_001.jsonl | 1603623900 | Tenten runs a weapon shop for ninjas. Today she is willing to sell $$$n$$$ shurikens which cost $$$1$$$, $$$2$$$, ..., $$$n$$$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.Tenten keeps a record for all events, and she ends up with a list of the following types of records: + means that she placed another shuriken on the showcase; - x means that the shuriken of price $$$x$$$ was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static ArrayList<Integer>[] g;
static long[] a;
static long[] b;
static boolean visted[];
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
boolean ans = true;
int[] arr = new int[n+1];
Queue<Integer> f = new LinkedList<>();
Stack<Integer> s = new Stack<>();
int v = -1;
int pos = 0;
int add = 0;
boolean[] skip = new boolean[n+1];
int current = n;
for (int i = 0; i <2*n; i++) {
char c = in.next().charAt(0);
if (c == '-')
{
if (s.size() == 0){
ans = false;
break;
}
if (add!=0) f.add(add);
add = 0;
v = in.nextInt();
arr[s.pop()] = v;
}
else {
s.add(pos);
pos+=1;
add++;
}
}
int ind = 0;
while (f.size()!=0){
int freq = f.poll();
for (int i = ind; i <ind+freq-1; i++) {
if (arr[i] < arr[i+1]){
ans = false;
break;
}
}
ind+=freq;
}
if (ans){
System.out.println("YES");
for (int i = 0; i <n; i++) {
System.out.print(arr[i]+" ");
}
}
else System.out.println("NO");
}
}
static long as = 0;
static long bs = 0;
static void dfs(int n){
visted[n]=true;
as+=a[n];
bs+=b[n];
for (int v : g[n]){
if (visted[v])continue;
dfs(v);
}
}
static class sort1 implements Comparator<point>
{
public int compare(point a, point b)
{
return a.x - b.x;
}
}
class sort2 implements Comparator<point>
{
public int compare(point a, point b)
{
return a.y - b.y;
}
}
static class point{
int x;
int y;
int id;
public point(int x, int y, int id) {
this.x = x;
this.y = y;
this.id = id;
}
}
static class edge {
int x;
int dis;
public edge(int x, int dis) {
this.x = x;
this.dis = dis;
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | Java | ["4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4", "1\n- 1\n+", "3\n+\n+\n+\n- 2\n- 1\n- 3"] | 1 second | ["YES\n4 2 3 1", "NO", "NO"] | NoteIn the first example Tenten first placed shurikens with prices $$$4$$$ and $$$2$$$. After this a customer came in and bought the cheapest shuriken which costed $$$2$$$. Next, Tenten added a shuriken with price $$$3$$$ on the showcase to the already placed $$$4$$$-ryo. Then a new customer bought this $$$3$$$-ryo shuriken. After this she added a $$$1$$$-ryo shuriken. Finally, the last two customers bought shurikens $$$1$$$ and $$$4$$$, respectively. Note that the order $$$[2, 4, 3, 1]$$$ is also valid.In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $$$2$$$. This is impossible since the shuriken was not the cheapest, we know that the $$$1$$$-ryo shuriken was also there. | Java 8 | standard input | [
"data structures",
"implementation",
"greedy"
] | 5fa2af185c4e3c8a1ce3df0983824bad | The first line contains the only integer $$$n$$$ ($$$1\leq n\leq 10^5$$$) standing for the number of shurikens. The following $$$2n$$$ lines describe the events in the format described above. It's guaranteed that there are exactly $$$n$$$ events of the first type, and each price from $$$1$$$ to $$$n$$$ occurs exactly once in the events of the second type. | 1,700 | If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain $$$n$$$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. | standard output | |
PASSED | 9ee0cf50877b1523a2a41c8560e6dbce | train_001.jsonl | 1603623900 | Tenten runs a weapon shop for ninjas. Today she is willing to sell $$$n$$$ shurikens which cost $$$1$$$, $$$2$$$, ..., $$$n$$$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.Tenten keeps a record for all events, and she ends up with a list of the following types of records: + means that she placed another shuriken on the showcase; - x means that the shuriken of price $$$x$$$ was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[])
{
FastReader input=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=1;
while(T-->0)
{
int n=input.nextInt();
int count=0;
int a[]=new int[n+1];
ArrayList<Integer> list=new ArrayList<>();
ArrayList<Integer> val=new ArrayList<>();
int flag=0;
String str[]=new String[2*n];
for(int i=0;i<2*n;i++)
{
String s="";
char c=input.next().charAt(0);
s+=c;
if(c=='+')
{
mergeSort(val,0,val.size()-1);
if(val.size()>list.size())
{
flag=1;
}
else
{
for(int j=0;j<val.size();j++)
{
int v=val.get(j);
int ind=list.get(list.size()-1);
a[ind]=v;
list.remove(list.size()-1);
}
}
count++;
list.add(count);
val=new ArrayList<>();
}
else
{
int v=input.nextInt();
s+=v;
val.add(v);
}
str[i]=s;
}
mergeSort(val,0,val.size()-1);
if(val.size()>list.size())
{
flag=1;
}
else
{
for(int j=0;j<val.size();j++)
{
int v=val.get(j);
int ind=list.get(list.size()-1);
a[ind]=v;
list.remove(list.size()-1);
}
}
if(flag==1)
{
out.println("NO");
}
else
{
/*
for(int i=1;i<=n;i++)
{
out.print(a[i]+" ");
}
out.println();
*/
int c=0;
HashSet<Integer> set=new HashSet<>();
PriorityQueue<Integer> q=new PriorityQueue<>();
for(int i=0;i<str.length;i++)
{
if(str[i].charAt(0)=='+')
{
c++;
q.add(a[c]);
}
else
{
int min=0;
while(true)
{
min=q.poll();
if(!set.contains(min))
{
break;
}
}
set.add(Integer.parseInt(str[i].substring(1,str[i].length())));
if(min!=Integer.parseInt(str[i].substring(1,str[i].length())))
{
flag=1;
break;
}
}
}
if(flag==1)
{
out.println("NO");
}
else
{
out.println("YES");
for(int i=1;i<=n;i++)
{
out.print(a[i]+" ");
}
}
}
}
out.close();
}
public static void mergeSort(ArrayList<Integer> a,int p,int r)
{
if(p<r)
{
int q=(p+r)/2;
mergeSort(a,p,q);
mergeSort(a,q+1,r);
merge(a,p,q,r);
}
}
public static void merge(ArrayList<Integer> a,int p,int q,int r)
{
int n1=q-p+2;
int L[]=new int[n1];
int n2=r-q+1;
int R[]=new int[n2];
for(int i=p;i<=q;i++)
{
L[i-p]=a.get(i);
}
L[n1-1]=Integer.MAX_VALUE;
for(int i=q+1;i<=r;i++)
{
R[i-q-1]=a.get(i);
}
R[n2-1]=Integer.MAX_VALUE;
int x=0,y=0;
for(int i=p;i<=r;i++)
{
if(L[x]<=R[y])
{
a.set(i,L[x]);
x++;
}
else
{
a.set(i,R[y]);
y++;
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4", "1\n- 1\n+", "3\n+\n+\n+\n- 2\n- 1\n- 3"] | 1 second | ["YES\n4 2 3 1", "NO", "NO"] | NoteIn the first example Tenten first placed shurikens with prices $$$4$$$ and $$$2$$$. After this a customer came in and bought the cheapest shuriken which costed $$$2$$$. Next, Tenten added a shuriken with price $$$3$$$ on the showcase to the already placed $$$4$$$-ryo. Then a new customer bought this $$$3$$$-ryo shuriken. After this she added a $$$1$$$-ryo shuriken. Finally, the last two customers bought shurikens $$$1$$$ and $$$4$$$, respectively. Note that the order $$$[2, 4, 3, 1]$$$ is also valid.In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $$$2$$$. This is impossible since the shuriken was not the cheapest, we know that the $$$1$$$-ryo shuriken was also there. | Java 8 | standard input | [
"data structures",
"implementation",
"greedy"
] | 5fa2af185c4e3c8a1ce3df0983824bad | The first line contains the only integer $$$n$$$ ($$$1\leq n\leq 10^5$$$) standing for the number of shurikens. The following $$$2n$$$ lines describe the events in the format described above. It's guaranteed that there are exactly $$$n$$$ events of the first type, and each price from $$$1$$$ to $$$n$$$ occurs exactly once in the events of the second type. | 1,700 | If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain $$$n$$$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. | standard output | |
PASSED | 4db110399d36bf4c30781aca68521f04 | train_001.jsonl | 1603623900 | Tenten runs a weapon shop for ninjas. Today she is willing to sell $$$n$$$ shurikens which cost $$$1$$$, $$$2$$$, ..., $$$n$$$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.Tenten keeps a record for all events, and she ends up with a list of the following types of records: + means that she placed another shuriken on the showcase; - x means that the shuriken of price $$$x$$$ was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main {
static long INF = (long) (2e10);
static FastScanner sc;
static PrintWriter pw;
public static void main(String[] args) throws IOException {
sc = new FastScanner(System.in);
pw = new PrintWriter(System.out);
int n = sc.nextInt();
TreeSet<Integer> indexes = new TreeSet<>();
int[] ans = new int[n];
char[] types = new char[n * 2];
int[] nums = new int[n * 2];
int cnt = 0;
for (int i = 0; i < n * 2; i++) {
types[i] = sc.next().charAt(0);
if (types[i] == '+')
indexes.add(cnt++);
else {
if(indexes.size() == 0){
pw.print("NO");
pw.close();
System.exit(0);
}
ans[indexes.pollLast()] = nums[i] = sc.nextInt();
}
}
TreeSet<Integer> min = new TreeSet<>();
cnt = 0;
for (int i = 0; i < n * 2; i++) {
if (types[i] == '+')
min.add(ans[cnt++]);
else {
if (min.pollFirst() != nums[i]) {
pw.print("NO");
pw.close();
System.exit(0);
}
}
}
pw.println("YES");
for (int num : ans) {
pw.print(num + " ");
}
pw.close();
}
}
class FastScanner {
static StringTokenizer st = new StringTokenizer("");
static BufferedReader br;
public FastScanner(InputStream inputStream) {
br = new BufferedReader(new InputStreamReader(inputStream));
}
public String next() throws IOException {
while (!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());
}
} | Java | ["4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4", "1\n- 1\n+", "3\n+\n+\n+\n- 2\n- 1\n- 3"] | 1 second | ["YES\n4 2 3 1", "NO", "NO"] | NoteIn the first example Tenten first placed shurikens with prices $$$4$$$ and $$$2$$$. After this a customer came in and bought the cheapest shuriken which costed $$$2$$$. Next, Tenten added a shuriken with price $$$3$$$ on the showcase to the already placed $$$4$$$-ryo. Then a new customer bought this $$$3$$$-ryo shuriken. After this she added a $$$1$$$-ryo shuriken. Finally, the last two customers bought shurikens $$$1$$$ and $$$4$$$, respectively. Note that the order $$$[2, 4, 3, 1]$$$ is also valid.In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $$$2$$$. This is impossible since the shuriken was not the cheapest, we know that the $$$1$$$-ryo shuriken was also there. | Java 8 | standard input | [
"data structures",
"implementation",
"greedy"
] | 5fa2af185c4e3c8a1ce3df0983824bad | The first line contains the only integer $$$n$$$ ($$$1\leq n\leq 10^5$$$) standing for the number of shurikens. The following $$$2n$$$ lines describe the events in the format described above. It's guaranteed that there are exactly $$$n$$$ events of the first type, and each price from $$$1$$$ to $$$n$$$ occurs exactly once in the events of the second type. | 1,700 | If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain $$$n$$$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. | standard output | |
PASSED | c9726b1f692bebe216d91f3516a17206 | train_001.jsonl | 1371992400 | In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2.Your program should handle the queries of the following two types: "1 x y" (x < y) — add the new interval (x, y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals. "2 a b" (a ≠ b) — answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval? Answer all the queries. Note, that initially you have an empty set of intervals. | 256 megabytes | import java.util.List;
import java.util.NavigableSet;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.SortedSet;
import java.util.Set;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.util.Collection;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
}
class TaskE {
static Segment tmp = new Segment();
static Segment tmp2 = new Segment();
static class Segment implements Comparable<Segment> {
int left;
int right;
int maxGen;
TreeSet<Segment> children = new TreeSet<Segment>();
Segment assignedTo;
public int compareTo(Segment o) {
return left - o.left;
}
Segment getAssigment() {
if (assignedTo == null) return this;
assignedTo = assignedTo.getAssigment();
return assignedTo;
}
public void mergeInside(int x) {
tmp.left = x;
NavigableSet<Segment> before = children.headSet(tmp, false);
if (!before.isEmpty()) {
Segment c = before.last();
if (c.right > x) {
c.mergeInside(x);
children.remove(c);
merge(this, c);
}
}
}
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
Segment[] s = new Segment[n];
int cnt = 0;
TreeSet<Segment> topLevel = new TreeSet<Segment>();
List<Segment> td = new ArrayList<Segment>();
int maxlen = 0;
for (int i = 0; i < n; ++i) {
int kind = in.nextInt();
if (kind == 1) {
int x = in.nextInt();
int y = in.nextInt();
if (y - x <= maxlen) throw new RuntimeException();
maxlen = y - x;
tmp.left = x;
NavigableSet<Segment> before = topLevel.headSet(tmp, false);
Segment containingX = null;
if (!before.isEmpty()) {
containingX = before.last();
if (containingX.right <= x) {
containingX = null;
}
}
tmp.left = y;
before = topLevel.headSet(tmp, false);
Segment containingY = null;
if (!before.isEmpty()) {
containingY = before.last();
if (containingY.right <= y) {
containingY = null;
}
}
Segment cur = new Segment();
cur.left = x;
cur.right = y;
cur.maxGen = i;
if (containingX != null)
merge(cur, containingX);
if (containingY != null)
merge(cur, containingY);
tmp2.left = cur.left - 1;
NavigableSet<Segment> toDelete = before.tailSet(tmp2, false);
td.clear();
td.addAll(toDelete);
for (Segment u : td) {
topLevel.remove(u);
if (u != containingX && u != containingY)
cur.children.add(u);
}
cur.mergeInside(x);
cur.mergeInside(y);
topLevel.add(cur);
s[cnt++] = cur;
} else {
int a = in.nextInt() - 1;
int b = in.nextInt() - 1;
Segment sa = s[a].getAssigment();
Segment sb = s[b].getAssigment();
if ((sa.left >= sb.left && sa.right <= sb.right) && (sa.maxGen <= sb.maxGen)) {
out.println("YES");
} else {
out.println("NO");
}
}
}
}
private static void merge(Segment cur, Segment other) {
cur.left = Math.min(cur.left, other.left);
cur.right = Math.max(cur.right, other.right);
other.assignedTo = cur;
if (cur.children.size() > other.children.size()) {
for (Segment s : other.children)
cur.children.add(s);
} else {
TreeSet<Segment> tmp = cur.children;
cur.children = other.children;
for (Segment s : tmp)
cur.children.add(s);
}
}
}
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());
}
}
| Java | ["5\n1 1 5\n1 5 11\n2 1 2\n1 2 9\n2 1 2"] | 2 seconds | ["NO\nYES"] | null | Java 7 | standard input | [
"data structures"
] | c686c3592542b70a3b617eb639c0e3f4 | The first line of the input contains integer n denoting the number of queries, (1 ≤ n ≤ 105). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value. It's guaranteed that all queries are correct. | 3,000 | For each query of the second type print "YES" or "NO" on a separate line depending on the answer. | standard output | |
PASSED | 69f88ef329f22b2fa2cd49278c61c332 | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | //package sept;
import java.io.*;
import java.util.*;
public class TimePass implements Runnable {
InputStream is;
PrintWriter out;
String INPUT = "10 6\r\n" +
"2 3 3 3 4 4 4 5 5 6";
//boolean debug=false;
boolean debug=true;
static long mod=998244353;
static long mod2=1000000007;
void solve() throws IOException
{
/*
int n=ni(),x=ni();
int[] a=na(n);
int[] from=new int[2*(n-1)];
int[] to=new int[2*(n-1)];
int ind=0;
for(int i=0;i<n-1;i++)
{
int u=ni()-1,v=ni()-1;
from[ind++]=u;
to[ind-1]=v;
from[ind++]=v;
to[ind-1]=u;
}
int[][] g=packD(n,from,to);
xor=new int[n];
int[] vis=new int[n];
dfs(0,a,g,vis);
if(xor[0]!=0 && xor[0]!=x)
{
out.println(0);
return;
}
*/
/*
int q=ni();
int[] a=na(q);
for(int i=0;i<q;i++)
{
for(int j=1;j<=Math.sqrt(a[i]);j++)
{
if(a[i]%j==0)
{
if(a[i]%(a[i]/j)==0 && j!=a[i]/j)
{
}
}
}
}
for(int bit=2;bit<=10;bit++)
{
for(int root=0;root<1;root++)
{
for(int curr_xor=0;curr_xor<2;curr_xor++)
{
int tot=0;
int[] next_perm_xor_cnt=new int[(1<<bit)];
for(int next_perm_xor=0;next_perm_xor<(1<<bit);next_perm_xor++)
{
int curr_next_xor=(curr_xor^next_perm_xor);
int no_of_change=0;
while(curr_next_xor!=0)
{
if(curr_next_xor%2==1)no_of_change++;
curr_next_xor/=2;
}
for(int breaks=0;breaks<(1<<bit);breaks++)
{
int and=(breaks & next_perm_xor);
int no_of_breaks=0;
while(and!=0)
{
if(and%2==1)no_of_breaks++;
and/=2;
}
if((no_of_change+no_of_breaks)%2==0)
{
if(root==1 && next_perm_xor>=breaks)
{
next_perm_xor_cnt[next_perm_xor]++;
tot++;
}
}
else
{
if(root==0 && next_perm_xor>=breaks)
{
next_perm_xor_cnt[next_perm_xor]++;
//out.println(next_perm_xor+" "+breaks);
tot++;
}
}
}
}
//for(int i=0;i<(1<<bit);i++)
//{
//out.println(i+" "+next_perm_xor_cnt[i]);
//}
out.println(bit+" "+root+" "+curr_xor+" "+tot);
}
}
}
*/
int n=ni(), k=ni(),m=ni();
int[] a=na(n);
if(n==1)
{
double ans=a[0]+Math.min(k, m);
out.println(ans);
return;
}
Arrays.sort(a);
double ans=0;
long[] sum=new long[n];
sum[0]=a[0];
for(int i=1;i<n;i++)
{
sum[i]=sum[i-1]+a[i];
}
int cnt=m/k;
for(int i=0;i<n;i++)
{
if(m-i<0)continue;
if(m-i>(long)k*(n-i))
{
ans=Math.max(ans, (double)(sum[n-1]-(i<1?0:sum[i-1])+(n-i)*k)/(n-i));
}
else
{
ans=Math.max(ans, (double)(sum[n-1]-(i<1?0:sum[i-1])+cnt*k+(m-cnt*k-i))/(n-i));
}
}
out.println(ans);
}
static int[] xor;
static void dfs(int u,int[] a,int[][] g,int[] vis)
{
xor[u]=a[u];
vis[u]=1;
for(int v:g[u])
{
if(vis[v]==0)
{
dfs(v,a,g,vis);
xor[u]=xor[u]^xor[v];
}
}
}
static int[][] packD(int n,int[] from,int[] to)
{
int[][] g=new int[n][];
int[] p=new int[n];
for(int f:from)
{
p[f]++;
}
int m=from.length;
for(int i=0;i<n;i++)
{
g[i]=new int[p[i]];
}
for(int i=0;i<m;i++)
{
g[from[i]][--p[from[i]]]=to[i];
}
return g;
}
static class Pair
{
int a,b;
public Pair(int a,int b)
{
this.a=a;
this.b=b;
//this.ind=ind;
}
}
static long lcm(int a,int b)
{
long val=a;
val*=b;
return (val/gcd(a,b));
}
static long gcd(long a,long b)
{
if(a==0)return b;
return gcd(b%a,a);
}
static int pow(int a, int b, int p)
{
long ans = 1, base = a;
while (b!=0)
{
if ((b & 1)!=0)
{
ans *= base;
ans%= p;
}
base *= base;
base%= p;
b >>= 1;
}
return (int)ans;
}
static int inv(int x, int p)
{
return pow(x, p - 2, p);
}
public void run()
{
if(debug)oj=true;
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
try {
solve();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception {new Thread(null,new TimePass(),"Main",1<<26).start();}
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 | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | 5a287e497b2994868788a92ee15909e0 | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class AveragePower {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int k = s.nextInt();
int m = s.nextInt();
int[] a = new int[n];
long sum = 0;
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
sum += a[i];
}
Arrays.sort(a);
int remaining = n;
int moves = m;
double answer = 0;
for (int d = 0; d <= Math.min(m, n - 1); d++) {
double delete_one = (sum + Math.min(moves, (long)remaining * k)) / (double)remaining;
answer = Math.max(answer, delete_one);
sum -= a[d];
moves--;
remaining--;
}
System.out.println(answer);
}
}
| Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | 9a6dd0dfe40e3881f5d0b6b4bca3c35a | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | // Feb 28, 2019
import java.util.*;
public class Superhero {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int n = scnr.nextInt();
int k = scnr.nextInt();
int m = scnr.nextInt();
scnr.nextLine();
int[] superHeroes = new int[n];
for (int i = 0; i < n; i++) {
superHeroes[i] = scnr.nextInt();
}
Arrays.sort(superHeroes);
double total = 0;
for (int i = 0; i < n; i++) {
total += superHeroes[i];
}
double maxAver = (total + (double) Math.min((double) k
* (double) n, (double) m)) / (double) n;
for (int i = 1; i <= Math.min(m, n - 1); i++) {
total -= (double) superHeroes[i - 1];
maxAver = Math.max(maxAver, (total + (double) Math.min((double) k
* (double) (n - i), (double) m - i)) / (double) (n - i));
}
System.out.println(maxAver);
scnr.close();
}
}
| Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | 6feee93fc4c56c6aeb53e52092a34e12 | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class CodeCraft {
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 sc = new FastReader();
long n = sc.nextLong();
long k = sc.nextLong();
long m = sc.nextLong();
long sum=0,temp=0;
double ans;
long[] a = new long[(int) (n+1)];
for(int i=1;i<=n;i++)
{
a[i] = sc.nextLong();
sum += a[i];
}
Arrays.sort(a);
ans = (double)(sum+Math.min(m, n*k))/(double)(n);
for(int i=1;i<=Math.min(n-1, m);i++)
{
sum -= a[i];
temp = sum + Math.min(m-i, (n-i)*k);
ans = Math.max(ans, (double)(temp)/(double)(n-i));
}
System.out.println(ans);
}
} | Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | b3a78d02717e7bdc3a682438f8f0905d | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 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.Arrays;
import java.util.StringTokenizer;
public class Main {
static PrintWriter out;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int n = sc.nextInt();
int k = sc.nextInt();
int m = sc.nextInt();;
double sum = 0;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
sum += arr[i];
}
double average = sum / n;
Arrays.sort(arr);
int i;
for (i = 0; i < n && m>0; i++) {
double add = ((sum+Math.min(m, k*(n-i)))/(n-i));
average = Math.max(add, average);
sum-= arr[i];
m--;
}
if(sum != 0 && n-i != 0)
average = Math.max(sum / (n-i), average);
out.println(average);
out.flush();
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
| Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | 3ceaf32bac4484b2a95a94174a9ca08b | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) throws Exception {
init();
int n = nextInt(),
k = nextInt(),
m = nextInt();
ArrayList<Integer> al = new ArrayList<>();
for (int i = 0; i < n; i++) {
al.add(nextInt());
}
al.sort((o1, o2) -> -Integer.compare(o1, o2));
Stack<Integer> stack = new Stack<>();
int left = m - Math.min(n - 1, m);
for (int i = 0; i < Math.min(n - 1, m); i++) {
stack.add(al.remove(al.size() - 1));
}
ArrayList<Integer> t = new ArrayList<>();
for (int i = 0; i < al.size(); i++) {
int delta = Math.min(k, left);
left -= delta;
t.add(al.get(i) + delta);
}
al = t;
double sm = 0.0;
for (int i : al)
sm += i * 1.0;
while (!stack.isEmpty()) {
int delta = Math.min(k, left + 1);
left -= delta - 1;
int pop = stack.pop();
if (1.0 * (sm + pop + delta) / (al.size() * 1.0 + 1.0) > 1.0 * sm / (al.size() * 1.0)) {
al.add(pop + delta);
sm += pop * 1.0 + delta;
} else break;
}
double sum = 0.0;
for (int i : al)
sum += i * 1.0;
out.printf("%.10f", sum / (al.size() * 1.0));
out.close();
}
static BufferedReader scan;
static PrintWriter out;
static StringTokenizer st;
static void init() {
scan = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
static void init(String name1, String name2) throws Exception {
try {
scan = new BufferedReader(new FileReader(name1));
out = new PrintWriter(name2);
} catch (Exception e) {
scan = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
}
static String next() throws Exception {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(scan.readLine());
return st.nextToken();
}
static int nextInt() throws Exception {
return Integer.parseInt(next());
}
static long nextLong() throws Exception {
return Long.parseLong(next());
}
} | Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | d95a4d926a7faa7d558e76ab3c65e366 | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class fast implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class pair implements Comparable<pair>{
int x;
int y;
pair(int xi, int yi){
x=xi;
y=yi;
}
@Override
public int compareTo(pair other){
if(this.x>other.x){return 1;}
if(this.x<other.x){return -1;}
return 0;
}
}
class dist{
int x;
int y;
int z;
dist(int xi, int yi, int zi){
x=xi;
y=yi;
z=zi;
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new fast(),"fast",1<<26).start();
}
public void sortbyColumn(int arr[][], final int col){
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else if(entry1[col] < entry2[col])
return -1;
return 0;
}
}); // End of function call sort().
}
public void sortbyColumn(long arr[][], final int col){
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<long[]>() {
@Override
// Compare values according to columns
public int compare(final long[] entry1,
final long[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else if(entry1[col] < entry2[col])
return -1;
return 0;
}
}); // End of function call sort().
}
long power(long x, long y, long p){
long res = 1;
x = x % p;
while (y > 0)
{
if((y & 1)==1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public void run(){
InputReader s = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int n=s.nextInt(),k=s.nextInt(),m=s.nextInt(),a[]=new int[n],big=n-1,sm=0,cnt[]=new int[n];
double sum=0,ans,all=n*k,a1,a2,done=0;
for(int i=0;i<n;i++){a[i]=s.nextInt();sum+=a[i];}
Arrays.sort(a);
ans=sum/n;
if(m>=n){
ans=a[big]+=Math.min(k,m-n+1);
}
else{
for(int i=0;i<m;i++){
a1=(sum+1)/n;
if(n==1){
a2=-1;
}
else{
a2=(sum-a[sm])/(n-1);
}
if(a1<a2){
sum-=a[sm];
sm++;
n--;
ans=a2;
all=n*k;
}
else if(a1>=a2){
if(done>=all){
break;
}
if(cnt[big]==k){
big--;
}
done++;
cnt[big]++;
a[big]++;
sum++;
ans=a1;
}
}
}
w.println(ans);
w.close();
}
} | Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | a2a1cee00a98435f86d7b5951c3c2ceb | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | import java.util.*;
import java.io.*;
import java.util.Queue;
import java.util.LinkedList;
import java.math.*;
public class Solution implements Runnable
{
public static void solve()
{
int n=i(); long k=i(); int m=i();
long[] arr = new long[n];
for(int i=0;i<n;i++)arr[i] = i();
Arrays.sort(arr);
for(int i=1;i<n;i++)arr[i]+=arr[i-1];
double ans =-1;
int del=m;
if(m>=n){
del = n-1;
}
if(n==1){
if(m>k){
System.out.println(arr[n-1]+(k));
} else {
System.out.println(arr[n-1]+(m));
}
return;
}
for(int i=del;i>=0;i--){
long sum=(arr[n-1]);
if(i!=0){
sum -= arr[i-1];
}
if((m-i)>=(n-i)*k){
sum+=(n-i)*k;
}else{
sum+=m-i;
}
ans =Math.max(ans,(1.0*sum)/(n-i));
}
System.out.println(ans);
}
public static boolean IsVowel(char c){
if(c =='a'||c=='e'||c=='i'||c=='o'||c=='u')return true;
else return false;
}
public static long lowerBound(long[] array, int length, long value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
if (value <= array[mid]){
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
public void run()
{
solve();
out.close();
}
public static void main(String[] args) throws IOException
{
new Thread(null, new Solution(), "whatever", 1<<26).start();
}
abstract static class Pair implements Comparable<Pair>
{
long a;
int b;
Pair(){}
Pair(long a,int b)
{
this.a=a;
this.b=b;
}
public int compareTo(Pair x)
{
return Long.compare(x.a,this.a);
}
}
////////////////////////////////////////////////////// Merge Sort ////////////////////////////////////////////////////////////////////////
static class Merge
{
public static void sort(long inputArr[])
{
int length = inputArr.length;
doMergeSort(inputArr,0, length - 1);
}
private static void doMergeSort(long[] arr,int lowerIndex, int higherIndex)
{
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
doMergeSort(arr,lowerIndex, middle);
doMergeSort(arr,middle + 1, higherIndex);
mergeParts(arr,lowerIndex, middle, higherIndex);
}
}
private static void mergeParts(long[]array,int lowerIndex, int middle, int higherIndex)
{
long[] temp=new long[higherIndex-lowerIndex+1];
for (int i = lowerIndex; i <= higherIndex; i++)
{
temp[i-lowerIndex] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex)
{
if (temp[i-lowerIndex] < temp[j-lowerIndex])
{
array[k] = temp[i-lowerIndex];
i++;
} else {
array[k] = temp[j-lowerIndex];
j++;
}
k++;
}
while (i <= middle)
{
array[k] = temp[i-lowerIndex];
k++;
i++;
}
while(j<=higherIndex)
{
array[k]=temp[j-lowerIndex];
k++;
j++;
}
}
}
/////////////////////////////////////////////////////////// Methods ////////////////////////////////////////////////////////////////////////
static boolean isPal(String s)
{
for(int i=0, j=s.length()-1;i<=j;i++,j--)
{
if(s.charAt(i)!=s.charAt(j)) return false;
}
return true;
}
static String rev(String s)
{
StringBuilder sb=new StringBuilder(s);
sb.reverse();
return sb.toString();
}
static long gcd(long a,long b){return (a==0)?b:gcd(b%a,a);}
static long gcdExtended(long a,long b,long[] x)
{
if(a==0){
x[0]=0;
x[1]=1;
return b;
}
long[] y=new long[2];
long gcd=gcdExtended(b%a, a, y);
x[0]=y[1]-(b/a)*y[0];
x[1]=y[0];
return gcd;
}
boolean findSum(int set[], int n, long sum)
{
if (sum == 0)
return true;
if (n == 0 && sum != 0)
return false;
if (set[n-1] > sum)
return findSum(set, n-1, sum);
return findSum(set, n-1, sum) ||findSum(set, n-1, sum-set[n-1]);
}
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;
}
base = (base * base) % mod;
exp = exp >> 1;
}
return result;
}
static long[] fac;
static long[] inv;
static long mod=(long)1e9+7;
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;
}
////////////////////////////////////////// Input Reader ////////////////////////////////////////////////////////////////////////////////////////////////////
static InputReader sc = new InputReader(System.in);
static PrintWriter out= new PrintWriter(System.out);
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public long[] nextLongArray(int n)
{
long a[] = new long[n];
for (int i = 0; i < n; i++)
{
a[i] = nextLong();
}
return a;
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
static int i()
{
return sc.nextInt();
}
static long l(){
return sc.nextLong();
}
static int[] iarr(int n)
{
return sc.nextIntArray(n);
}
static long[] larr(int n)
{
return sc.nextLongArray(n);
}
static String s(){
return sc.nextLine();
}
}
| Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | ead0bae3bc90eb98772a11ccf2b0706d | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes |
import java.io.InputStream;
import java.io.PrintStream;
import java.util.function.Supplier;
import java.util.*;
public class GangPower {
static Supplier<InputStream> inputSupplier = () -> System.in;
static Supplier<PrintStream> outSupplier = () -> System.out;
public static void main(String[] args) {
Scanner sc = new Scanner(inputSupplier.get());
PrintStream out = outSupplier.get();
int n = sc.nextInt();
int k = sc.nextInt(); // max op per avenger
int m = sc.nextInt(); // total max
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++) {
a.add(sc.nextInt());
}
Collections.sort(a);
long s[] = new long[n+1];
s[0] = 0;
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + a.get(i-1);
}
double max = 0;
for (int i = 0; i <n; i++) {
if(i>m) break;
double total = s[n] - s[i];
int num = n-i;
double additional = Math.min((double)num*k, m-i);
double curr = (total+additional)/(double)num;
if(curr>max) {
max = curr;
}
}
out.println( max);
}
}
| Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | a8f82c106292938b24e407fbd4278447 | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class SolutionCF1111B6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numOfHero = sc.nextInt();
int kLimit = sc.nextInt();
int mOp = sc.nextInt();
int[] powerArray = new int[numOfHero + 1];
double sum = 0d;
for (int i = 1; i <= numOfHero; i++) {
powerArray[i] = sc.nextInt();
sum += powerArray[i];
}
Arrays.sort(powerArray);
double resPowerAvg = calAvg(mOp, numOfHero, kLimit, sum);
for (int i = 1; i < numOfHero; i++) {
sum -= powerArray[i];
double remainMop = mOp - i;
double remainNumOfHero = numOfHero - i;
double remainPowerAvg = calAvg(remainMop, remainNumOfHero, kLimit, sum);
resPowerAvg = Math.max(resPowerAvg, remainPowerAvg);
if (remainMop == 0) {
break;
}
}
System.out.println(resPowerAvg);
}
private static double calAvg(double mOp, double numOfHero, int kLimit, double sum) {
return (Math.min(mOp, numOfHero * kLimit) + sum) * 1. / numOfHero;
}
} | Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | b4ad4eaceeb03410ac4475bba8b050c1 | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class SolutionCF1111B6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numOfHero = sc.nextInt();
int kLimit = sc.nextInt();
int mOp = sc.nextInt();
int[] powerArray = new int[numOfHero + 1];
double sum = 0d;
for (int i = 1; i <= numOfHero; i++) {
powerArray[i] = sc.nextInt();
sum += powerArray[i];
}
Arrays.sort(powerArray);
double resPowerAvg = calAvg(mOp, numOfHero, kLimit, sum);
for (int i = 1; i <= numOfHero; i++) {
if (numOfHero == 1){
break;
}
sum -= powerArray[i];
double remainMop = mOp - i;
double remainNumOfHero = numOfHero - i;
double remainPowerAvg = calAvg(remainMop, remainNumOfHero, kLimit, sum);
resPowerAvg = Math.max(resPowerAvg, remainPowerAvg);
if (remainMop == 0 || remainNumOfHero == 1) {
break;
}
}
System.out.println(resPowerAvg);
}
private static double calAvg(double mOp, double numOfHero, int kLimit, double sum) {
return (Math.min(mOp, numOfHero * kLimit) + sum) / numOfHero;
}
} | Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | 9e55f8e1be0f6f46ebc8ab3d93fb223d | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class SolutionCF1111B3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numOfHero = sc.nextInt();
int kLimit = sc.nextInt();
int mOp = sc.nextInt();
int[] powerArray = new int[numOfHero + 1];
double sum = 0d;
for (int i = 1; i <= numOfHero; i++) {
powerArray[i] = sc.nextInt();
sum += powerArray[i];
}
Arrays.sort(powerArray);
double resPowerAvg = (Math.min(mOp, numOfHero * kLimit * 1.)+sum) / numOfHero;
for (int i = 1; i < numOfHero; i++) {
sum -= powerArray[i];
double remainMop = mOp - i;
double remainNumOfHero = numOfHero - i;
double remainPowerAvg = (sum+Math.min(remainMop, remainNumOfHero * kLimit)) * 1. / remainNumOfHero;
resPowerAvg = Math.max(resPowerAvg, remainPowerAvg);
if (remainMop == 0){
break;
}
}
System.out.println(resPowerAvg);
}
} | Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | 4708321d5801e4a5d2ee88134c698aec | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class SolutionCF1111B8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numOfHero = sc.nextInt();
int kLimit = sc.nextInt();
int mOp = sc.nextInt();
int[] powerArray = new int[numOfHero + 1];
double sum = 0d;
for (int i = 1; i <= numOfHero; i++) {
powerArray[i] = sc.nextInt();
sum += powerArray[i];
}
Arrays.sort(powerArray);
double resPowerAvg = calAvg(mOp, numOfHero, kLimit, sum);
if (numOfHero == 1) {
System.out.println(resPowerAvg);
} else {
for (int i = 1; i <= numOfHero; i++) {
sum -= powerArray[i];
double remainMop = mOp - i;
double remainNumOfHero = numOfHero - i;
double remainPowerAvg = calAvg(remainMop, remainNumOfHero, kLimit, sum);
resPowerAvg = Math.max(resPowerAvg, remainPowerAvg);
if (remainMop == 0 || remainNumOfHero == 1) {
break;
}
}
System.out.println(resPowerAvg);
}
}
private static double calAvg(double mOp, double numOfHero, int kLimit, double sum) {
return (Math.min(mOp, numOfHero * kLimit) + sum) / numOfHero;
}
} | Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | 6eb3d43f97b6754e0dbef55468040885 | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class SolutionCF1111B6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numOfHero = sc.nextInt();
int kLimit = sc.nextInt();
int mOp = sc.nextInt();
int[] powerArray = new int[numOfHero + 1];
double sum = 0d;
for (int i = 1; i <= numOfHero; i++) {
powerArray[i] = sc.nextInt();
sum += powerArray[i];
}
Arrays.sort(powerArray);
double resPowerAvg = calAvg(mOp, numOfHero, kLimit, sum);
for (int i = 1; i <= numOfHero; i++) {
if (numOfHero == 1){
break;
}
sum -= powerArray[i];
double remainMop = mOp - i;
double remainNumOfHero = numOfHero - i;
double remainPowerAvg = calAvg(remainMop, remainNumOfHero, kLimit, sum);
resPowerAvg = Math.max(resPowerAvg, remainPowerAvg);
if (remainMop == 0 || remainNumOfHero == 1) {
break;
}
}
System.out.println(resPowerAvg);
}
private static double calAvg(double mOp, double numOfHero, int kLimit, double sum) {
return (Math.min(mOp, numOfHero * kLimit) + sum) * 1. / numOfHero;
}
} | Java | ["2 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output | |
PASSED | cfad5b1b0dc4bbc8462c4f59a59a992e | train_001.jsonl | 1549208100 | Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
public class Average_Superhero {
public static void main(String[] args) {
FastReader input = new FastReader();
long n = input.nextLong();
long times = input.nextLong();
long operations = input.nextLong();
double[] arr = new double[(int)n];
double sum = 0;
for(int i = 0;i < n;i++){
arr[i] = input.nextDouble();
sum += arr[i];
}
double add = Math.min(operations,n * times);
double avg = (sum + add) / n; // avg without deleting any element
Arrays.sort(arr);
for(int i = 1;i <= Math.min(operations,n-1);i++){
sum -= arr[i-1]; // deleting each element and checking if avg is greater
avg = Math.max(avg,(sum + Math.min((n-i) * times,operations - i)) / (n - i));
}
System.out.println((double)avg);
}
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 4 6\n4 7", "4 2 6\n1 3 2 3"] | 1 second | ["11.00000000000000000000", "5.00000000000000000000"] | NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each. | Java 8 | standard input | [
"implementation",
"brute force",
"math"
] | d6e44bd8ac03876cb03be0731f7dda3d | The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n \le 10^{5}$$$, $$$1 \le k \le 10^{5}$$$, $$$1 \le m \le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers. | 1,700 | Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.